Fixed handling of DGA2.0 keyboard events.
[wine] / scheduler / semaphore.c
1 /*
2  * Win32 semaphores
3  *
4  * Copyright 1998 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <string.h>
9 #include "winerror.h"
10 #include "server.h"
11
12
13 /***********************************************************************
14  *           CreateSemaphore32A   (KERNEL32.174)
15  */
16 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
17 {
18     struct create_semaphore_request *req = get_req_buffer();
19
20     /* Check parameters */
21
22     if ((max <= 0) || (initial < 0) || (initial > max))
23     {
24         SetLastError( ERROR_INVALID_PARAMETER );
25         return 0;
26     }
27
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;
36 }
37
38
39 /***********************************************************************
40  *           CreateSemaphore32W   (KERNEL32.175)
41  */
42 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
43                                     LONG max, LPCWSTR name )
44 {
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;
63 }
64
65
66 /***********************************************************************
67  *           OpenSemaphore32A   (KERNEL32.545)
68  */
69 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
70 {
71     struct open_semaphore_request *req = get_req_buffer();
72
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;
79 }
80
81
82 /***********************************************************************
83  *           OpenSemaphore32W   (KERNEL32.546)
84  */
85 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
86 {
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;
95 }
96
97
98 /***********************************************************************
99  *           ReleaseSemaphore   (KERNEL32.583)
100  */
101 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
102 {
103     BOOL ret = FALSE;
104     struct release_semaphore_request *req = get_req_buffer();
105
106     if (count < 0)
107     {
108         SetLastError( ERROR_INVALID_PARAMETER );
109         return FALSE;
110     }
111     req->handle = handle;
112     req->count  = (unsigned int)count;
113     if (!server_call( REQ_RELEASE_SEMAPHORE ))
114     {
115         if (previous) *previous = req->prev_count;
116         ret = TRUE;
117     }
118     return ret;
119 }