update dev300-m58
[ooovba.git] / sal / osl / os2 / semaphor.c
blob69801cae2fcfdfc153edd69554e1f0cc8ba444b1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: semaphor.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "system.h"
33 #include <osl/diagnose.h>
34 #include <osl/semaphor.h>
37 Implemetation notes:
38 The void* represented by oslSemaphore is used
39 to store a OS/2 HANDLE.
42 typedef struct _oslSemaphoreImpl
44 HEV hevReachedZero;
45 int nCount;
46 } oslSemaphoreImpl;
48 // static mutex to control access to private members of oslMutexImpl
49 static HMTX MutexLock = NULL;
51 /*****************************************************************************/
52 /* osl_createSemaphore */
53 /*****************************************************************************/
56 - Erzeugen der Semaphore
57 - Z„hler auf initialCount setzen
59 oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
61 APIRET rc;
62 oslSemaphoreImpl * pSemaphoreImpl;
64 /* alloc mem. for our internal data structure */
65 pSemaphoreImpl = (oslSemaphoreImpl *) malloc(sizeof(oslSemaphoreImpl));
66 if( pSemaphoreImpl == NULL )
67 return NULL;
69 /* create semaphore */
70 rc = DosCreateEventSem( NULL,
71 &pSemaphoreImpl->hevReachedZero,
72 DC_SEM_SHARED,
73 FALSE );
74 if( rc != NO_ERROR )
76 free( pSemaphoreImpl );
77 return NULL;
80 pSemaphoreImpl->nCount = initialCount;
82 // create static mutex for private members
83 if (MutexLock == NULL)
84 DosCreateMutexSem( NULL, &MutexLock, 0, FALSE );
86 return (oslSemaphore) pSemaphoreImpl;
89 /*****************************************************************************/
90 /* osl_destroySemaphore */
91 /*****************************************************************************/
94 - Semaphore l”schen
97 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
99 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
100 OSL_ASSERT(Semaphore != 0);
102 DosCloseEventSem( pSemaphoreImpl->hevReachedZero );
104 free( pSemaphoreImpl );
107 /*****************************************************************************/
108 /* osl_acquireSemaphore */
109 /*****************************************************************************/
111 - Z„hler -1
112 - wenn Z„hler < 0: blockieren
115 sal_Bool SAL_CALL osl_acquireSemaphore(oslSemaphore Semaphore)
117 APIRET rc;
118 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
119 int nCount;
120 OSL_ASSERT(Semaphore != 0);
122 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
124 while( pSemaphoreImpl->nCount < 1 )
126 sal_uInt32 nPostCount;
128 DosReleaseMutexSem( MutexLock);
130 rc = DosWaitEventSem(pSemaphoreImpl->hevReachedZero, SEM_INDEFINITE_WAIT );
131 DosResetEventSem(pSemaphoreImpl->hevReachedZero, &nPostCount);
133 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
136 pSemaphoreImpl->nCount--;
137 DosReleaseMutexSem( MutexLock);
139 return( rc == NO_ERROR );
142 /*****************************************************************************/
143 /* osl_tryToAcquireSemaphore */
144 /*****************************************************************************/
146 - Z„hler -1, wenn vorher > 0
147 - wenn Z„hler < 0: mit FALSE zurueck
149 sal_Bool SAL_CALL osl_tryToAcquireSemaphore(oslSemaphore Semaphore)
151 APIRET rc;
152 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
153 int nCount;
154 OSL_ASSERT(Semaphore != 0);
156 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
158 nCount = pSemaphoreImpl->nCount;
159 if( pSemaphoreImpl->nCount > 0 )
160 pSemaphoreImpl->nCount--;
162 DosReleaseMutexSem( MutexLock);
164 return( nCount > 0 );
167 /*****************************************************************************/
168 /* osl_releaseSemaphore */
169 /*****************************************************************************/
171 - Z„hler +1
173 sal_Bool SAL_CALL osl_releaseSemaphore(oslSemaphore Semaphore)
175 APIRET rc;
176 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore;
177 int nCount;
178 OSL_ASSERT(Semaphore != 0);
180 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
182 nCount = pSemaphoreImpl->nCount;
183 pSemaphoreImpl->nCount++;
185 DosReleaseMutexSem( MutexLock);
187 if( nCount == 0 )
188 DosPostEventSem(pSemaphoreImpl->hevReachedZero);
190 return( rc == NO_ERROR );