mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / kernelbase / tests / sync.c
blob50ce62602b08edfe040126a4bd244cb25f68269a
1 /*
2 * Synchronization tests
4 * Copyright 2018 Daniel Lehman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <stdlib.h>
25 #include <winerror.h>
27 #include "wine/test.h"
29 static BOOL (WINAPI *pWaitOnAddress)(volatile void *, void *, SIZE_T, DWORD);
30 static void (WINAPI *pWakeByAddressAll)(void *);
31 static void (WINAPI *pWakeByAddressSingle)(void *);
33 static LONG address;
34 static DWORD WINAPI test_WaitOnAddress_func(void *arg)
36 BOOL ret = FALSE;
37 LONG compare;
41 while (!(compare = address))
43 SetLastError(0xdeadbeef);
44 ret = pWaitOnAddress(&address, &compare, sizeof(compare), INFINITE);
45 ok(ret, "wait failed\n");
46 ok(GetLastError() == 0xdeadbeef || broken(GetLastError() == ERROR_SUCCESS) /* Win 8 */,
47 "got error %d\n", GetLastError());
49 } while (InterlockedCompareExchange(&address, compare - 1, compare) != compare);
51 return 0;
54 static void test_WaitOnAddress(void)
56 DWORD gle, val, nthreads;
57 HANDLE threads[8];
58 LONG compare;
59 BOOL ret;
60 int i;
62 if (!pWaitOnAddress)
64 win_skip("WaitOnAddress not supported, skipping test\n");
65 return;
68 address = 0;
69 compare = 0;
70 if (0) /* crash on Windows */
72 ret = pWaitOnAddress(&address, NULL, 4, 0);
73 ret = pWaitOnAddress(NULL, &compare, 4, 0);
76 /* invalid arguments */
77 SetLastError(0xdeadbeef);
78 pWakeByAddressSingle(NULL);
79 gle = GetLastError();
80 ok(gle == 0xdeadbeef, "got %d\n", gle);
82 SetLastError(0xdeadbeef);
83 pWakeByAddressAll(NULL);
84 gle = GetLastError();
85 ok(gle == 0xdeadbeef, "got %d\n", gle);
87 SetLastError(0xdeadbeef);
88 ret = pWaitOnAddress(NULL, NULL, 0, 0);
89 gle = GetLastError();
90 ok(gle == ERROR_INVALID_PARAMETER, "got %d\n", gle);
91 ok(!ret, "got %d\n", ret);
93 address = 0;
94 compare = 0;
95 SetLastError(0xdeadbeef);
96 ret = pWaitOnAddress(&address, &compare, 5, 0);
97 gle = GetLastError();
98 ok(gle == ERROR_INVALID_PARAMETER, "got %d\n", gle);
99 ok(!ret, "got %d\n", ret);
100 ok(address == 0, "got %s\n", wine_dbgstr_longlong(address));
101 ok(compare == 0, "got %s\n", wine_dbgstr_longlong(compare));
103 /* no waiters */
104 address = 0;
105 SetLastError(0xdeadbeef);
106 pWakeByAddressSingle(&address);
107 gle = GetLastError();
108 ok(gle == 0xdeadbeef, "got %d\n", gle);
109 ok(address == 0, "got %s\n", wine_dbgstr_longlong(address));
111 SetLastError(0xdeadbeef);
112 pWakeByAddressAll(&address);
113 gle = GetLastError();
114 ok(gle == 0xdeadbeef, "got %d\n", gle);
115 ok(address == 0, "got %s\n", wine_dbgstr_longlong(address));
117 /* different address size */
118 address = 0;
119 compare = 0xff00;
120 SetLastError(0xdeadbeef);
121 ret = pWaitOnAddress(&address, &compare, 2, 0);
122 gle = GetLastError();
123 ok(gle == 0xdeadbeef || broken(gle == ERROR_SUCCESS) /* Win 8 */, "got %d\n", gle);
124 ok(ret, "got %d\n", ret);
126 SetLastError(0xdeadbeef);
127 ret = pWaitOnAddress(&address, &compare, 1, 0);
128 gle = GetLastError();
129 ok(gle == ERROR_TIMEOUT, "got %d\n", gle);
130 ok(!ret, "got %d\n", ret);
132 /* simple wait case */
133 address = 0;
134 compare = 1;
135 SetLastError(0xdeadbeef);
136 ret = pWaitOnAddress(&address, &compare, 4, 0);
137 gle = GetLastError();
138 ok(gle == 0xdeadbeef || broken(gle == ERROR_SUCCESS) /* Win 8 */, "got %d\n", gle);
139 ok(ret, "got %d\n", ret);
141 /* WakeByAddressAll */
142 address = 0;
143 for (i = 0; i < ARRAY_SIZE(threads); i++)
144 threads[i] = CreateThread(NULL, 0, test_WaitOnAddress_func, NULL, 0, NULL);
146 Sleep(100);
147 address = ARRAY_SIZE(threads);
148 pWakeByAddressAll(&address);
149 val = WaitForMultipleObjects(ARRAY_SIZE(threads), threads, TRUE, 5000);
150 ok(val == WAIT_OBJECT_0, "got %d\n", val);
151 for (i = 0; i < ARRAY_SIZE(threads); i++)
152 CloseHandle(threads[i]);
153 ok(!address, "got unexpected value %s\n", wine_dbgstr_longlong(address));
155 /* WakeByAddressSingle */
156 address = 0;
157 for (i = 0; i < ARRAY_SIZE(threads); i++)
158 threads[i] = CreateThread(NULL, 0, test_WaitOnAddress_func, NULL, 0, NULL);
160 Sleep(100);
161 nthreads = ARRAY_SIZE(threads);
162 address = ARRAY_SIZE(threads);
163 while (nthreads)
165 pWakeByAddressSingle(&address);
166 val = WaitForMultipleObjects(nthreads, threads, FALSE, 2000);
167 ok(val < WAIT_OBJECT_0 + nthreads, "got %u\n", val);
168 CloseHandle(threads[val]);
169 memmove(&threads[val], &threads[val+1], (nthreads - val - 1) * sizeof(threads[0]));
170 nthreads--;
172 ok(!address, "got unexpected value %s\n", wine_dbgstr_longlong(address));
175 static void test_Sleep(void)
177 LARGE_INTEGER frequency;
178 LARGE_INTEGER t1, t2;
179 double elapsed_time;
180 BOOL ret;
181 int i;
183 ret = QueryPerformanceFrequency(&frequency);
184 ok(ret, "QueryPerformanceFrequency failed\n");
186 ret = QueryPerformanceCounter(&t1);
187 ok(ret, "QueryPerformanceCounter failed\n");
189 for (i = 0; i < 100; i++) {
190 Sleep(1);
193 ret = QueryPerformanceCounter(&t2);
194 ok(ret, "QueryPerformanceCounter failed\n");
196 elapsed_time = (t2.QuadPart - t1.QuadPart) / (double)frequency.QuadPart;
197 todo_wine ok(elapsed_time >= 1.5 && elapsed_time <= 4.0, "got %f\n", elapsed_time);
200 START_TEST(sync)
202 HMODULE hmod;
204 hmod = LoadLibraryA("kernel32.dll");
205 pWaitOnAddress = (void *)GetProcAddress(hmod, "WaitOnAddress");
206 ok(!pWaitOnAddress, "expected only in kernelbase.dll\n");
208 hmod = LoadLibraryA("kernelbase.dll");
209 pWaitOnAddress = (void *)GetProcAddress(hmod, "WaitOnAddress");
210 pWakeByAddressAll = (void *)GetProcAddress(hmod, "WakeByAddressAll");
211 pWakeByAddressSingle = (void *)GetProcAddress(hmod, "WakeByAddressSingle");
213 test_WaitOnAddress();
214 test_Sleep();