Use the new header for COM definitions.
[wine/wine-kai.git] / scheduler / semaphore.c
blob298df02e78790b8f2e007fe0f0c80382a362ea25
1 /*
2 * Win32 semaphores
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include "winerror.h"
10 #include "heap.h"
11 #include "server/request.h"
12 #include "server.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 );
30 return 0;
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 );
38 SetLastError(0);
39 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
40 if (reply.handle == -1) return 0;
41 return reply.handle;
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 );
54 return ret;
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;
68 req.access = access;
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 */
73 return reply.handle;
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 );
85 return ret;
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;
97 if (count < 0)
99 SetLastError( ERROR_INVALID_PARAMETER );
100 return FALSE;
102 req.handle = handle;
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;
107 return TRUE;