FreeRDP
Loading...
Searching...
No Matches
TestSynchEvent.c
1
2#include <winpr/crt.h>
3#include <winpr/synch.h>
4
5#ifndef _WIN32
6#include <fcntl.h>
7#include <unistd.h>
8#endif
9
10static BOOL test_auto_reset(void)
11{
12 HANDLE event = CreateEvent(nullptr, FALSE, TRUE, nullptr);
13 if (!event)
14 {
15 printf("CreateEvent (auto-reset) failure\n");
16 return FALSE;
17 }
18
19 BOOL rc = FALSE;
20 if (WaitForSingleObject(event, 0) != WAIT_OBJECT_0)
21 printf("auto-reset: initial state not signaled\n");
22 else if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
23 printf("auto-reset: still signaled after a successful wait\n");
24 else if (!SetEvent(event) || (WaitForSingleObject(event, 0) != WAIT_OBJECT_0))
25 printf("auto-reset: not signaled after SetEvent\n");
26 else if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
27 printf("auto-reset: still signaled after the second wait\n");
28 else
29 rc = TRUE;
30
31 (void)CloseHandle(event);
32 return rc;
33}
34
35#ifndef _WIN32
36/* An event that wraps a descriptor of the caller must not consume the caller's data. */
37static BOOL test_attached_descriptor(void)
38{
39 int fds[2] = WINPR_C_ARRAY_INIT;
40 if (pipe(fds) != 0)
41 {
42 printf("pipe failure\n");
43 return FALSE;
44 }
45
46 BOOL rc = FALSE;
47 HANDLE event = nullptr;
48 const char data[] = "abc";
49 char buffer[sizeof(data)] = WINPR_C_ARRAY_INIT;
50
51 const int flags = fcntl(fds[0], F_GETFL);
52 if ((flags < 0) || (fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) < 0))
53 {
54 printf("fcntl failure\n");
55 goto out;
56 }
57
58 event = CreateFileDescriptorEvent(nullptr, FALSE, FALSE, fds[0], WINPR_FD_READ);
59 if (!event)
60 {
61 printf("CreateFileDescriptorEvent failure\n");
62 goto out;
63 }
64
65 if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
66 {
67 printf("attached descriptor: signaled without data\n");
68 goto out;
69 }
70
71 if (write(fds[1], data, sizeof(data)) != (ssize_t)sizeof(data))
72 {
73 printf("write failure\n");
74 goto out;
75 }
76
77 if (WaitForSingleObject(event, 0) != WAIT_OBJECT_0)
78 {
79 printf("attached descriptor: not signaled with data\n");
80 goto out;
81 }
82
83 if (WaitForSingleObject(event, 0) != WAIT_OBJECT_0)
84 {
85 printf("attached descriptor: data consumed by the first wait\n");
86 goto out;
87 }
88
89 if ((read(fds[0], buffer, sizeof(buffer)) != (ssize_t)sizeof(buffer)) ||
90 (memcmp(buffer, data, sizeof(data)) != 0))
91 {
92 printf("attached descriptor: data lost\n");
93 goto out;
94 }
95
96 rc = TRUE;
97out:
98 if (event)
99 (void)CloseHandle(event);
100 (void)close(fds[0]);
101 (void)close(fds[1]);
102 return rc;
103}
104#endif
105
106int TestSynchEvent(int argc, char* argv[])
107{
108 HANDLE event = nullptr;
109 WINPR_UNUSED(argc);
110 WINPR_UNUSED(argv);
111 if (ResetEvent(nullptr))
112 {
113 printf("ResetEvent(nullptr) unexpectedly succeeded\n");
114 return -1;
115 }
116
117 if (SetEvent(nullptr))
118 {
119 printf("SetEvent(nullptr) unexpectedly succeeded\n");
120 return -1;
121 }
122
123 event = CreateEvent(nullptr, TRUE, TRUE, nullptr);
124
125 if (!event)
126 {
127 printf("CreateEvent failure\n");
128 return -1;
129 }
130
131 if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0)
132 {
133 printf("WaitForSingleObject failure 1\n");
134 return -1;
135 }
136
137 if (!ResetEvent(event))
138 {
139 printf("ResetEvent failure with signaled event object\n");
140 return -1;
141 }
142
143 if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
144 {
145 printf("WaitForSingleObject failure 2\n");
146 return -1;
147 }
148
149 if (!ResetEvent(event))
150 {
151 /* Note: ResetEvent must also succeed if event is currently nonsignaled */
152 printf("ResetEvent failure with nonsignaled event object\n");
153 return -1;
154 }
155
156 if (!SetEvent(event))
157 {
158 printf("SetEvent failure with nonsignaled event object\n");
159 return -1;
160 }
161
162 if (WaitForSingleObject(event, 0) != WAIT_OBJECT_0)
163 {
164 printf("WaitForSingleObject failure 3\n");
165 return -1;
166 }
167
168 for (int i = 0; i < 10000; i++)
169 {
170 if (!SetEvent(event))
171 {
172 printf("SetEvent failure with signaled event object (i = %d)\n", i);
173 return -1;
174 }
175 }
176
177 if (!ResetEvent(event))
178 {
179 printf("ResetEvent failure after multiple SetEvent calls\n");
180 return -1;
181 }
182
183 /* Independent of the amount of the previous SetEvent calls, a single
184 ResetEvent must be sufficient to get into nonsignaled state */
185
186 if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
187 {
188 printf("WaitForSingleObject failure 4\n");
189 return -1;
190 }
191
192 (void)CloseHandle(event);
193
194 if (!test_auto_reset())
195 return -1;
196
197#ifndef _WIN32
198 if (!test_attached_descriptor())
199 return -1;
200#endif
201
202 return 0;
203}