Fixed CHECK_STRING display.
[wine/multimedia.git] / scheduler / synchro.c
blobc102c41609c2fe01e263a4f45df7e019b272140c
1 /*
2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <signal.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include "k32obj.h"
12 #include "heap.h"
13 #include "process.h"
14 #include "thread.h"
15 #include "winerror.h"
16 #include "syslevel.h"
17 #include "server.h"
18 #include "debug.h"
20 /***********************************************************************
21 * SYNC_BuildWaitStruct
23 static BOOL32 SYNC_BuildWaitStruct( DWORD count, const HANDLE32 *handles,
24 BOOL32 wait_all, WAIT_STRUCT *wait )
26 DWORD i;
27 K32OBJ **ptr;
29 SYSTEM_LOCK();
30 wait->count = count;
31 wait->signaled = WAIT_FAILED;
32 wait->wait_all = wait_all;
33 for (i = 0, ptr = wait->objs; i < count; i++, ptr++)
35 if (!(*ptr = HANDLE_GetObjPtr( PROCESS_Current(), handles[i],
36 K32OBJ_UNKNOWN, SYNCHRONIZE,
37 &wait->server[i] )))
39 ERR(win32, "Bad handle %08x\n", handles[i]);
40 break;
42 if (wait->server[i] == -1)
43 WARN(win32,"No server handle for %08x (type %d)\n",
44 handles[i], (*ptr)->type );
47 if (i != count)
49 /* There was an error */
50 while (i--) K32OBJ_DecCount( wait->objs[i] );
52 SYSTEM_UNLOCK();
53 return (i == count);
57 /***********************************************************************
58 * SYNC_FreeWaitStruct
60 static void SYNC_FreeWaitStruct( WAIT_STRUCT *wait )
62 DWORD i;
63 K32OBJ **ptr;
64 SYSTEM_LOCK();
65 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
66 K32OBJ_DecCount( *ptr );
67 SYSTEM_UNLOCK();
71 /***********************************************************************
72 * SYNC_DoWait
74 static DWORD SYNC_DoWait( DWORD count, const HANDLE32 *handles,
75 BOOL32 wait_all, DWORD timeout, BOOL32 alertable )
77 WAIT_STRUCT *wait = &THREAD_Current()->wait_struct;
79 if (count > MAXIMUM_WAIT_OBJECTS)
81 SetLastError( ERROR_INVALID_PARAMETER );
82 return WAIT_FAILED;
85 if (alertable)
86 FIXME(win32, "alertable not implemented\n" );
88 if (!SYNC_BuildWaitStruct( count, handles, wait_all, wait ))
89 wait->signaled = WAIT_FAILED;
90 else
92 int flags = 0;
93 if (wait_all) flags |= SELECT_ALL;
94 if (alertable) flags |= SELECT_ALERTABLE;
95 if (timeout != INFINITE32) flags |= SELECT_TIMEOUT;
96 wait->signaled = CLIENT_Select( count, wait->server, flags, timeout );
97 SYNC_FreeWaitStruct( wait );
99 return wait->signaled;
102 /***********************************************************************
103 * Sleep (KERNEL32.679)
105 VOID WINAPI Sleep( DWORD timeout )
107 SYNC_DoWait( 0, NULL, FALSE, timeout, FALSE );
110 /******************************************************************************
111 * SleepEx (KERNEL32.680)
113 DWORD WINAPI SleepEx( DWORD timeout, BOOL32 alertable )
115 DWORD ret = SYNC_DoWait( 0, NULL, FALSE, timeout, alertable );
116 if (ret != WAIT_IO_COMPLETION) ret = 0;
117 return ret;
121 /***********************************************************************
122 * WaitForSingleObject (KERNEL32.723)
124 DWORD WINAPI WaitForSingleObject( HANDLE32 handle, DWORD timeout )
126 return SYNC_DoWait( 1, &handle, FALSE, timeout, FALSE );
130 /***********************************************************************
131 * WaitForSingleObjectEx (KERNEL32.724)
133 DWORD WINAPI WaitForSingleObjectEx( HANDLE32 handle, DWORD timeout,
134 BOOL32 alertable )
136 return SYNC_DoWait( 1, &handle, FALSE, timeout, alertable );
140 /***********************************************************************
141 * WaitForMultipleObjects (KERNEL32.721)
143 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE32 *handles,
144 BOOL32 wait_all, DWORD timeout )
146 return SYNC_DoWait( count, handles, wait_all, timeout, FALSE );
150 /***********************************************************************
151 * WaitForMultipleObjectsEx (KERNEL32.722)
153 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE32 *handles,
154 BOOL32 wait_all, DWORD timeout,
155 BOOL32 alertable )
157 return SYNC_DoWait( count, handles, wait_all, timeout, alertable );
161 /***********************************************************************
162 * WIN16_WaitForSingleObject (KERNEL.460)
164 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE32 handle, DWORD timeout )
166 DWORD retval;
168 SYSLEVEL_ReleaseWin16Lock();
169 retval = WaitForSingleObject( handle, timeout );
170 SYSLEVEL_RestoreWin16Lock();
172 return retval;
175 /***********************************************************************
176 * WIN16_WaitForMultipleObjects (KERNEL.461)
178 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE32 *handles,
179 BOOL32 wait_all, DWORD timeout )
181 DWORD retval;
183 SYSLEVEL_ReleaseWin16Lock();
184 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
185 SYSLEVEL_RestoreWin16Lock();
187 return retval;