1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "system.h" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <osl/mutex.h> 31*cdf0e10cSrcweir #include <osl/diagnose.h> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <pthread.h> 34*cdf0e10cSrcweir #include <stdlib.h> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #if defined LINUX /* bad hack */ 37*cdf0e10cSrcweir int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int); 38*cdf0e10cSrcweir #define pthread_mutexattr_settype pthread_mutexattr_setkind_np 39*cdf0e10cSrcweir #define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP 40*cdf0e10cSrcweir #endif 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir /* 43*cdf0e10cSrcweir Implementation notes: 44*cdf0e10cSrcweir oslMutex hides a pointer to the oslMutexImpl structure, which 45*cdf0e10cSrcweir ist needed to manage recursive locks on a mutex. 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir */ 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir typedef struct _oslMutexImpl 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir pthread_mutex_t mutex; 52*cdf0e10cSrcweir } oslMutexImpl; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir /*****************************************************************************/ 56*cdf0e10cSrcweir /* osl_createMutex */ 57*cdf0e10cSrcweir /*****************************************************************************/ 58*cdf0e10cSrcweir oslMutex SAL_CALL osl_createMutex() 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl)); 61*cdf0e10cSrcweir pthread_mutexattr_t aMutexAttr; 62*cdf0e10cSrcweir int nRet=0; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir OSL_ASSERT(pMutex); 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir if ( pMutex == 0 ) 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir return 0; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir pthread_mutexattr_init(&aMutexAttr); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE); 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr); 76*cdf0e10cSrcweir if ( nRet != 0 ) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir OSL_TRACE("osl_createMutex : mutex init failed. Errno: %d; %s\n", 79*cdf0e10cSrcweir nRet, strerror(nRet)); 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir free(pMutex); 82*cdf0e10cSrcweir pMutex = 0; 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir pthread_mutexattr_destroy(&aMutexAttr); 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir return (oslMutex) pMutex; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir /*****************************************************************************/ 91*cdf0e10cSrcweir /* osl_destroyMutex */ 92*cdf0e10cSrcweir /*****************************************************************************/ 93*cdf0e10cSrcweir void SAL_CALL osl_destroyMutex(oslMutex Mutex) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir oslMutexImpl* pMutex = (oslMutexImpl*) Mutex; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir OSL_ASSERT(pMutex); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir if ( pMutex != 0 ) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir int nRet=0; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir nRet = pthread_mutex_destroy(&(pMutex->mutex)); 104*cdf0e10cSrcweir if ( nRet != 0 ) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n", 107*cdf0e10cSrcweir nRet, strerror(nRet)); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir free(pMutex); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir return; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir /*****************************************************************************/ 117*cdf0e10cSrcweir /* osl_acquireMutex */ 118*cdf0e10cSrcweir /*****************************************************************************/ 119*cdf0e10cSrcweir sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir oslMutexImpl* pMutex = (oslMutexImpl*) Mutex; 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir OSL_ASSERT(pMutex); 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir if ( pMutex != 0 ) 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir int nRet=0; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir nRet = pthread_mutex_lock(&(pMutex->mutex)); 130*cdf0e10cSrcweir if ( nRet != 0 ) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n", 133*cdf0e10cSrcweir nRet, strerror(nRet)); 134*cdf0e10cSrcweir return sal_False; 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir return sal_True; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir /* not initialized */ 140*cdf0e10cSrcweir return sal_False; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir /*****************************************************************************/ 144*cdf0e10cSrcweir /* osl_tryToAcquireMutex */ 145*cdf0e10cSrcweir /*****************************************************************************/ 146*cdf0e10cSrcweir sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir oslMutexImpl* pMutex = (oslMutexImpl*) Mutex; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir OSL_ASSERT(pMutex); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir if ( pMutex ) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir int nRet = 0; 155*cdf0e10cSrcweir nRet = pthread_mutex_trylock(&(pMutex->mutex)); 156*cdf0e10cSrcweir if ( nRet != 0 ) 157*cdf0e10cSrcweir return sal_False; 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir return sal_True; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir /* not initialized */ 163*cdf0e10cSrcweir return sal_False; 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir /*****************************************************************************/ 167*cdf0e10cSrcweir /* osl_releaseMutex */ 168*cdf0e10cSrcweir /*****************************************************************************/ 169*cdf0e10cSrcweir sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir oslMutexImpl* pMutex = (oslMutexImpl*) Mutex; 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir OSL_ASSERT(pMutex); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir if ( pMutex ) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir int nRet=0; 178*cdf0e10cSrcweir nRet = pthread_mutex_unlock(&(pMutex->mutex)); 179*cdf0e10cSrcweir if ( nRet != 0 ) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n", 182*cdf0e10cSrcweir nRet, strerror(nRet)); 183*cdf0e10cSrcweir return sal_False; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir return sal_True; 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir /* not initialized */ 190*cdf0e10cSrcweir return sal_False; 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir /*****************************************************************************/ 194*cdf0e10cSrcweir /* osl_getGlobalMutex */ 195*cdf0e10cSrcweir /*****************************************************************************/ 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir static oslMutexImpl globalMutexImpl; 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir static void globalMutexInitImpl(void) { 200*cdf0e10cSrcweir pthread_mutexattr_t attr; 201*cdf0e10cSrcweir if (pthread_mutexattr_init(&attr) != 0 || 202*cdf0e10cSrcweir pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) || 203*cdf0e10cSrcweir pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 || 204*cdf0e10cSrcweir pthread_mutexattr_destroy(&attr) != 0) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir abort(); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir oslMutex * SAL_CALL osl_getGlobalMutex() 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir /* necessary to get a "oslMutex *" */ 213*cdf0e10cSrcweir static oslMutex globalMutex = (oslMutex) &globalMutexImpl; 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir static pthread_once_t once = PTHREAD_ONCE_INIT; 216*cdf0e10cSrcweir if (pthread_once(&once, &globalMutexInitImpl) != 0) { 217*cdf0e10cSrcweir abort(); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir return &globalMutex; 221*cdf0e10cSrcweir } 222