Fix PrintDlg collate icons. Add orientation icons.
[wine.git] / scheduler / semaphore.c
blob650802665e716590ba744f00633d741425b2ea99
1 /*
2 * Win32 semaphores
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include "winerror.h"
10 #include "wine/unicode.h"
11 #include "server.h"
14 /***********************************************************************
15 * CreateSemaphoreA (KERNEL32.174)
17 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
19 HANDLE ret;
20 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
22 /* Check parameters */
24 if ((max <= 0) || (initial < 0) || (initial > max))
26 SetLastError( ERROR_INVALID_PARAMETER );
27 return 0;
29 if (len >= MAX_PATH)
31 SetLastError( ERROR_FILENAME_EXCED_RANGE );
32 return 0;
35 SERVER_START_REQ
37 struct create_semaphore_request *req = server_alloc_req( sizeof(*req),
38 len * sizeof(WCHAR) );
40 req->initial = (unsigned int)initial;
41 req->max = (unsigned int)max;
42 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
43 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
44 SetLastError(0);
45 server_call( REQ_CREATE_SEMAPHORE );
46 ret = req->handle;
48 SERVER_END_REQ;
49 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
50 return ret;
54 /***********************************************************************
55 * CreateSemaphoreW (KERNEL32.175)
57 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
58 LONG max, LPCWSTR name )
60 HANDLE ret;
61 DWORD len = name ? strlenW(name) : 0;
63 /* Check parameters */
65 if ((max <= 0) || (initial < 0) || (initial > max))
67 SetLastError( ERROR_INVALID_PARAMETER );
68 return 0;
70 if (len >= MAX_PATH)
72 SetLastError( ERROR_FILENAME_EXCED_RANGE );
73 return 0;
76 SERVER_START_REQ
78 struct create_semaphore_request *req = server_alloc_req( sizeof(*req),
79 len * sizeof(WCHAR) );
81 req->initial = (unsigned int)initial;
82 req->max = (unsigned int)max;
83 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
84 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
85 SetLastError(0);
86 server_call( REQ_CREATE_SEMAPHORE );
87 ret = req->handle;
89 SERVER_END_REQ;
90 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
91 return ret;
95 /***********************************************************************
96 * OpenSemaphoreA (KERNEL32.545)
98 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
100 HANDLE ret;
101 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
102 if (len >= MAX_PATH)
104 SetLastError( ERROR_FILENAME_EXCED_RANGE );
105 return 0;
107 SERVER_START_REQ
109 struct open_semaphore_request *req = server_alloc_req( sizeof(*req),
110 len * sizeof(WCHAR) );
111 req->access = access;
112 req->inherit = inherit;
113 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
114 server_call( REQ_OPEN_SEMAPHORE );
115 ret = req->handle;
117 SERVER_END_REQ;
118 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
119 return ret;
123 /***********************************************************************
124 * OpenSemaphoreW (KERNEL32.546)
126 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
128 HANDLE ret;
129 DWORD len = name ? strlenW(name) : 0;
130 if (len >= MAX_PATH)
132 SetLastError( ERROR_FILENAME_EXCED_RANGE );
133 return 0;
135 SERVER_START_REQ
137 struct open_semaphore_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
138 req->access = access;
139 req->inherit = inherit;
140 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
141 server_call( REQ_OPEN_SEMAPHORE );
142 ret = req->handle;
144 SERVER_END_REQ;
145 if (ret == -1) ret = 0; /* must return 0 on failure, not -1 */
146 return ret;
150 /***********************************************************************
151 * ReleaseSemaphore (KERNEL32.583)
153 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
155 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
156 if (status) SetLastError( RtlNtStatusToDosError(status) );
157 return !status;