UDK 3.2.7 C/C++ API Reference
osl/mutex.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _OSL_MUTEX_HXX_
00030 #define _OSL_MUTEX_HXX_
00031 
00032 #ifdef __cplusplus
00033 
00034 #include <osl/mutex.h>
00035 
00036 
00037 namespace osl
00038 {
00041     class Mutex {
00042 
00043     public:
00048         Mutex()
00049         {
00050             mutex = osl_createMutex();
00051         }
00052 
00056         ~Mutex()
00057         {
00058             osl_destroyMutex(mutex);
00059         }
00060 
00065         sal_Bool acquire()
00066         {
00067             return osl_acquireMutex(mutex);
00068         }
00069 
00074         sal_Bool tryToAcquire()
00075         {
00076             return osl_tryToAcquireMutex(mutex);
00077         }
00078 
00083         sal_Bool release()
00084         {
00085             return osl_releaseMutex(mutex);
00086         }
00087 
00094         static Mutex * getGlobalMutex()
00095         {
00096             return (Mutex *)osl_getGlobalMutex();
00097         }
00098 
00099     private:
00100         oslMutex mutex;
00101 
00108         Mutex(const Mutex&);
00109 
00116         Mutex(oslMutex Mutex);
00117 
00121         Mutex& operator= (const Mutex&);
00122 
00126         Mutex& operator= (oslMutex);
00127     };
00128 
00131     template<class T>
00132     class Guard
00133     {
00134     private:
00135         Guard( const Guard& );
00136         const Guard& operator = ( const Guard& );
00137 
00138     protected:
00139         T * pT;
00140     public:
00141 
00144         Guard(T * pT_) : pT(pT_)
00145         {
00146             pT->acquire();
00147         }
00148 
00151         Guard(T & t) : pT(&t)
00152         {
00153             pT->acquire();
00154         }
00155 
00157         ~Guard()
00158         {
00159             pT->release();
00160         }
00161     };
00162 
00165     template<class T>
00166     class ClearableGuard
00167     {
00168     private:
00169         ClearableGuard( const ClearableGuard& );
00170         const ClearableGuard& operator = ( const ClearableGuard& );
00171     protected:
00172         T * pT;
00173     public:
00174 
00177         ClearableGuard(T * pT_) : pT(pT_)
00178         {
00179             pT->acquire();
00180         }
00181 
00184         ClearableGuard(T & t) : pT(&t)
00185         {
00186             pT->acquire();
00187         }
00188 
00191         ~ClearableGuard()
00192         {
00193             if (pT)
00194                 pT->release();
00195         }
00196 
00199         void clear()
00200         {
00201             if(pT)
00202             {
00203                 pT->release();
00204                 pT = NULL;
00205             }
00206         }
00207     };
00208 
00211     template< class T >
00212     class ResettableGuard : public ClearableGuard< T >
00213     {
00214     private:
00215         ResettableGuard(ResettableGuard &); // not defined
00216         void operator =(ResettableGuard &); // not defined
00217 
00218     protected:
00219         T* pResetT;
00220     public:
00223         ResettableGuard( T* pT_ ) :
00224                 ClearableGuard<T>( pT_ ),
00225                 pResetT( pT_ )
00226         {}
00227 
00230         ResettableGuard( T& rT ) :
00231                 ClearableGuard<T>( rT ),
00232                 pResetT( &rT )
00233         {}
00234 
00237         void reset()
00238         {
00239             if( pResetT )
00240             {
00241                 this->pT = pResetT;
00242                 this->pT->acquire();
00243             }
00244         }
00245     };
00246 
00247     typedef Guard<Mutex> MutexGuard;
00248     typedef ClearableGuard<Mutex> ClearableMutexGuard;
00249     typedef ResettableGuard< Mutex > ResettableMutexGuard;
00250 
00254     class SolarMutex
00255     {
00256         public:
00259             virtual void SAL_CALL acquire() = 0;
00260 
00263             virtual sal_Bool SAL_CALL tryToAcquire() = 0;
00264 
00267             virtual void SAL_CALL release() = 0;
00268 
00269         protected:
00270             SolarMutex() {}
00271             virtual ~SolarMutex() {}
00272     };
00273     typedef osl::Guard< SolarMutex > SolarGuard;
00274     typedef osl::ClearableGuard< SolarMutex > ClearableSolarGuard;
00275     typedef osl::ResettableGuard< SolarMutex > ResettableSolarGuard;
00276 }
00277 
00278 #endif  /* __cplusplus */
00279 #endif  /* _OSL_MUTEX_HXX_ */
00280 
00281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines