2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
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
30 #include "../kernel/kernel_private.h" /* FIXME: to be changed when moving file to dlls/kernel */
33 /***********************************************************************
36 VOID WINAPI
Sleep( DWORD timeout
)
38 SleepEx( timeout
, FALSE
);
41 /******************************************************************************
42 * SleepEx (KERNEL32.@)
44 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
48 if (timeout
== INFINITE
) status
= NtDelayExecution( alertable
, NULL
);
53 time
.QuadPart
= timeout
* (ULONGLONG
)10000;
54 time
.QuadPart
= -time
.QuadPart
;
55 status
= NtDelayExecution( alertable
, &time
);
57 if (status
!= STATUS_USER_APC
) status
= STATUS_SUCCESS
;
62 /***********************************************************************
63 * WaitForSingleObject (KERNEL32.@)
65 DWORD WINAPI
WaitForSingleObject( HANDLE handle
, DWORD timeout
)
67 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, FALSE
);
71 /***********************************************************************
72 * WaitForSingleObjectEx (KERNEL32.@)
74 DWORD WINAPI
WaitForSingleObjectEx( HANDLE handle
, DWORD timeout
,
77 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, alertable
);
81 /***********************************************************************
82 * WaitForMultipleObjects (KERNEL32.@)
84 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
85 BOOL wait_all
, DWORD timeout
)
87 return WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
91 /***********************************************************************
92 * WaitForMultipleObjectsEx (KERNEL32.@)
94 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE
*handles
,
95 BOOL wait_all
, DWORD timeout
,
99 HANDLE hloc
[MAXIMUM_WAIT_OBJECTS
];
102 if (count
>= MAXIMUM_WAIT_OBJECTS
)
104 SetLastError(ERROR_INVALID_PARAMETER
);
107 for (i
= 0; i
< count
; i
++)
109 if ((handles
[i
] == (HANDLE
)STD_INPUT_HANDLE
) ||
110 (handles
[i
] == (HANDLE
)STD_OUTPUT_HANDLE
) ||
111 (handles
[i
] == (HANDLE
)STD_ERROR_HANDLE
))
112 hloc
[i
] = GetStdHandle( (DWORD
)handles
[i
] );
114 hloc
[i
] = handles
[i
];
116 /* yes, even screen buffer console handles are waitable, and are
117 * handled as a handle to the console itself !!
119 if (is_console_handle(hloc
[i
]))
121 if (!VerifyConsoleIoHandle(hloc
[i
]))
125 hloc
[i
] = GetConsoleInputWaitHandle();
129 if (timeout
== INFINITE
)
131 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, NULL
);
137 time
.QuadPart
= timeout
* (ULONGLONG
)10000;
138 time
.QuadPart
= -time
.QuadPart
;
139 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, &time
);
142 if (HIWORD(status
)) /* is it an error code? */
144 SetLastError( RtlNtStatusToDosError(status
) );
145 status
= WAIT_FAILED
;
151 /***********************************************************************
152 * WaitForSingleObject (KERNEL.460)
154 DWORD WINAPI
WaitForSingleObject16( HANDLE handle
, DWORD timeout
)
156 DWORD retval
, mutex_count
;
158 ReleaseThunkLock( &mutex_count
);
159 retval
= WaitForSingleObject( handle
, timeout
);
160 RestoreThunkLock( mutex_count
);
164 /***********************************************************************
165 * WaitForMultipleObjects (KERNEL.461)
167 DWORD WINAPI
WaitForMultipleObjects16( DWORD count
, const HANDLE
*handles
,
168 BOOL wait_all
, DWORD timeout
)
170 DWORD retval
, mutex_count
;
172 ReleaseThunkLock( &mutex_count
);
173 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
174 RestoreThunkLock( mutex_count
);
178 /***********************************************************************
179 * WaitForMultipleObjectsEx (KERNEL.495)
181 DWORD WINAPI
WaitForMultipleObjectsEx16( DWORD count
, const HANDLE
*handles
,
182 BOOL wait_all
, DWORD timeout
, BOOL alertable
)
184 DWORD retval
, mutex_count
;
186 ReleaseThunkLock( &mutex_count
);
187 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, alertable
);
188 RestoreThunkLock( mutex_count
);