Added #ifdef HAVE_WCTYPE_H.
[wine] / scheduler / synchro.c
1 /*
2  * Win32 process and thread synchronisation
3  *
4  * Copyright 1997 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <signal.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include "heap.h"
12 #include "process.h"
13 #include "thread.h"
14 #include "winerror.h"
15 #include "syslevel.h"
16 #include "server.h"
17 #include "debug.h"
18
19
20 /***********************************************************************
21  *              Sleep  (KERNEL32.679)
22  */
23 VOID WINAPI Sleep( DWORD timeout )
24 {
25     WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
26 }
27
28 /******************************************************************************
29  *              SleepEx   (KERNEL32.680)
30  */
31 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
32 {
33     DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
34     if (ret != WAIT_IO_COMPLETION) ret = 0;
35     return ret;
36 }
37
38
39 /***********************************************************************
40  *           WaitForSingleObject   (KERNEL32.723)
41  */
42 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
43 {
44     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
45 }
46
47
48 /***********************************************************************
49  *           WaitForSingleObjectEx   (KERNEL32.724)
50  */
51 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
52                                     BOOL alertable )
53 {
54     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
55 }
56
57
58 /***********************************************************************
59  *           WaitForMultipleObjects   (KERNEL32.721)
60  */
61 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
62                                      BOOL wait_all, DWORD timeout )
63 {
64     return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
65 }
66
67
68 /***********************************************************************
69  *           WaitForMultipleObjectsEx   (KERNEL32.722)
70  */
71 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
72                                        BOOL wait_all, DWORD timeout,
73                                        BOOL alertable )
74 {
75     struct select_request req;
76     struct select_reply reply;
77     int server_handle[MAXIMUM_WAIT_OBJECTS];
78     void *apc[32];
79     int i, len;
80
81     if (count > MAXIMUM_WAIT_OBJECTS)
82     {
83         SetLastError( ERROR_INVALID_PARAMETER );
84         return WAIT_FAILED;
85     }
86
87     for (i = 0; i < count; i++) server_handle[i] = handles[i];
88
89     req.count   = count;
90     req.flags   = 0;
91     req.timeout = timeout;
92
93     if (wait_all) req.flags |= SELECT_ALL;
94     if (alertable) req.flags |= SELECT_ALERTABLE;
95     if (timeout != INFINITE) req.flags |= SELECT_TIMEOUT;
96
97     CLIENT_SendRequest( REQ_SELECT, -1, 2,
98                         &req, sizeof(req),
99                         server_handle, count * sizeof(int) );
100     CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
101                       apc, sizeof(apc) );
102     if ((reply.signaled == STATUS_USER_APC) && (len > sizeof(reply)))
103     {
104         int i;
105         len -= sizeof(reply);
106         for (i = 0; i < len / sizeof(void*); i += 2)
107         {
108             PAPCFUNC func = (PAPCFUNC)apc[i];
109             func( (ULONG_PTR)apc[i+1] );
110         }
111     }
112     return reply.signaled;
113 }
114
115
116 /***********************************************************************
117  *           WIN16_WaitForSingleObject   (KERNEL.460)
118  */
119 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
120 {
121     DWORD retval;
122
123     SYSLEVEL_ReleaseWin16Lock();
124     retval = WaitForSingleObject( handle, timeout );
125     SYSLEVEL_RestoreWin16Lock();
126
127     return retval;
128 }
129
130 /***********************************************************************
131  *           WIN16_WaitForMultipleObjects   (KERNEL.461)
132  */
133 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
134                                            BOOL wait_all, DWORD timeout )
135 {
136     DWORD retval;
137
138     SYSLEVEL_ReleaseWin16Lock();
139     retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
140     SYSLEVEL_RestoreWin16Lock();
141
142     return retval;
143 }
144
145 /***********************************************************************
146  *           WIN16_WaitForMultipleObjectsEx   (KERNEL.495)
147  */
148 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count, 
149                                              const HANDLE *handles,
150                                              BOOL wait_all, DWORD timeout,
151                                              BOOL alertable )
152 {
153     DWORD retval;
154
155     SYSLEVEL_ReleaseWin16Lock();
156     retval = WaitForMultipleObjectsEx( count, handles, 
157                                        wait_all, timeout, alertable );
158     SYSLEVEL_RestoreWin16Lock();
159
160     return retval;
161 }
162