4 * Copyright 1998 Alexandre Julliard
11 #include "server/request.h"
15 /***********************************************************************
16 * CreateSemaphore32A (KERNEL32.174)
18 HANDLE WINAPI
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
19 LONG max
, LPCSTR name
)
21 struct create_semaphore_request req
;
22 struct create_semaphore_reply reply
;
23 int len
= name
? strlen(name
) + 1 : 0;
25 /* Check parameters */
27 if ((max
<= 0) || (initial
< 0) || (initial
> max
))
29 SetLastError( ERROR_INVALID_PARAMETER
);
33 req
.initial
= (unsigned int)initial
;
34 req
.max
= (unsigned int)max
;
35 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
37 CLIENT_SendRequest( REQ_CREATE_SEMAPHORE
, -1, 2, &req
, sizeof(req
), name
, len
);
39 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
40 if (reply
.handle
== -1) return 0;
45 /***********************************************************************
46 * CreateSemaphore32W (KERNEL32.175)
48 HANDLE WINAPI
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
49 LONG max
, LPCWSTR name
)
51 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
52 HANDLE ret
= CreateSemaphoreA( sa
, initial
, max
, nameA
);
53 if (nameA
) HeapFree( GetProcessHeap(), 0, nameA
);
58 /***********************************************************************
59 * OpenSemaphore32A (KERNEL32.545)
61 HANDLE WINAPI
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
63 struct open_named_obj_request req
;
64 struct open_named_obj_reply reply
;
65 int len
= name
? strlen(name
) + 1 : 0;
67 req
.type
= OPEN_SEMAPHORE
;
69 req
.inherit
= inherit
;
70 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ
, -1, 2, &req
, sizeof(req
), name
, len
);
71 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
72 if (reply
.handle
== -1) return 0; /* must return 0 on failure, not -1 */
77 /***********************************************************************
78 * OpenSemaphore32W (KERNEL32.546)
80 HANDLE WINAPI
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
82 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
83 HANDLE ret
= OpenSemaphoreA( access
, inherit
, nameA
);
84 if (nameA
) HeapFree( GetProcessHeap(), 0, nameA
);
89 /***********************************************************************
90 * ReleaseSemaphore (KERNEL32.583)
92 BOOL WINAPI
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
94 struct release_semaphore_request req
;
95 struct release_semaphore_reply reply
;
99 SetLastError( ERROR_INVALID_PARAMETER
);
103 req
.count
= (unsigned int)count
;
104 CLIENT_SendRequest( REQ_RELEASE_SEMAPHORE
, -1, 1, &req
, sizeof(req
) );
105 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
106 if (previous
) *previous
= reply
.prev_count
;