Bump for 3.6-28
[LibreOffice.git] / sal / osl / unx / mutex.c
blobc784113d6393b0e494e0b718a7f003c4940661d9
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 ************************************************************************/
29 #include "system.h"
31 #include <osl/mutex.h>
32 #include <osl/diagnose.h>
34 #include <pthread.h>
35 #include <stdlib.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
41 #endif
43 typedef struct _oslMutexImpl
45 pthread_mutex_t mutex;
46 } oslMutexImpl;
49 /*****************************************************************************/
50 /* osl_createMutex */
51 /*****************************************************************************/
52 oslMutex SAL_CALL osl_createMutex()
54 oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
55 pthread_mutexattr_t aMutexAttr;
56 int nRet=0;
58 OSL_ASSERT(pMutex);
60 if ( pMutex == 0 )
62 return 0;
65 pthread_mutexattr_init(&aMutexAttr);
67 nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
68 if( nRet == 0 )
69 nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
70 if ( nRet != 0 )
72 OSL_TRACE("osl_createMutex : mutex init/setattr failed. Errno: %d; %s\n",
73 nRet, strerror(nRet));
75 free(pMutex);
76 pMutex = 0;
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;
91 OSL_ASSERT(pMutex);
93 if ( pMutex != 0 )
95 int nRet=0;
97 nRet = pthread_mutex_destroy(&(pMutex->mutex));
98 if ( nRet != 0 )
100 OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
101 nRet, strerror(nRet));
104 free(pMutex);
107 return;
110 /*****************************************************************************/
111 /* osl_acquireMutex */
112 /*****************************************************************************/
113 sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
115 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
117 OSL_ASSERT(pMutex);
119 if ( pMutex != 0 )
121 int nRet=0;
123 nRet = pthread_mutex_lock(&(pMutex->mutex));
124 if ( nRet != 0 )
126 OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
127 nRet, strerror(nRet));
128 return sal_False;
130 return sal_True;
133 /* not initialized */
134 return sal_False;
137 /*****************************************************************************/
138 /* osl_tryToAcquireMutex */
139 /*****************************************************************************/
140 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
142 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
144 OSL_ASSERT(pMutex);
146 if ( pMutex )
148 int nRet = 0;
149 nRet = pthread_mutex_trylock(&(pMutex->mutex));
150 if ( nRet != 0 )
151 return sal_False;
153 return sal_True;
156 /* not initialized */
157 return sal_False;
160 /*****************************************************************************/
161 /* osl_releaseMutex */
162 /*****************************************************************************/
163 sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
165 oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
167 OSL_ASSERT(pMutex);
169 if ( pMutex )
171 int nRet=0;
172 nRet = pthread_mutex_unlock(&(pMutex->mutex));
173 if ( nRet != 0 )
175 OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
176 nRet, strerror(nRet));
177 return sal_False;
180 return sal_True;
183 /* not initialized */
184 return sal_False;
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)
200 abort();
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) {
211 abort();
214 return &globalMutex;
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */