2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
20 /***********************************************************************
21 * SYNC_BuildWaitStruct
23 static BOOL32
SYNC_BuildWaitStruct( DWORD count
, const HANDLE32
*handles
,
24 BOOL32 wait_all
, WAIT_STRUCT
*wait
)
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
,
39 ERR(win32
, "Bad handle %08x\n", handles
[i
]);
42 if (wait
->server
[i
] == -1)
43 WARN(win32
,"No server handle for %08x (type %d)\n",
44 handles
[i
], (*ptr
)->type
);
49 /* There was an error */
50 while (i
--) K32OBJ_DecCount( wait
->objs
[i
] );
57 /***********************************************************************
60 static void SYNC_FreeWaitStruct( WAIT_STRUCT
*wait
)
65 for (i
= 0, ptr
= wait
->objs
; i
< wait
->count
; i
++, ptr
++)
66 K32OBJ_DecCount( *ptr
);
71 /***********************************************************************
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
);
86 FIXME(win32
, "alertable not implemented\n" );
88 if (!SYNC_BuildWaitStruct( count
, handles
, wait_all
, wait
))
89 wait
->signaled
= WAIT_FAILED
;
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;
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
,
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
,
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
)
168 SYSLEVEL_ReleaseWin16Lock();
169 retval
= WaitForSingleObject( handle
, timeout
);
170 SYSLEVEL_RestoreWin16Lock();
175 /***********************************************************************
176 * WIN16_WaitForMultipleObjects (KERNEL.461)
178 DWORD WINAPI
WIN16_WaitForMultipleObjects( DWORD count
, const HANDLE32
*handles
,
179 BOOL32 wait_all
, DWORD timeout
)
183 SYSLEVEL_ReleaseWin16Lock();
184 retval
= WaitForMultipleObjects( count
, handles
, wait_all
, timeout
);
185 SYSLEVEL_RestoreWin16Lock();