blob: a9b130fb23dc521a92ab7b0c1be144b82121ae0f [file] [log] [blame]
Alexandre Julliard02e90081998-01-04 17:49:09 +00001/*
2 * Win32 semaphores
3 *
4 * Copyright 1998 Alexandre Julliard
5 */
6
7#include <assert.h>
David Luyeree517e81999-02-28 12:27:56 +00008#include <string.h>
Alexandre Julliard02e90081998-01-04 17:49:09 +00009#include "winerror.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000010#include "server.h"
Alexandre Julliard02e90081998-01-04 17:49:09 +000011
Alexandre Julliard02e90081998-01-04 17:49:09 +000012
13/***********************************************************************
14 * CreateSemaphore32A (KERNEL32.174)
15 */
Alexandre Julliard5bc78081999-06-22 17:26:53 +000016HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
Alexandre Julliard02e90081998-01-04 17:49:09 +000017{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000018 struct create_semaphore_request *req = get_req_buffer();
Alexandre Julliard02e90081998-01-04 17:49:09 +000019
20 /* Check parameters */
21
22 if ((max <= 0) || (initial < 0) || (initial > max))
23 {
24 SetLastError( ERROR_INVALID_PARAMETER );
Alexandre Julliard55443871998-12-31 15:52:06 +000025 return 0;
Alexandre Julliard02e90081998-01-04 17:49:09 +000026 }
27
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000028 req->initial = (unsigned int)initial;
29 req->max = (unsigned int)max;
30 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
Alexandre Julliardd16319c1999-11-25 21:30:24 +000031 server_strcpyAtoW( req->name, name );
Alexandre Julliard3f09ec51999-02-28 19:25:51 +000032 SetLastError(0);
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000033 server_call( REQ_CREATE_SEMAPHORE );
34 if (req->handle == -1) return 0;
35 return req->handle;
Alexandre Julliard02e90081998-01-04 17:49:09 +000036}
37
38
39/***********************************************************************
40 * CreateSemaphore32W (KERNEL32.175)
41 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000042HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
Alexandre Julliard02e90081998-01-04 17:49:09 +000043 LONG max, LPCWSTR name )
44{
Alexandre Julliardd16319c1999-11-25 21:30:24 +000045 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;
Alexandre Julliard02e90081998-01-04 17:49:09 +000063}
64
65
66/***********************************************************************
67 * OpenSemaphore32A (KERNEL32.545)
68 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000069HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
Alexandre Julliard02e90081998-01-04 17:49:09 +000070{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000071 struct open_semaphore_request *req = get_req_buffer();
Alexandre Julliard55443871998-12-31 15:52:06 +000072
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000073 req->access = access;
74 req->inherit = inherit;
Alexandre Julliardd16319c1999-11-25 21:30:24 +000075 server_strcpyAtoW( req->name, name );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000076 server_call( REQ_OPEN_SEMAPHORE );
77 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
78 return req->handle;
Alexandre Julliard02e90081998-01-04 17:49:09 +000079}
80
81
82/***********************************************************************
83 * OpenSemaphore32W (KERNEL32.546)
84 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000085HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
Alexandre Julliard02e90081998-01-04 17:49:09 +000086{
Alexandre Julliardd16319c1999-11-25 21:30:24 +000087 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;
Alexandre Julliard02e90081998-01-04 17:49:09 +000095}
96
97
98/***********************************************************************
99 * ReleaseSemaphore (KERNEL32.583)
100 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000101BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000102{
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000103 BOOL ret = FALSE;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000104 struct release_semaphore_request *req = get_req_buffer();
Alexandre Julliard02e90081998-01-04 17:49:09 +0000105
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000106 if (count < 0)
107 {
108 SetLastError( ERROR_INVALID_PARAMETER );
109 return FALSE;
110 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000111 req->handle = handle;
112 req->count = (unsigned int)count;
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000113 if (!server_call( REQ_RELEASE_SEMAPHORE ))
114 {
115 if (previous) *previous = req->prev_count;
116 ret = TRUE;
117 }
118 return ret;
Alexandre Julliard02e90081998-01-04 17:49:09 +0000119}