LCOV - code coverage report
Current view: top level - sal/osl/unx - mutex.c (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 45 55 81.8 %
Date: 2014-04-11 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             : /* osl_createMutex */
      41             : /*****************************************************************************/
      42     3391057 : oslMutex SAL_CALL osl_createMutex()
      43             : {
      44     3391057 :     oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
      45             :     pthread_mutexattr_t aMutexAttr;
      46     3391057 :     int nRet=0;
      47             : 
      48             :     OSL_ASSERT(pMutex);
      49             : 
      50     3391057 :     if ( pMutex == 0 )
      51             :     {
      52           0 :         return 0;
      53             :     }
      54             : 
      55     3391057 :     pthread_mutexattr_init(&aMutexAttr);
      56             : 
      57     3391057 :     nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
      58     3391057 :     if( nRet == 0 )
      59     3391057 :         nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
      60     3391057 :     if ( nRet != 0 )
      61             :     {
      62             :         OSL_TRACE("osl_createMutex : mutex init/setattr failed. Errno: %d; %s\n",
      63             :                   nRet, strerror(nRet));
      64             : 
      65           0 :         free(pMutex);
      66           0 :         pMutex = 0;
      67             :     }
      68             : 
      69     3391057 :     pthread_mutexattr_destroy(&aMutexAttr);
      70             : 
      71     3391056 :     return pMutex;
      72             : }
      73             : 
      74     3223345 : void SAL_CALL osl_destroyMutex(oslMutexImpl *pMutex)
      75             : {
      76             :     OSL_ASSERT(pMutex);
      77             : 
      78     3223345 :     if ( pMutex != 0 )
      79             :     {
      80     3223345 :         int nRet=0;
      81             : 
      82     3223345 :         nRet = pthread_mutex_destroy(&(pMutex->mutex));
      83             :         if ( nRet != 0 )
      84             :         {
      85             :             OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
      86             :                       nRet, strerror(nRet));
      87             :         }
      88             : 
      89     3223356 :         free(pMutex);
      90             :     }
      91             : 
      92     3223356 :     return;
      93             : }
      94             : 
      95   163439915 : sal_Bool SAL_CALL osl_acquireMutex(oslMutexImpl *pMutex)
      96             : {
      97             :     OSL_ASSERT(pMutex);
      98             : 
      99   163439915 :     if ( pMutex != 0 )
     100             :     {
     101   163439915 :         int nRet=0;
     102             : 
     103   163439915 :         nRet = pthread_mutex_lock(&(pMutex->mutex));
     104   163439905 :         if ( nRet != 0 )
     105             :         {
     106             :             OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
     107             :                       nRet, strerror(nRet));
     108           0 :             return sal_False;
     109             :         }
     110   163439905 :         return sal_True;
     111             :     }
     112             : 
     113             :     /* not initialized */
     114           0 :     return sal_False;
     115             : }
     116             : 
     117          30 : sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutexImpl *pMutex)
     118             : {
     119             :     OSL_ASSERT(pMutex);
     120             : 
     121          30 :     if ( pMutex )
     122             :     {
     123          30 :         int nRet = 0;
     124          30 :         nRet = pthread_mutex_trylock(&(pMutex->mutex));
     125          30 :         if ( nRet != 0  )
     126           0 :             return sal_False;
     127             : 
     128          30 :         return sal_True;
     129             :     }
     130             : 
     131             :     /* not initialized */
     132           0 :     return sal_False;
     133             : }
     134             : 
     135   163440076 : sal_Bool SAL_CALL osl_releaseMutex(oslMutexImpl *pMutex)
     136             : {
     137             :     OSL_ASSERT(pMutex);
     138             : 
     139   163440076 :     if ( pMutex )
     140             :     {
     141   163440076 :         int nRet=0;
     142   163440076 :         nRet = pthread_mutex_unlock(&(pMutex->mutex));
     143   163440201 :         if ( nRet != 0 )
     144             :         {
     145             :             OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
     146             :                       nRet, strerror(nRet));
     147           5 :             return sal_False;
     148             :         }
     149             : 
     150   163440196 :         return sal_True;
     151             :     }
     152             : 
     153             :     /* not initialized */
     154           0 :     return sal_False;
     155             : }
     156             : 
     157             : static oslMutexImpl globalMutexImpl;
     158             : 
     159         905 : static void globalMutexInitImpl(void) {
     160             :     pthread_mutexattr_t attr;
     161        1810 :     if (pthread_mutexattr_init(&attr) != 0 ||
     162        1810 :         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
     163        1810 :         pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
     164         905 :         pthread_mutexattr_destroy(&attr) != 0)
     165             :     {
     166           0 :         abort();
     167             :     }
     168         905 : }
     169             : 
     170     1434647 : oslMutex * SAL_CALL osl_getGlobalMutex()
     171             : {
     172             :     /* necessary to get a "oslMutex *" */
     173             :     static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
     174             : 
     175             :     static pthread_once_t once = PTHREAD_ONCE_INIT;
     176     1434647 :     if (pthread_once(&once, &globalMutexInitImpl) != 0) {
     177           0 :         abort();
     178             :     }
     179             : 
     180     1434648 :     return &globalMutex;
     181             : }
     182             : 
     183             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10