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 ************************************************************************/
31 #include <osl/semaphor.h>
32 #include <osl/diagnose.h>
34 #ifndef OSL_USE_SYS_V_SEMAPHORE
36 /* This is the (default) POSIX thread-local semaphore variant */
40 The void* represented by oslSemaphore is used
41 as a pointer to an sem_t struct
44 /*****************************************************************************/
45 /* osl_createSemaphore */
46 /*****************************************************************************/
48 oslSemaphore SAL_CALL
osl_createSemaphore(sal_uInt32 initialCount
)
51 oslSemaphore Semaphore
;
53 Semaphore
= malloc(sizeof(sem_t
));
55 OSL_ASSERT(Semaphore
); /* ptr valid? */
62 /* unnamed semaphore, not shared between processes */
64 ret
= sem_init((sem_t
*)Semaphore
, 0, initialCount
);
69 OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
80 /*****************************************************************************/
81 /* osl_destroySemaphore */
82 /*****************************************************************************/
83 void SAL_CALL
osl_destroySemaphore(oslSemaphore Semaphore
)
85 if(Semaphore
) /* ptr valid? */
87 sem_destroy((sem_t
*)Semaphore
);
92 /*****************************************************************************/
93 /* osl_acquireSemaphore */
94 /*****************************************************************************/
95 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
) {
97 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
99 if (Semaphore
!= 0) /* be tolerant in release mode */
101 return (sem_wait((sem_t
*)Semaphore
) == 0);
107 /*****************************************************************************/
108 /* osl_tryToAcquireSemaphore */
109 /*****************************************************************************/
110 sal_Bool SAL_CALL
osl_tryToAcquireSemaphore(oslSemaphore Semaphore
) {
112 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
113 if (Semaphore
!= 0) /* be tolerant in release mode */
115 return (sem_trywait((sem_t
*)Semaphore
) == 0);
121 /*****************************************************************************/
122 /* osl_releaseSemaphore */
123 /*****************************************************************************/
124 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
) {
126 OSL_ASSERT(Semaphore
!= 0); /* abort in debug mode */
128 if (Semaphore
!= 0) /* be tolerant in release mode */
130 return (sem_post((sem_t
*)Semaphore
) == 0);
136 #else /* OSL_USE_SYS_V_SEMAPHORE */
138 /*******************************************************************************/
140 /* This is the SYS V private semaphore variant */
144 The void* represented by oslSemaphore is used
145 as a pointer to an osl_TSemImpl struct
151 int val
; /* value for SETVAL */
152 struct semid_ds
*buf
; /* buffer for IPC_STAT & IPC_SET */
153 u_short
*array
; /* array for GETALL & SETALL */
157 typedef struct _osl_TSemImpl
163 /*****************************************************************************/
164 /* osl_createSemaphore */
165 /*****************************************************************************/
166 oslSemaphore SAL_CALL
osl_createSemaphore(sal_uInt32 initialCount
)
170 oslSemaphore Semaphore
;
173 Semaphore
= malloc(sizeof(osl_TSemImpl
));
174 OSL_POSTCOND(Semaphore
, "malloc failed\n"); /* ptr valid? */
176 pSem
= (osl_TSemImpl
*)Semaphore
;
179 /* unnamed (private) semaphore */
181 pSem
->m_Id
= semget(IPC_PRIVATE
, 1, 0666 | IPC_CREAT
);
187 OSL_TRACE("osl_createSemaphore failed (semget). Errno: %d; %s\n",
195 /* set initial count */
197 arg
.val
= initialCount
;
199 if(semctl(pSem
->m_Id
, 0, SETVAL
, arg
) < 0)
201 OSL_TRACE("osl_createSemaphore failed (semctl(SETVAL)). Errno: %d; %s\n",
205 if(semctl(pSem
->m_Id
, 0, IPC_RMID
, arg
) < 0)
207 OSL_TRACE("semctl(IPC_RMID) failed. Errno: %d; %s\n", errno
, strerror(errno
));
218 /*****************************************************************************/
219 /* osl_destroySemaphore */
220 /*****************************************************************************/
221 void SAL_CALL
osl_destroySemaphore(oslSemaphore Semaphore
) {
223 if(Semaphore
) /* ptr valid? */
227 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
229 if(semctl(pSem
->m_Id
, 0, IPC_RMID
, arg
) < 0)
232 OSL_TRACE("osl_destroySemaphore failed. (semctl(IPC_RMID)). Errno: %d; %s\n",
241 /*****************************************************************************/
242 /* osl_acquireSemaphore */
243 /*****************************************************************************/
244 sal_Bool SAL_CALL
osl_acquireSemaphore(oslSemaphore Semaphore
) {
246 /* abort in debug mode */
247 OSL_PRECOND(Semaphore
!= 0, "Semaphore not created\n");
250 if (Semaphore
!= 0) /* be tolerant in release mode */
253 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
257 op
.sem_flg
= SEM_UNDO
;
259 return semop(pSem
->m_Id
, &op
, 1) >= 0;
266 /*****************************************************************************/
267 /* osl_tryToAcquireSemaphore */
268 /*****************************************************************************/
269 sal_Bool SAL_CALL
osl_tryToAcquireSemaphore(oslSemaphore Semaphore
) {
271 /* abort in debug mode */
272 OSL_PRECOND(Semaphore
!= 0, "Semaphore not created\n");
274 if (Semaphore
!= 0) /* be tolerant in release mode */
277 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
281 op
.sem_flg
= SEM_UNDO
| IPC_NOWAIT
;
283 return semop(pSem
->m_Id
, &op
, 1) >= 0;
289 /*****************************************************************************/
290 /* osl_releaseSemaphore */
291 /*****************************************************************************/
292 sal_Bool SAL_CALL
osl_releaseSemaphore(oslSemaphore Semaphore
)
295 /* abort in debug mode */
296 OSL_PRECOND(Semaphore
!= 0, "Semaphore not created\n");
298 if (Semaphore
!= 0) /* be tolerant in release mode */
301 osl_TSemImpl
* pSem
= (osl_TSemImpl
*)Semaphore
;
305 op
.sem_flg
= SEM_UNDO
;
307 return semop(pSem
->m_Id
, &op
, 1) >= 0;
313 #endif /* OSL_USE_SYS_V_SEMAPHORE */