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