- Added global data mutual exclusion
[wine.git] / scheduler / semaphore.c
bloba9b130fb23dc521a92ab7b0c1be144b82121ae0f
1 /*
2 * Win32 semaphores
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include "winerror.h"
10 #include "server.h"
13 /***********************************************************************
14 * CreateSemaphore32A (KERNEL32.174)
16 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
18 struct create_semaphore_request *req = get_req_buffer();
20 /* Check parameters */
22 if ((max <= 0) || (initial < 0) || (initial > max))
24 SetLastError( ERROR_INVALID_PARAMETER );
25 return 0;
28 req->initial = (unsigned int)initial;
29 req->max = (unsigned int)max;
30 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
31 server_strcpyAtoW( req->name, name );
32 SetLastError(0);
33 server_call( REQ_CREATE_SEMAPHORE );
34 if (req->handle == -1) return 0;
35 return req->handle;
39 /***********************************************************************
40 * CreateSemaphore32W (KERNEL32.175)
42 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
43 LONG max, LPCWSTR name )
45 struct create_semaphore_request *req = get_req_buffer();
47 /* Check parameters */
49 if ((max <= 0) || (initial < 0) || (initial > max))
51 SetLastError( ERROR_INVALID_PARAMETER );
52 return 0;
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;
66 /***********************************************************************
67 * OpenSemaphore32A (KERNEL32.545)
69 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
71 struct open_semaphore_request *req = get_req_buffer();
73 req->access = access;
74 req->inherit = inherit;
75 server_strcpyAtoW( req->name, name );
76 server_call( REQ_OPEN_SEMAPHORE );
77 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
78 return req->handle;
82 /***********************************************************************
83 * OpenSemaphore32W (KERNEL32.546)
85 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
87 struct open_semaphore_request *req = get_req_buffer();
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;
98 /***********************************************************************
99 * ReleaseSemaphore (KERNEL32.583)
101 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
103 BOOL ret = FALSE;
104 struct release_semaphore_request *req = get_req_buffer();
106 if (count < 0)
108 SetLastError( ERROR_INVALID_PARAMETER );
109 return FALSE;
111 req->handle = handle;
112 req->count = (unsigned int)count;
113 if (!server_call( REQ_RELEASE_SEMAPHORE ))
115 if (previous) *previous = req->prev_count;
116 ret = TRUE;
118 return ret;