LCOV - code coverage report
Current view: top level - libreoffice/solver/unxlngi6.pro/inc/osl - mutex.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 52 52 100.0 %
Date: 2012-12-17 Functions: 22 27 81.5 %
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 _OSL_MUTEX_HXX_
      21             : #define _OSL_MUTEX_HXX_
      22             : 
      23             : #ifdef __cplusplus
      24             : 
      25             : #include <osl/mutex.h>
      26             : 
      27             : 
      28             : namespace osl
      29             : {
      30             :     /** A mutual exclusion synchronization object
      31             :     */
      32             :     class SAL_WARN_UNUSED Mutex {
      33             : 
      34             :     public:
      35             :         /** Create a thread-local mutex.
      36             :             @return 0 if the mutex could not be created, otherwise a handle to the mutex.
      37             :             @seealso ::osl_createMutex()
      38             :         */
      39      617721 :         Mutex()
      40             :         {
      41      617721 :             mutex = osl_createMutex();
      42      617721 :         }
      43             : 
      44             :         /** Release the OS-structures and free mutex data-structure.
      45             :             @seealso ::osl_destroyMutex()
      46             :         */
      47      499151 :         ~Mutex()
      48             :         {
      49      499151 :             osl_destroyMutex(mutex);
      50      499151 :         }
      51             : 
      52             :         /** Acquire the mutex, block if already acquired by another thread.
      53             :             @return sal_False if system-call fails.
      54             :             @seealso ::osl_acquireMutex()
      55             :         */
      56    24770264 :         sal_Bool acquire()
      57             :         {
      58    24770264 :             return osl_acquireMutex(mutex);
      59             :         }
      60             : 
      61             :         /** Try to acquire the mutex without blocking.
      62             :             @return sal_False if it could not be acquired.
      63             :             @seealso ::osl_tryToAcquireMutex()
      64             :         */
      65          38 :         sal_Bool tryToAcquire()
      66             :         {
      67          38 :             return osl_tryToAcquireMutex(mutex);
      68             :         }
      69             : 
      70             :         /** Release the mutex.
      71             :             @return sal_False if system-call fails.
      72             :             @seealso ::osl_releaseMutex()
      73             :         */
      74    24770314 :         sal_Bool release()
      75             :         {
      76    24770314 :             return osl_releaseMutex(mutex);
      77             :         }
      78             : 
      79             :         /** Returns a global static mutex object.
      80             :             The global and static mutex object can be used to initialize other
      81             :             static objects in a thread safe manner.
      82             :             @return the global mutex object
      83             :             @seealso ::osl_getGlobalMutex()
      84             :         */
      85      303236 :         static Mutex * getGlobalMutex()
      86             :         {
      87      303236 :             return (Mutex *)osl_getGlobalMutex();
      88             :         }
      89             : 
      90             :     private:
      91             :         oslMutex mutex;
      92             : 
      93             :         /** The underlying oslMutex has no reference count.
      94             : 
      95             :         Since the underlying oslMutex is not a reference counted object, copy
      96             :         constructed Mutex may work on an already destructed oslMutex object.
      97             : 
      98             :         */
      99             :         Mutex(const Mutex&);
     100             : 
     101             :         /** The underlying oslMutex has no reference count.
     102             : 
     103             :         When destructed, the Mutex object destroys the undelying oslMutex,
     104             :         which might cause severe problems in case it's a temporary object.
     105             : 
     106             :         */
     107             :         Mutex(oslMutex Mutex);
     108             : 
     109             :         /** This assignment operator is private for the same reason as
     110             :             the copy constructor.
     111             :         */
     112             :         Mutex& operator= (const Mutex&);
     113             : 
     114             :         /** This assignment operator is private for the same reason as
     115             :             the constructor taking a oslMutex argument.
     116             :         */
     117             :         Mutex& operator= (oslMutex);
     118             :     };
     119             : 
     120             :     /** A helper class for mutex objects and interfaces.
     121             :     */
     122             :     template<class T>
     123             :     class Guard
     124             :     {
     125             :     private:
     126             :         Guard( const Guard& );
     127             :         const Guard& operator = ( const Guard& );
     128             : 
     129             :     protected:
     130             :         T * pT;
     131             :     public:
     132             : 
     133             :         /** Acquires the object specified as parameter.
     134             :         */
     135     1219764 :         Guard(T * pT_) : pT(pT_)
     136             :         {
     137     1219764 :             pT->acquire();
     138     1219764 :         }
     139             : 
     140             :         /** Acquires the object specified as parameter.
     141             :         */
     142    19357375 :         Guard(T & t) : pT(&t)
     143             :         {
     144    19357375 :             pT->acquire();
     145    19357375 :         }
     146             : 
     147             :         /** Releases the mutex or interface. */
     148    20577139 :         ~Guard()
     149             :         {
     150    20577139 :             pT->release();
     151    20577139 :         }
     152             :     };
     153             : 
     154             :     /** A helper class for mutex objects and interfaces.
     155             :     */
     156             :     template<class T>
     157             :     class ClearableGuard
     158             :     {
     159             :     private:
     160             :         ClearableGuard( const ClearableGuard& );
     161             :         const ClearableGuard& operator = ( const ClearableGuard& );
     162             :     protected:
     163             :         T * pT;
     164             :     public:
     165             : 
     166             :         /** Acquires the object specified as parameter.
     167             :         */
     168        3432 :         ClearableGuard(T * pT_) : pT(pT_)
     169             :         {
     170        3432 :             pT->acquire();
     171        3432 :         }
     172             : 
     173             :         /** Acquires the object specified as parameter.
     174             :         */
     175     2324998 :         ClearableGuard(T & t) : pT(&t)
     176             :         {
     177     2324998 :             pT->acquire();
     178     2324998 :         }
     179             : 
     180             :         /** Releases the mutex or interface if not already released by clear().
     181             :         */
     182     2328430 :         ~ClearableGuard()
     183             :         {
     184     2328430 :             if (pT)
     185      561771 :                 pT->release();
     186     2328430 :         }
     187             : 
     188             :         /** Releases the mutex or interface.
     189             :         */
     190     1776286 :         void clear()
     191             :         {
     192     1776286 :             if(pT)
     193             :             {
     194     1776220 :                 pT->release();
     195     1776220 :                 pT = NULL;
     196             :             }
     197     1776286 :         }
     198             :     };
     199             : 
     200             :     /** A helper class for mutex objects and interfaces.
     201             :     */
     202             :     template< class T >
     203     1345247 :     class ResettableGuard : public ClearableGuard< T >
     204             :     {
     205             :     private:
     206             :         ResettableGuard(ResettableGuard &); // not defined
     207             :         void operator =(ResettableGuard &); // not defined
     208             : 
     209             :     protected:
     210             :         T* pResetT;
     211             :     public:
     212             :         /** Acquires the object specified as parameter.
     213             :         */
     214        3124 :         ResettableGuard( T* pT_ ) :
     215             :                 ClearableGuard<T>( pT_ ),
     216        3124 :                 pResetT( pT_ )
     217        3124 :         {}
     218             : 
     219             :         /** Acquires the object specified as parameter.
     220             :         */
     221     1342123 :         ResettableGuard( T& rT ) :
     222             :                 ClearableGuard<T>( rT ),
     223     1342123 :                 pResetT( &rT )
     224     1342123 :         {}
     225             : 
     226             :         /** Re-aquires the mutex or interface.
     227             :         */
     228        9561 :         void reset()
     229             :         {
     230        9561 :             if( pResetT )
     231             :             {
     232        9561 :                 this->pT = pResetT;
     233        9561 :                 this->pT->acquire();
     234             :             }
     235        9561 :         }
     236             :     };
     237             : 
     238             :     typedef Guard<Mutex> MutexGuard;
     239             :     typedef ClearableGuard<Mutex> ClearableMutexGuard;
     240             :     typedef ResettableGuard< Mutex > ResettableMutexGuard;
     241             : 
     242             :     /** SolarMutex interface, needed for SolarMutex.
     243             :         Deprecated, used just for Application::GetSolarMutex().
     244             :     */
     245             :     class SolarMutex
     246             :     {
     247             :         public:
     248             :             /** Blocks if mutex is already in use
     249             :             */
     250             :             virtual void SAL_CALL acquire() = 0;
     251             : 
     252             :             /** Tries to get the mutex without blocking.
     253             :             */
     254             :             virtual sal_Bool SAL_CALL tryToAcquire() = 0;
     255             : 
     256             :             /** Releases the mutex.
     257             :             */
     258             :             virtual void SAL_CALL release() = 0;
     259             : 
     260             :         protected:
     261       63102 :             SolarMutex() {}
     262       61246 :             virtual ~SolarMutex() {}
     263             :     };
     264             :     typedef osl::Guard< SolarMutex > SolarGuard;
     265             :     typedef osl::ClearableGuard< SolarMutex > ClearableSolarGuard;
     266             :     typedef osl::ResettableGuard< SolarMutex > ResettableSolarGuard;
     267             : }
     268             : 
     269             : #endif  /* __cplusplus */
     270             : #endif  /* _OSL_MUTEX_HXX_ */
     271             : 
     272             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10