blob: 552568d773bfa5dcf47b7fc6cb1195616c6ff6f2 [file] [log] [blame]
Alexandre Julliard02e90081998-01-04 17:49:09 +00001/*
2 * Win32 events
3 *
4 * Copyright 1998 Alexandre Julliard
5 */
6
7#include <assert.h>
8#include "windows.h"
9#include "winerror.h"
10#include "k32obj.h"
11#include "process.h"
12#include "thread.h"
13#include "heap.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000014#include "syslevel.h"
15#include "server/request.h"
16#include "server.h"
Alexandre Julliard02e90081998-01-04 17:49:09 +000017
18typedef struct
19{
20 K32OBJ header;
21 THREAD_QUEUE wait_queue;
22 BOOL32 manual_reset;
23 BOOL32 signaled;
24} EVENT;
25
26static BOOL32 EVENT_Signaled( K32OBJ *obj, DWORD thread_id );
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000027static BOOL32 EVENT_Satisfied( K32OBJ *obj, DWORD thread_id );
Alexandre Julliard02e90081998-01-04 17:49:09 +000028static void EVENT_AddWait( K32OBJ *obj, DWORD thread_id );
29static void EVENT_RemoveWait( K32OBJ *obj, DWORD thread_id );
30static void EVENT_Destroy( K32OBJ *obj );
31
32const K32OBJ_OPS EVENT_Ops =
33{
34 EVENT_Signaled, /* signaled */
35 EVENT_Satisfied, /* satisfied */
36 EVENT_AddWait, /* add_wait */
37 EVENT_RemoveWait, /* remove_wait */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +000038 NULL, /* read */
39 NULL, /* write */
Alexandre Julliard02e90081998-01-04 17:49:09 +000040 EVENT_Destroy /* destroy */
41};
42
43
44/***********************************************************************
45 * EVENT_Set
46 *
47 * Implementation of SetEvent. Used by ExitThread and ExitProcess.
48 */
49void EVENT_Set( K32OBJ *obj )
50{
51 EVENT *event = (EVENT *)obj;
52 assert( obj->type == K32OBJ_EVENT );
53 SYSTEM_LOCK();
54 event->signaled = TRUE;
55 SYNC_WakeUp( &event->wait_queue, event->manual_reset ? INFINITE32 : 1 );
56 SYSTEM_UNLOCK();
57}
58
59/***********************************************************************
60 * EVENT_Create
61 *
62 * Partial implementation of CreateEvent.
63 * Used internally by processes and threads.
64 */
65K32OBJ *EVENT_Create( BOOL32 manual_reset, BOOL32 initial_state )
66{
67 EVENT *event;
68
69 SYSTEM_LOCK();
70 if ((event = HeapAlloc( SystemHeap, 0, sizeof(*event) )))
71 {
72 event->header.type = K32OBJ_EVENT;
73 event->header.refcount = 1;
74 event->wait_queue = NULL;
75 event->manual_reset = manual_reset;
76 event->signaled = initial_state;
77 }
78 SYSTEM_UNLOCK();
79 return event ? &event->header : NULL;
80}
81
82
83/***********************************************************************
84 * CreateEvent32A (KERNEL32.156)
85 */
86HANDLE32 WINAPI CreateEvent32A( SECURITY_ATTRIBUTES *sa, BOOL32 manual_reset,
87 BOOL32 initial_state, LPCSTR name )
88{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000089 struct create_event_request req;
90 struct create_event_reply reply;
91 int len = name ? strlen(name) + 1 : 0;
Alexandre Julliard02e90081998-01-04 17:49:09 +000092 HANDLE32 handle;
93 EVENT *event;
94
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000095 req.manual_reset = manual_reset;
96 req.initial_state = initial_state;
97 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
98
99 CLIENT_SendRequest( REQ_CREATE_EVENT, -1, 2, &req, sizeof(req), name, len );
100 CLIENT_WaitReply( &len, NULL, 1, &reply, sizeof(reply) );
101 CHECK_LEN( len, sizeof(reply) );
102 if (reply.handle == -1) return NULL;
103
Alexandre Julliard02e90081998-01-04 17:49:09 +0000104 SYSTEM_LOCK();
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000105 event = (EVENT *)K32OBJ_Create( K32OBJ_EVENT, sizeof(*event), name,
106 reply.handle, EVENT_ALL_ACCESS, sa, &handle );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000107 if (event)
108 {
109 /* Finish initializing it */
110 event->wait_queue = NULL;
111 event->manual_reset = manual_reset;
112 event->signaled = initial_state;
113 K32OBJ_DecCount( &event->header );
114 }
115 SYSTEM_UNLOCK();
116 return handle;
117}
118
119
120/***********************************************************************
121 * CreateEvent32W (KERNEL32.157)
122 */
123HANDLE32 WINAPI CreateEvent32W( SECURITY_ATTRIBUTES *sa, BOOL32 manual_reset,
124 BOOL32 initial_state, LPCWSTR name )
125{
126 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
127 HANDLE32 ret = CreateEvent32A( sa, manual_reset, initial_state, nameA );
128 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
129 return ret;
130}
131
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000132/***********************************************************************
133 * WIN16_CreateEvent (KERNEL.457)
134 */
135HANDLE32 WINAPI WIN16_CreateEvent( BOOL32 manual_reset, BOOL32 initial_state )
136{
137 return CreateEvent32A( NULL, manual_reset, initial_state, NULL );
138}
139
Alexandre Julliard02e90081998-01-04 17:49:09 +0000140
141/***********************************************************************
142 * OpenEvent32A (KERNEL32.536)
143 */
144HANDLE32 WINAPI OpenEvent32A( DWORD access, BOOL32 inherit, LPCSTR name )
145{
146 HANDLE32 handle = 0;
147 K32OBJ *obj;
148 SYSTEM_LOCK();
149 if ((obj = K32OBJ_FindNameType( name, K32OBJ_EVENT )) != NULL)
150 {
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000151 handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, -1 );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000152 K32OBJ_DecCount( obj );
153 }
154 SYSTEM_UNLOCK();
155 return handle;
156}
157
158
159/***********************************************************************
160 * OpenEvent32W (KERNEL32.537)
161 */
162HANDLE32 WINAPI OpenEvent32W( DWORD access, BOOL32 inherit, LPCWSTR name )
163{
164 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
165 HANDLE32 ret = OpenEvent32A( access, inherit, nameA );
166 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
167 return ret;
168}
169
170
171/***********************************************************************
172 * PulseEvent (KERNEL32.557)
173 */
174BOOL32 WINAPI PulseEvent( HANDLE32 handle )
175{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000176 struct event_op_request req;
Alexandre Julliard02e90081998-01-04 17:49:09 +0000177 EVENT *event;
178 SYSTEM_LOCK();
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000179 if (!(event = (EVENT *)HANDLE_GetObjPtr(PROCESS_Current(), handle,
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000180 K32OBJ_EVENT, EVENT_MODIFY_STATE,
181 &req.handle )))
Alexandre Julliard02e90081998-01-04 17:49:09 +0000182 {
183 SYSTEM_UNLOCK();
184 return FALSE;
185 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000186 if (req.handle != -1)
187 {
188 SYSTEM_UNLOCK();
189 req.op = PULSE_EVENT;
190 CLIENT_SendRequest( REQ_EVENT_OP, -1, 1, &req, sizeof(req) );
191 return !CLIENT_WaitReply( NULL, NULL, 0 );
192 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000193 event->signaled = TRUE;
194 SYNC_WakeUp( &event->wait_queue, event->manual_reset ? INFINITE32 : 1 );
195 event->signaled = FALSE;
196 K32OBJ_DecCount( &event->header );
197 SYSTEM_UNLOCK();
198 return TRUE;
199}
200
201
202/***********************************************************************
203 * SetEvent (KERNEL32.644)
204 */
205BOOL32 WINAPI SetEvent( HANDLE32 handle )
206{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000207 struct event_op_request req;
Alexandre Julliard02e90081998-01-04 17:49:09 +0000208 EVENT *event;
209 SYSTEM_LOCK();
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000210 if (!(event = (EVENT *)HANDLE_GetObjPtr(PROCESS_Current(), handle,
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000211 K32OBJ_EVENT, EVENT_MODIFY_STATE,
212 &req.handle )))
Alexandre Julliard02e90081998-01-04 17:49:09 +0000213 {
214 SYSTEM_UNLOCK();
215 return FALSE;
216 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000217 if (req.handle != -1)
218 {
219 SYSTEM_UNLOCK();
220 req.op = SET_EVENT;
221 CLIENT_SendRequest( REQ_EVENT_OP, -1, 1, &req, sizeof(req) );
222 return !CLIENT_WaitReply( NULL, NULL, 0 );
223 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000224 event->signaled = TRUE;
225 SYNC_WakeUp( &event->wait_queue, event->manual_reset ? INFINITE32 : 1 );
226 K32OBJ_DecCount( &event->header );
227 SYSTEM_UNLOCK();
228 return TRUE;
229}
230
231
232/***********************************************************************
233 * ResetEvent (KERNEL32.586)
234 */
235BOOL32 WINAPI ResetEvent( HANDLE32 handle )
236{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000237 struct event_op_request req;
Alexandre Julliard02e90081998-01-04 17:49:09 +0000238 EVENT *event;
239 SYSTEM_LOCK();
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000240 if (!(event = (EVENT *)HANDLE_GetObjPtr(PROCESS_Current(), handle,
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000241 K32OBJ_EVENT, EVENT_MODIFY_STATE,
242 &req.handle )))
Alexandre Julliard02e90081998-01-04 17:49:09 +0000243 {
244 SYSTEM_UNLOCK();
245 return FALSE;
246 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000247 if (req.handle != -1)
248 {
249 SYSTEM_UNLOCK();
250 req.op = RESET_EVENT;
251 CLIENT_SendRequest( REQ_EVENT_OP, -1, 1, &req, sizeof(req) );
252 return !CLIENT_WaitReply( NULL, NULL, 0 );
253 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000254 event->signaled = FALSE;
255 K32OBJ_DecCount( &event->header );
256 SYSTEM_UNLOCK();
257 return TRUE;
258}
259
260
261/***********************************************************************
262 * EVENT_Signaled
263 */
264static BOOL32 EVENT_Signaled( K32OBJ *obj, DWORD thread_id )
265{
266 EVENT *event = (EVENT *)obj;
267 assert( obj->type == K32OBJ_EVENT );
268 return event->signaled;
269}
270
271
272/***********************************************************************
273 * EVENT_Satisfied
274 *
275 * Wait on this object has been satisfied.
276 */
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000277static BOOL32 EVENT_Satisfied( K32OBJ *obj, DWORD thread_id )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000278{
279 EVENT *event = (EVENT *)obj;
280 assert( obj->type == K32OBJ_EVENT );
281 /* Reset if it's an auto-reset event */
282 if (!event->manual_reset) event->signaled = FALSE;
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000283 return FALSE; /* Not abandoned */
Alexandre Julliard02e90081998-01-04 17:49:09 +0000284}
285
286
287/***********************************************************************
288 * EVENT_AddWait
289 *
290 * Add thread to object wait queue.
291 */
292static void EVENT_AddWait( K32OBJ *obj, DWORD thread_id )
293{
294 EVENT *event = (EVENT *)obj;
295 assert( obj->type == K32OBJ_EVENT );
296 THREAD_AddQueue( &event->wait_queue, THREAD_ID_TO_THDB(thread_id) );
297}
298
299
300/***********************************************************************
301 * EVENT_RemoveWait
302 *
303 * Remove thread from object wait queue.
304 */
305static void EVENT_RemoveWait( K32OBJ *obj, DWORD thread_id )
306{
307 EVENT *event = (EVENT *)obj;
308 assert( obj->type == K32OBJ_EVENT );
309 THREAD_RemoveQueue( &event->wait_queue, THREAD_ID_TO_THDB(thread_id) );
310}
311
312
313/***********************************************************************
314 * EVENT_Destroy
315 */
316static void EVENT_Destroy( K32OBJ *obj )
317{
318 EVENT *event = (EVENT *)obj;
319 assert( obj->type == K32OBJ_EVENT );
320 /* There cannot be any thread on the list since the ref count is 0 */
321 assert( event->wait_queue == NULL );
322 obj->type = K32OBJ_UNKNOWN;
323 HeapFree( SystemHeap, 0, event );
324}
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000325
326
327
328
329/***********************************************************************
330 * NOTE: The Win95 VWin32_Event routines given below are really low-level
331 * routines implemented directly by VWin32. The user-mode libraries
332 * implement Win32 synchronisation routines on top of these low-level
333 * primitives. We do it the other way around here :-)
334 */
335
336/***********************************************************************
337 * VWin32_EventCreate (KERNEL.442)
338 */
339HANDLE32 WINAPI VWin32_EventCreate(VOID)
340{
Ulrich Weigand4a546901998-10-24 12:00:33 +0000341 HANDLE32 hEvent = CreateEvent32A( NULL, FALSE, 0, NULL );
342 return ConvertToGlobalHandle( hEvent );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000343}
344
345/***********************************************************************
346 * VWin32_EventDestroy (KERNEL.443)
347 */
348VOID WINAPI VWin32_EventDestroy(HANDLE32 event)
349{
350 CloseHandle( event );
351}
352
353/***********************************************************************
354 * VWin32_EventWait (KERNEL.450)
355 */
356VOID WINAPI VWin32_EventWait(HANDLE32 event)
357{
358 SYSLEVEL_ReleaseWin16Lock();
359 WaitForSingleObject( event, INFINITE32 );
360 SYSLEVEL_RestoreWin16Lock();
361}
362
363/***********************************************************************
364 * VWin32_EventSet (KERNEL.451)
365 */
366VOID WINAPI VWin32_EventSet(HANDLE32 event)
367{
368 SetEvent( event );
369}
370