Removed a lot of occurences of windows.h (and added necessary other
[wine] / scheduler / event.c
1 /*
2  * Win32 events
3  *
4  * Copyright 1998 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include "winerror.h"
9 #include "k32obj.h"
10 #include "process.h"
11 #include "thread.h"
12 #include "heap.h"
13 #include "syslevel.h"
14 #include "server/request.h"
15 #include "server.h"
16
17 typedef struct
18 {
19     K32OBJ        header;
20 } EVENT;
21
22
23 /***********************************************************************
24  *           CreateEvent32A    (KERNEL32.156)
25  */
26 HANDLE32 WINAPI CreateEvent32A( SECURITY_ATTRIBUTES *sa, BOOL32 manual_reset,
27                                 BOOL32 initial_state, LPCSTR name )
28 {
29     struct create_event_request req;
30     struct create_event_reply reply;
31     int len = name ? strlen(name) + 1 : 0;
32     HANDLE32 handle;
33     EVENT *event;
34
35     req.manual_reset = manual_reset;
36     req.initial_state = initial_state;
37     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
38
39     CLIENT_SendRequest( REQ_CREATE_EVENT, -1, 2, &req, sizeof(req), name, len );
40     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
41     if (reply.handle == -1) return 0;
42
43     SYSTEM_LOCK();
44     event = (EVENT *)K32OBJ_Create( K32OBJ_EVENT, sizeof(*event), name,
45                                     reply.handle, EVENT_ALL_ACCESS, sa, &handle );
46     if (event)
47         K32OBJ_DecCount( &event->header );
48     SYSTEM_UNLOCK();
49     if (handle == INVALID_HANDLE_VALUE32) handle = 0;
50     return handle;
51 }
52
53
54 /***********************************************************************
55  *           CreateEvent32W    (KERNEL32.157)
56  */
57 HANDLE32 WINAPI CreateEvent32W( SECURITY_ATTRIBUTES *sa, BOOL32 manual_reset,
58                                 BOOL32 initial_state, LPCWSTR name )
59 {
60     LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
61     HANDLE32 ret = CreateEvent32A( sa, manual_reset, initial_state, nameA );
62     if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
63     return ret;
64 }
65
66 /***********************************************************************
67  *           WIN16_CreateEvent    (KERNEL.457)
68  */
69 HANDLE32 WINAPI WIN16_CreateEvent( BOOL32 manual_reset, BOOL32 initial_state )
70 {
71     return CreateEvent32A( NULL, manual_reset, initial_state, NULL );
72 }
73
74
75 /***********************************************************************
76  *           OpenEvent32A    (KERNEL32.536)
77  */
78 HANDLE32 WINAPI OpenEvent32A( DWORD access, BOOL32 inherit, LPCSTR name )
79 {
80     HANDLE32 handle = 0;
81     K32OBJ *obj;
82     struct open_named_obj_request req;
83     struct open_named_obj_reply reply;
84     int len = name ? strlen(name) + 1 : 0;
85
86     req.type    = OPEN_EVENT;
87     req.access  = access;
88     req.inherit = inherit;
89     CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ, -1, 2, &req, sizeof(req), name, len );
90     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
91     if (reply.handle != -1)
92     {
93         SYSTEM_LOCK();
94         if ((obj = K32OBJ_FindNameType( name, K32OBJ_EVENT )) != NULL)
95         {
96             handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, reply.handle );
97             K32OBJ_DecCount( obj );
98             if (handle == INVALID_HANDLE_VALUE32)
99                 handle = 0; /* must return 0 on failure, not -1 */
100         }
101         else CLIENT_CloseHandle( reply.handle );
102         SYSTEM_UNLOCK();
103     }
104     return handle;
105 }
106
107
108 /***********************************************************************
109  *           OpenEvent32W    (KERNEL32.537)
110  */
111 HANDLE32 WINAPI OpenEvent32W( DWORD access, BOOL32 inherit, LPCWSTR name )
112 {
113     LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
114     HANDLE32 ret = OpenEvent32A( access, inherit, nameA );
115     if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
116     return ret;
117 }
118
119
120 /***********************************************************************
121  *           EVENT_Operation
122  *
123  * Execute an event operation (set,reset,pulse).
124  */
125 static BOOL32 EVENT_Operation( HANDLE32 handle, enum event_op op )
126 {
127     struct event_op_request req;
128
129     req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
130                                          K32OBJ_EVENT, EVENT_MODIFY_STATE );
131     if (req.handle == -1) return FALSE;
132     req.op = op;
133     CLIENT_SendRequest( REQ_EVENT_OP, -1, 1, &req, sizeof(req) );
134     return !CLIENT_WaitReply( NULL, NULL, 0 );
135 }
136
137
138 /***********************************************************************
139  *           PulseEvent    (KERNEL32.557)
140  */
141 BOOL32 WINAPI PulseEvent( HANDLE32 handle )
142 {
143     return EVENT_Operation( handle, PULSE_EVENT );
144 }
145
146
147 /***********************************************************************
148  *           SetEvent    (KERNEL32.644)
149  */
150 BOOL32 WINAPI SetEvent( HANDLE32 handle )
151 {
152     return EVENT_Operation( handle, SET_EVENT );
153 }
154
155
156 /***********************************************************************
157  *           ResetEvent    (KERNEL32.586)
158  */
159 BOOL32 WINAPI ResetEvent( HANDLE32 handle )
160 {
161     return EVENT_Operation( handle, RESET_EVENT );
162 }
163
164
165 /***********************************************************************
166  * NOTE: The Win95 VWin32_Event routines given below are really low-level
167  *       routines implemented directly by VWin32. The user-mode libraries
168  *       implement Win32 synchronisation routines on top of these low-level
169  *       primitives. We do it the other way around here :-)
170  */
171
172 /***********************************************************************
173  *       VWin32_EventCreate     (KERNEL.442)
174  */
175 HANDLE32 WINAPI VWin32_EventCreate(VOID)
176 {
177     HANDLE32 hEvent = CreateEvent32A( NULL, FALSE, 0, NULL );
178     return ConvertToGlobalHandle( hEvent );
179 }
180
181 /***********************************************************************
182  *       VWin32_EventDestroy    (KERNEL.443)
183  */
184 VOID WINAPI VWin32_EventDestroy(HANDLE32 event)
185 {
186     CloseHandle( event );
187 }
188
189 /***********************************************************************
190  *       VWin32_EventWait       (KERNEL.450) 
191  */
192 VOID WINAPI VWin32_EventWait(HANDLE32 event)
193 {
194     SYSLEVEL_ReleaseWin16Lock();
195     WaitForSingleObject( event, INFINITE32 );
196     SYSLEVEL_RestoreWin16Lock();
197 }
198
199 /***********************************************************************
200  *       VWin32_EventSet        (KERNEL.451)
201  */
202 VOID WINAPI VWin32_EventSet(HANDLE32 event)
203 {
204     SetEvent( event );
205 }
206