LCOV - code coverage report
Current view: top level - include/osl - mutex.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 50 50 100.0 %
Date: 2015-06-13 12:38:46 Functions: 22 24 91.7 %
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             : #ifndef INCLUDED_OSL_MUTEX_HXX
      21             : #define INCLUDED_OSL_MUTEX_HXX
      22             : 
      23             : #include <osl/mutex.h>
      24             : 
      25             : 
      26             : namespace osl
      27             : {
      28             :     /** A mutual exclusion synchronization object
      29             :     */
      30             :     class SAL_WARN_UNUSED Mutex {
      31             : 
      32             :     public:
      33             :         /** Create a mutex.
      34             :             @return 0 if the mutex could not be created, otherwise a handle to the mutex.
      35             :             @see ::osl_createMutex()
      36             :         */
      37     5655274 :         Mutex()
      38             :         {
      39     5655274 :             mutex = osl_createMutex();
      40     5655274 :         }
      41             : 
      42             :         /** Release the OS-structures and free mutex data-structure.
      43             :             @see ::osl_destroyMutex()
      44             :         */
      45     5502940 :         ~Mutex()
      46             :         {
      47     5502940 :             osl_destroyMutex(mutex);
      48     5502941 :         }
      49             : 
      50             :         /** Acquire the mutex, block if already acquired by another thread.
      51             :             @return false if system-call fails.
      52             :             @see ::osl_acquireMutex()
      53             :         */
      54 13813214145 :         bool acquire()
      55             :         {
      56 13813214145 :             return osl_acquireMutex(mutex);
      57             :         }
      58             : 
      59             :         /** Try to acquire the mutex without blocking.
      60             :             @return false if it could not be acquired.
      61             :             @see ::osl_tryToAcquireMutex()
      62             :         */
      63          49 :         bool tryToAcquire()
      64             :         {
      65          49 :             return osl_tryToAcquireMutex(mutex);
      66             :         }
      67             : 
      68             :         /** Release the mutex.
      69             :             @return false if system-call fails.
      70             :             @see ::osl_releaseMutex()
      71             :         */
      72 13813214534 :         bool release()
      73             :         {
      74 13813214534 :             return osl_releaseMutex(mutex);
      75             :         }
      76             : 
      77             :         /** Returns a global static mutex object.
      78             :             The global and static mutex object can be used to initialize other
      79             :             static objects in a thread safe manner.
      80             :             @return the global mutex object
      81             :             @see ::osl_getGlobalMutex()
      82             :         */
      83   223503587 :         static Mutex * getGlobalMutex()
      84             :         {
      85   223503587 :             return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
      86             :         }
      87             : 
      88             :     private:
      89             :         oslMutex mutex;
      90             : 
      91             :         /** The underlying oslMutex has no reference count.
      92             : 
      93             :         Since the underlying oslMutex is not a reference counted object, copy
      94             :         constructed Mutex may work on an already destructed oslMutex object.
      95             : 
      96             :         */
      97             :         Mutex(const Mutex&) SAL_DELETED_FUNCTION;
      98             : 
      99             :         /** This assignment operator is deleted for the same reason as
     100             :             the copy constructor.
     101             :         */
     102             :         Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
     103             :     };
     104             : 
     105             :     /** A helper class for mutex objects and interfaces.
     106             :     */
     107             :     template<class T>
     108             :     class Guard
     109             :     {
     110             :     private:
     111             :         Guard( const Guard& ) SAL_DELETED_FUNCTION;
     112             :         const Guard& operator = ( const Guard& ) SAL_DELETED_FUNCTION;
     113             : 
     114             :     protected:
     115             :         T * pT;
     116             :     public:
     117             : 
     118             :         /** Acquires the object specified as parameter.
     119             :         */
     120   413301866 :         Guard(T * pT_) : pT(pT_)
     121             :         {
     122   413301866 :             pT->acquire();
     123   413301867 :         }
     124             : 
     125             :         /** Acquires the object specified as parameter.
     126             :         */
     127 12955238234 :         Guard(T & t) : pT(&t)
     128             :         {
     129 12955238234 :             pT->acquire();
     130 12955237907 :         }
     131             : 
     132             :         /** Releases the mutex or interface. */
     133 13368539726 :         ~Guard()
     134             :         {
     135 13368539726 :             pT->release();
     136 13368540430 :         }
     137             :     };
     138             : 
     139             :     /** A helper class for mutex objects and interfaces.
     140             :     */
     141             :     template<class T>
     142             :     class ClearableGuard
     143             :     {
     144             :     private:
     145             :         ClearableGuard( const ClearableGuard& ) SAL_DELETED_FUNCTION;
     146             :         const ClearableGuard& operator = ( const ClearableGuard& )
     147             :             SAL_DELETED_FUNCTION;
     148             :     protected:
     149             :         T * pT;
     150             :     public:
     151             : 
     152             :         /** Acquires the object specified as parameter.
     153             :         */
     154       29308 :         ClearableGuard(T * pT_) : pT(pT_)
     155             :         {
     156       29308 :             pT->acquire();
     157       29308 :         }
     158             : 
     159             :         /** Acquires the object specified as parameter.
     160             :         */
     161    57292755 :         ClearableGuard(T & t) : pT(&t)
     162             :         {
     163    57292755 :             pT->acquire();
     164    57292754 :         }
     165             : 
     166             :         /** Releases the mutex or interface if not already released by clear().
     167             :         */
     168    57322059 :         ~ClearableGuard()
     169             :         {
     170    57322059 :             if (pT)
     171    33655115 :                 pT->release();
     172    57322059 :         }
     173             : 
     174             :         /** Releases the mutex or interface.
     175             :         */
     176    23779460 :         void clear()
     177             :         {
     178    23779460 :             if(pT)
     179             :             {
     180    23778889 :                 pT->release();
     181    23778888 :                 pT = NULL;
     182             :             }
     183    23779459 :         }
     184             :     };
     185             : 
     186             :     /** A helper class for mutex objects and interfaces.
     187             :     */
     188             :     template< class T >
     189    38556829 :     class ResettableGuard : public ClearableGuard< T >
     190             :     {
     191             :     private:
     192             :         ResettableGuard(ResettableGuard &) SAL_DELETED_FUNCTION;
     193             :         void operator =(ResettableGuard &) SAL_DELETED_FUNCTION;
     194             : 
     195             :     protected:
     196             :         T* pResetT;
     197             :     public:
     198             :         /** Acquires the object specified as parameter.
     199             :         */
     200       28802 :         ResettableGuard( T* pT_ ) :
     201             :                 ClearableGuard<T>( pT_ ),
     202       28802 :                 pResetT( pT_ )
     203       28802 :         {}
     204             : 
     205             :         /** Acquires the object specified as parameter.
     206             :         */
     207    38528029 :         ResettableGuard( T& rT ) :
     208             :                 ClearableGuard<T>( rT ),
     209    38528029 :                 pResetT( &rT )
     210    38528029 :         {}
     211             : 
     212             :         /** Re-acquires the mutex or interface.
     213             :         */
     214      111928 :         void reset()
     215             :         {
     216      111928 :             if( pResetT )
     217             :             {
     218      111929 :                 this->pT = pResetT;
     219      111929 :                 this->pT->acquire();
     220             :             }
     221      111945 :         }
     222             :     };
     223             : 
     224             :     typedef Guard<Mutex> MutexGuard;
     225             :     typedef ClearableGuard<Mutex> ClearableMutexGuard;
     226             :     typedef ResettableGuard< Mutex > ResettableMutexGuard;
     227             : }
     228             : 
     229             : #endif // INCLUDED_OSL_MUTEX_HXX
     230             : 
     231             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11