merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / unx / semaphor.c
blobc514b2dacff61d80b3f5d6d83c67f9c3488f7747
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 ************************************************************************/
29 #include "system.h"
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 */
39 Implemetation notes:
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)
50 int ret = 0;
51 oslSemaphore Semaphore;
53 Semaphore= malloc(sizeof(sem_t));
55 OSL_ASSERT(Semaphore); /* ptr valid? */
57 if ( Semaphore == 0 )
59 return 0;
62 /* unnamed semaphore, not shared between processes */
64 ret= sem_init((sem_t*)Semaphore, 0, initialCount);
66 /* create failed? */
67 if (ret != 0)
69 OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
70 errno,
71 strerror(errno));
73 free(Semaphore);
74 Semaphore = NULL;
77 return Semaphore;
80 /*****************************************************************************/
81 /* osl_destroySemaphore */
82 /*****************************************************************************/
83 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
85 if(Semaphore) /* ptr valid? */
87 sem_destroy((sem_t*)Semaphore);
88 free(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);
104 return sal_False;
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);
118 return sal_False;
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);
133 return sal_False;
136 #else /* OSL_USE_SYS_V_SEMAPHORE */
138 /*******************************************************************************/
140 /* This is the SYS V private semaphore variant */
143 Implemetation notes:
144 The void* represented by oslSemaphore is used
145 as a pointer to an osl_TSemImpl struct
149 #if defined(NETBSD)
150 union semun {
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 */
155 #endif
157 typedef struct _osl_TSemImpl
159 int m_Id;
161 } osl_TSemImpl;
163 /*****************************************************************************/
164 /* osl_createSemaphore */
165 /*****************************************************************************/
166 oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
168 union semun arg;
170 oslSemaphore Semaphore;
171 osl_TSemImpl* pSem;
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);
184 /* create failed? */
185 if (pSem->m_Id < 0)
187 OSL_TRACE("osl_createSemaphore failed (semget). Errno: %d; %s\n",
188 errno,
189 strerror(errno));
191 free(Semaphore);
192 return 0;
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",
202 errno,
203 strerror(errno));
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));
210 free(Semaphore);
211 return 0;
215 return Semaphore;
218 /*****************************************************************************/
219 /* osl_destroySemaphore */
220 /*****************************************************************************/
221 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore) {
223 if(Semaphore) /* ptr valid? */
225 union semun arg;
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",
233 errno,
234 strerror(errno));
237 free(Semaphore);
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 */
252 struct sembuf op;
253 osl_TSemImpl* pSem= (osl_TSemImpl*)Semaphore;
255 op.sem_num= 0;
256 op.sem_op= -1;
257 op.sem_flg= SEM_UNDO;
259 return semop(pSem->m_Id, &op, 1) >= 0;
263 return sal_False;
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 */
276 struct sembuf op;
277 osl_TSemImpl* pSem= (osl_TSemImpl*)Semaphore;
279 op.sem_num= 0;
280 op.sem_op= -1;
281 op.sem_flg= SEM_UNDO | IPC_NOWAIT;
283 return semop(pSem->m_Id, &op, 1) >= 0;
286 return sal_False;
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 */
300 struct sembuf op;
301 osl_TSemImpl* pSem= (osl_TSemImpl*)Semaphore;
303 op.sem_num= 0;
304 op.sem_op= 1;
305 op.sem_flg= SEM_UNDO;
307 return semop(pSem->m_Id, &op, 1) >= 0;
310 return sal_False;
313 #endif /* OSL_USE_SYS_V_SEMAPHORE */