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