Signed/unsigned warnings and some formatting.
[wine.git] / scheduler / synchro.c
blobb2ce0fe383b88f81dc7561f1296b4e14385b1c76
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
25 #include "ntstatus.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "../kernel/kernel_private.h" /* FIXME: to be changed when moving file to dlls/kernel */
33 /***********************************************************************
34 * Sleep (KERNEL32.@)
36 VOID WINAPI Sleep( DWORD timeout )
38 SleepEx( timeout, FALSE );
41 /******************************************************************************
42 * SleepEx (KERNEL32.@)
44 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
46 NTSTATUS status;
48 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
49 else
51 LARGE_INTEGER time;
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;
58 return status;
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,
75 BOOL alertable )
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,
96 BOOL alertable )
98 NTSTATUS status;
99 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
100 int i;
102 if (count >= MAXIMUM_WAIT_OBJECTS)
104 SetLastError(ERROR_INVALID_PARAMETER);
105 return WAIT_FAILED;
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] );
113 else
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]))
123 return FALSE;
125 hloc[i] = GetConsoleInputWaitHandle();
129 if (timeout == INFINITE)
131 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
133 else
135 LARGE_INTEGER time;
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;
147 return status;
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 );
161 return retval;
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 );
175 return retval;
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 );
189 return retval;