Fix the case of product and company names.
[wine] / scheduler / synchro.c
1 /*
2  * Win32 process and thread synchronisation
3  *
4  * Copyright 1997 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
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 */
31
32
33 /***********************************************************************
34  *              Sleep  (KERNEL32.@)
35  */
36 VOID WINAPI Sleep( DWORD timeout )
37 {
38     SleepEx( timeout, FALSE );
39 }
40
41 /******************************************************************************
42  *              SleepEx   (KERNEL32.@)
43  */
44 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
45 {
46     NTSTATUS status;
47
48     if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
49     else
50     {
51         LARGE_INTEGER time;
52
53         time.QuadPart = timeout * (ULONGLONG)10000;
54         time.QuadPart = -time.QuadPart;
55         status = NtDelayExecution( alertable, &time );
56     }
57     if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
58     return status;
59 }
60
61
62 /***********************************************************************
63  *           WaitForSingleObject   (KERNEL32.@)
64  */
65 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
66 {
67     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
68 }
69
70
71 /***********************************************************************
72  *           WaitForSingleObjectEx   (KERNEL32.@)
73  */
74 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
75                                     BOOL alertable )
76 {
77     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
78 }
79
80
81 /***********************************************************************
82  *           WaitForMultipleObjects   (KERNEL32.@)
83  */
84 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
85                                      BOOL wait_all, DWORD timeout )
86 {
87     return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
88 }
89
90
91 /***********************************************************************
92  *           WaitForMultipleObjectsEx   (KERNEL32.@)
93  */
94 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
95                                        BOOL wait_all, DWORD timeout,
96                                        BOOL alertable )
97 {
98     NTSTATUS status;
99     HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
100     int i;
101
102     if (count >= MAXIMUM_WAIT_OBJECTS)
103     {
104         SetLastError(ERROR_INVALID_PARAMETER);
105         return WAIT_FAILED;
106     }
107     for (i = 0; i < count; i++)
108     {
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];
115
116         /* yes, even screen buffer console handles are waitable, and are
117          * handled as a handle to the console itself !!
118          */
119         if (is_console_handle(hloc[i]))
120         {
121             if (!VerifyConsoleIoHandle(hloc[i]))
122             {
123                 return FALSE;
124             }
125             hloc[i] = GetConsoleInputWaitHandle();
126         }
127     }
128
129     if (timeout == INFINITE)
130     {
131         status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
132     }
133     else
134     {
135         LARGE_INTEGER time;
136
137         time.QuadPart = timeout * (ULONGLONG)10000;
138         time.QuadPart = -time.QuadPart;
139         status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
140     }
141
142     if (HIWORD(status))  /* is it an error code? */
143     {
144         SetLastError( RtlNtStatusToDosError(status) );
145         status = WAIT_FAILED;
146     }
147     return status;
148 }
149
150
151 /***********************************************************************
152  *           WaitForSingleObject   (KERNEL.460)
153  */
154 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
155 {
156     DWORD retval, mutex_count;
157
158     ReleaseThunkLock( &mutex_count );
159     retval = WaitForSingleObject( handle, timeout );
160     RestoreThunkLock( mutex_count );
161     return retval;
162 }
163
164 /***********************************************************************
165  *           WaitForMultipleObjects   (KERNEL.461)
166  */
167 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
168                                        BOOL wait_all, DWORD timeout )
169 {
170     DWORD retval, mutex_count;
171
172     ReleaseThunkLock( &mutex_count );
173     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
174     RestoreThunkLock( mutex_count );
175     return retval;
176 }
177
178 /***********************************************************************
179  *           WaitForMultipleObjectsEx   (KERNEL.495)
180  */
181 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
182                                          BOOL wait_all, DWORD timeout, BOOL alertable )
183 {
184     DWORD retval, mutex_count;
185
186     ReleaseThunkLock( &mutex_count );
187     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
188     RestoreThunkLock( mutex_count );
189     return retval;
190 }