| 1 |
|
| 2 |
#include <winpr/crt.h> |
| 3 |
#include <winpr/synch.h> |
| 4 |
|
| 5 |
int TestSynchEvent(int argc, char* argv[]) |
| 6 |
{ |
| 7 |
HANDLE event = nullptr; |
| 8 |
WINPR_UNUSED(argc); |
| 9 |
WINPR_UNUSED(argv); |
| 10 |
if (ResetEvent(nullptr)) |
| 11 |
{ |
| 12 |
printf("ResetEvent(nullptr) unexpectedly succeeded\n"); |
| 13 |
return -1; |
| 14 |
} |
| 15 |
|
| 16 |
if (SetEvent(nullptr)) |
| 17 |
{ |
| 18 |
printf("SetEvent(nullptr) unexpectedly succeeded\n"); |
| 19 |
return -1; |
| 20 |
} |
| 21 |
|
| 22 |
event = CreateEvent(nullptr, TRUE, TRUE, nullptr); |
| 23 |
|
| 24 |
if (!event) |
| 25 |
{ |
| 26 |
printf("CreateEvent failure\n"); |
| 27 |
return -1; |
| 28 |
} |
| 29 |
|
| 30 |
if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) |
| 31 |
{ |
| 32 |
printf("WaitForSingleObject failure 1\n"); |
| 33 |
return -1; |
| 34 |
} |
| 35 |
|
| 36 |
if (!ResetEvent(event)) |
| 37 |
{ |
| 38 |
printf("ResetEvent failure with signaled event object\n"); |
| 39 |
return -1; |
| 40 |
} |
| 41 |
|
| 42 |
if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT) |
| 43 |
{ |
| 44 |
printf("WaitForSingleObject failure 2\n"); |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
if (!ResetEvent(event)) |
| 49 |
{ |
| 50 |
/* Note: ResetEvent must also succeed if event is currently nonsignaled */ |
| 51 |
printf("ResetEvent failure with nonsignaled event object\n"); |
| 52 |
return -1; |
| 53 |
} |
| 54 |
|
| 55 |
if (!SetEvent(event)) |
| 56 |
{ |
| 57 |
printf("SetEvent failure with nonsignaled event object\n"); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
|
| 61 |
if (WaitForSingleObject(event, 0) != WAIT_OBJECT_0) |
| 62 |
{ |
| 63 |
printf("WaitForSingleObject failure 3\n"); |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
for (int i = 0; i < 10000; i++) |
| 68 |
{ |
| 69 |
if (!SetEvent(event)) |
| 70 |
{ |
| 71 |
printf("SetEvent failure with signaled event object (i = %d)\n", i); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (!ResetEvent(event)) |
| 77 |
{ |
| 78 |
printf("ResetEvent failure after multiple SetEvent calls\n"); |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
/* Independent of the amount of the previous SetEvent calls, a single |
| 83 |
ResetEvent must be sufficient to get into nonsignaled state */ |
| 84 |
|
| 85 |
if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT) |
| 86 |
{ |
| 87 |
printf("WaitForSingleObject failure 4\n"); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
(void)CloseHandle(event); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |