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
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 *);
34 static DWORD WINAPI
test_WaitOnAddress_func(void *arg
)
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
);
54 static void test_WaitOnAddress(void)
56 DWORD gle
, val
, nthreads
;
64 win_skip("WaitOnAddress not supported, skipping test\n");
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
);
80 ok(gle
== 0xdeadbeef, "got %d\n", gle
);
82 SetLastError(0xdeadbeef);
83 pWakeByAddressAll(NULL
);
85 ok(gle
== 0xdeadbeef, "got %d\n", gle
);
87 SetLastError(0xdeadbeef);
88 ret
= pWaitOnAddress(NULL
, NULL
, 0, 0);
90 ok(gle
== ERROR_INVALID_PARAMETER
, "got %d\n", gle
);
91 ok(!ret
, "got %d\n", ret
);
95 SetLastError(0xdeadbeef);
96 ret
= pWaitOnAddress(&address
, &compare
, 5, 0);
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
));
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 */
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 */
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 */
143 for (i
= 0; i
< ARRAY_SIZE(threads
); i
++)
144 threads
[i
] = CreateThread(NULL
, 0, test_WaitOnAddress_func
, NULL
, 0, NULL
);
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 */
157 for (i
= 0; i
< ARRAY_SIZE(threads
); i
++)
158 threads
[i
] = CreateThread(NULL
, 0, test_WaitOnAddress_func
, NULL
, 0, NULL
);
161 nthreads
= ARRAY_SIZE(threads
);
162 address
= ARRAY_SIZE(threads
);
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]));
172 ok(!address
, "got unexpected value %s\n", wine_dbgstr_longlong(address
));
179 hmod
= LoadLibraryA("kernel32.dll");
180 pWaitOnAddress
= (void *)GetProcAddress(hmod
, "WaitOnAddress");
181 ok(!pWaitOnAddress
, "expected only in kernelbase.dll\n");
183 hmod
= LoadLibraryA("kernelbase.dll");
184 pWaitOnAddress
= (void *)GetProcAddress(hmod
, "WaitOnAddress");
185 pWakeByAddressAll
= (void *)GetProcAddress(hmod
, "WakeByAddressAll");
186 pWakeByAddressSingle
= (void *)GetProcAddress(hmod
, "WakeByAddressSingle");
188 test_WaitOnAddress();