Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Win32 semaphores |
| 3 | * |
| 4 | * Copyright 1998 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
David Luyer | ee517e8 | 1999-02-28 12:27:56 +0000 | [diff] [blame] | 8 | #include <string.h> |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 9 | #include "winerror.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 10 | #include "server.h" |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 11 | |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 12 | |
| 13 | /*********************************************************************** |
| 14 | * CreateSemaphore32A (KERNEL32.174) |
| 15 | */ |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 16 | HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name ) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 17 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 18 | struct create_semaphore_request *req = get_req_buffer(); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 19 | |
| 20 | /* Check parameters */ |
| 21 | |
| 22 | if ((max <= 0) || (initial < 0) || (initial > max)) |
| 23 | { |
| 24 | SetLastError( ERROR_INVALID_PARAMETER ); |
Alexandre Julliard | 5544387 | 1998-12-31 15:52:06 +0000 | [diff] [blame] | 25 | return 0; |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 28 | req->initial = (unsigned int)initial; |
| 29 | req->max = (unsigned int)max; |
| 30 | req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 31 | server_strcpyAtoW( req->name, name ); |
Alexandre Julliard | 3f09ec5 | 1999-02-28 19:25:51 +0000 | [diff] [blame] | 32 | SetLastError(0); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 33 | server_call( REQ_CREATE_SEMAPHORE ); |
| 34 | if (req->handle == -1) return 0; |
| 35 | return req->handle; |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
| 39 | /*********************************************************************** |
| 40 | * CreateSemaphore32W (KERNEL32.175) |
| 41 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 42 | HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial, |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 43 | LONG max, LPCWSTR name ) |
| 44 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 45 | struct create_semaphore_request *req = get_req_buffer(); |
| 46 | |
| 47 | /* Check parameters */ |
| 48 | |
| 49 | if ((max <= 0) || (initial < 0) || (initial > max)) |
| 50 | { |
| 51 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | req->initial = (unsigned int)initial; |
| 56 | req->max = (unsigned int)max; |
| 57 | req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle); |
| 58 | server_strcpyW( req->name, name ); |
| 59 | SetLastError(0); |
| 60 | server_call( REQ_CREATE_SEMAPHORE ); |
| 61 | if (req->handle == -1) return 0; |
| 62 | return req->handle; |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | /*********************************************************************** |
| 67 | * OpenSemaphore32A (KERNEL32.545) |
| 68 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 69 | HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name ) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 70 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 71 | struct open_semaphore_request *req = get_req_buffer(); |
Alexandre Julliard | 5544387 | 1998-12-31 15:52:06 +0000 | [diff] [blame] | 72 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 73 | req->access = access; |
| 74 | req->inherit = inherit; |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 75 | server_strcpyAtoW( req->name, name ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 76 | server_call( REQ_OPEN_SEMAPHORE ); |
| 77 | if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */ |
| 78 | return req->handle; |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | /*********************************************************************** |
| 83 | * OpenSemaphore32W (KERNEL32.546) |
| 84 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 85 | HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name ) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 86 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 87 | struct open_semaphore_request *req = get_req_buffer(); |
| 88 | |
| 89 | req->access = access; |
| 90 | req->inherit = inherit; |
| 91 | server_strcpyW( req->name, name ); |
| 92 | server_call( REQ_OPEN_SEMAPHORE ); |
| 93 | if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */ |
| 94 | return req->handle; |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | /*********************************************************************** |
| 99 | * ReleaseSemaphore (KERNEL32.583) |
| 100 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 101 | BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous ) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 102 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 103 | BOOL ret = FALSE; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 104 | struct release_semaphore_request *req = get_req_buffer(); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 105 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 106 | if (count < 0) |
| 107 | { |
| 108 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 109 | return FALSE; |
| 110 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 111 | req->handle = handle; |
| 112 | req->count = (unsigned int)count; |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 113 | if (!server_call( REQ_RELEASE_SEMAPHORE )) |
| 114 | { |
| 115 | if (previous) *previous = req->prev_count; |
| 116 | ret = TRUE; |
| 117 | } |
| 118 | return ret; |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 119 | } |