2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
19 /***********************************************************************
20 * SYNC_BuildWaitStruct
22 static BOOL32 SYNC_BuildWaitStruct( DWORD count, const HANDLE32 *handles,
23 BOOL32 wait_all, WAIT_STRUCT *wait )
29 wait->signaled = WAIT_FAILED;
30 wait->wait_all = wait_all;
32 for (i = 0, ptr = wait->objs; i < count; i++, ptr++)
34 if (!(*ptr = HANDLE_GetObjPtr( PROCESS_Current(), handles[i],
35 K32OBJ_UNKNOWN, SYNCHRONIZE )))
37 if (!K32OBJ_OPS( *ptr )->signaled)
39 /* This object type cannot be waited upon */
40 K32OBJ_DecCount( *ptr );
47 /* There was an error */
48 while (i--) K32OBJ_DecCount( wait->objs[i] );
55 /***********************************************************************
58 static void SYNC_FreeWaitStruct( WAIT_STRUCT *wait )
63 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
64 K32OBJ_DecCount( *ptr );
69 /***********************************************************************
72 static BOOL32 SYNC_CheckCondition( WAIT_STRUCT *wait, DWORD thread_id )
80 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
82 if (!K32OBJ_OPS( *ptr )->signaled( *ptr, thread_id ))
88 /* Wait satisfied: tell it to all objects */
89 wait->signaled = WAIT_OBJECT_0;
90 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
91 if (K32OBJ_OPS( *ptr )->satisfied( *ptr, thread_id ))
92 wait->signaled = WAIT_ABANDONED_0;
98 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
100 if (K32OBJ_OPS( *ptr )->signaled( *ptr, thread_id ))
102 /* Wait satisfied: tell it to the object */
103 wait->signaled = WAIT_OBJECT_0 + i;
104 if (K32OBJ_OPS( *ptr )->satisfied( *ptr, thread_id ))
105 wait->signaled = WAIT_ABANDONED_0 + i;
116 /***********************************************************************
117 * SYNC_WaitForCondition
119 void SYNC_WaitForCondition( WAIT_STRUCT *wait, DWORD timeout )
121 DWORD i, thread_id = GetCurrentThreadId();
127 if (SYNC_CheckCondition( wait, thread_id ))
128 goto done; /* Condition already satisfied */
131 /* No need to wait */
132 wait->signaled = WAIT_TIMEOUT;
136 /* Add ourselves to the waiting list of all objects */
138 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
139 K32OBJ_OPS( *ptr )->add_wait( *ptr, thread_id );
141 /* Release the system lock completely */
143 count = SYSTEM_LOCK_COUNT();
144 for (i = count; i > 0; i--) SYSTEM_UNLOCK();
146 /* Now wait for it */
148 TRACE(win32, "starting wait (%p %04x)\n",
149 THREAD_Current(), THREAD_Current()->teb_sel );
151 sigprocmask( SIG_SETMASK, NULL, &set );
152 sigdelset( &set, SIGUSR1 );
153 sigdelset( &set, SIGALRM );
154 if (timeout != INFINITE32)
156 while (wait->signaled == WAIT_FAILED)
158 struct itimerval timer;
159 DWORD start_ticks, elapsed;
160 timer.it_interval.tv_sec = timer.it_interval.tv_usec = 0;
161 timer.it_value.tv_sec = timeout / 1000;
162 timer.it_value.tv_usec = (timeout % 1000) * 1000;
163 start_ticks = GetTickCount();
164 setitimer( ITIMER_REAL, &timer, NULL );
166 if (wait->signaled != WAIT_FAILED) break;
167 /* Recompute the timer value */
168 elapsed = GetTickCount() - start_ticks;
169 if (elapsed >= timeout) wait->signaled = WAIT_TIMEOUT;
170 else timeout -= elapsed;
175 while (wait->signaled == WAIT_FAILED)
181 /* Grab the system lock again */
183 while (count--) SYSTEM_LOCK();
184 TRACE(win32, "wait finished (%p %04x)\n",
185 THREAD_Current(), THREAD_Current()->teb_sel );
187 /* Remove ourselves from the lists */
189 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
190 K32OBJ_OPS( *ptr )->remove_wait( *ptr, thread_id );
197 /***********************************************************************
198 * SYNC_DummySigHandler
200 * Dummy signal handler
202 static void SYNC_DummySigHandler(void)
207 /***********************************************************************
210 * Setup signal handlers for a new thread.
211 * FIXME: should merge with SIGNAL_Init.
213 void SYNC_SetupSignals(void)
216 SIGNAL_SetHandler( SIGUSR1, SYNC_DummySigHandler, 0 );
217 /* FIXME: conflicts with system timers */
218 SIGNAL_SetHandler( SIGALRM, SYNC_DummySigHandler, 0 );
220 /* Make sure these are blocked by default */
221 sigaddset( &set, SIGUSR1 );
222 sigaddset( &set, SIGALRM );
223 sigprocmask( SIG_BLOCK , &set, NULL);
227 /***********************************************************************
230 void SYNC_WakeUp( THREAD_QUEUE *wait_queue, DWORD max )
234 if (!max) max = INFINITE32;
241 entry = (*wait_queue)->next;
244 THDB *thdb = entry->thread;
245 if (SYNC_CheckCondition( &thdb->wait_struct, THDB_TO_THREAD_ID(thdb) ))
247 TRACE(win32, "waking up %04x\n", thdb->teb_sel );
248 kill( thdb->unix_pid, SIGUSR1 );
251 if (entry == *wait_queue) break;
258 /***********************************************************************
259 * WaitForSingleObject (KERNEL32.723)
261 DWORD WINAPI WaitForSingleObject( HANDLE32 handle, DWORD timeout )
263 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
267 /***********************************************************************
268 * WaitForSingleObjectEx (KERNEL32.724)
270 DWORD WINAPI WaitForSingleObjectEx( HANDLE32 handle, DWORD timeout,
273 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
277 /***********************************************************************
278 * WaitForMultipleObjects (KERNEL32.721)
280 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE32 *handles,
281 BOOL32 wait_all, DWORD timeout )
283 return WaitForMultipleObjectsEx(count, handles, wait_all, timeout, FALSE);
287 /***********************************************************************
288 * WaitForMultipleObjectsEx (KERNEL32.722)
290 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE32 *handles,
291 BOOL32 wait_all, DWORD timeout,
294 WAIT_STRUCT *wait = &THREAD_Current()->wait_struct;
296 if (count > MAXIMUM_WAIT_OBJECTS)
298 SetLastError( ERROR_INVALID_PARAMETER );
304 "WaitForMultipleObjectEx: alertable not implemented\n" );
307 if (!SYNC_BuildWaitStruct( count, handles, wait_all, wait ))
308 wait->signaled = WAIT_FAILED;
311 /* Now wait for it */
312 SYNC_WaitForCondition( wait, timeout );
313 SYNC_FreeWaitStruct( wait );
316 return wait->signaled;