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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/test.h"
29 static void test_signalandwait(void)
31 DWORD (WINAPI *pSignalObjectAndWait)(HANDLE, HANDLE, DWORD, BOOL);
34 HANDLE event[2], semaphore[2], file;
36 kernel32 = GetModuleHandle("kernel32");
37 pSignalObjectAndWait = (void*) GetProcAddress(kernel32, "SignalObjectAndWait");
39 if (!pSignalObjectAndWait)
43 event[0] = CreateEvent(NULL, 0, 0, NULL);
44 event[1] = CreateEvent(NULL, 1, 1, NULL);
46 ok( event[0] && event[1], "failed to create event flags\n");
48 /* invalid parameters */
49 r = pSignalObjectAndWait(NULL, NULL, 0, 0);
50 ok( r == WAIT_FAILED, "should fail\n");
52 r = pSignalObjectAndWait(event[0], NULL, 0, FALSE);
53 ok( r == WAIT_FAILED, "should fail\n");
55 r = pSignalObjectAndWait(NULL, event[0], 0, FALSE);
56 ok( r == WAIT_FAILED, "should fail\n");
59 /* valid parameters */
60 r = pSignalObjectAndWait(event[0], event[1], 0, FALSE);
61 ok( r == WAIT_OBJECT_0, "should succeed\n");
63 /* event[0] is now signalled */
64 r = pSignalObjectAndWait(event[0], event[0], 0, FALSE);
65 ok( r == WAIT_OBJECT_0, "should succeed\n");
67 /* event[0] is not signalled */
68 r = WaitForSingleObject(event[0], 0);
69 ok( r == WAIT_TIMEOUT, "event was signalled\n");
71 r = pSignalObjectAndWait(event[0], event[0], 0, FALSE);
72 ok( r == WAIT_OBJECT_0, "should succeed\n");
74 /* clear event[1] and check for a timeout */
75 ok(ResetEvent(event[1]), "failed to clear event[1]\n");
76 r = pSignalObjectAndWait(event[0], event[1], 0, FALSE);
77 ok( r == WAIT_TIMEOUT, "should timeout\n");
79 CloseHandle(event[0]);
80 CloseHandle(event[1]);
84 semaphore[0] = CreateSemaphore( NULL, 0, 1, NULL );
85 semaphore[1] = CreateSemaphore( NULL, 1, 1, NULL );
86 ok( semaphore[0] && semaphore[1], "failed to create semaphore\n");
88 r = pSignalObjectAndWait(semaphore[0], semaphore[1], 0, FALSE);
89 ok( r == WAIT_OBJECT_0, "should succeed\n");
91 r = pSignalObjectAndWait(semaphore[0], semaphore[1], 0, FALSE);
92 ok( r == WAIT_FAILED, "should fail\n");
94 r = ReleaseSemaphore(semaphore[0],1,NULL);
95 ok( r == FALSE, "should fail\n");
97 r = ReleaseSemaphore(semaphore[1],1,NULL);
98 ok( r == TRUE, "should succeed\n");
100 CloseHandle(semaphore[0]);
101 CloseHandle(semaphore[1]);
103 /* try a registry key */
104 file = CreateFile("x", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
105 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
106 r = pSignalObjectAndWait(file, file, 0, FALSE);
107 ok( r == WAIT_FAILED, "should fail\n");
109 ok( ERROR_INVALID_HANDLE == GetLastError(), "should return invalid handle error\n");
116 test_signalandwait();