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
, BOOL32 wait_msg
,
32 wait
->signaled
= WAIT_FAILED
;
33 wait
->wait_all
= wait_all
;
34 wait
->wait_msg
= wait_msg
;
35 for (i
= 0, ptr
= wait
->objs
; i
< count
; i
++, ptr
++)
37 if (!(*ptr
= HANDLE_GetObjPtr( PROCESS_Current(), handles
[i
],
38 K32OBJ_UNKNOWN
, SYNCHRONIZE
,
41 ERR(win32
, "Bad handle %08x\n", handles
[i
]);
44 if (wait
->server
[i
] == -1)
45 WARN(win32
,"No server handle for %08x (type %d)\n",
46 handles
[i
], (*ptr
)->type
);
51 /* There was an error */
52 wait
->wait_msg
= FALSE
;
53 while (i
--) K32OBJ_DecCount( wait
->objs
[i
] );
60 /***********************************************************************
63 static void SYNC_FreeWaitStruct( WAIT_STRUCT
*wait
)
68 wait
->wait_msg
= FALSE
;
69 for (i
= 0, ptr
= wait
->objs
; i
< wait
->count
; i
++, ptr
++)
70 K32OBJ_DecCount( *ptr
);
75 /***********************************************************************
78 DWORD
SYNC_DoWait( DWORD count
, const HANDLE32
*handles
,
79 BOOL32 wait_all
, DWORD timeout
,
80 BOOL32 alertable
, BOOL32 wait_msg
)
82 WAIT_STRUCT
*wait
= &THREAD_Current()->wait_struct
;
84 if (count
> MAXIMUM_WAIT_OBJECTS
)
86 SetLastError( ERROR_INVALID_PARAMETER
);
91 FIXME(win32
, "alertable not implemented\n" );
93 if (!SYNC_BuildWaitStruct( count
, handles
, wait_all
, wait_msg
, wait
))
94 wait
->signaled
= WAIT_FAILED
;
98 if (wait_all
) flags
|= SELECT_ALL
;
99 if (alertable
) flags
|= SELECT_ALERTABLE
;
100 if (wait_msg
) flags
|= SELECT_MSG
;
101 if (timeout
!= INFINITE32
) flags
|= SELECT_TIMEOUT
;
102 wait
->signaled
= CLIENT_Select( count
, wait
->server
, flags
, timeout
);
103 SYNC_FreeWaitStruct( wait
);
105 return wait
->signaled
;
108 /***********************************************************************
109 * Sleep (KERNEL32.679)
111 VOID WINAPI
Sleep( DWORD timeout
)
113 SYNC_DoWait( 0, NULL
, FALSE
, timeout
, FALSE
, FALSE
);
116 /******************************************************************************
117 * SleepEx (KERNEL32.680)
119 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL32 alertable
)
121 DWORD ret
= SYNC_DoWait( 0, NULL
, FALSE
, timeout
, alertable
, FALSE
);
122 if (ret
!= WAIT_IO_COMPLETION
) ret
= 0;
127 /***********************************************************************
128 * WaitForSingleObject (KERNEL32.723)
130 DWORD WINAPI
WaitForSingleObject( HANDLE32 handle
, DWORD timeout
)
132 return SYNC_DoWait( 1, &handle
, FALSE
, timeout
, FALSE
, FALSE
);
136 /***********************************************************************
137 * WaitForSingleObjectEx (KERNEL32.724)
139 DWORD WINAPI
WaitForSingleObjectEx( HANDLE32 handle
, DWORD timeout
,
142 return SYNC_DoWait( 1, &handle
, FALSE
, timeout
, alertable
, FALSE
);
146 /***********************************************************************
147 * WaitForMultipleObjects (KERNEL32.721)
149 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE32
*handles
,
150 BOOL32 wait_all
, DWORD timeout
)
152 return SYNC_DoWait( count
, handles
, wait_all
, timeout
, FALSE
, FALSE
);
156 /***********************************************************************
157 * WaitForMultipleObjectsEx (KERNEL32.722)
159 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE32
*handles
,
160 BOOL32 wait_all
, DWORD timeout
,
163 return SYNC_DoWait( count
, handles
, wait_all
, timeout
, alertable
, FALSE
);
167 /***********************************************************************
168 * WIN16_WaitForSingleObject (KERNEL.460)
170 DWORD WINAPI
WIN16_WaitForSingleObject( HANDLE32 handle
, DWORD timeout
)
174 SYSLEVEL_ReleaseWin16Lock();
175 retval
= WaitForSingleObject( handle
, timeout
);
176 SYSLEVEL_RestoreWin16Lock();
181 /***********************************************************************
182 * WIN16_WaitForMultipleObjects (KERNEL.461)
184 DWORD WINAPI
WIN16_WaitForMultipleObjects( DWORD count
, const HANDLE32
*handles
,
185 BOOL32 wait_all
, DWORD timeout
)
189 SYSLEVEL_ReleaseWin16Lock();
190 retval
= WaitForMultipleObjects( count
, handles
, wait_all
, timeout
);
191 SYSLEVEL_RestoreWin16Lock();