2018-10-26 08:50:15 +02:00
|
|
|
/*
|
|
|
|
* Synchronization tests
|
|
|
|
*
|
|
|
|
* Copyright 2018 Daniel Lehman
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <winerror.h>
|
|
|
|
|
|
|
|
#include "wine/test.h"
|
|
|
|
|
|
|
|
static BOOL (WINAPI *pWaitOnAddress)(volatile void *, void *, SIZE_T, DWORD);
|
|
|
|
static void (WINAPI *pWakeByAddressAll)(void *);
|
|
|
|
static void (WINAPI *pWakeByAddressSingle)(void *);
|
|
|
|
|
2019-02-06 03:21:06 +01:00
|
|
|
static LONG address;
|
2018-10-26 08:50:15 +02:00
|
|
|
static DWORD WINAPI test_WaitOnAddress_func(void *arg)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
2019-02-06 03:21:06 +01:00
|
|
|
LONG compare;
|
|
|
|
|
|
|
|
do
|
2018-10-26 08:50:15 +02:00
|
|
|
{
|
2019-02-06 03:21:06 +01:00
|
|
|
while (!(compare = address))
|
|
|
|
{
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
ret = pWaitOnAddress(&address, &compare, sizeof(compare), INFINITE);
|
|
|
|
ok(ret, "wait failed\n");
|
|
|
|
ok(GetLastError() == 0xdeadbeef || broken(GetLastError() == ERROR_SUCCESS) /* Win 8 */,
|
|
|
|
"got error %d\n", GetLastError());
|
|
|
|
}
|
|
|
|
} while (InterlockedCompareExchange(&address, compare - 1, compare) != compare);
|
|
|
|
|
2018-10-26 08:50:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_WaitOnAddress(void)
|
|
|
|
{
|
|
|
|
DWORD gle, val, nthreads;
|
|
|
|
HANDLE threads[8];
|
2019-02-06 03:21:06 +01:00
|
|
|
LONG compare;
|
2018-10-26 08:50:15 +02:00
|
|
|
BOOL ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!pWaitOnAddress)
|
|
|
|
{
|
|
|
|
win_skip("WaitOnAddress not supported, skipping test\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
address = 0;
|
|
|
|
compare = 0;
|
|
|
|
if (0) /* crash on Windows */
|
|
|
|
{
|
2019-02-06 03:21:06 +01:00
|
|
|
ret = pWaitOnAddress(&address, NULL, 4, 0);
|
|
|
|
ret = pWaitOnAddress(NULL, &compare, 4, 0);
|
2018-10-26 08:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid arguments */
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
pWakeByAddressSingle(NULL);
|
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == 0xdeadbeef, "got %d\n", gle);
|
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
pWakeByAddressAll(NULL);
|
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == 0xdeadbeef, "got %d\n", gle);
|
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
ret = pWaitOnAddress(NULL, NULL, 0, 0);
|
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == ERROR_INVALID_PARAMETER, "got %d\n", gle);
|
|
|
|
ok(!ret, "got %d\n", ret);
|
|
|
|
|
|
|
|
address = 0;
|
|
|
|
compare = 0;
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
ret = pWaitOnAddress(&address, &compare, 5, 0);
|
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == ERROR_INVALID_PARAMETER, "got %d\n", gle);
|
|
|
|
ok(!ret, "got %d\n", ret);
|
|
|
|
ok(address == 0, "got %s\n", wine_dbgstr_longlong(address));
|
|
|
|
ok(compare == 0, "got %s\n", wine_dbgstr_longlong(compare));
|
|
|
|
|
|
|
|
/* no waiters */
|
|
|
|
address = 0;
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
pWakeByAddressSingle(&address);
|
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == 0xdeadbeef, "got %d\n", gle);
|
|
|
|
ok(address == 0, "got %s\n", wine_dbgstr_longlong(address));
|
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
pWakeByAddressAll(&address);
|
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == 0xdeadbeef, "got %d\n", gle);
|
|
|
|
ok(address == 0, "got %s\n", wine_dbgstr_longlong(address));
|
|
|
|
|
|
|
|
/* different address size */
|
|
|
|
address = 0;
|
2019-02-06 03:21:06 +01:00
|
|
|
compare = 0xff00;
|
2018-10-26 08:50:15 +02:00
|
|
|
SetLastError(0xdeadbeef);
|
2019-02-06 03:21:06 +01:00
|
|
|
ret = pWaitOnAddress(&address, &compare, 2, 0);
|
2018-10-26 08:50:15 +02:00
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == 0xdeadbeef || broken(gle == ERROR_SUCCESS) /* Win 8 */, "got %d\n", gle);
|
|
|
|
ok(ret, "got %d\n", ret);
|
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
2019-02-06 03:21:06 +01:00
|
|
|
ret = pWaitOnAddress(&address, &compare, 1, 0);
|
2018-10-26 08:50:15 +02:00
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == ERROR_TIMEOUT, "got %d\n", gle);
|
|
|
|
ok(!ret, "got %d\n", ret);
|
|
|
|
|
|
|
|
/* simple wait case */
|
|
|
|
address = 0;
|
|
|
|
compare = 1;
|
|
|
|
SetLastError(0xdeadbeef);
|
2019-02-06 03:21:06 +01:00
|
|
|
ret = pWaitOnAddress(&address, &compare, 4, 0);
|
2018-10-26 08:50:15 +02:00
|
|
|
gle = GetLastError();
|
|
|
|
ok(gle == 0xdeadbeef || broken(gle == ERROR_SUCCESS) /* Win 8 */, "got %d\n", gle);
|
|
|
|
ok(ret, "got %d\n", ret);
|
|
|
|
|
|
|
|
/* WakeByAddressAll */
|
|
|
|
address = 0;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(threads); i++)
|
|
|
|
threads[i] = CreateThread(NULL, 0, test_WaitOnAddress_func, NULL, 0, NULL);
|
|
|
|
|
|
|
|
Sleep(100);
|
2019-02-06 03:21:06 +01:00
|
|
|
address = ARRAY_SIZE(threads);
|
2018-10-26 08:50:15 +02:00
|
|
|
pWakeByAddressAll(&address);
|
|
|
|
val = WaitForMultipleObjects(ARRAY_SIZE(threads), threads, TRUE, 5000);
|
|
|
|
ok(val == WAIT_OBJECT_0, "got %d\n", val);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(threads); i++)
|
|
|
|
CloseHandle(threads[i]);
|
2019-02-06 03:21:06 +01:00
|
|
|
ok(!address, "got unexpected value %s\n", wine_dbgstr_longlong(address));
|
2018-10-26 08:50:15 +02:00
|
|
|
|
|
|
|
/* WakeByAddressSingle */
|
|
|
|
address = 0;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(threads); i++)
|
|
|
|
threads[i] = CreateThread(NULL, 0, test_WaitOnAddress_func, NULL, 0, NULL);
|
|
|
|
|
|
|
|
Sleep(100);
|
|
|
|
nthreads = ARRAY_SIZE(threads);
|
2019-02-06 03:21:06 +01:00
|
|
|
address = ARRAY_SIZE(threads);
|
2018-10-26 08:50:15 +02:00
|
|
|
while (nthreads)
|
|
|
|
{
|
|
|
|
pWakeByAddressSingle(&address);
|
|
|
|
val = WaitForMultipleObjects(nthreads, threads, FALSE, 2000);
|
|
|
|
ok(val < WAIT_OBJECT_0 + nthreads, "got %u\n", val);
|
|
|
|
CloseHandle(threads[val]);
|
|
|
|
memmove(&threads[val], &threads[val+1], (nthreads - val - 1) * sizeof(threads[0]));
|
|
|
|
nthreads--;
|
|
|
|
}
|
2019-02-06 03:21:06 +01:00
|
|
|
ok(!address, "got unexpected value %s\n", wine_dbgstr_longlong(address));
|
2018-10-26 08:50:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-09 17:41:04 +02:00
|
|
|
static void test_Sleep(void)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER frequency;
|
|
|
|
LARGE_INTEGER t1, t2;
|
|
|
|
double elapsed_time;
|
|
|
|
BOOL ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = QueryPerformanceFrequency(&frequency);
|
|
|
|
ok(ret, "QueryPerformanceFrequency failed\n");
|
|
|
|
|
|
|
|
ret = QueryPerformanceCounter(&t1);
|
|
|
|
ok(ret, "QueryPerformanceCounter failed\n");
|
|
|
|
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
Sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = QueryPerformanceCounter(&t2);
|
|
|
|
ok(ret, "QueryPerformanceCounter failed\n");
|
|
|
|
|
|
|
|
elapsed_time = (t2.QuadPart - t1.QuadPart) / (double)frequency.QuadPart;
|
|
|
|
todo_wine ok(elapsed_time >= 1.5 && elapsed_time <= 4.0, "got %f\n", elapsed_time);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:50:15 +02:00
|
|
|
START_TEST(sync)
|
|
|
|
{
|
|
|
|
HMODULE hmod;
|
|
|
|
|
|
|
|
hmod = LoadLibraryA("kernel32.dll");
|
|
|
|
pWaitOnAddress = (void *)GetProcAddress(hmod, "WaitOnAddress");
|
|
|
|
ok(!pWaitOnAddress, "expected only in kernelbase.dll\n");
|
|
|
|
|
|
|
|
hmod = LoadLibraryA("kernelbase.dll");
|
|
|
|
pWaitOnAddress = (void *)GetProcAddress(hmod, "WaitOnAddress");
|
|
|
|
pWakeByAddressAll = (void *)GetProcAddress(hmod, "WakeByAddressAll");
|
|
|
|
pWakeByAddressSingle = (void *)GetProcAddress(hmod, "WakeByAddressSingle");
|
|
|
|
|
|
|
|
test_WaitOnAddress();
|
2020-08-09 17:41:04 +02:00
|
|
|
test_Sleep();
|
2018-10-26 08:50:15 +02:00
|
|
|
}
|