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: mutex.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 ************************************************************************/
33 #include <osl/mutex.h>
34 #include <osl/diagnose.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
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
;
58 /*****************************************************************************/
60 /*****************************************************************************/
61 oslMutex SAL_CALL
osl_createMutex()
63 oslMutexImpl
* pMutex
= (oslMutexImpl
*) malloc(sizeof(oslMutexImpl
));
64 pthread_mutexattr_t aMutexAttr
;
74 pthread_mutexattr_init(&aMutexAttr
);
76 nRet
= pthread_mutexattr_settype(&aMutexAttr
, PTHREAD_MUTEX_RECURSIVE
);
78 nRet
= pthread_mutex_init(&(pMutex
->mutex
), &aMutexAttr
);
81 OSL_TRACE("osl_createMutex : mutex init failed. Errno: %d; %s\n",
82 nRet
, strerror(nRet
));
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
;
106 nRet
= pthread_mutex_destroy(&(pMutex
->mutex
));
109 OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
110 nRet
, strerror(nRet
));
119 /*****************************************************************************/
120 /* osl_acquireMutex */
121 /*****************************************************************************/
122 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex Mutex
)
124 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
132 nRet
= pthread_mutex_lock(&(pMutex
->mutex
));
135 OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
136 nRet
, strerror(nRet
));
142 /* not initialized */
146 /*****************************************************************************/
147 /* osl_tryToAcquireMutex */
148 /*****************************************************************************/
149 sal_Bool SAL_CALL
osl_tryToAcquireMutex(oslMutex Mutex
)
151 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
158 nRet
= pthread_mutex_trylock(&(pMutex
->mutex
));
165 /* not initialized */
169 /*****************************************************************************/
170 /* osl_releaseMutex */
171 /*****************************************************************************/
172 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex Mutex
)
174 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
181 nRet
= pthread_mutex_unlock(&(pMutex
->mutex
));
184 OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
185 nRet
, strerror(nRet
));
192 /* not initialized */
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)
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) {