Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / unx / mutex.c
bloba2a6df7abcc09eb6a818c90ebc1bb6bdc8a32ee4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "system.h"
22 #include <osl/mutex.h>
23 #include <osl/diagnose.h>
25 #include <pthread.h>
26 #include <stdlib.h>
28 #if defined LINUX /* bad hack */
29 int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int);
30 #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
31 #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
32 #endif
34 typedef struct _oslMutexImpl
36 pthread_mutex_t mutex;
37 } oslMutexImpl;
40 /*****************************************************************************/
41 /* osl_createMutex */
42 /*****************************************************************************/
43 oslMutex SAL_CALL osl_createMutex()
45 oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
46 pthread_mutexattr_t aMutexAttr;
47 int nRet=0;
49 OSL_ASSERT(pMutex);
51 if ( pMutex == 0 )
53 return 0;
56 pthread_mutexattr_init(&aMutexAttr);
58 nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
59 if( nRet == 0 )
60 nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
61 if ( nRet != 0 )
63 OSL_TRACE("osl_createMutex : mutex init/setattr failed. Errno: %d; %s\n",
64 nRet, strerror(nRet));
66 free(pMutex);
67 pMutex = 0;
70 pthread_mutexattr_destroy(&aMutexAttr);
72 return pMutex;
75 void SAL_CALL osl_destroyMutex(oslMutexImpl *pMutex)
77 OSL_ASSERT(pMutex);
79 if ( pMutex != 0 )
81 int nRet=0;
83 nRet = pthread_mutex_destroy(&(pMutex->mutex));
84 if ( nRet != 0 )
86 OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
87 nRet, strerror(nRet));
90 free(pMutex);
93 return;
96 sal_Bool SAL_CALL osl_acquireMutex(oslMutexImpl *pMutex)
98 OSL_ASSERT(pMutex);
100 if ( pMutex != 0 )
102 int nRet=0;
104 nRet = pthread_mutex_lock(&(pMutex->mutex));
105 if ( nRet != 0 )
107 OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
108 nRet, strerror(nRet));
109 return sal_False;
111 return sal_True;
114 /* not initialized */
115 return sal_False;
118 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutexImpl *pMutex)
120 OSL_ASSERT(pMutex);
122 if ( pMutex )
124 int nRet = 0;
125 nRet = pthread_mutex_trylock(&(pMutex->mutex));
126 if ( nRet != 0 )
127 return sal_False;
129 return sal_True;
132 /* not initialized */
133 return sal_False;
136 sal_Bool SAL_CALL osl_releaseMutex(oslMutexImpl *pMutex)
138 OSL_ASSERT(pMutex);
140 if ( pMutex )
142 int nRet=0;
143 nRet = pthread_mutex_unlock(&(pMutex->mutex));
144 if ( nRet != 0 )
146 OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
147 nRet, strerror(nRet));
148 return sal_False;
151 return sal_True;
154 /* not initialized */
155 return sal_False;
159 static oslMutexImpl globalMutexImpl;
161 static void globalMutexInitImpl(void) {
162 pthread_mutexattr_t attr;
163 if (pthread_mutexattr_init(&attr) != 0 ||
164 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
165 pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
166 pthread_mutexattr_destroy(&attr) != 0)
168 abort();
172 oslMutex * SAL_CALL osl_getGlobalMutex()
174 /* necessary to get a "oslMutex *" */
175 static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
177 static pthread_once_t once = PTHREAD_ONCE_INIT;
178 if (pthread_once(&once, &globalMutexInitImpl) != 0) {
179 abort();
182 return &globalMutex;
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */