1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 #include <osl/diagnose.h>
31 #include <osl/semaphor.h>
35 The void* represented by oslSemaphore is used
36 to store a OS/2 HANDLE.
39 typedef struct _oslSemaphoreImpl
45 // static mutex to control access to private members of oslMutexImpl
46 static HMTX MutexLock
= NULL
;
48 /*****************************************************************************/
49 /* osl_createSemaphore */
50 /*****************************************************************************/
53 - Erzeugen der Semaphore
54 - Z„hler auf initialCount setzen
56 oslSemaphore SAL_CALL
osl_createSemaphore(sal_uInt32 initialCount
)
59 oslSemaphoreImpl
* pSemaphoreImpl
;
61 /* alloc mem. for our internal data structure */
62 pSemaphoreImpl
= (oslSemaphoreImpl
*) malloc(sizeof(oslSemaphoreImpl
));
63 if( pSemaphoreImpl
== NULL
)
66 /* create semaphore */
67 rc
= DosCreateEventSem( NULL
,
68 &pSemaphoreImpl
->hevReachedZero
,
73 free( pSemaphoreImpl
);
77 pSemaphoreImpl
->nCount
= initialCount
;
79 // create static mutex for private members
80 if (MutexLock
== NULL
)
81 DosCreateMutexSem( NULL
, &MutexLock
, 0, FALSE
);
83 return (oslSemaphore
) pSemaphoreImpl
;
86 /*****************************************************************************/
87 /* osl_destroySemaphore */
88 /*****************************************************************************/
94 void SAL_CALL
osl_destroySemaphore(oslSemaphore Semaphore
)
96 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
97 OSL_ASSERT(Semaphore
!= 0);
99 DosCloseEventSem( pSemaphoreImpl
->hevReachedZero
);
101 free( pSemaphoreImpl
);
104 /*****************************************************************************/
105 /* osl_acquireSemaphore */
106 /*****************************************************************************/
109 - wenn Z„hler < 0: blockieren
112 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
)
115 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
117 OSL_ASSERT(Semaphore
!= 0);
119 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
121 while( pSemaphoreImpl
->nCount
< 1 )
123 sal_uInt32 nPostCount
;
125 DosReleaseMutexSem( MutexLock
);
127 rc
= DosWaitEventSem(pSemaphoreImpl
->hevReachedZero
, SEM_INDEFINITE_WAIT
);
128 DosResetEventSem(pSemaphoreImpl
->hevReachedZero
, &nPostCount
);
130 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
133 pSemaphoreImpl
->nCount
--;
134 DosReleaseMutexSem( MutexLock
);
136 return( rc
== NO_ERROR
);
139 /*****************************************************************************/
140 /* osl_tryToAcquireSemaphore */
141 /*****************************************************************************/
143 - Z„hler -1, wenn vorher > 0
144 - wenn Z„hler < 0: mit FALSE zurueck
146 sal_Bool SAL_CALL
osl_tryToAcquireSemaphore(oslSemaphore Semaphore
)
149 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
151 OSL_ASSERT(Semaphore
!= 0);
153 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
155 nCount
= pSemaphoreImpl
->nCount
;
156 if( pSemaphoreImpl
->nCount
> 0 )
157 pSemaphoreImpl
->nCount
--;
159 DosReleaseMutexSem( MutexLock
);
161 return( nCount
> 0 );
164 /*****************************************************************************/
165 /* osl_releaseSemaphore */
166 /*****************************************************************************/
170 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
)
173 oslSemaphoreImpl
* pSemaphoreImpl
= (oslSemaphoreImpl
*)Semaphore
;
175 OSL_ASSERT(Semaphore
!= 0);
177 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
179 nCount
= pSemaphoreImpl
->nCount
;
180 pSemaphoreImpl
->nCount
++;
182 DosReleaseMutexSem( MutexLock
);
185 DosPostEventSem(pSemaphoreImpl
->hevReachedZero
);
187 return( rc
== NO_ERROR
);