Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
9.1 KiB

  1. #include "windows.h"
  2. #include "stdio.h"
  3. //
  4. // Set by the master when it isn't going to do the tests anymore.
  5. // Manual Reset
  6. //
  7. HANDLE AllDoneEvent;
  8. //
  9. // Set by the thread to tell the master to go ahead and write.
  10. // Auto reset.
  11. //
  12. HANDLE WriteWaitEvent;
  13. //
  14. // Set by the master to indicate that the timeouts have been
  15. // set to the proper value and it's ok to proceed with the reads.
  16. // Auto reset.
  17. //
  18. HANDLE ProceedWithReads;
  19. //
  20. // Set by the thread to indicate that it is ok for the master
  21. // to proceed with the next write loop.
  22. // Auto reset.
  23. //
  24. HANDLE ProceedWithNextWriteLoop;
  25. //
  26. // Handle to the comm port.
  27. //
  28. HANDLE ComHandle;
  29. OVERLAPPED ReadOverLap = {0};
  30. OVERLAPPED WriteOverLap = {0};
  31. DWORD
  32. ReadThread(
  33. LPVOID ThreadCount
  34. ) {
  35. UCHAR buff[10];
  36. DWORD i;
  37. DWORD startOfWait;
  38. float waitTimeSoFar;
  39. DWORD numberActuallyRead;
  40. //
  41. // Keep doing these tests until the master sets the all done signal.
  42. //
  43. while (WaitForSingleObject(
  44. AllDoneEvent,
  45. 0
  46. ) == WAIT_TIMEOUT) {
  47. WaitForSingleObject(
  48. ProceedWithReads,
  49. INFINITE
  50. );
  51. waitTimeSoFar = 0.0;
  52. //
  53. // Execute the read 10 times. Do the wait for
  54. // overlapped. We only go into the GetOverlapped code
  55. // when the overlapped write code completes
  56. // we capture the time just before the overlapped and
  57. // just after the overlapped. We add up all the
  58. // milliseconds it took for the getoverlapped to complete
  59. // and we average them and print out the result.
  60. //
  61. for (
  62. i = 0;
  63. i <= 9;
  64. i++
  65. ) {
  66. if (ReadFile(
  67. ComHandle,
  68. &buff[0],
  69. 10,
  70. &numberActuallyRead,
  71. &ReadOverLap
  72. )) {
  73. printf("Didn't get the read error\n");
  74. exit(1);
  75. }
  76. if (GetLastError() != ERROR_IO_PENDING) {
  77. printf("Didn't get pending\n");
  78. exit(1);
  79. }
  80. //
  81. // Tell the write to go ahead.
  82. //
  83. SetEvent(WriteWaitEvent);
  84. //
  85. // Wait for the event that is set by the write.
  86. //
  87. WaitForSingleObject(
  88. WriteOverLap.hEvent,
  89. INFINITE
  90. );
  91. startOfWait = GetTickCount();
  92. if (!GetOverlappedResult(
  93. ComHandle,
  94. &ReadOverLap,
  95. &numberActuallyRead,
  96. TRUE
  97. )) {
  98. printf("getover returned false\n");
  99. exit(1);
  100. }
  101. waitTimeSoFar += GetTickCount() - startOfWait;
  102. if (numberActuallyRead != 5) {
  103. printf("Wrong amount in IO\n");
  104. exit(1);
  105. }
  106. }
  107. printf("Total Time: %f - average time: %f\n",
  108. waitTimeSoFar,waitTimeSoFar/10.0);
  109. SetEvent(ProceedWithNextWriteLoop);
  110. }
  111. return 1;
  112. }
  113. int __cdecl main(int argc, char *argv[]) {
  114. DWORD startingMilli = 100;
  115. DWORD endingMilli = 20000;
  116. DWORD currentMilli;
  117. HANDLE threadHandle;
  118. CHAR *myPort = "COM1";
  119. DCB myDcb;
  120. DWORD junk;
  121. COMMTIMEOUTS myTimeOuts;
  122. DWORD numberActuallyWritten;
  123. UCHAR buff[5] = {0,1,2,3,4};
  124. if (argc > 1) {
  125. sscanf(argv[1],"%d",&startingMilli);
  126. if (argc > 2) {
  127. sscanf(argv[2],"%d",&endingMilli);
  128. if (argc > 3) {
  129. myPort = argv[3];
  130. }
  131. }
  132. }
  133. if ((ComHandle = CreateFile(
  134. myPort,
  135. GENERIC_READ | GENERIC_WRITE,
  136. 0,
  137. NULL,
  138. CREATE_ALWAYS,
  139. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  140. NULL
  141. )) == ((HANDLE)-1)) {
  142. printf("Couldn't open the port\n");
  143. exit(1);
  144. }
  145. if (!GetCommState(
  146. ComHandle,
  147. &myDcb
  148. )) {
  149. printf("Cound't get the comm state\n");
  150. exit(1);
  151. }
  152. myDcb.BaudRate = 19200;
  153. myDcb.ByteSize = 8;
  154. myDcb.StopBits = ONESTOPBIT;
  155. myDcb.Parity = NOPARITY;
  156. myDcb.fOutxCtsFlow = FALSE;
  157. myDcb.fOutxDsrFlow = FALSE;
  158. myDcb.fDsrSensitivity = FALSE;
  159. myDcb.fOutX = FALSE;
  160. myDcb.fInX = FALSE;
  161. myDcb.fRtsControl = RTS_CONTROL_ENABLE;
  162. myDcb.fDtrControl = DTR_CONTROL_ENABLE;
  163. if (!SetCommState(
  164. ComHandle,
  165. &myDcb
  166. )) {
  167. printf("Can't set the state\n");
  168. exit(1);
  169. }
  170. myTimeOuts.ReadIntervalTimeout = startingMilli;
  171. myTimeOuts.ReadTotalTimeoutMultiplier = 0;
  172. myTimeOuts.ReadTotalTimeoutConstant = 0;
  173. myTimeOuts.WriteTotalTimeoutMultiplier = 0;
  174. myTimeOuts.WriteTotalTimeoutConstant = 0;
  175. if (!SetCommTimeouts(
  176. ComHandle,
  177. &myTimeOuts
  178. )) {
  179. printf("Couldn't set the initial timeouts\n");
  180. exit(1);
  181. }
  182. if (!(AllDoneEvent = CreateEvent(
  183. NULL,
  184. TRUE,
  185. FALSE,
  186. NULL
  187. ))) {
  188. printf("Could not create the all done event\n");
  189. exit(1);
  190. }
  191. if (!(WriteWaitEvent = CreateEvent(
  192. NULL,
  193. FALSE,
  194. FALSE,
  195. NULL
  196. ))) {
  197. printf("Could not create the write wait event\n");
  198. exit(1);
  199. }
  200. if (!(ProceedWithReads = CreateEvent(
  201. NULL,
  202. TRUE,
  203. TRUE,
  204. NULL
  205. ))) {
  206. printf("Could not create the proceed with reads event\n");
  207. exit(1);
  208. }
  209. if (!(ProceedWithNextWriteLoop = CreateEvent(
  210. NULL,
  211. TRUE,
  212. FALSE,
  213. NULL
  214. ))) {
  215. printf("Could not create the proceed with writes event\n");
  216. exit(1);
  217. }
  218. if (!(ReadOverLap.hEvent = CreateEvent(
  219. NULL,
  220. TRUE,
  221. FALSE,
  222. NULL
  223. ))) {
  224. printf("Could not create the read event\n");
  225. exit(1);
  226. }
  227. if (!(WriteOverLap.hEvent = CreateEvent(
  228. NULL,
  229. TRUE,
  230. FALSE,
  231. NULL
  232. ))) {
  233. printf("Could not create the write event\n");
  234. exit(1);
  235. }
  236. threadHandle = CreateThread(
  237. NULL,
  238. 0,
  239. ReadThread,
  240. 0,
  241. 0,
  242. &junk
  243. );
  244. if (!threadHandle) {
  245. printf("Couldn't create the thread\n");
  246. exit(1);
  247. }
  248. currentMilli = startingMilli;
  249. while (currentMilli <= endingMilli) {
  250. printf("Interval timeout test for %d milliseconds\n",currentMilli);
  251. for (
  252. junk = 0;
  253. junk <= 9;
  254. junk++
  255. ) {
  256. //
  257. // Wait for the read thread to say that it's ok to write.
  258. //
  259. WaitForSingleObject(
  260. WriteWaitEvent,
  261. INFINITE
  262. );
  263. if (!WriteFile(
  264. ComHandle,
  265. &buff[0],
  266. 5,
  267. &numberActuallyWritten,
  268. &WriteOverLap
  269. )) {
  270. if (GetLastError() != ERROR_IO_PENDING) {
  271. printf("Write went bad\n");
  272. exit(1);
  273. }
  274. GetOverlappedResult(
  275. ComHandle,
  276. &WriteOverLap,
  277. &numberActuallyWritten,
  278. TRUE
  279. );
  280. }
  281. }
  282. currentMilli += 100;
  283. myTimeOuts.ReadIntervalTimeout = currentMilli;
  284. if (!SetCommTimeouts(
  285. ComHandle,
  286. &myTimeOuts
  287. )) {
  288. printf("Couldn't set the new timeouts\n");
  289. exit(1);
  290. }
  291. SetEvent(ProceedWithReads);
  292. WaitForSingleObject(ProceedWithNextWriteLoop,INFINITE);
  293. }
  294. SetEvent(AllDoneEvent);
  295. WaitForSingleObject(threadHandle,INFINITE);
  296. return 1;
  297. }