xref: /aoo41x/main/sal/osl/os2/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 OS/2 HANDLE.
33 */
34 
35 typedef struct _oslSemaphoreImpl
36 {
37     HEV hevReachedZero;
38     int nCount;
39 } oslSemaphoreImpl;
40 
41 // static mutex to control access to private members of oslMutexImpl
42 static HMTX MutexLock = NULL;
43 
44 /*****************************************************************************/
45 /* osl_createSemaphore */
46 /*****************************************************************************/
47 
48 /*
49 - Erzeugen der Semaphore
50 - Z�hler auf initialCount setzen
51 */
osl_createSemaphore(sal_uInt32 initialCount)52 oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
53 {
54     APIRET rc;
55     oslSemaphoreImpl * pSemaphoreImpl;
56 
57     /* alloc mem. for our internal data structure */
58     pSemaphoreImpl = (oslSemaphoreImpl *) malloc(sizeof(oslSemaphoreImpl));
59     if( pSemaphoreImpl == NULL )
60         return NULL;
61 
62     /* create semaphore */
63     rc = DosCreateEventSem( NULL,
64                             &pSemaphoreImpl->hevReachedZero,
65                             DC_SEM_SHARED,
66                             FALSE );
67     if( rc != NO_ERROR )
68     {
69         free( pSemaphoreImpl );
70         return NULL;
71     }
72 
73     pSemaphoreImpl->nCount = initialCount;
74 
75 	// create static mutex for private members
76 	if (MutexLock == NULL)
77 		DosCreateMutexSem( NULL, &MutexLock, 0, FALSE );
78 
79     return (oslSemaphore) pSemaphoreImpl;
80 }
81 
82 /*****************************************************************************/
83 /* osl_destroySemaphore */
84 /*****************************************************************************/
85 
86 /*
87 - Semaphore l�schen
88 */
89 
osl_destroySemaphore(oslSemaphore Semaphore)90 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
91 {
92     oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
93     OSL_ASSERT(Semaphore != 0);
94 
95     DosCloseEventSem( pSemaphoreImpl->hevReachedZero );
96 
97     free( pSemaphoreImpl );
98 }
99 
100 /*****************************************************************************/
101 /* osl_acquireSemaphore */
102 /*****************************************************************************/
103 /*
104 - Z�hler -1
105 - wenn Z�hler < 0: blockieren
106 */
107 
osl_acquireSemaphore(oslSemaphore Semaphore)108 sal_Bool SAL_CALL osl_acquireSemaphore(oslSemaphore Semaphore)
109 {
110     APIRET rc;
111     oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
112     int nCount;
113     OSL_ASSERT(Semaphore != 0);
114 
115     DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
116 
117     while( pSemaphoreImpl->nCount < 1 )
118     {
119         sal_uInt32 nPostCount;
120 
121 		DosReleaseMutexSem( MutexLock);
122 
123         rc = DosWaitEventSem(pSemaphoreImpl->hevReachedZero, SEM_INDEFINITE_WAIT );
124         DosResetEventSem(pSemaphoreImpl->hevReachedZero, &nPostCount);
125 
126 		DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
127     }
128 
129     pSemaphoreImpl->nCount--;
130     DosReleaseMutexSem( MutexLock);
131 
132     return( rc == NO_ERROR );
133 }
134 
135 /*****************************************************************************/
136 /* osl_tryToAcquireSemaphore */
137 /*****************************************************************************/
138 /*
139 - Z�hler -1, wenn vorher > 0
140 - wenn Z�hler < 0: mit FALSE zurueck
141 */
osl_tryToAcquireSemaphore(oslSemaphore Semaphore)142 sal_Bool SAL_CALL osl_tryToAcquireSemaphore(oslSemaphore Semaphore)
143 {
144     APIRET rc;
145     oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
146     int nCount;
147     OSL_ASSERT(Semaphore != 0);
148 
149     DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
150 
151     nCount = pSemaphoreImpl->nCount;
152     if( pSemaphoreImpl->nCount > 0 )
153         pSemaphoreImpl->nCount--;
154 
155     DosReleaseMutexSem( MutexLock);
156 
157     return( nCount > 0 );
158 }
159 
160 /*****************************************************************************/
161 /* osl_releaseSemaphore */
162 /*****************************************************************************/
163 /*
164 - Z�hler +1
165 */
osl_releaseSemaphore(oslSemaphore Semaphore)166 sal_Bool SAL_CALL osl_releaseSemaphore(oslSemaphore Semaphore)
167 {
168     APIRET rc;
169     oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
170     int nCount;
171     OSL_ASSERT(Semaphore != 0);
172 
173     DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
174 
175     nCount = pSemaphoreImpl->nCount;
176     pSemaphoreImpl->nCount++;
177 
178     DosReleaseMutexSem( MutexLock);
179 
180     if( nCount == 0 )
181         DosPostEventSem(pSemaphoreImpl->hevReachedZero);
182 
183     return( rc == NO_ERROR );
184 }
185 
186 
187