4 * Copyright 1998 Alexandre Julliard
13 #include "server/request.h"
22 /***********************************************************************
23 * CreateSemaphore32A (KERNEL32.174)
25 HANDLE WINAPI
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
26 LONG max
, LPCSTR name
)
28 struct create_semaphore_request req
;
29 struct create_semaphore_reply reply
;
30 int len
= name
? strlen(name
) + 1 : 0;
34 /* Check parameters */
36 if ((max
<= 0) || (initial
< 0) || (initial
> max
))
38 SetLastError( ERROR_INVALID_PARAMETER
);
42 req
.initial
= (unsigned int)initial
;
43 req
.max
= (unsigned int)max
;
44 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
46 CLIENT_SendRequest( REQ_CREATE_SEMAPHORE
, -1, 2, &req
, sizeof(req
), name
, len
);
47 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
48 if (reply
.handle
== -1) return 0;
51 sem
= (SEMAPHORE
*)K32OBJ_Create( K32OBJ_SEMAPHORE
, sizeof(*sem
),
52 name
, reply
.handle
, SEMAPHORE_ALL_ACCESS
,
54 if (sem
) K32OBJ_DecCount( &sem
->header
);
56 if (handle
== INVALID_HANDLE_VALUE
) handle
= 0;
61 /***********************************************************************
62 * CreateSemaphore32W (KERNEL32.175)
64 HANDLE WINAPI
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
65 LONG max
, LPCWSTR name
)
67 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
68 HANDLE ret
= CreateSemaphoreA( sa
, initial
, max
, nameA
);
69 if (nameA
) HeapFree( GetProcessHeap(), 0, nameA
);
74 /***********************************************************************
75 * OpenSemaphore32A (KERNEL32.545)
77 HANDLE WINAPI
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
81 struct open_named_obj_request req
;
82 struct open_named_obj_reply reply
;
83 int len
= name
? strlen(name
) + 1 : 0;
85 req
.type
= OPEN_SEMAPHORE
;
87 req
.inherit
= inherit
;
88 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ
, -1, 2, &req
, sizeof(req
), name
, len
);
89 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
90 if (reply
.handle
!= -1)
93 if ((obj
= K32OBJ_FindNameType( name
, K32OBJ_SEMAPHORE
)) != NULL
)
95 handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, reply
.handle
);
96 K32OBJ_DecCount( obj
);
97 if (handle
== INVALID_HANDLE_VALUE
)
98 handle
= 0; /* must return 0 on failure, not -1 */
100 else CLIENT_CloseHandle( reply
.handle
);
107 /***********************************************************************
108 * OpenSemaphore32W (KERNEL32.546)
110 HANDLE WINAPI
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
112 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
113 HANDLE ret
= OpenSemaphoreA( access
, inherit
, nameA
);
114 if (nameA
) HeapFree( GetProcessHeap(), 0, nameA
);
119 /***********************************************************************
120 * ReleaseSemaphore (KERNEL32.583)
122 BOOL WINAPI
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
124 struct release_semaphore_request req
;
125 struct release_semaphore_reply reply
;
129 SetLastError( ERROR_INVALID_PARAMETER
);
132 req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), handle
,
133 K32OBJ_SEMAPHORE
, SEMAPHORE_MODIFY_STATE
);
134 if (req
.handle
== -1) return FALSE
;
135 req
.count
= (unsigned int)count
;
136 CLIENT_SendRequest( REQ_RELEASE_SEMAPHORE
, -1, 1, &req
, sizeof(req
) );
137 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
138 if (previous
) *previous
= reply
.prev_count
;