2 * Synchronization tests
4 * Copyright 2005 Mike McCormack for CodeWeavers
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
25 #define _WIN32_WINNT 0x500
28 #include "wine/test.h"
30 static BOOL (WINAPI *pChangeTimerQueueTimer)(HANDLE, HANDLE, ULONG, ULONG);
31 static HANDLE (WINAPI *pCreateTimerQueue)(void);
32 static BOOL (WINAPI *pCreateTimerQueueTimer)(PHANDLE, HANDLE, WAITORTIMERCALLBACK,
33 PVOID, DWORD, DWORD, ULONG);
34 static HANDLE (WINAPI *pCreateWaitableTimerA)(SECURITY_ATTRIBUTES*,BOOL,LPCSTR);
35 static BOOL (WINAPI *pDeleteTimerQueueEx)(HANDLE, HANDLE);
36 static BOOL (WINAPI *pDeleteTimerQueueTimer)(HANDLE, HANDLE, HANDLE);
37 static HANDLE (WINAPI *pOpenWaitableTimerA)(DWORD,BOOL,LPCSTR);
39 static void test_signalandwait(void)
41 DWORD (WINAPI *pSignalObjectAndWait)(HANDLE, HANDLE, DWORD, BOOL);
45 HANDLE event[2], maxevents[MAXIMUM_WAIT_OBJECTS], semaphore[2], file;
47 kernel32 = GetModuleHandle("kernel32");
48 pSignalObjectAndWait = (void*) GetProcAddress(kernel32, "SignalObjectAndWait");
50 if (!pSignalObjectAndWait)
53 /* invalid parameters */
54 r = pSignalObjectAndWait(NULL, NULL, 0, 0);
55 if (r == ERROR_INVALID_FUNCTION)
57 skip("SignalObjectAndWait is not implemented\n");
58 return; /* Win98/ME */
60 ok( r == WAIT_FAILED, "should fail\n");
62 event[0] = CreateEvent(NULL, 0, 0, NULL);
63 event[1] = CreateEvent(NULL, 1, 1, NULL);
65 ok( event[0] && event[1], "failed to create event flags\n");
67 r = pSignalObjectAndWait(event[0], NULL, 0, FALSE);
68 ok( r == WAIT_FAILED, "should fail\n");
70 r = pSignalObjectAndWait(NULL, event[0], 0, FALSE);
71 ok( r == WAIT_FAILED, "should fail\n");
74 /* valid parameters */
75 r = pSignalObjectAndWait(event[0], event[1], 0, FALSE);
76 ok( r == WAIT_OBJECT_0, "should succeed\n");
78 /* event[0] is now signalled */
79 r = pSignalObjectAndWait(event[0], event[0], 0, FALSE);
80 ok( r == WAIT_OBJECT_0, "should succeed\n");
82 /* event[0] is not signalled */
83 r = WaitForSingleObject(event[0], 0);
84 ok( r == WAIT_TIMEOUT, "event was signalled\n");
86 r = pSignalObjectAndWait(event[0], event[0], 0, FALSE);
87 ok( r == WAIT_OBJECT_0, "should succeed\n");
89 /* clear event[1] and check for a timeout */
90 ok(ResetEvent(event[1]), "failed to clear event[1]\n");
91 r = pSignalObjectAndWait(event[0], event[1], 0, FALSE);
92 ok( r == WAIT_TIMEOUT, "should timeout\n");
94 CloseHandle(event[0]);
95 CloseHandle(event[1]);
97 /* create the maximum number of events and make sure
98 * we can wait on that many */
99 for (i=0; i<MAXIMUM_WAIT_OBJECTS; i++)
101 maxevents[i] = CreateEvent(NULL, 1, 1, NULL);
102 ok( maxevents[i] != 0, "should create enough events\n");
104 r = WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, maxevents, 0, 0);
105 ok( r != WAIT_FAILED && r != WAIT_TIMEOUT, "should succeed\n");
107 for (i=0; i<MAXIMUM_WAIT_OBJECTS; i++)
108 if (maxevents[i]) CloseHandle(maxevents[i]);
111 semaphore[0] = CreateSemaphore( NULL, 0, 1, NULL );
112 semaphore[1] = CreateSemaphore( NULL, 1, 1, NULL );
113 ok( semaphore[0] && semaphore[1], "failed to create semaphore\n");
115 r = pSignalObjectAndWait(semaphore[0], semaphore[1], 0, FALSE);
116 ok( r == WAIT_OBJECT_0, "should succeed\n");
118 r = pSignalObjectAndWait(semaphore[0], semaphore[1], 0, FALSE);
119 ok( r == WAIT_FAILED, "should fail\n");
121 r = ReleaseSemaphore(semaphore[0],1,NULL);
122 ok( r == FALSE, "should fail\n");
124 r = ReleaseSemaphore(semaphore[1],1,NULL);
125 ok( r == TRUE, "should succeed\n");
127 CloseHandle(semaphore[0]);
128 CloseHandle(semaphore[1]);
130 /* try a registry key */
131 file = CreateFile("x", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
132 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
133 r = pSignalObjectAndWait(file, file, 0, FALSE);
134 ok( r == WAIT_FAILED, "should fail\n");
135 ok( ERROR_INVALID_HANDLE == GetLastError(), "should return invalid handle error\n");
139 static void test_mutex(void)
146 hCreated = CreateMutex(NULL, FALSE, "WineTestMutex");
147 ok(hCreated != NULL, "CreateMutex failed with error %d\n", GetLastError());
148 wait_ret = WaitForSingleObject(hCreated, INFINITE);
149 ok(wait_ret == WAIT_OBJECT_0, "WaitForSingleObject failed with error 0x%08x\n", wait_ret);
151 /* yes, opening with just READ_CONTROL access allows us to successfully
152 * call ReleaseMutex */
153 hOpened = OpenMutex(READ_CONTROL, FALSE, "WineTestMutex");
154 ok(hOpened != NULL, "OpenMutex failed with error %d\n", GetLastError());
155 ret = ReleaseMutex(hOpened);
156 todo_wine ok(ret, "ReleaseMutex failed with error %d\n", GetLastError());
157 ret = ReleaseMutex(hCreated);
158 todo_wine ok(!ret && (GetLastError() == ERROR_NOT_OWNER),
159 "ReleaseMutex should have failed with ERROR_NOT_OWNER instead of %d\n", GetLastError());
161 /* test case sensitivity */
163 SetLastError(0xdeadbeef);
164 hOpened = OpenMutex(READ_CONTROL, FALSE, "WINETESTMUTEX");
165 ok(!hOpened, "OpenMutex succeeded\n");
166 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
167 GetLastError() == ERROR_INVALID_NAME, /* win9x */
168 "wrong error %u\n", GetLastError());
170 SetLastError(0xdeadbeef);
171 hOpened = OpenMutex(READ_CONTROL, FALSE, "winetestmutex");
172 ok(!hOpened, "OpenMutex succeeded\n");
173 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
174 GetLastError() == ERROR_INVALID_NAME, /* win9x */
175 "wrong error %u\n", GetLastError());
177 SetLastError(0xdeadbeef);
178 hOpened = CreateMutex(NULL, FALSE, "WineTestMutex");
179 ok(hOpened != NULL, "CreateMutex failed with error %d\n", GetLastError());
180 ok(GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
181 CloseHandle(hOpened);
183 SetLastError(0xdeadbeef);
184 hOpened = CreateMutex(NULL, FALSE, "WINETESTMUTEX");
185 ok(hOpened != NULL, "CreateMutex failed with error %d\n", GetLastError());
186 ok(GetLastError() == 0, "wrong error %u\n", GetLastError());
187 CloseHandle(hOpened);
189 CloseHandle(hCreated);
192 static void test_slist(void)
198 } item1, item2, item3, *pitem;
200 SLIST_HEADER slist_header, test_header;
204 VOID (WINAPI *pInitializeSListHead)(PSLIST_HEADER);
205 USHORT (WINAPI *pQueryDepthSList)(PSLIST_HEADER);
206 PSLIST_ENTRY (WINAPI *pInterlockedFlushSList)(PSLIST_HEADER);
207 PSLIST_ENTRY (WINAPI *pInterlockedPopEntrySList)(PSLIST_HEADER);
208 PSLIST_ENTRY (WINAPI *pInterlockedPushEntrySList)(PSLIST_HEADER,PSLIST_ENTRY);
211 kernel32 = GetModuleHandle("KERNEL32.DLL");
212 pInitializeSListHead = (void*) GetProcAddress(kernel32, "InitializeSListHead");
213 pQueryDepthSList = (void*) GetProcAddress(kernel32, "QueryDepthSList");
214 pInterlockedFlushSList = (void*) GetProcAddress(kernel32, "InterlockedFlushSList");
215 pInterlockedPopEntrySList = (void*) GetProcAddress(kernel32, "InterlockedPopEntrySList");
216 pInterlockedPushEntrySList = (void*) GetProcAddress(kernel32, "InterlockedPushEntrySList");
217 if (pInitializeSListHead == NULL ||
218 pQueryDepthSList == NULL ||
219 pInterlockedFlushSList == NULL ||
220 pInterlockedPopEntrySList == NULL ||
221 pInterlockedPushEntrySList == NULL)
223 skip("some required slist entrypoints were not found, skipping tests\n");
227 memset(&test_header, 0, sizeof(test_header));
228 memset(&slist_header, 0xFF, sizeof(slist_header));
229 pInitializeSListHead(&slist_header);
230 ok(memcmp(&test_header, &slist_header, sizeof(SLIST_HEADER)) == 0,
231 "InitializeSListHead didn't zero-fill list header\n");
232 size = pQueryDepthSList(&slist_header);
233 ok(size == 0, "initially created slist has size %d, expected 0\n", size);
236 ok(pInterlockedPushEntrySList(&slist_header, &item1.entry) == NULL,
237 "previous entry in empty slist wasn't NULL\n");
238 size = pQueryDepthSList(&slist_header);
239 ok(size == 1, "slist with 1 item has size %d\n", size);
242 entry = pInterlockedPushEntrySList(&slist_header, &item2.entry);
243 ok(entry != NULL, "previous entry in non-empty slist was NULL\n");
246 pitem = (struct item*) entry;
247 ok(pitem->value == 1, "previous entry in slist wasn't the one added\n");
249 size = pQueryDepthSList(&slist_header);
250 ok(size == 2, "slist with 2 items has size %d\n", size);
253 entry = pInterlockedPushEntrySList(&slist_header, &item3.entry);
254 ok(entry != NULL, "previous entry in non-empty slist was NULL\n");
257 pitem = (struct item*) entry;
258 ok(pitem->value == 2, "previous entry in slist wasn't the one added\n");
260 size = pQueryDepthSList(&slist_header);
261 ok(size == 3, "slist with 3 items has size %d\n", size);
263 entry = pInterlockedPopEntrySList(&slist_header);
264 ok(entry != NULL, "entry shouldn't be NULL\n");
267 pitem = (struct item*) entry;
268 ok(pitem->value == 3, "unexpected entry removed\n");
270 size = pQueryDepthSList(&slist_header);
271 ok(size == 2, "slist with 2 items has size %d\n", size);
273 entry = pInterlockedFlushSList(&slist_header);
274 size = pQueryDepthSList(&slist_header);
275 ok(size == 0, "flushed slist should be empty, size is %d\n", size);
278 ok(pInterlockedPopEntrySList(&slist_header) == NULL,
279 "popping empty slist didn't return NULL\n");
281 ok(((struct item*)entry)->value == 2, "item 2 not in front of list\n");
282 ok(((struct item*)entry->Next)->value == 1, "item 1 not at the back of list\n");
285 static void test_event(void)
287 HANDLE handle, handle2;
288 SECURITY_ATTRIBUTES sa;
289 SECURITY_DESCRIPTOR sd;
293 handle = CreateEventA(NULL, FALSE, FALSE, __FILE__ ": Test Event");
294 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
297 sa.nLength = sizeof(sa);
298 sa.lpSecurityDescriptor = &sd;
299 sa.bInheritHandle = FALSE;
301 InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
304 handle = CreateEventA(&sa, FALSE, FALSE, __FILE__ ": Test Event");
305 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
308 /* sd with NULL dacl */
309 SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
310 handle = CreateEventA(&sa, FALSE, FALSE, __FILE__ ": Test Event");
311 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
314 /* sd with empty dacl */
315 InitializeAcl(&acl, sizeof(acl), ACL_REVISION);
316 SetSecurityDescriptorDacl(&sd, TRUE, &acl, FALSE);
317 handle = CreateEventA(&sa, FALSE, FALSE, __FILE__ ": Test Event");
318 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
321 /* test case sensitivity */
323 SetLastError(0xdeadbeef);
324 handle = CreateEventA(NULL, FALSE, FALSE, __FILE__ ": Test Event");
325 ok( handle != NULL, "CreateEvent failed with error %u\n", GetLastError());
326 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
328 SetLastError(0xdeadbeef);
329 handle2 = CreateEventA(NULL, FALSE, FALSE, __FILE__ ": Test Event");
330 ok( handle2 != NULL, "CreateEvent failed with error %d\n", GetLastError());
331 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
332 CloseHandle( handle2 );
334 SetLastError(0xdeadbeef);
335 handle2 = CreateEventA(NULL, FALSE, FALSE, __FILE__ ": TEST EVENT");
336 ok( handle2 != NULL, "CreateEvent failed with error %d\n", GetLastError());
337 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
338 CloseHandle( handle2 );
340 SetLastError(0xdeadbeef);
341 handle2 = OpenEventA( EVENT_ALL_ACCESS, FALSE, __FILE__ ": Test Event");
342 ok( handle2 != NULL, "OpenEvent failed with error %d\n", GetLastError());
343 CloseHandle( handle2 );
345 SetLastError(0xdeadbeef);
346 handle2 = OpenEventA( EVENT_ALL_ACCESS, FALSE, __FILE__ ": TEST EVENT");
347 ok( !handle2, "OpenEvent succeeded\n");
348 ok( GetLastError() == ERROR_FILE_NOT_FOUND ||
349 GetLastError() == ERROR_INVALID_NAME, /* win9x */
350 "wrong error %u\n", GetLastError());
352 CloseHandle( handle );
355 static void test_semaphore(void)
357 HANDLE handle, handle2;
359 /* test case sensitivity */
361 SetLastError(0xdeadbeef);
362 handle = CreateSemaphoreA(NULL, 0, 1, __FILE__ ": Test Semaphore");
363 ok(handle != NULL, "CreateSemaphore failed with error %u\n", GetLastError());
364 ok(GetLastError() == 0, "wrong error %u\n", GetLastError());
366 SetLastError(0xdeadbeef);
367 handle2 = CreateSemaphoreA(NULL, 0, 1, __FILE__ ": Test Semaphore");
368 ok( handle2 != NULL, "CreateSemaphore failed with error %d\n", GetLastError());
369 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
370 CloseHandle( handle2 );
372 SetLastError(0xdeadbeef);
373 handle2 = CreateSemaphoreA(NULL, 0, 1, __FILE__ ": TEST SEMAPHORE");
374 ok( handle2 != NULL, "CreateSemaphore failed with error %d\n", GetLastError());
375 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
376 CloseHandle( handle2 );
378 SetLastError(0xdeadbeef);
379 handle2 = OpenSemaphoreA( SEMAPHORE_ALL_ACCESS, FALSE, __FILE__ ": Test Semaphore");
380 ok( handle2 != NULL, "OpenSemaphore failed with error %d\n", GetLastError());
381 CloseHandle( handle2 );
383 SetLastError(0xdeadbeef);
384 handle2 = OpenSemaphoreA( SEMAPHORE_ALL_ACCESS, FALSE, __FILE__ ": TEST SEMAPHORE");
385 ok( !handle2, "OpenSemaphore succeeded\n");
386 ok( GetLastError() == ERROR_FILE_NOT_FOUND ||
387 GetLastError() == ERROR_INVALID_NAME, /* win9x */
388 "wrong error %u\n", GetLastError());
390 CloseHandle( handle );
393 static void test_waitable_timer(void)
395 HANDLE handle, handle2;
397 if (!pCreateWaitableTimerA || !pOpenWaitableTimerA)
399 skip("{Create,Open}WaitableTimerA() is not available\n");
403 /* test case sensitivity */
405 SetLastError(0xdeadbeef);
406 handle = pCreateWaitableTimerA(NULL, FALSE, __FILE__ ": Test WaitableTimer");
407 ok(handle != NULL, "CreateWaitableTimer failed with error %u\n", GetLastError());
408 ok(GetLastError() == 0, "wrong error %u\n", GetLastError());
410 SetLastError(0xdeadbeef);
411 handle2 = pCreateWaitableTimerA(NULL, FALSE, __FILE__ ": Test WaitableTimer");
412 ok( handle2 != NULL, "CreateWaitableTimer failed with error %d\n", GetLastError());
413 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
414 CloseHandle( handle2 );
416 SetLastError(0xdeadbeef);
417 handle2 = pCreateWaitableTimerA(NULL, FALSE, __FILE__ ": TEST WAITABLETIMER");
418 ok( handle2 != NULL, "CreateWaitableTimer failed with error %d\n", GetLastError());
419 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
420 CloseHandle( handle2 );
422 SetLastError(0xdeadbeef);
423 handle2 = pOpenWaitableTimerA( TIMER_ALL_ACCESS, FALSE, __FILE__ ": Test WaitableTimer");
424 ok( handle2 != NULL, "OpenWaitableTimer failed with error %d\n", GetLastError());
425 CloseHandle( handle2 );
427 SetLastError(0xdeadbeef);
428 handle2 = pOpenWaitableTimerA( TIMER_ALL_ACCESS, FALSE, __FILE__ ": TEST WAITABLETIMER");
429 ok( !handle2, "OpenWaitableTimer succeeded\n");
430 ok( GetLastError() == ERROR_FILE_NOT_FOUND ||
431 GetLastError() == ERROR_INVALID_NAME, /* win98 */
432 "wrong error %u\n", GetLastError());
434 CloseHandle( handle );
437 static HANDLE sem = 0;
439 static void CALLBACK iocp_callback(DWORD dwErrorCode, DWORD dwNumberOfBytesTransferred, LPOVERLAPPED lpOverlapped)
441 ReleaseSemaphore(sem, 1, NULL);
444 static BOOL (WINAPI *p_BindIoCompletionCallback)( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags) = NULL;
446 static void test_iocp_callback(void)
448 char temp_path[MAX_PATH];
449 char filename[MAX_PATH];
452 static const char prefix[] = "pfx";
454 HMODULE hmod = GetModuleHandleA("kernel32.dll");
456 const char *buffer = "12345678123456781234567812345678";
457 OVERLAPPED overlapped;
459 p_BindIoCompletionCallback = (void*)GetProcAddress(hmod, "BindIoCompletionCallback");
460 if(!p_BindIoCompletionCallback) {
461 skip("BindIoCompletionCallback not found in this DLL\n");
465 sem = CreateSemaphore(NULL, 0, 1, NULL);
466 ok(sem != INVALID_HANDLE_VALUE, "Creating a semaphore failed\n");
468 ret = GetTempPathA(MAX_PATH, temp_path);
469 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
470 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
472 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
473 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
475 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
476 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
477 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
479 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 0);
480 ok(retb == FALSE, "BindIoCompletionCallback succeeded on a file that wasn't created with FILE_FLAG_OVERLAPPED\n");
481 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Last error is %d\n", GetLastError());
483 ret = CloseHandle(hFile);
484 ok( ret, "CloseHandle: error %d\n", GetLastError());
485 ret = DeleteFileA(filename);
486 ok( ret, "DeleteFileA: error %d\n", GetLastError());
488 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
489 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_OVERLAPPED, 0);
490 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
492 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 0);
493 ok(retb == TRUE, "BindIoCompletionCallback failed\n");
495 memset(&overlapped, 0, sizeof(overlapped));
496 retb = WriteFile(hFile, (const void *) buffer, 4, &bytesWritten, &overlapped);
497 ok(retb == TRUE || GetLastError() == ERROR_IO_PENDING, "WriteFile failed, lastError = %d\n", GetLastError());
499 ret = WaitForSingleObject(sem, 5000);
500 ok(ret == WAIT_OBJECT_0, "Wait for the IO completion callback failed\n");
503 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 0);
504 ok(retb == FALSE, "BindIoCompletionCallback succeeded when setting the same callback on the file again\n");
505 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Last error is %d\n", GetLastError());
506 retb = p_BindIoCompletionCallback(hFile, NULL, 0);
507 ok(retb == FALSE, "BindIoCompletionCallback succeeded when setting the callback to NULL\n");
508 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Last error is %d\n", GetLastError());
510 ret = CloseHandle(hFile);
511 ok( ret, "CloseHandle: error %d\n", GetLastError());
512 ret = DeleteFileA(filename);
513 ok( ret, "DeleteFileA: error %d\n", GetLastError());
515 /* win2k3 requires the Flags parameter to be zero */
516 SetLastError(0xdeadbeef);
517 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
518 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_OVERLAPPED, 0);
519 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
520 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 12345);
522 ok(GetLastError() == ERROR_INVALID_PARAMETER,
523 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
525 ok(retb == TRUE, "BindIoCompletionCallback failed with Flags != 0\n");
526 ret = CloseHandle(hFile);
527 ok( ret, "CloseHandle: error %d\n", GetLastError());
528 ret = DeleteFileA(filename);
529 ok( ret, "DeleteFileA: error %d\n", GetLastError());
531 retb = p_BindIoCompletionCallback(NULL, iocp_callback, 0);
532 ok(retb == FALSE, "BindIoCompletionCallback succeeded on a NULL file\n");
533 ok(GetLastError() == ERROR_INVALID_HANDLE ||
534 GetLastError() == ERROR_INVALID_PARAMETER, /* vista */
535 "Last error is %d\n", GetLastError());
538 static void CALLBACK timer_queue_cb1(PVOID p, BOOLEAN timedOut)
541 ok(timedOut, "Timer callbacks should always time out\n");
545 struct timer_queue_data1
552 static void CALLBACK timer_queue_cb2(PVOID p, BOOLEAN timedOut)
554 struct timer_queue_data1 *d = p;
555 ok(timedOut, "Timer callbacks should always time out\n");
556 if (d->t && ++d->num_calls == d->max_calls)
559 SetLastError(0xdeadbeef);
560 /* Note, XP SP2 does *not* do any deadlock checking, so passing
561 INVALID_HANDLE_VALUE here will just hang. */
562 ret = pDeleteTimerQueueTimer(d->q, d->t, NULL);
563 ok(!ret, "DeleteTimerQueueTimer\n");
564 ok(GetLastError() == ERROR_IO_PENDING, "DeleteTimerQueueTimer\n");
568 static void CALLBACK timer_queue_cb3(PVOID p, BOOLEAN timedOut)
570 struct timer_queue_data1 *d = p;
571 ok(timedOut, "Timer callbacks should always time out\n");
572 if (d->t && ++d->num_calls == d->max_calls)
574 /* Basically kill the timer since it won't have time to run
576 BOOL ret = pChangeTimerQueueTimer(d->q, d->t, 10000, 0);
577 ok(ret, "ChangeTimerQueueTimer\n");
581 static void CALLBACK timer_queue_cb4(PVOID p, BOOLEAN timedOut)
583 struct timer_queue_data1 *d = p;
584 ok(timedOut, "Timer callbacks should always time out\n");
587 /* This tests whether a timer gets flagged for deletion before
588 or after the callback runs. If we start this timer with a
589 period of zero (run once), then ChangeTimerQueueTimer will
590 fail if the timer is already flagged. Hence we really run
591 only once. Otherwise we will run multiple times. */
592 BOOL ret = pChangeTimerQueueTimer(d->q, d->t, 50, 50);
593 ok(ret, "ChangeTimerQueueTimer\n");
598 static void CALLBACK timer_queue_cb5(PVOID p, BOOLEAN timedOut)
600 DWORD delay = (DWORD) p;
601 ok(timedOut, "Timer callbacks should always time out\n");
606 static void CALLBACK timer_queue_cb6(PVOID p, BOOLEAN timedOut)
608 struct timer_queue_data1 *d = p;
609 ok(timedOut, "Timer callbacks should always time out\n");
610 /* This tests an original implementation bug where a deleted timer may get
611 to run, but it is tricky to set up. */
612 if (d->q && d->num_calls++ == 0)
614 /* First run: delete ourselves, then insert and remove a timer
615 that goes in front of us in the sorted timeout list. Once
616 removed, we will still timeout at the faster timer's due time,
617 but this should be a no-op if we are bug-free. There should
618 not be a second run. We can test the value of num_calls later. */
622 /* The delete will pend while we are in this callback. */
623 SetLastError(0xdeadbeef);
624 ret = pDeleteTimerQueueTimer(d->q, d->t, NULL);
625 ok(!ret, "DeleteTimerQueueTimer\n");
626 ok(GetLastError() == ERROR_IO_PENDING, "DeleteTimerQueueTimer\n");
628 ret = pCreateTimerQueueTimer(&t, d->q, timer_queue_cb1, NULL, 100, 0, 0);
629 ok(ret, "CreateTimerQueueTimer\n");
630 ok(t != NULL, "CreateTimerQueueTimer\n");
632 ret = pDeleteTimerQueueTimer(d->q, t, INVALID_HANDLE_VALUE);
633 ok(ret, "DeleteTimerQueueTimer\n");
635 /* Now we stay alive by hanging around in the callback. */
640 static void test_timer_queue(void)
642 HANDLE q, t1, t2, t3, t4, t5;
643 int n1, n2, n3, n4, n5;
644 struct timer_queue_data1 d1, d2, d3, d4;
648 if (!pChangeTimerQueueTimer || !pCreateTimerQueue || !pCreateTimerQueueTimer
649 || !pDeleteTimerQueueEx || !pDeleteTimerQueueTimer)
651 skip("TimerQueue API not present\n");
655 /* Test asynchronous deletion of the queue. */
656 q = pCreateTimerQueue();
657 ok(q != NULL, "CreateTimerQueue\n");
659 SetLastError(0xdeadbeef);
660 ret = pDeleteTimerQueueEx(q, NULL);
661 ok(!ret, "DeleteTimerQueueEx call was expected to fail\n");
662 ok(GetLastError() == ERROR_IO_PENDING,
663 "DeleteTimerQueueEx, GetLastError: expected ERROR_IO_PENDING, got %d\n",
666 /* Test synchronous deletion of the queue and running timers. */
667 q = pCreateTimerQueue();
668 ok(q != NULL, "CreateTimerQueue\n");
673 ret = pCreateTimerQueueTimer(&t1, q, timer_queue_cb1, &n1, 0,
675 ok(ret, "CreateTimerQueueTimer\n");
676 ok(t1 != NULL, "CreateTimerQueueTimer\n");
681 ret = pCreateTimerQueueTimer(&t2, q, timer_queue_cb1, &n2, 0,
683 ok(ret, "CreateTimerQueueTimer\n");
684 ok(t2 != NULL, "CreateTimerQueueTimer\n");
689 ret = pCreateTimerQueueTimer(&t3, q, timer_queue_cb1, &n3, 0,
691 ok(ret, "CreateTimerQueueTimer\n");
692 ok(t3 != NULL, "CreateTimerQueueTimer\n");
694 /* Start really late (it won't start). */
697 ret = pCreateTimerQueueTimer(&t4, q, timer_queue_cb1, &n4, 10000,
699 ok(ret, "CreateTimerQueueTimer\n");
700 ok(t4 != NULL, "CreateTimerQueueTimer\n");
702 /* Start soon, but delay so long it won't run again. */
705 ret = pCreateTimerQueueTimer(&t5, q, timer_queue_cb1, &n5, 0,
707 ok(ret, "CreateTimerQueueTimer\n");
708 ok(t5 != NULL, "CreateTimerQueueTimer\n");
710 /* Give them a chance to do some work. */
713 /* Test deleting a once-only timer. */
714 ret = pDeleteTimerQueueTimer(q, t1, INVALID_HANDLE_VALUE);
715 ok(ret, "DeleteTimerQueueTimer\n");
717 /* A periodic timer. */
718 ret = pDeleteTimerQueueTimer(q, t2, INVALID_HANDLE_VALUE);
719 ok(ret, "DeleteTimerQueueTimer\n");
721 ret = pDeleteTimerQueueEx(q, INVALID_HANDLE_VALUE);
722 ok(ret, "DeleteTimerQueueEx\n");
723 ok(n1 == 1, "Timer callback 1\n");
724 ok(n2 < n3, "Timer callback 2 should be much slower than 3\n");
725 ok(n4 == 0, "Timer callback 4\n");
726 ok(n5 == 1, "Timer callback 5\n");
728 /* Test synchronous deletion of the timer/queue with event trigger. */
729 e = CreateEvent(NULL, TRUE, FALSE, NULL);
730 et1 = CreateEvent(NULL, TRUE, FALSE, NULL);
731 et2 = CreateEvent(NULL, TRUE, FALSE, NULL);
732 if (!e || !et1 || !et2)
734 skip("Failed to create timer queue descruction event\n");
738 q = pCreateTimerQueue();
739 ok(q != NULL, "CreateTimerQueue\n");
741 /* Run once and finish quickly (should be done when we delete it). */
743 ret = pCreateTimerQueueTimer(&t1, q, timer_queue_cb5, (PVOID) 0, 0,
745 ok(ret, "CreateTimerQueueTimer\n");
746 ok(t1 != NULL, "CreateTimerQueueTimer\n");
748 /* Run once and finish slowly (shouldn't be done when we delete it). */
750 ret = pCreateTimerQueueTimer(&t2, q, timer_queue_cb5, (PVOID) 1000, 0,
752 ok(ret, "CreateTimerQueueTimer\n");
753 ok(t2 != NULL, "CreateTimerQueueTimer\n");
755 /* Run once and finish quickly (should be done when we delete it). */
757 ret = pCreateTimerQueueTimer(&t3, q, timer_queue_cb5, (PVOID) 0, 0,
759 ok(ret, "CreateTimerQueueTimer\n");
760 ok(t3 != NULL, "CreateTimerQueueTimer\n");
762 /* Run once and finish slowly (shouldn't be done when we delete it). */
764 ret = pCreateTimerQueueTimer(&t4, q, timer_queue_cb5, (PVOID) 1000, 0,
766 ok(ret, "CreateTimerQueueTimer\n");
767 ok(t4 != NULL, "CreateTimerQueueTimer\n");
769 /* Give them a chance to start. */
772 /* DeleteTimerQueueTimer always returns PENDING with a NULL event,
773 even if the timer is finished. */
774 SetLastError(0xdeadbeef);
775 ret = pDeleteTimerQueueTimer(q, t1, NULL);
776 ok(!ret, "DeleteTimerQueueTimer call was expected to fail\n");
777 ok(GetLastError() == ERROR_IO_PENDING,
778 "DeleteTimerQueueTimer, GetLastError: expected ERROR_IO_PENDING, got %d\n",
781 SetLastError(0xdeadbeef);
782 ret = pDeleteTimerQueueTimer(q, t2, NULL);
783 ok(!ret, "DeleteTimerQueueTimer call was expected to fail\n");
784 ok(GetLastError() == ERROR_IO_PENDING,
785 "DeleteTimerQueueTimer, GetLastError: expected ERROR_IO_PENDING, got %d\n",
788 SetLastError(0xdeadbeef);
789 ret = pDeleteTimerQueueTimer(q, t3, et1);
790 ok(ret, "DeleteTimerQueueTimer call was expected to fail\n");
791 ok(GetLastError() == 0xdeadbeef,
792 "DeleteTimerQueueTimer, GetLastError: expected 0xdeadbeef, got %d\n",
794 ok(WaitForSingleObject(et1, 250) == WAIT_OBJECT_0,
795 "Timer destruction event not triggered\n");
797 SetLastError(0xdeadbeef);
798 ret = pDeleteTimerQueueTimer(q, t4, et2);
799 ok(!ret, "DeleteTimerQueueTimer call was expected to fail\n");
800 ok(GetLastError() == ERROR_IO_PENDING,
801 "DeleteTimerQueueTimer, GetLastError: expected ERROR_IO_PENDING, got %d\n",
803 ok(WaitForSingleObject(et2, 1000) == WAIT_OBJECT_0,
804 "Timer destruction event not triggered\n");
806 SetLastError(0xdeadbeef);
807 ret = pDeleteTimerQueueEx(q, e);
808 ok(!ret, "DeleteTimerQueueEx call was expected to fail\n");
809 ok(GetLastError() == ERROR_IO_PENDING,
810 "DeleteTimerQueueEx, GetLastError: expected ERROR_IO_PENDING, got %d\n",
812 ok(WaitForSingleObject(e, 250) == WAIT_OBJECT_0,
813 "Queue destruction event not triggered\n");
816 /* Test deleting/changing a timer in execution. */
817 q = pCreateTimerQueue();
818 ok(q != NULL, "CreateTimerQueue\n");
820 /* Test changing a once-only timer before it fires (this is allowed,
821 whereas after it fires you cannot). */
823 ret = pCreateTimerQueueTimer(&t1, q, timer_queue_cb1, &n1, 10000,
825 ok(ret, "CreateTimerQueueTimer\n");
826 ok(t1 != NULL, "CreateTimerQueueTimer\n");
827 ret = pChangeTimerQueueTimer(q, t1, 0, 0);
828 ok(ret, "ChangeTimerQueueTimer\n");
834 ret = pCreateTimerQueueTimer(&t2, q, timer_queue_cb2, &d2, 10,
837 ok(ret, "CreateTimerQueueTimer\n");
838 ok(t2 != NULL, "CreateTimerQueueTimer\n");
844 ret = pCreateTimerQueueTimer(&t3, q, timer_queue_cb3, &d3, 10,
847 ok(ret, "CreateTimerQueueTimer\n");
848 ok(t3 != NULL, "CreateTimerQueueTimer\n");
853 ret = pCreateTimerQueueTimer(&t4, q, timer_queue_cb4, &d4, 10,
856 ok(ret, "CreateTimerQueueTimer\n");
857 ok(t4 != NULL, "CreateTimerQueueTimer\n");
861 ret = pDeleteTimerQueueEx(q, INVALID_HANDLE_VALUE);
862 ok(ret, "DeleteTimerQueueEx\n");
863 ok(n1 == 1, "ChangeTimerQueueTimer\n");
864 ok(d2.num_calls == d2.max_calls, "DeleteTimerQueueTimer\n");
865 ok(d3.num_calls == d3.max_calls, "ChangeTimerQueueTimer\n");
866 ok(d4.num_calls == 1, "Timer flagged for deletion incorrectly\n");
868 /* Test an obscure bug that was in the original implementation. */
869 q = pCreateTimerQueue();
870 ok(q != NULL, "CreateTimerQueue\n");
872 /* All the work is done in the callback. */
876 ret = pCreateTimerQueueTimer(&t1, q, timer_queue_cb6, &d1, 100,
877 100, WT_EXECUTELONGFUNCTION);
879 ok(ret, "CreateTimerQueueTimer\n");
880 ok(t1 != NULL, "CreateTimerQueueTimer\n");
884 SetLastError(0xdeadbeef);
885 ret = pDeleteTimerQueueEx(q, NULL);
886 ok(!ret, "DeleteTimerQueueEx call was expected to fail\n");
887 ok(GetLastError() == ERROR_IO_PENDING,
888 "DeleteTimerQueueEx, GetLastError: expected ERROR_IO_PENDING, got %d\n",
890 ok(d1.num_calls == 1, "DeleteTimerQueueTimer\n");
892 /* Test functions on the default timer queue. */
895 ret = pCreateTimerQueueTimer(&t1, NULL, timer_queue_cb1, &n1, 1000,
897 ok(ret, "CreateTimerQueueTimer, default queue\n");
898 ok(t1 != NULL, "CreateTimerQueueTimer, default queue\n");
900 ret = pChangeTimerQueueTimer(NULL, t1, 2000, 2000);
901 ok(ret, "ChangeTimerQueueTimer, default queue\n");
903 ret = pDeleteTimerQueueTimer(NULL, t1, INVALID_HANDLE_VALUE);
904 ok(ret, "DeleteTimerQueueTimer, default queue\n");
906 /* Try mixing default and non-default queues. Apparently this works. */
907 q = pCreateTimerQueue();
908 ok(q != NULL, "CreateTimerQueue\n");
912 ret = pCreateTimerQueueTimer(&t1, q, timer_queue_cb1, &n1, 1000,
914 ok(ret, "CreateTimerQueueTimer\n");
915 ok(t1 != NULL, "CreateTimerQueueTimer\n");
919 ret = pCreateTimerQueueTimer(&t2, NULL, timer_queue_cb1, &n2, 1000,
921 ok(ret, "CreateTimerQueueTimer\n");
922 ok(t2 != NULL, "CreateTimerQueueTimer\n");
924 ret = pChangeTimerQueueTimer(NULL, t1, 2000, 2000);
925 ok(ret, "ChangeTimerQueueTimer\n");
927 ret = pChangeTimerQueueTimer(q, t2, 2000, 2000);
928 ok(ret, "ChangeTimerQueueTimer\n");
930 ret = pDeleteTimerQueueTimer(NULL, t1, INVALID_HANDLE_VALUE);
931 ok(ret, "DeleteTimerQueueTimer\n");
933 ret = pDeleteTimerQueueTimer(q, t2, INVALID_HANDLE_VALUE);
934 ok(ret, "DeleteTimerQueueTimer\n");
936 /* Try to delete the default queue? In any case: not allowed. */
937 SetLastError(0xdeadbeef);
938 ret = pDeleteTimerQueueEx(NULL, NULL);
939 ok(!ret, "DeleteTimerQueueEx call was expected to fail\n");
940 ok(GetLastError() == ERROR_INVALID_HANDLE,
941 "DeleteTimerQueueEx, GetLastError: expected ERROR_INVALID_HANDLE, got %d\n",
944 SetLastError(0xdeadbeef);
945 ret = pDeleteTimerQueueEx(q, NULL);
946 ok(!ret, "DeleteTimerQueueEx call was expected to fail\n");
947 ok(GetLastError() == ERROR_IO_PENDING,
948 "DeleteTimerQueueEx, GetLastError: expected ERROR_IO_PENDING, got %d\n",
954 HMODULE hdll = GetModuleHandle("kernel32");
955 pChangeTimerQueueTimer = (void*)GetProcAddress(hdll, "ChangeTimerQueueTimer");
956 pCreateTimerQueue = (void*)GetProcAddress(hdll, "CreateTimerQueue");
957 pCreateTimerQueueTimer = (void*)GetProcAddress(hdll, "CreateTimerQueueTimer");
958 pCreateWaitableTimerA = (void*)GetProcAddress(hdll, "CreateWaitableTimerA");
959 pDeleteTimerQueueEx = (void*)GetProcAddress(hdll, "DeleteTimerQueueEx");
960 pDeleteTimerQueueTimer = (void*)GetProcAddress(hdll, "DeleteTimerQueueTimer");
961 pOpenWaitableTimerA = (void*)GetProcAddress(hdll, "OpenWaitableTimerA");
963 test_signalandwait();
968 test_waitable_timer();
969 test_iocp_callback();