xref: /aoo41x/main/sal/osl/w32/semaphor.c (revision 647f063d)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647f063dSAndrew Rist  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647f063dSAndrew Rist  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19*647f063dSAndrew Rist  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <osl/semaphor.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /*
30cdf0e10cSrcweir 	Implemetation notes:
31cdf0e10cSrcweir 	The void* represented by oslSemaphore is used
32cdf0e10cSrcweir 	to store a WIN32 HANDLE.
33cdf0e10cSrcweir */
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /*****************************************************************************/
37cdf0e10cSrcweir /* osl_createSemaphore */
38cdf0e10cSrcweir /*****************************************************************************/
osl_createSemaphore(sal_uInt32 initialCount)39cdf0e10cSrcweir oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 	oslSemaphore Semaphore;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	Semaphore= CreateSemaphore(0, initialCount, INT_MAX, 0);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 	/* create failed? */
46cdf0e10cSrcweir 	if((HANDLE)Semaphore == INVALID_HANDLE_VALUE)
47cdf0e10cSrcweir 	{
48cdf0e10cSrcweir 		Semaphore= 0;
49cdf0e10cSrcweir 	}
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	return Semaphore;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir /*****************************************************************************/
55cdf0e10cSrcweir /* osl_destroySemaphore */
56cdf0e10cSrcweir /*****************************************************************************/
osl_destroySemaphore(oslSemaphore Semaphore)57cdf0e10cSrcweir void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	if(Semaphore != 0)
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		CloseHandle((HANDLE)Semaphore);
64cdf0e10cSrcweir 	}
65cdf0e10cSrcweir 
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir /*****************************************************************************/
69cdf0e10cSrcweir /* osl_acquireSemaphore */
70cdf0e10cSrcweir /*****************************************************************************/
osl_acquireSemaphore(oslSemaphore Semaphore)71cdf0e10cSrcweir sal_Bool SAL_CALL osl_acquireSemaphore(oslSemaphore Semaphore)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	OSL_ASSERT(Semaphore != 0);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	switch ( WaitForSingleObject( (HANDLE)Semaphore, INFINITE ) )
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		case WAIT_OBJECT_0:
78cdf0e10cSrcweir 			return sal_True;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		default:
81cdf0e10cSrcweir 			return (sal_False);
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir /*****************************************************************************/
86cdf0e10cSrcweir /* osl_tryToAcquireSemaphore */
87cdf0e10cSrcweir /*****************************************************************************/
osl_tryToAcquireSemaphore(oslSemaphore Semaphore)88cdf0e10cSrcweir sal_Bool SAL_CALL osl_tryToAcquireSemaphore(oslSemaphore Semaphore)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	OSL_ASSERT(Semaphore != 0);
91cdf0e10cSrcweir 	return (sal_Bool)(WaitForSingleObject((HANDLE)Semaphore, 0) == WAIT_OBJECT_0);
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
95cdf0e10cSrcweir /*****************************************************************************/
96cdf0e10cSrcweir /* osl_releaseSemaphore */
97cdf0e10cSrcweir /*****************************************************************************/
osl_releaseSemaphore(oslSemaphore Semaphore)98cdf0e10cSrcweir sal_Bool SAL_CALL osl_releaseSemaphore(oslSemaphore Semaphore)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	OSL_ASSERT(Semaphore != 0);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /* increase count by one, not interested in previous count */
103cdf0e10cSrcweir 	return (sal_Bool)(ReleaseSemaphore((HANDLE)Semaphore, 1, NULL) != FALSE);
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108