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 ************************************************************************/
34 #include <osl/semaphor.h>
35 #include <osl/diagnose.h>
37 #ifndef OSL_USE_SYS_V_SEMAPHORE
39 /* This is the (default) POSIX thread-local semaphore variant */
43 The void* represented by oslSemaphore is used
44 as a pointer to an sem_t struct
47 /*****************************************************************************/
48 /* osl_createSemaphore */
49 /*****************************************************************************/
51 oslSemaphore SAL_CALL
osl_createSemaphore(sal_uInt32 initialCount
)
54 oslSemaphore Semaphore
;
56 Semaphore
= malloc(sizeof(sem_t
));
58 OSL_ASSERT(Semaphore
); /* ptr valid? */
65 /* unnamed semaphore, not shared between processes */
67 ret
= sem_init((sem_t
*)Semaphore
, 0, initialCount
);
72 OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
83 /*****************************************************************************/
84 /* osl_destroySemaphore */
85 /*****************************************************************************/
86 void SAL_CALL
osl_destroySemaphore(oslSemaphore Semaphore
)
88 if(Semaphore
) /* ptr valid? */
90 sem_destroy((sem_t
*)Semaphore
);
95 /*****************************************************************************/
96 /* osl_acquireSemaphore */
97 /*****************************************************************************/
98 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
) {
100 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
102 if (Semaphore
!= 0) /* be tolerant in release mode */
104 return (sem_wait((sem_t
*)Semaphore
) == 0);
110 /*****************************************************************************/
111 /* osl_tryToAcquireSemaphore */
112 /*****************************************************************************/
113 sal_Bool SAL_CALL
osl_tryToAcquireSemaphore(oslSemaphore Semaphore
) {
115 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
116 if (Semaphore
!= 0) /* be tolerant in release mode */
118 return (sem_trywait((sem_t
*)Semaphore
) == 0);
124 /*****************************************************************************/
125 /* osl_releaseSemaphore */
126 /*****************************************************************************/
127 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
) {
129 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
131 if (Semaphore
!= 0) /* be tolerant in release mode */
133 return (sem_post((sem_t
*)Semaphore
) == 0);
139 #else /* OSL_USE_SYS_V_SEMAPHORE */
141 /*******************************************************************************/
143 /* This is the SYS V private semaphore variant */
147 The void* represented by oslSemaphore is used
148 as a pointer to an osl_TSemImpl struct
154 int val
; /* value for SETVAL */
155 struct semid_ds
*buf
; /* buffer for IPC_STAT & IPC_SET */
156 u_short
*array
; /* array for GETALL & SETALL */
160 typedef struct _osl_TSemImpl
166 /*****************************************************************************/
167 /* osl_createSemaphore */
168 /*****************************************************************************/
169 oslSemaphore SAL_CALL
osl_createSemaphore(sal_uInt32 initialCount
)
173 oslSemaphore Semaphore
;
176 Semaphore
= malloc(sizeof(osl_TSemImpl
));
177 OSL_POSTCOND(Semaphore
, "malloc failed\n"); /* ptr valid? */
179 pSem
= (osl_TSemImpl
*)Semaphore
;
182 /* unnamed (private) semaphore */
184 pSem
->m_Id
= semget(IPC_PRIVATE
, 1, 0666 | IPC_CREAT
);
190 OSL_TRACE("osl_createSemaphore failed (semget). Errno: %d; %s\n",
198 /* set initial count */
200 arg
.val
= initialCount
;
202 if(semctl(pSem
->m_Id
, 0, SETVAL
, arg
) < 0)
204 OSL_TRACE("osl_createSemaphore failed (semctl(SETVAL)). Errno: %d; %s\n",
208 if(semctl(pSem
->m_Id
, 0, IPC_RMID
, arg
) < 0)
210 OSL_TRACE("semctl(IPC_RMID) failed. Errno: %d; %s\n", errno
, strerror(errno
));
221 /*****************************************************************************/
222 /* osl_destroySemaphore */
223 /*****************************************************************************/
224 void SAL_CALL
osl_destroySemaphore(oslSemaphore Semaphore
) {
226 if(Semaphore
) /* ptr valid? */
230 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
232 if(semctl(pSem
->m_Id
, 0, IPC_RMID
, arg
) < 0)
235 OSL_TRACE("osl_destroySemaphore failed. (semctl(IPC_RMID)). Errno: %d; %s\n",
244 /*****************************************************************************/
245 /* osl_acquireSemaphore */
246 /*****************************************************************************/
247 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
) {
249 /* abort in debug mode */
250 OSL_PRECOND(Semaphore
!= 0, "Semaphore not created\n");
253 if (Semaphore
!= 0) /* be tolerant in release mode */
256 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
260 op
.sem_flg
= SEM_UNDO
;
262 return semop(pSem
->m_Id
, &op
, 1) >= 0;
269 /*****************************************************************************/
270 /* osl_tryToAcquireSemaphore */
271 /*****************************************************************************/
272 sal_Bool SAL_CALL
osl_tryToAcquireSemaphore(oslSemaphore Semaphore
) {
274 /* abort in debug mode */
275 OSL_PRECOND(Semaphore
!= 0, "Semaphore not created\n");
277 if (Semaphore
!= 0) /* be tolerant in release mode */
280 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
284 op
.sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
286 return semop(pSem
->m_Id
, &op
, 1) >= 0;
292 /*****************************************************************************/
293 /* osl_releaseSemaphore */
294 /*****************************************************************************/
295 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
)
298 /* abort in debug mode */
299 OSL_PRECOND(Semaphore
!= 0, "Semaphore not created\n");
301 if (Semaphore
!= 0) /* be tolerant in release mode */
304 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
308 op
.sem_flg
= SEM_UNDO
;
310 return semop(pSem
->m_Id
, &op
, 1) >= 0;
316 #endif /* OSL_USE_SYS_V_SEMAPHORE */