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 ************************************************************************/
30 #include <osl/mutex.h>
31 #include <osl/diagnose.h>
36 #if defined LINUX /* bad hack */
37 int pthread_mutexattr_setkind_np(pthread_mutexattr_t
*, int);
38 #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
39 #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
44 oslMutex hides a pointer to the oslMutexImpl structure, which
45 ist needed to manage recursive locks on a mutex.
49 typedef struct _oslMutexImpl
51 pthread_mutex_t mutex
;
55 /*****************************************************************************/
57 /*****************************************************************************/
58 oslMutex SAL_CALL
osl_createMutex()
60 oslMutexImpl
* pMutex
= (oslMutexImpl
*) malloc(sizeof(oslMutexImpl
));
61 pthread_mutexattr_t aMutexAttr
;
71 pthread_mutexattr_init(&aMutexAttr
);
73 nRet
= pthread_mutexattr_settype(&aMutexAttr
, PTHREAD_MUTEX_RECURSIVE
);
75 nRet
= pthread_mutex_init(&(pMutex
->mutex
), &aMutexAttr
);
78 OSL_TRACE("osl_createMutex : mutex init failed. Errno: %d; %s\n",
79 nRet
, strerror(nRet
));
85 pthread_mutexattr_destroy(&aMutexAttr
);
87 return (oslMutex
) pMutex
;
90 /*****************************************************************************/
91 /* osl_destroyMutex */
92 /*****************************************************************************/
93 void SAL_CALL
osl_destroyMutex(oslMutex Mutex
)
95 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
103 nRet
= pthread_mutex_destroy(&(pMutex
->mutex
));
106 OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
107 nRet
, strerror(nRet
));
116 /*****************************************************************************/
117 /* osl_acquireMutex */
118 /*****************************************************************************/
119 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex Mutex
)
121 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
129 nRet
= pthread_mutex_lock(&(pMutex
->mutex
));
132 OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
133 nRet
, strerror(nRet
));
139 /* not initialized */
143 /*****************************************************************************/
144 /* osl_tryToAcquireMutex */
145 /*****************************************************************************/
146 sal_Bool SAL_CALL
osl_tryToAcquireMutex(oslMutex Mutex
)
148 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
155 nRet
= pthread_mutex_trylock(&(pMutex
->mutex
));
162 /* not initialized */
166 /*****************************************************************************/
167 /* osl_releaseMutex */
168 /*****************************************************************************/
169 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex Mutex
)
171 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
178 nRet
= pthread_mutex_unlock(&(pMutex
->mutex
));
181 OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
182 nRet
, strerror(nRet
));
189 /* not initialized */
193 /*****************************************************************************/
194 /* osl_getGlobalMutex */
195 /*****************************************************************************/
197 static oslMutexImpl globalMutexImpl
;
199 static void globalMutexInitImpl(void) {
200 pthread_mutexattr_t attr
;
201 if (pthread_mutexattr_init(&attr
) != 0 ||
202 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
) ||
203 pthread_mutex_init(&globalMutexImpl
.mutex
, &attr
) != 0 ||
204 pthread_mutexattr_destroy(&attr
) != 0)
210 oslMutex
* SAL_CALL
osl_getGlobalMutex()
212 /* necessary to get a "oslMutex *" */
213 static oslMutex globalMutex
= (oslMutex
) &globalMutexImpl
;
215 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
216 if (pthread_once(&once
, &globalMutexInitImpl
) != 0) {