3 #include <winpr/synch.h>
4 #include <winpr/thread.h>
6 static BOOL test_mutex_basic(
void)
11 if (!(mutex = CreateMutex(NULL, FALSE, NULL)))
13 printf(
"%s: CreateMutex failed\n", __func__);
17 rc = WaitForSingleObject(mutex, INFINITE);
19 if (rc != WAIT_OBJECT_0)
21 printf(
"%s: WaitForSingleObject on mutex failed with %" PRIu32
"\n", __func__, rc);
25 if (!ReleaseMutex(mutex))
27 printf(
"%s: ReleaseMutex failed\n", __func__);
31 if (ReleaseMutex(mutex))
33 printf(
"%s: ReleaseMutex unexpectedly succeeded on released mutex\n", __func__);
37 if (!CloseHandle(mutex))
39 printf(
"%s: CloseHandle on mutex failed\n", __func__);
46 static BOOL test_mutex_recursive(
void)
52 if (!(mutex = CreateMutex(NULL, TRUE, NULL)))
54 printf(
"%s: CreateMutex failed\n", __func__);
58 for (UINT32 i = 0; i < cnt; i++)
60 rc = WaitForSingleObject(mutex, INFINITE);
62 if (rc != WAIT_OBJECT_0)
64 printf(
"%s: WaitForSingleObject #%" PRIu32
" on mutex failed with %" PRIu32
"\n",
70 for (UINT32 i = 0; i < cnt; i++)
72 if (!ReleaseMutex(mutex))
74 printf(
"%s: ReleaseMutex #%" PRIu32
" failed\n", __func__, i);
79 if (!ReleaseMutex(mutex))
82 printf(
"%s: Final ReleaseMutex failed\n", __func__);
86 if (ReleaseMutex(mutex))
88 printf(
"%s: ReleaseMutex unexpectedly succeeded on released mutex\n", __func__);
92 if (!CloseHandle(mutex))
94 printf(
"%s: CloseHandle on mutex failed\n", __func__);
101 static HANDLE thread1_mutex1 = NULL;
102 static HANDLE thread1_mutex2 = NULL;
103 static BOOL thread1_failed = TRUE;
105 static DWORD WINAPI test_mutex_thread1(LPVOID lpParam)
107 HANDLE hStartEvent = (HANDLE)lpParam;
110 if (WaitForSingleObject(hStartEvent, INFINITE) != WAIT_OBJECT_0)
112 (void)fprintf(stderr,
"%s: failed to wait for start event\n", __func__);
124 rc = WaitForSingleObject(thread1_mutex1, 10);
126 if (rc != WAIT_TIMEOUT)
128 (void)fprintf(stderr,
129 "%s: WaitForSingleObject on thread1_mutex1 unexpectedly returned %" PRIu32
130 " instead of WAIT_TIMEOUT (%u)\n",
131 __func__, rc, WAIT_TIMEOUT);
135 rc = WaitForSingleObject(thread1_mutex2, 10);
137 if (rc != WAIT_OBJECT_0)
139 (void)fprintf(stderr,
140 "%s: WaitForSingleObject on thread1_mutex2 unexpectedly returned %" PRIu32
141 " instead of WAIT_OBJECT_0\n",
146 if (!ReleaseMutex(thread1_mutex2))
148 (void)fprintf(stderr,
"%s: ReleaseMutex failed on thread1_mutex2\n", __func__);
152 thread1_failed = FALSE;
156 static BOOL test_mutex_threading(
void)
158 HANDLE hThread = NULL;
159 HANDLE hStartEvent = NULL;
161 if (!(thread1_mutex1 = CreateMutex(NULL, TRUE, NULL)))
163 printf(
"%s: CreateMutex thread1_mutex1 failed\n", __func__);
167 if (!(thread1_mutex2 = CreateMutex(NULL, FALSE, NULL)))
169 printf(
"%s: CreateMutex thread1_mutex2 failed\n", __func__);
173 if (!(hStartEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
175 (void)fprintf(stderr,
"%s: error creating start event\n", __func__);
179 thread1_failed = TRUE;
181 if (!(hThread = CreateThread(NULL, 0, test_mutex_thread1, (LPVOID)hStartEvent, 0, NULL)))
183 (void)fprintf(stderr,
"%s: error creating test_mutex_thread_1\n", __func__);
191 (void)fprintf(stderr,
"%s: thread1 premature success\n", __func__);
195 (void)SetEvent(hStartEvent);
197 if (WaitForSingleObject(hThread, 2000) != WAIT_OBJECT_0)
199 (void)fprintf(stderr,
"%s: thread1 premature success\n", __func__);
205 (void)fprintf(stderr,
"%s: thread1 has not reported success\n", __func__);
214 if (!ReleaseMutex(thread1_mutex1))
216 printf(
"%s: ReleaseMutex unexpectedly failed on thread1_mutex1\n", __func__);
220 if (ReleaseMutex(thread1_mutex2))
222 printf(
"%s: ReleaseMutex unexpectedly succeeded on thread1_mutex2\n", __func__);
226 (void)CloseHandle(hThread);
227 (void)CloseHandle(hStartEvent);
228 (void)CloseHandle(thread1_mutex1);
229 (void)CloseHandle(thread1_mutex2);
232 (void)ReleaseMutex(thread1_mutex1);
233 (void)ReleaseMutex(thread1_mutex2);
234 (void)CloseHandle(thread1_mutex1);
235 (void)CloseHandle(thread1_mutex2);
236 (void)CloseHandle(hStartEvent);
237 (void)CloseHandle(hThread);
241 int TestSynchMutex(
int argc,
char* argv[])
247 if (!test_mutex_basic())
250 if (!test_mutex_recursive())
253 if (!test_mutex_threading())
256 printf(
"TestSynchMutex result %d\n", rc);