LCOV - code coverage report
Current view: top level - sal/osl/unx - mutex.c (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 45 55 81.8 %
Date: 2014-11-03 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             : #if defined LINUX
      21             : // to define __USE_UNIX98, via _XOPEN_SOURCE, enabling pthread_mutexattr_settype
      22             : #ifndef _GNU_SOURCE
      23             : #define _GNU_SOURCE 1
      24             : #endif
      25             : #endif
      26             : #include "system.h"
      27             : 
      28             : #include <osl/mutex.h>
      29             : #include <osl/diagnose.h>
      30             : 
      31             : #include <pthread.h>
      32             : #include <stdlib.h>
      33             : 
      34             : 
      35             : typedef struct _oslMutexImpl
      36             : {
      37             :     pthread_mutex_t mutex;
      38             : } oslMutexImpl;
      39             : 
      40             : /*****************************************************************************/
      41             : /* osl_createMutex */
      42             : /*****************************************************************************/
      43     8092056 : oslMutex SAL_CALL osl_createMutex()
      44             : {
      45     8092056 :     oslMutexImpl* pMutex = (oslMutexImpl*) malloc(sizeof(oslMutexImpl));
      46             :     pthread_mutexattr_t aMutexAttr;
      47     8092056 :     int nRet=0;
      48             : 
      49             :     OSL_ASSERT(pMutex);
      50             : 
      51     8092056 :     if ( pMutex == 0 )
      52             :     {
      53           0 :         return 0;
      54             :     }
      55             : 
      56     8092056 :     pthread_mutexattr_init(&aMutexAttr);
      57             : 
      58     8092056 :     nRet = pthread_mutexattr_settype(&aMutexAttr, PTHREAD_MUTEX_RECURSIVE);
      59     8092056 :     if( nRet == 0 )
      60     8092056 :         nRet = pthread_mutex_init(&(pMutex->mutex), &aMutexAttr);
      61     8092056 :     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     8092056 :     pthread_mutexattr_destroy(&aMutexAttr);
      71             : 
      72     8092054 :     return pMutex;
      73             : }
      74             : 
      75     7864981 : void SAL_CALL osl_destroyMutex(oslMutexImpl *pMutex)
      76             : {
      77             :     OSL_ASSERT(pMutex);
      78             : 
      79     7864981 :     if ( pMutex != 0 )
      80             :     {
      81     7864981 :         int nRet=0;
      82             : 
      83     7864981 :         nRet = pthread_mutex_destroy(&(pMutex->mutex));
      84             :         if ( nRet != 0 )
      85             :         {
      86             :             OSL_TRACE("osl_destroyMutex : mutex destroy failed. Errno: %d; %s\n",
      87             :                       nRet, strerror(nRet));
      88             :         }
      89             : 
      90     7864982 :         free(pMutex);
      91             :     }
      92             : 
      93     7864982 :     return;
      94             : }
      95             : 
      96   387603386 : sal_Bool SAL_CALL osl_acquireMutex(oslMutexImpl *pMutex)
      97             : {
      98             :     OSL_ASSERT(pMutex);
      99             : 
     100   387603386 :     if ( pMutex != 0 )
     101             :     {
     102   387603386 :         int nRet=0;
     103             : 
     104   387603386 :         nRet = pthread_mutex_lock(&(pMutex->mutex));
     105   387622312 :         if ( nRet != 0 )
     106             :         {
     107             :             OSL_TRACE("osl_acquireMutex : mutex lock failed. Errno: %d; %s\n",
     108             :                       nRet, strerror(nRet));
     109           0 :             return sal_False;
     110             :         }
     111   387622312 :         return sal_True;
     112             :     }
     113             : 
     114             :     /* not initialized */
     115           0 :     return sal_False;
     116             : }
     117             : 
     118          76 : sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutexImpl *pMutex)
     119             : {
     120             :     OSL_ASSERT(pMutex);
     121             : 
     122          76 :     if ( pMutex )
     123             :     {
     124          76 :         int nRet = 0;
     125          76 :         nRet = pthread_mutex_trylock(&(pMutex->mutex));
     126          76 :         if ( nRet != 0  )
     127           0 :             return sal_False;
     128             : 
     129          76 :         return sal_True;
     130             :     }
     131             : 
     132             :     /* not initialized */
     133           0 :     return sal_False;
     134             : }
     135             : 
     136   387608306 : sal_Bool SAL_CALL osl_releaseMutex(oslMutexImpl *pMutex)
     137             : {
     138             :     OSL_ASSERT(pMutex);
     139             : 
     140   387608306 :     if ( pMutex )
     141             :     {
     142   387608306 :         int nRet=0;
     143   387608306 :         nRet = pthread_mutex_unlock(&(pMutex->mutex));
     144   387623279 :         if ( nRet != 0 )
     145             :         {
     146             :             OSL_TRACE("osl_releaseMutex : mutex unlock failed. Errno: %d; %s\n",
     147             :                       nRet, strerror(nRet));
     148          10 :             return sal_False;
     149             :         }
     150             : 
     151   387623269 :         return sal_True;
     152             :     }
     153             : 
     154             :     /* not initialized */
     155           0 :     return sal_False;
     156             : }
     157             : 
     158             : static oslMutexImpl globalMutexImpl;
     159             : 
     160        1632 : static void globalMutexInitImpl(void) {
     161             :     pthread_mutexattr_t attr;
     162        3264 :     if (pthread_mutexattr_init(&attr) != 0 ||
     163        3264 :         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) ||
     164        3264 :         pthread_mutex_init(&globalMutexImpl.mutex, &attr) != 0 ||
     165        1632 :         pthread_mutexattr_destroy(&attr) != 0)
     166             :     {
     167           0 :         abort();
     168             :     }
     169        1632 : }
     170             : 
     171     3613403 : oslMutex * SAL_CALL osl_getGlobalMutex()
     172             : {
     173             :     /* necessary to get a "oslMutex *" */
     174             :     static oslMutex globalMutex = (oslMutex) &globalMutexImpl;
     175             : 
     176             :     static pthread_once_t once = PTHREAD_ONCE_INIT;
     177     3613403 :     if (pthread_once(&once, &globalMutexInitImpl) != 0) {
     178           0 :         abort();
     179             :     }
     180             : 
     181     3613404 :     return &globalMutex;
     182             : }
     183             : 
     184             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10