1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: semaphor.c,v $
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 ************************************************************************/
33 #include <osl/diagnose.h>
34 #include <osl/semaphor.h>
38 The void* represented by oslSemaphore is used
39 to store a OS/2 HANDLE.
42 typedef struct _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
)
62 oslSemaphoreImpl
* pSemaphoreImpl
;
64 /* alloc mem. for our internal data structure */
65 pSemaphoreImpl
= (oslSemaphoreImpl
*) malloc(sizeof(oslSemaphoreImpl
));
66 if( pSemaphoreImpl
== NULL
)
69 /* create semaphore */
70 rc
= DosCreateEventSem( NULL
,
71 &pSemaphoreImpl
->hevReachedZero
,
76 free( pSemaphoreImpl
);
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 /*****************************************************************************/
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 /*****************************************************************************/
112 - wenn Z„hler < 0: blockieren
115 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
)
118 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
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
)
152 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
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 /*****************************************************************************/
173 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
)
176 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
178 OSL_ASSERT(Semaphore
!= 0);
180 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
182 nCount
= pSemaphoreImpl
->nCount
;
183 pSemaphoreImpl
->nCount
++;
185 DosReleaseMutexSem( MutexLock
);
188 DosPostEventSem(pSemaphoreImpl
->hevReachedZero
);
190 return( rc
== NO_ERROR
);