update dev300-m57
[ooovba.git] / sal / osl / unx / semaphor.c
blobca3cb1217c4516aa9dec14accd6d816f5921fc54
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: semaphor.c,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
32 #include "system.h"
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 */
42 Implemetation notes:
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)
53 int ret = 0;
54 oslSemaphore Semaphore;
56 Semaphore= malloc(sizeof(sem_t));
58 OSL_ASSERT(Semaphore); /* ptr valid? */
60 if ( Semaphore == 0 )
62 return 0;
65 /* unnamed semaphore, not shared between processes */
67 ret= sem_init((sem_t*)Semaphore, 0, initialCount);
69 /* create failed? */
70 if (ret != 0)
72 OSL_TRACE("osl_createSemaphore failed. Errno: %d; %s\n",
73 errno,
74 strerror(errno));
76 free(Semaphore);
77 Semaphore = NULL;
80 return Semaphore;
83 /*****************************************************************************/
84 /* osl_destroySemaphore */
85 /*****************************************************************************/
86 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore)
88 if(Semaphore) /* ptr valid? */
90 sem_destroy((sem_t*)Semaphore);
91 free(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);
107 return sal_False;
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);
121 return sal_False;
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);
136 return sal_False;
139 #else /* OSL_USE_SYS_V_SEMAPHORE */
141 /*******************************************************************************/
143 /* This is the SYS V private semaphore variant */
146 Implemetation notes:
147 The void* represented by oslSemaphore is used
148 as a pointer to an osl_TSemImpl struct
152 #if defined(NETBSD)
153 union semun {
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 */
158 #endif
160 typedef struct _osl_TSemImpl
162 int m_Id;
164 } osl_TSemImpl;
166 /*****************************************************************************/
167 /* osl_createSemaphore */
168 /*****************************************************************************/
169 oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount)
171 union semun arg;
173 oslSemaphore Semaphore;
174 osl_TSemImpl* pSem;
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);
187 /* create failed? */
188 if (pSem->m_Id < 0)
190 OSL_TRACE("osl_createSemaphore failed (semget). Errno: %d; %s\n",
191 errno,
192 strerror(errno));
194 free(Semaphore);
195 return 0;
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",
205 errno,
206 strerror(errno));
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));
213 free(Semaphore);
214 return 0;
218 return Semaphore;
221 /*****************************************************************************/
222 /* osl_destroySemaphore */
223 /*****************************************************************************/
224 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore) {
226 if(Semaphore) /* ptr valid? */
228 union semun arg;
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",
236 errno,
237 strerror(errno));
240 free(Semaphore);
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 */
255 struct sembuf op;
256 osl_TSemImpl* pSem= (osl_TSemImpl*)Semaphore;
258 op.sem_num= 0;
259 op.sem_op= -1;
260 op.sem_flg= SEM_UNDO;
262 return semop(pSem->m_Id, &op, 1) >= 0;
266 return sal_False;
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 */
279 struct sembuf op;
280 osl_TSemImpl* pSem= (osl_TSemImpl*)Semaphore;
282 op.sem_num= 0;
283 op.sem_op= -1;
284 op.sem_flg= SEM_UNDO | IPC_NOWAIT;
286 return semop(pSem->m_Id, &op, 1) >= 0;
289 return sal_False;
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 */
303 struct sembuf op;
304 osl_TSemImpl* pSem= (osl_TSemImpl*)Semaphore;
306 op.sem_num= 0;
307 op.sem_op= 1;
308 op.sem_flg= SEM_UNDO;
310 return semop(pSem->m_Id, &op, 1) >= 0;
313 return sal_False;
316 #endif /* OSL_USE_SYS_V_SEMAPHORE */