blob: 298df02e78790b8f2e007fe0f0c80382a362ea25 [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 Julliard02e90081998-01-04 17:49:09 +000010#include "heap.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000011#include "server/request.h"
12#include "server.h"
Alexandre Julliard02e90081998-01-04 17:49:09 +000013
Alexandre Julliard02e90081998-01-04 17:49:09 +000014
15/***********************************************************************
16 * CreateSemaphore32A (KERNEL32.174)
17 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000018HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial,
Alexandre Julliard02e90081998-01-04 17:49:09 +000019 LONG max, LPCSTR name )
20{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000021 struct create_semaphore_request req;
22 struct create_semaphore_reply reply;
23 int len = name ? strlen(name) + 1 : 0;
Alexandre Julliard02e90081998-01-04 17:49:09 +000024
25 /* Check parameters */
26
27 if ((max <= 0) || (initial < 0) || (initial > max))
28 {
29 SetLastError( ERROR_INVALID_PARAMETER );
Alexandre Julliard55443871998-12-31 15:52:06 +000030 return 0;
Alexandre Julliard02e90081998-01-04 17:49:09 +000031 }
32
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000033 req.initial = (unsigned int)initial;
34 req.max = (unsigned int)max;
35 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
36
37 CLIENT_SendRequest( REQ_CREATE_SEMAPHORE, -1, 2, &req, sizeof(req), name, len );
Alexandre Julliard3f09ec51999-02-28 19:25:51 +000038 SetLastError(0);
Alexandre Julliard6ebbe3c1999-01-01 17:04:00 +000039 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
Alexandre Julliard55443871998-12-31 15:52:06 +000040 if (reply.handle == -1) return 0;
Alexandre Julliard96c08d81999-02-28 13:27:56 +000041 return reply.handle;
Alexandre Julliard02e90081998-01-04 17:49:09 +000042}
43
44
45/***********************************************************************
46 * CreateSemaphore32W (KERNEL32.175)
47 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000048HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
Alexandre Julliard02e90081998-01-04 17:49:09 +000049 LONG max, LPCWSTR name )
50{
51 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
Alexandre Julliarda3960291999-02-26 11:11:13 +000052 HANDLE ret = CreateSemaphoreA( sa, initial, max, nameA );
Alexandre Julliard02e90081998-01-04 17:49:09 +000053 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
54 return ret;
55}
56
57
58/***********************************************************************
59 * OpenSemaphore32A (KERNEL32.545)
60 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000061HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
Alexandre Julliard02e90081998-01-04 17:49:09 +000062{
Alexandre Julliard55443871998-12-31 15:52:06 +000063 struct open_named_obj_request req;
64 struct open_named_obj_reply reply;
65 int len = name ? strlen(name) + 1 : 0;
66
67 req.type = OPEN_SEMAPHORE;
68 req.access = access;
69 req.inherit = inherit;
70 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ, -1, 2, &req, sizeof(req), name, len );
Alexandre Julliard6ebbe3c1999-01-01 17:04:00 +000071 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
Alexandre Julliard96c08d81999-02-28 13:27:56 +000072 if (reply.handle == -1) return 0; /* must return 0 on failure, not -1 */
73 return reply.handle;
Alexandre Julliard02e90081998-01-04 17:49:09 +000074}
75
76
77/***********************************************************************
78 * OpenSemaphore32W (KERNEL32.546)
79 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000080HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
Alexandre Julliard02e90081998-01-04 17:49:09 +000081{
82 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
Alexandre Julliarda3960291999-02-26 11:11:13 +000083 HANDLE ret = OpenSemaphoreA( access, inherit, nameA );
Alexandre Julliard02e90081998-01-04 17:49:09 +000084 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
85 return ret;
86}
87
88
89/***********************************************************************
90 * ReleaseSemaphore (KERNEL32.583)
91 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000092BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
Alexandre Julliard02e90081998-01-04 17:49:09 +000093{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000094 struct release_semaphore_request req;
Alexandre Julliard55443871998-12-31 15:52:06 +000095 struct release_semaphore_reply reply;
Alexandre Julliard02e90081998-01-04 17:49:09 +000096
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000097 if (count < 0)
98 {
99 SetLastError( ERROR_INVALID_PARAMETER );
100 return FALSE;
101 }
Alexandre Julliard96c08d81999-02-28 13:27:56 +0000102 req.handle = handle;
103 req.count = (unsigned int)count;
Alexandre Julliard55443871998-12-31 15:52:06 +0000104 CLIENT_SendRequest( REQ_RELEASE_SEMAPHORE, -1, 1, &req, sizeof(req) );
Alexandre Julliard6ebbe3c1999-01-01 17:04:00 +0000105 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
Alexandre Julliard55443871998-12-31 15:52:06 +0000106 if (previous) *previous = reply.prev_count;
Alexandre Julliard02e90081998-01-04 17:49:09 +0000107 return TRUE;
108}