Rearrange winver detection code and cache the winver value we
[wine.git] / scheduler / semaphore.c
blobdbe2eecddca8caafb8169754546764d362656cf2
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_semaphore_request req;
64 struct open_semaphore_reply reply;
65 int len = name ? strlen(name) + 1 : 0;
67 req.access = access;
68 req.inherit = inherit;
69 CLIENT_SendRequest( REQ_OPEN_SEMAPHORE, -1, 2, &req, sizeof(req), name, len );
70 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
71 if (reply.handle == -1) return 0; /* must return 0 on failure, not -1 */
72 return reply.handle;
76 /***********************************************************************
77 * OpenSemaphore32W (KERNEL32.546)
79 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
81 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
82 HANDLE ret = OpenSemaphoreA( access, inherit, nameA );
83 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
84 return ret;
88 /***********************************************************************
89 * ReleaseSemaphore (KERNEL32.583)
91 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
93 struct release_semaphore_request req;
94 struct release_semaphore_reply reply;
96 if (count < 0)
98 SetLastError( ERROR_INVALID_PARAMETER );
99 return FALSE;
101 req.handle = handle;
102 req.count = (unsigned int)count;
103 CLIENT_SendRequest( REQ_RELEASE_SEMAPHORE, -1, 1, &req, sizeof(req) );
104 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
105 if (previous) *previous = reply.prev_count;
106 return TRUE;