3 #include <winpr/crypto.h>
4 #include <winpr/synch.h>
5 #include <winpr/thread.h>
6 #include <winpr/interlocked.h>
8 #define TEST_NUM_THREADS 100
9 #define TEST_NUM_FAILURES 10
11 static INIT_ONCE initOnceTest = INIT_ONCE_STATIC_INIT;
13 static HANDLE hStartEvent = NULL;
14 static LONG* pErrors = NULL;
15 static LONG* pTestThreadFunctionCalls = NULL;
16 static LONG* pTestOnceFunctionCalls = NULL;
17 static LONG* pInitOnceExecuteOnceCalls = NULL;
19 static UINT32 prand(UINT32 max)
24 winpr_RAND(&tmp,
sizeof(tmp));
25 return tmp % (max - 1) + 1;
28 static BOOL CALLBACK TestOnceFunction(
PINIT_ONCE once, PVOID param, PVOID* context)
30 LONG calls = InterlockedIncrement(pTestOnceFunctionCalls) - 1;
34 WINPR_UNUSED(context);
37 Sleep(30 + prand(40));
39 if (calls < TEST_NUM_FAILURES)
44 if (calls == TEST_NUM_FAILURES)
48 (void)fprintf(stderr,
"%s: error: called again after success\n", __func__);
49 InterlockedIncrement(pErrors);
53 static DWORD WINAPI TestThreadFunction(LPVOID lpParam)
58 WINPR_UNUSED(lpParam);
60 InterlockedIncrement(pTestThreadFunctionCalls);
61 if (WaitForSingleObject(hStartEvent, INFINITE) != WAIT_OBJECT_0)
63 (void)fprintf(stderr,
"%s: error: failed to wait for start event\n", __func__);
64 InterlockedIncrement(pErrors);
68 ok = InitOnceExecuteOnce(&initOnceTest, TestOnceFunction, NULL, NULL);
69 calls = InterlockedIncrement(pInitOnceExecuteOnceCalls);
70 if (!ok && calls > TEST_NUM_FAILURES)
72 (void)fprintf(stderr,
"%s: InitOnceExecuteOnce failed unexpectedly\n", __func__);
73 InterlockedIncrement(pErrors);
78 int TestSynchInit(
int argc,
char* argv[])
80 HANDLE hThreads[TEST_NUM_THREADS];
81 DWORD dwCreatedThreads = 0;
87 pErrors = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
88 pTestThreadFunctionCalls = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
89 pTestOnceFunctionCalls = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
90 pInitOnceExecuteOnceCalls = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
92 if (!pErrors || !pTestThreadFunctionCalls || !pTestOnceFunctionCalls ||
93 !pInitOnceExecuteOnceCalls)
95 (void)fprintf(stderr,
"error: _aligned_malloc failed\n");
100 *pTestThreadFunctionCalls = 0;
101 *pTestOnceFunctionCalls = 0;
102 *pInitOnceExecuteOnceCalls = 0;
104 if (!(hStartEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
106 (void)fprintf(stderr,
"error creating start event\n");
107 InterlockedIncrement(pErrors);
111 for (DWORD i = 0; i < TEST_NUM_THREADS; i++)
113 if (!(hThreads[i] = CreateThread(NULL, 0, TestThreadFunction, NULL, 0, NULL)))
115 (void)fprintf(stderr,
"error creating thread #%" PRIu32
"\n", i);
116 InterlockedIncrement(pErrors);
123 (void)SetEvent(hStartEvent);
125 for (DWORD i = 0; i < dwCreatedThreads; i++)
127 if (WaitForSingleObject(hThreads[i], INFINITE) != WAIT_OBJECT_0)
129 (void)fprintf(stderr,
"error: error waiting for thread #%" PRIu32
"\n", i);
130 InterlockedIncrement(pErrors);
135 if (*pErrors == 0 && *pTestThreadFunctionCalls == TEST_NUM_THREADS &&
136 *pInitOnceExecuteOnceCalls == TEST_NUM_THREADS &&
137 *pTestOnceFunctionCalls == TEST_NUM_FAILURES + 1)
143 (void)fprintf(stderr,
"Test result: %s\n", result ?
"OK" :
"ERROR");
144 (void)fprintf(stderr,
"Error count: %" PRId32
"\n", pErrors ? *pErrors : -1);
145 (void)fprintf(stderr,
"Threads created: %" PRIu32
"\n", dwCreatedThreads);
146 (void)fprintf(stderr,
"TestThreadFunctionCalls: %" PRId32
"\n",
147 pTestThreadFunctionCalls ? *pTestThreadFunctionCalls : -1);
148 (void)fprintf(stderr,
"InitOnceExecuteOnceCalls: %" PRId32
"\n",
149 pInitOnceExecuteOnceCalls ? *pInitOnceExecuteOnceCalls : -1);
150 (void)fprintf(stderr,
"TestOnceFunctionCalls: %" PRId32
"\n",
151 pTestOnceFunctionCalls ? *pTestOnceFunctionCalls : -1);
153 winpr_aligned_free(pErrors);
154 winpr_aligned_free(pTestThreadFunctionCalls);
155 winpr_aligned_free(pTestOnceFunctionCalls);
156 winpr_aligned_free(pInitOnceExecuteOnceCalls);
158 (void)CloseHandle(hStartEvent);
160 for (DWORD i = 0; i < dwCreatedThreads; i++)
162 (void)CloseHandle(hThreads[i]);
165 return (result ? 0 : 1);