Implement anonymous pipes on top of named pipes.
[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 "winbase.h"
24 #include "winternl.h"
25
26
27 /***********************************************************************
28  *              Sleep  (KERNEL32.@)
29  */
30 VOID WINAPI Sleep( DWORD timeout )
31 {
32     WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
33 }
34
35 /******************************************************************************
36  *              SleepEx   (KERNEL32.@)
37  */
38 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
39 {
40     DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
41     if (ret != WAIT_IO_COMPLETION) ret = 0;
42     return ret;
43 }
44
45
46 /***********************************************************************
47  *           WaitForSingleObject   (KERNEL32.@)
48  */
49 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
50 {
51     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
52 }
53
54
55 /***********************************************************************
56  *           WaitForSingleObjectEx   (KERNEL32.@)
57  */
58 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
59                                     BOOL alertable )
60 {
61     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
62 }
63
64
65 /***********************************************************************
66  *           WaitForMultipleObjects   (KERNEL32.@)
67  */
68 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
69                                      BOOL wait_all, DWORD timeout )
70 {
71     return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
72 }
73
74
75 /***********************************************************************
76  *           WaitForMultipleObjectsEx   (KERNEL32.@)
77  */
78 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
79                                        BOOL wait_all, DWORD timeout,
80                                        BOOL alertable )
81 {
82     NTSTATUS status;
83
84     if (timeout == INFINITE)
85     {
86         status = NtWaitForMultipleObjects( count, handles, wait_all, alertable, NULL );
87     }
88     else
89     {
90         LARGE_INTEGER time;
91
92         time.QuadPart = timeout * (ULONGLONG)10000;
93         time.QuadPart = -time.QuadPart;
94         status = NtWaitForMultipleObjects( count, handles, wait_all, alertable, &time );
95     }
96
97     if (HIWORD(status))  /* is it an error code? */
98     {
99         SetLastError( RtlNtStatusToDosError(status) );
100         status = WAIT_FAILED;
101     }
102     return status;
103 }
104
105
106 /***********************************************************************
107  *           WaitForSingleObject   (KERNEL.460)
108  */
109 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
110 {
111     DWORD retval, mutex_count;
112
113     ReleaseThunkLock( &mutex_count );
114     retval = WaitForSingleObject( handle, timeout );
115     RestoreThunkLock( mutex_count );
116     return retval;
117 }
118
119 /***********************************************************************
120  *           WaitForMultipleObjects   (KERNEL.461)
121  */
122 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
123                                        BOOL wait_all, DWORD timeout )
124 {
125     DWORD retval, mutex_count;
126
127     ReleaseThunkLock( &mutex_count );
128     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
129     RestoreThunkLock( mutex_count );
130     return retval;
131 }
132
133 /***********************************************************************
134  *           WaitForMultipleObjectsEx   (KERNEL.495)
135  */
136 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
137                                          BOOL wait_all, DWORD timeout, BOOL alertable )
138 {
139     DWORD retval, mutex_count;
140
141     ReleaseThunkLock( &mutex_count );
142     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
143     RestoreThunkLock( mutex_count );
144     return retval;
145 }