merge the formfield patch from ooo-build
[ooovba.git] / sal / osl / unx / mutex.c
blobdb94eece83c7c938eb31a72f7412524c473b1e88
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: mutex.c,v $
10 * $Revision: 1.15 $
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 ************************************************************************/
31 #include "system.h"
33 #include <osl/mutex.h>
34 #include <osl/diagnose.h>
36 #include <pthread.h>
37 #include <stdlib.h>
39 #if defined LINUX /* bad hack */
40 int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int);
41 #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
42 #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
43 #endif
46 Implementation notes:
47 oslMutex hides a pointer to the oslMutexImpl structure, which
48 ist needed to manage recursive locks on a mutex.
52 typedef struct _oslMutexImpl
54 pthread_mutex_t mutex;
55 } oslMutexImpl;
58 /*****************************************************************************/
59 /* osl_createMutex */
60 /*****************************************************************************/
61 oslMutex SAL_CALL osl_createMutex()
63 oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
64 pthread_mutexattr_t aMutexAttr;
65 int nRet=0;
67 OSL_ASSERT(pMutex);
69 if ( pMutex == 0 )
71 return 0;
74 pthread_mutexattr_init(&aMutexAttr);
76 nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
78 nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
79 if ( nRet != 0 )
81 OSL_TRACE("osl_createMutex : mutex init failed. Errno: %d; %s\n",
82 nRet, strerror(nRet));
84 free(pMutex);
85 pMutex = 0;
88 pthread_mutexattr_destroy(&aMutexAttr);
90 return (oslMutex) pMutex;
93 /*****************************************************************************/
94 /* osl_destroyMutex */
95 /*****************************************************************************/
96 void SAL_CALL osl_destroyMutex(oslMutex Mutex)
98 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
100 OSL_ASSERT(pMutex);
102 if ( pMutex != 0 )
104 int nRet=0;
106 nRet = pthread_mutex_destroy(&(pMutex->mutex));
107 if ( nRet != 0 )
109 OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
110 nRet, strerror(nRet));
113 free(pMutex);
116 return;
119 /*****************************************************************************/
120 /* osl_acquireMutex */
121 /*****************************************************************************/
122 sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
124 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
126 OSL_ASSERT(pMutex);
128 if ( pMutex != 0 )
130 int nRet=0;
132 nRet = pthread_mutex_lock(&(pMutex->mutex));
133 if ( nRet != 0 )
135 OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
136 nRet, strerror(nRet));
137 return sal_False;
139 return sal_True;
142 /* not initialized */
143 return sal_False;
146 /*****************************************************************************/
147 /* osl_tryToAcquireMutex */
148 /*****************************************************************************/
149 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
151 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
153 OSL_ASSERT(pMutex);
155 if ( pMutex )
157 int nRet = 0;
158 nRet = pthread_mutex_trylock(&(pMutex->mutex));
159 if ( nRet != 0 )
160 return sal_False;
162 return sal_True;
165 /* not initialized */
166 return sal_False;
169 /*****************************************************************************/
170 /* osl_releaseMutex */
171 /*****************************************************************************/
172 sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
174 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
176 OSL_ASSERT(pMutex);
178 if ( pMutex )
180 int nRet=0;
181 nRet = pthread_mutex_unlock(&(pMutex->mutex));
182 if ( nRet != 0 )
184 OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
185 nRet, strerror(nRet));
186 return sal_False;
189 return sal_True;
192 /* not initialized */
193 return sal_False;
196 /*****************************************************************************/
197 /* osl_getGlobalMutex */
198 /*****************************************************************************/
200 static oslMutexImpl globalMutexImpl;
202 static void globalMutexInitImpl(void) {
203 pthread_mutexattr_t attr;
204 if (pthread_mutexattr_init(&attr) != 0 ||
205 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
206 pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
207 pthread_mutexattr_destroy(&attr) != 0)
209 abort();
213 oslMutex * SAL_CALL osl_getGlobalMutex()
215 /* necessary to get a "oslMutex *" */
216 static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
218 static pthread_once_t once = PTHREAD_ONCE_INIT;
219 if (pthread_once(&once, &globalMutexInitImpl) != 0) {
220 abort();
223 return &globalMutex;