LCOV - code coverage report
Current view: top level - libreoffice/sal/osl/unx - mutex.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 49 59 83.1 %
Date: 2012-12-27 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "system.h"
      21             : 
      22             : #include <osl/mutex.h>
      23             : #include <osl/diagnose.h>
      24             : 
      25             : #include <pthread.h>
      26             : #include <stdlib.h>
      27             : 
      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
      33             : 
      34             : typedef struct _oslMutexImpl
      35             : {
      36             :     pthread_mutex_t mutex;
      37             : } oslMutexImpl;
      38             : 
      39             : 
      40             : /*****************************************************************************/
      41             : /* osl_createMutex */
      42             : /*****************************************************************************/
      43      278481 : oslMutex SAL_CALL osl_createMutex()
      44             : {
      45      278481 :     oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
      46             :     pthread_mutexattr_t aMutexAttr;
      47      278481 :     int nRet=0;
      48             : 
      49             :     OSL_ASSERT(pMutex);
      50             : 
      51      278481 :     if ( pMutex == 0 )
      52             :     {
      53           0 :         return 0;
      54             :     }
      55             : 
      56      278481 :     pthread_mutexattr_init(&aMutexAttr);
      57             : 
      58      278481 :     nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
      59      278481 :     if( nRet == 0 )
      60      278481 :         nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
      61      278481 :     if ( nRet != 0 )
      62             :     {
      63             :         OSL_TRACE("osl_createMutex : mutex init/setattr failed. Errno: %d; %s\n",
      64             :                   nRet, strerror(nRet));
      65             : 
      66           0 :         free(pMutex);
      67           0 :         pMutex = 0;
      68             :     }
      69             : 
      70      278481 :     pthread_mutexattr_destroy(&aMutexAttr);
      71             : 
      72      278481 :     return (oslMutex) pMutex;
      73             : }
      74             : 
      75             : /*****************************************************************************/
      76             : /* osl_destroyMutex */
      77             : /*****************************************************************************/
      78      230686 : void SAL_CALL osl_destroyMutex(oslMutex Mutex)
      79             : {
      80      230686 :     oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
      81             : 
      82             :     OSL_ASSERT(pMutex);
      83             : 
      84      230686 :     if ( pMutex != 0 )
      85             :     {
      86      230686 :         int nRet=0;
      87             : 
      88      230686 :         nRet = pthread_mutex_destroy(&(pMutex->mutex));
      89             :         if ( nRet != 0 )
      90             :         {
      91             :             OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
      92             :                       nRet, strerror(nRet));
      93             :         }
      94             : 
      95      230686 :         free(pMutex);
      96             :     }
      97             : 
      98      230686 :     return;
      99             : }
     100             : 
     101             : /*****************************************************************************/
     102             : /* osl_acquireMutex */
     103             : /*****************************************************************************/
     104    17158287 : sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
     105             : {
     106    17158287 :     oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
     107             : 
     108             :     OSL_ASSERT(pMutex);
     109             : 
     110    17158287 :     if ( pMutex != 0 )
     111             :     {
     112    17158287 :         int nRet=0;
     113             : 
     114    17158287 :         nRet = pthread_mutex_lock(&(pMutex->mutex));
     115    17158287 :         if ( nRet != 0 )
     116             :         {
     117             :             OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
     118             :                       nRet, strerror(nRet));
     119           0 :             return sal_False;
     120             :         }
     121    17158287 :         return sal_True;
     122             :     }
     123             : 
     124             :     /* not initialized */
     125           0 :     return sal_False;
     126             : }
     127             : 
     128             : /*****************************************************************************/
     129             : /* osl_tryToAcquireMutex */
     130             : /*****************************************************************************/
     131           9 : sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
     132             : {
     133           9 :     oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
     134             : 
     135             :     OSL_ASSERT(pMutex);
     136             : 
     137           9 :     if ( pMutex )
     138             :     {
     139           9 :         int nRet = 0;
     140           9 :         nRet = pthread_mutex_trylock(&(pMutex->mutex));
     141           9 :         if ( nRet != 0  )
     142           0 :             return sal_False;
     143             : 
     144           9 :         return sal_True;
     145             :     }
     146             : 
     147             :     /* not initialized */
     148           0 :     return sal_False;
     149             : }
     150             : 
     151             : /*****************************************************************************/
     152             : /* osl_releaseMutex */
     153             : /*****************************************************************************/
     154    17158269 : sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
     155             : {
     156    17158269 :     oslMutexImpl* pMutex = (oslMutexImpl*) Mutex;
     157             : 
     158             :     OSL_ASSERT(pMutex);
     159             : 
     160    17158269 :     if ( pMutex )
     161             :     {
     162    17158269 :         int nRet=0;
     163    17158269 :         nRet = pthread_mutex_unlock(&(pMutex->mutex));
     164    17158270 :         if ( nRet != 0 )
     165             :         {
     166             :             OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
     167             :                       nRet, strerror(nRet));
     168           5 :             return sal_False;
     169             :         }
     170             : 
     171    17158265 :         return sal_True;
     172             :     }
     173             : 
     174             :     /* not initialized */
     175           0 :     return sal_False;
     176             : }
     177             : 
     178             : /*****************************************************************************/
     179             : /* osl_getGlobalMutex */
     180             : /*****************************************************************************/
     181             : 
     182             : static oslMutexImpl globalMutexImpl;
     183             : 
     184         310 : static void globalMutexInitImpl(void) {
     185             :     pthread_mutexattr_t attr;
     186         620 :     if (pthread_mutexattr_init(&attr) != 0 ||
     187         620 :         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
     188         620 :         pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
     189         310 :         pthread_mutexattr_destroy(&attr) != 0)
     190             :     {
     191           0 :         abort();
     192             :     }
     193         310 : }
     194             : 
     195      110152 : oslMutex * SAL_CALL osl_getGlobalMutex()
     196             : {
     197             :     /* necessary to get a "oslMutex *" */
     198             :     static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
     199             : 
     200             :     static pthread_once_t once = PTHREAD_ONCE_INIT;
     201      110152 :     if (pthread_once(&once, &globalMutexInitImpl) != 0) {
     202           0 :         abort();
     203             :     }
     204             : 
     205      110152 :     return &globalMutex;
     206             : }
     207             : 
     208             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10