1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include <osl/mutex.h>
32 #include <osl/diagnose.h>
37 #if defined LINUX /* bad hack */
38 int pthread_mutexattr_setkind_np(pthread_mutexattr_t
*, int);
39 #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
40 #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
43 typedef struct _oslMutexImpl
45 pthread_mutex_t mutex
;
49 /*****************************************************************************/
51 /*****************************************************************************/
52 oslMutex SAL_CALL
osl_createMutex()
54 oslMutexImpl
* pMutex
= (oslMutexImpl
*) malloc(sizeof(oslMutexImpl
));
55 pthread_mutexattr_t aMutexAttr
;
65 pthread_mutexattr_init(&aMutexAttr
);
67 nRet
= pthread_mutexattr_settype(&aMutexAttr
, PTHREAD_MUTEX_RECURSIVE
);
69 nRet
= pthread_mutex_init(&(pMutex
->mutex
), &aMutexAttr
);
72 OSL_TRACE("osl_createMutex : mutex init/setattr failed. Errno: %d; %s\n",
73 nRet
, strerror(nRet
));
79 pthread_mutexattr_destroy(&aMutexAttr
);
81 return (oslMutex
) pMutex
;
84 /*****************************************************************************/
85 /* osl_destroyMutex */
86 /*****************************************************************************/
87 void SAL_CALL
osl_destroyMutex(oslMutex Mutex
)
89 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
97 nRet
= pthread_mutex_destroy(&(pMutex
->mutex
));
100 OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
101 nRet
, strerror(nRet
));
110 /*****************************************************************************/
111 /* osl_acquireMutex */
112 /*****************************************************************************/
113 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex Mutex
)
115 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
123 nRet
= pthread_mutex_lock(&(pMutex
->mutex
));
126 OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
127 nRet
, strerror(nRet
));
133 /* not initialized */
137 /*****************************************************************************/
138 /* osl_tryToAcquireMutex */
139 /*****************************************************************************/
140 sal_Bool SAL_CALL
osl_tryToAcquireMutex(oslMutex Mutex
)
142 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
149 nRet
= pthread_mutex_trylock(&(pMutex
->mutex
));
156 /* not initialized */
160 /*****************************************************************************/
161 /* osl_releaseMutex */
162 /*****************************************************************************/
163 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex Mutex
)
165 oslMutexImpl
* pMutex
= (oslMutexImpl
*) Mutex
;
172 nRet
= pthread_mutex_unlock(&(pMutex
->mutex
));
175 OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
176 nRet
, strerror(nRet
));
183 /* not initialized */
187 /*****************************************************************************/
188 /* osl_getGlobalMutex */
189 /*****************************************************************************/
191 static oslMutexImpl globalMutexImpl
;
193 static void globalMutexInitImpl(void) {
194 pthread_mutexattr_t attr
;
195 if (pthread_mutexattr_init(&attr
) != 0 ||
196 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
) ||
197 pthread_mutex_init(&globalMutexImpl
.mutex
, &attr
) != 0 ||
198 pthread_mutexattr_destroy(&attr
) != 0)
204 oslMutex
* SAL_CALL
osl_getGlobalMutex()
206 /* necessary to get a "oslMutex *" */
207 static oslMutex globalMutex
= (oslMutex
) &globalMutexImpl
;
209 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
210 if (pthread_once(&once
, &globalMutexInitImpl
) != 0) {
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */