Use server file mapping objects.
[wine/multimedia.git] / scheduler / event.c
blob30a1c76d48c9863219fa1609eab3c5262ddfb638
1 /*
2 * Win32 events
4 * Copyright 1998 Alexandre Julliard
5 */
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"
14 #include "syslevel.h"
15 #include "server/request.h"
16 #include "server.h"
18 typedef struct
20 K32OBJ header;
21 } EVENT;
23 static void EVENT_Destroy( K32OBJ *obj );
25 const K32OBJ_OPS EVENT_Ops =
27 EVENT_Destroy /* destroy */
31 /***********************************************************************
32 * CreateEvent32A (KERNEL32.156)
34 HANDLE32 WINAPI CreateEvent32A( SECURITY_ATTRIBUTES *sa, BOOL32 manual_reset,
35 BOOL32 initial_state, LPCSTR name )
37 struct create_event_request req;
38 struct create_event_reply reply;
39 int len = name ? strlen(name) + 1 : 0;
40 HANDLE32 handle;
41 EVENT *event;
43 req.manual_reset = manual_reset;
44 req.initial_state = initial_state;
45 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
47 CLIENT_SendRequest( REQ_CREATE_EVENT, -1, 2, &req, sizeof(req), name, len );
48 CLIENT_WaitReply( &len, NULL, 1, &reply, sizeof(reply) );
49 CHECK_LEN( len, sizeof(reply) );
50 if (reply.handle == -1) return 0;
52 SYSTEM_LOCK();
53 event = (EVENT *)K32OBJ_Create( K32OBJ_EVENT, sizeof(*event), name,
54 reply.handle, EVENT_ALL_ACCESS, sa, &handle );
55 if (event)
56 K32OBJ_DecCount( &event->header );
57 SYSTEM_UNLOCK();
58 if (handle == INVALID_HANDLE_VALUE32) handle = 0;
59 return handle;
63 /***********************************************************************
64 * CreateEvent32W (KERNEL32.157)
66 HANDLE32 WINAPI CreateEvent32W( SECURITY_ATTRIBUTES *sa, BOOL32 manual_reset,
67 BOOL32 initial_state, LPCWSTR name )
69 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
70 HANDLE32 ret = CreateEvent32A( sa, manual_reset, initial_state, nameA );
71 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
72 return ret;
75 /***********************************************************************
76 * WIN16_CreateEvent (KERNEL.457)
78 HANDLE32 WINAPI WIN16_CreateEvent( BOOL32 manual_reset, BOOL32 initial_state )
80 return CreateEvent32A( NULL, manual_reset, initial_state, NULL );
84 /***********************************************************************
85 * OpenEvent32A (KERNEL32.536)
87 HANDLE32 WINAPI OpenEvent32A( DWORD access, BOOL32 inherit, LPCSTR name )
89 HANDLE32 handle = 0;
90 K32OBJ *obj;
91 struct open_named_obj_request req;
92 struct open_named_obj_reply reply;
93 int len = name ? strlen(name) + 1 : 0;
95 req.type = OPEN_EVENT;
96 req.access = access;
97 req.inherit = inherit;
98 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ, -1, 2, &req, sizeof(req), name, len );
99 CLIENT_WaitReply( &len, NULL, 1, &reply, sizeof(reply) );
100 CHECK_LEN( len, sizeof(reply) );
101 if (reply.handle != -1)
103 SYSTEM_LOCK();
104 if ((obj = K32OBJ_FindNameType( name, K32OBJ_EVENT )) != NULL)
106 handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, reply.handle );
107 K32OBJ_DecCount( obj );
108 if (handle == INVALID_HANDLE_VALUE32)
109 handle = 0; /* must return 0 on failure, not -1 */
111 else CLIENT_CloseHandle( reply.handle );
112 SYSTEM_UNLOCK();
114 return handle;
118 /***********************************************************************
119 * OpenEvent32W (KERNEL32.537)
121 HANDLE32 WINAPI OpenEvent32W( DWORD access, BOOL32 inherit, LPCWSTR name )
123 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
124 HANDLE32 ret = OpenEvent32A( access, inherit, nameA );
125 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
126 return ret;
130 /***********************************************************************
131 * EVENT_Operation
133 * Execute an event operation (set,reset,pulse).
135 static BOOL32 EVENT_Operation( HANDLE32 handle, enum event_op op )
137 struct event_op_request req;
139 req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
140 K32OBJ_EVENT, EVENT_MODIFY_STATE );
141 if (req.handle == -1) return FALSE;
142 req.op = op;
143 CLIENT_SendRequest( REQ_EVENT_OP, -1, 1, &req, sizeof(req) );
144 return !CLIENT_WaitReply( NULL, NULL, 0 );
148 /***********************************************************************
149 * PulseEvent (KERNEL32.557)
151 BOOL32 WINAPI PulseEvent( HANDLE32 handle )
153 return EVENT_Operation( handle, PULSE_EVENT );
157 /***********************************************************************
158 * SetEvent (KERNEL32.644)
160 BOOL32 WINAPI SetEvent( HANDLE32 handle )
162 return EVENT_Operation( handle, SET_EVENT );
166 /***********************************************************************
167 * ResetEvent (KERNEL32.586)
169 BOOL32 WINAPI ResetEvent( HANDLE32 handle )
171 return EVENT_Operation( handle, RESET_EVENT );
175 /***********************************************************************
176 * EVENT_Destroy
178 static void EVENT_Destroy( K32OBJ *obj )
180 EVENT *event = (EVENT *)obj;
181 assert( obj->type == K32OBJ_EVENT );
182 obj->type = K32OBJ_UNKNOWN;
183 HeapFree( SystemHeap, 0, event );
189 /***********************************************************************
190 * NOTE: The Win95 VWin32_Event routines given below are really low-level
191 * routines implemented directly by VWin32. The user-mode libraries
192 * implement Win32 synchronisation routines on top of these low-level
193 * primitives. We do it the other way around here :-)
196 /***********************************************************************
197 * VWin32_EventCreate (KERNEL.442)
199 HANDLE32 WINAPI VWin32_EventCreate(VOID)
201 HANDLE32 hEvent = CreateEvent32A( NULL, FALSE, 0, NULL );
202 return ConvertToGlobalHandle( hEvent );
205 /***********************************************************************
206 * VWin32_EventDestroy (KERNEL.443)
208 VOID WINAPI VWin32_EventDestroy(HANDLE32 event)
210 CloseHandle( event );
213 /***********************************************************************
214 * VWin32_EventWait (KERNEL.450)
216 VOID WINAPI VWin32_EventWait(HANDLE32 event)
218 SYSLEVEL_ReleaseWin16Lock();
219 WaitForSingleObject( event, INFINITE32 );
220 SYSLEVEL_RestoreWin16Lock();
223 /***********************************************************************
224 * VWin32_EventSet (KERNEL.451)
226 VOID WINAPI VWin32_EventSet(HANDLE32 event)
228 SetEvent( event );