2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
10 #include "wine/unicode.h"
12 #include "debugtools.h"
14 DEFAULT_DEBUG_CHANNEL(win32);
21 /***********************************************************************
22 * CreateEventA (KERNEL32.156)
24 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
25 BOOL initial_state, LPCSTR name )
28 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
31 SetLastError( ERROR_FILENAME_EXCED_RANGE );
36 struct create_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
38 req->manual_reset = manual_reset;
39 req->initial_state = initial_state;
40 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
41 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
43 server_call( REQ_CREATE_EVENT );
47 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
52 /***********************************************************************
53 * CreateEventW (KERNEL32.157)
55 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
56 BOOL initial_state, LPCWSTR name )
59 DWORD len = name ? strlenW(name) : 0;
62 SetLastError( ERROR_FILENAME_EXCED_RANGE );
67 struct create_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
69 req->manual_reset = manual_reset;
70 req->initial_state = initial_state;
71 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
72 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
74 server_call( REQ_CREATE_EVENT );
78 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
83 /***********************************************************************
84 * WIN16_CreateEvent (KERNEL.457)
86 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
88 return CreateEventA( NULL, manual_reset, initial_state, NULL );
92 /***********************************************************************
93 * OpenEventA (KERNEL32.536)
95 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
98 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
101 SetLastError( ERROR_FILENAME_EXCED_RANGE );
106 struct open_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
108 req->access = access;
109 req->inherit = inherit;
110 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
111 server_call( REQ_OPEN_EVENT );
115 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
120 /***********************************************************************
121 * OpenEventW (KERNEL32.537)
123 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
126 DWORD len = name ? strlenW(name) : 0;
129 SetLastError( ERROR_FILENAME_EXCED_RANGE );
134 struct open_event_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
136 req->access = access;
137 req->inherit = inherit;
138 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
139 server_call( REQ_OPEN_EVENT );
143 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
148 /***********************************************************************
151 * Execute an event operation (set,reset,pulse).
153 static BOOL EVENT_Operation( HANDLE handle, enum event_op op )
158 struct event_op_request *req = server_alloc_req( sizeof(*req), 0 );
159 req->handle = handle;
161 ret = !server_call( REQ_EVENT_OP );
168 /***********************************************************************
169 * PulseEvent (KERNEL32.557)
171 BOOL WINAPI PulseEvent( HANDLE handle )
173 return EVENT_Operation( handle, PULSE_EVENT );
177 /***********************************************************************
178 * SetEvent (KERNEL32.644)
180 BOOL WINAPI SetEvent( HANDLE handle )
182 return EVENT_Operation( handle, SET_EVENT );
186 /***********************************************************************
187 * ResetEvent (KERNEL32.586)
189 BOOL WINAPI ResetEvent( HANDLE handle )
191 return EVENT_Operation( handle, RESET_EVENT );
195 /***********************************************************************
196 * NOTE: The Win95 VWin32_Event routines given below are really low-level
197 * routines implemented directly by VWin32. The user-mode libraries
198 * implement Win32 synchronisation routines on top of these low-level
199 * primitives. We do it the other way around here :-)
202 /***********************************************************************
203 * VWin32_EventCreate (KERNEL.442)
205 HANDLE WINAPI VWin32_EventCreate(VOID)
207 HANDLE hEvent = CreateEventA( NULL, FALSE, 0, NULL );
208 return ConvertToGlobalHandle( hEvent );
211 /***********************************************************************
212 * VWin32_EventDestroy (KERNEL.443)
214 VOID WINAPI VWin32_EventDestroy(HANDLE event)
216 CloseHandle( event );
219 /***********************************************************************
220 * VWin32_EventWait (KERNEL.450)
222 VOID WINAPI VWin32_EventWait(HANDLE event)
226 ReleaseThunkLock( &mutex_count );
227 WaitForSingleObject( event, INFINITE );
228 RestoreThunkLock( mutex_count );
231 /***********************************************************************
232 * VWin32_EventSet (KERNEL.451)
234 VOID WINAPI VWin32_EventSet(HANDLE event)
241 /***********************************************************************
242 * CreateMutexA (KERNEL32.166)
244 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
247 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
250 SetLastError( ERROR_FILENAME_EXCED_RANGE );
255 struct create_mutex_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
258 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
259 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
261 server_call( REQ_CREATE_MUTEX );
265 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
270 /***********************************************************************
271 * CreateMutexW (KERNEL32.167)
273 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
276 DWORD len = name ? strlenW(name) : 0;
279 SetLastError( ERROR_FILENAME_EXCED_RANGE );
284 struct create_mutex_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
287 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
288 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
290 server_call( REQ_CREATE_MUTEX );
294 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
304 /***********************************************************************
305 * OpenMutexA (KERNEL32.541)
307 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
310 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
313 SetLastError( ERROR_FILENAME_EXCED_RANGE );
318 struct open_mutex_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
320 req->access = access;
321 req->inherit = inherit;
322 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
323 server_call( REQ_OPEN_MUTEX );
327 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
332 /***********************************************************************
333 * OpenMutexW (KERNEL32.542)
335 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
338 DWORD len = name ? strlenW(name) : 0;
341 SetLastError( ERROR_FILENAME_EXCED_RANGE );
346 struct open_mutex_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
348 req->access = access;
349 req->inherit = inherit;
350 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
351 server_call( REQ_OPEN_MUTEX );
355 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
360 /***********************************************************************
361 * ReleaseMutex (KERNEL32.582)
363 BOOL WINAPI ReleaseMutex( HANDLE handle )
368 struct release_mutex_request *req = server_alloc_req( sizeof(*req), 0 );
369 req->handle = handle;
370 ret = !server_call( REQ_RELEASE_MUTEX );
382 /***********************************************************************
383 * CreateSemaphoreA (KERNEL32.174)
385 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
388 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
390 /* Check parameters */
392 if ((max <= 0) || (initial < 0) || (initial > max))
394 SetLastError( ERROR_INVALID_PARAMETER );
399 SetLastError( ERROR_FILENAME_EXCED_RANGE );
405 struct create_semaphore_request *req = server_alloc_req( sizeof(*req),
406 len * sizeof(WCHAR) );
408 req->initial = (unsigned int)initial;
409 req->max = (unsigned int)max;
410 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
411 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
413 server_call( REQ_CREATE_SEMAPHORE );
417 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
422 /***********************************************************************
423 * CreateSemaphoreW (KERNEL32.175)
425 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
426 LONG max, LPCWSTR name )
429 DWORD len = name ? strlenW(name) : 0;
431 /* Check parameters */
433 if ((max <= 0) || (initial < 0) || (initial > max))
435 SetLastError( ERROR_INVALID_PARAMETER );
440 SetLastError( ERROR_FILENAME_EXCED_RANGE );
446 struct create_semaphore_request *req = server_alloc_req( sizeof(*req),
447 len * sizeof(WCHAR) );
449 req->initial = (unsigned int)initial;
450 req->max = (unsigned int)max;
451 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
452 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
454 server_call( REQ_CREATE_SEMAPHORE );
458 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
463 /***********************************************************************
464 * OpenSemaphoreA (KERNEL32.545)
466 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
469 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
472 SetLastError( ERROR_FILENAME_EXCED_RANGE );
477 struct open_semaphore_request *req = server_alloc_req( sizeof(*req),
478 len * sizeof(WCHAR) );
479 req->access = access;
480 req->inherit = inherit;
481 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
482 server_call( REQ_OPEN_SEMAPHORE );
486 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
491 /***********************************************************************
492 * OpenSemaphoreW (KERNEL32.546)
494 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
497 DWORD len = name ? strlenW(name) : 0;
500 SetLastError( ERROR_FILENAME_EXCED_RANGE );
505 struct open_semaphore_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
506 req->access = access;
507 req->inherit = inherit;
508 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
509 server_call( REQ_OPEN_SEMAPHORE );
513 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
518 /***********************************************************************
519 * ReleaseSemaphore (KERNEL32.583)
521 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
523 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
524 if (status) SetLastError( RtlNtStatusToDosError(status) );
534 /***********************************************************************
535 * CreateNamedPipeA (KERNEL32.168)
537 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
538 DWORD dwPipeMode, DWORD nMaxInstances,
539 DWORD nOutBufferSize, DWORD nInBufferSize,
540 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
542 FIXME("(Name=%s, OpenMode=%#08lx, dwPipeMode=%#08lx, MaxInst=%ld, OutBSize=%ld, InBuffSize=%ld, DefTimeOut=%ld, SecAttr=%p): stub\n",
543 debugstr_a(name), dwOpenMode, dwPipeMode, nMaxInstances,
544 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
545 SetLastError (ERROR_UNKNOWN);
546 return INVALID_HANDLE_VALUE;
550 /***********************************************************************
551 * CreateNamedPipeW (KERNEL32.169)
553 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
554 DWORD dwPipeMode, DWORD nMaxInstances,
555 DWORD nOutBufferSize, DWORD nInBufferSize,
556 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
558 FIXME("(Name=%s, OpenMode=%#08lx, dwPipeMode=%#08lx, MaxInst=%ld, OutBSize=%ld, InBuffSize=%ld, DefTimeOut=%ld, SecAttr=%p): stub\n",
559 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
560 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
562 SetLastError (ERROR_UNKNOWN);
563 return INVALID_HANDLE_VALUE;
567 /***********************************************************************
568 * PeekNamedPipe (KERNEL32.552)
570 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
571 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
573 FIXME("(%08x, %p, %08lx, %p, %p, %p): stub\n",
574 hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
575 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
580 /***********************************************************************
581 * WaitNamedPipeA (KERNEL32.@)
583 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
585 FIXME("%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
586 SetLastError(ERROR_PIPE_NOT_CONNECTED);
591 /***********************************************************************
592 * WaitNamedPipeW (KERNEL32.@)
594 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
596 FIXME("%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
597 SetLastError(ERROR_PIPE_NOT_CONNECTED);