00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 00002 /* 00003 * This file is part of the LibreOffice project. 00004 * 00005 * This Source Code Form is subject to the terms of the Mozilla Public 00006 * License, v. 2.0. If a copy of the MPL was not distributed with this 00007 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 00008 * 00009 * This file incorporates work covered by the following license notice: 00010 * 00011 * Licensed to the Apache Software Foundation (ASF) under one or more 00012 * contributor license agreements. See the NOTICE file distributed 00013 * with this work for additional information regarding copyright 00014 * ownership. The ASF licenses this file to you under the Apache 00015 * License, Version 2.0 (the "License"); you may not use this file 00016 * except in compliance with the License. You may obtain a copy of 00017 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 00018 */ 00019 00020 #ifndef _OSL_MUTEX_HXX_ 00021 #define _OSL_MUTEX_HXX_ 00022 00023 #ifdef __cplusplus 00024 00025 #include <osl/mutex.h> 00026 00027 00028 namespace osl 00029 { 00032 class SAL_WARN_UNUSED Mutex { 00033 00034 public: 00039 Mutex() 00040 { 00041 mutex = osl_createMutex(); 00042 } 00043 00047 ~Mutex() 00048 { 00049 osl_destroyMutex(mutex); 00050 } 00051 00056 sal_Bool acquire() 00057 { 00058 return osl_acquireMutex(mutex); 00059 } 00060 00065 sal_Bool tryToAcquire() 00066 { 00067 return osl_tryToAcquireMutex(mutex); 00068 } 00069 00074 sal_Bool release() 00075 { 00076 return osl_releaseMutex(mutex); 00077 } 00078 00085 static Mutex * getGlobalMutex() 00086 { 00087 return (Mutex *)osl_getGlobalMutex(); 00088 } 00089 00090 private: 00091 oslMutex mutex; 00092 00099 Mutex(const Mutex&); 00100 00107 Mutex(oslMutex Mutex); 00108 00112 Mutex& operator= (const Mutex&); 00113 00117 Mutex& operator= (oslMutex); 00118 }; 00119 00122 template<class T> 00123 class Guard 00124 { 00125 private: 00126 Guard( const Guard& ); 00127 const Guard& operator = ( const Guard& ); 00128 00129 protected: 00130 T * pT; 00131 public: 00132 00135 Guard(T * pT_) : pT(pT_) 00136 { 00137 pT->acquire(); 00138 } 00139 00142 Guard(T & t) : pT(&t) 00143 { 00144 pT->acquire(); 00145 } 00146 00148 ~Guard() 00149 { 00150 pT->release(); 00151 } 00152 }; 00153 00156 template<class T> 00157 class ClearableGuard 00158 { 00159 private: 00160 ClearableGuard( const ClearableGuard& ); 00161 const ClearableGuard& operator = ( const ClearableGuard& ); 00162 protected: 00163 T * pT; 00164 public: 00165 00168 ClearableGuard(T * pT_) : pT(pT_) 00169 { 00170 pT->acquire(); 00171 } 00172 00175 ClearableGuard(T & t) : pT(&t) 00176 { 00177 pT->acquire(); 00178 } 00179 00182 ~ClearableGuard() 00183 { 00184 if (pT) 00185 pT->release(); 00186 } 00187 00190 void clear() 00191 { 00192 if(pT) 00193 { 00194 pT->release(); 00195 pT = NULL; 00196 } 00197 } 00198 }; 00199 00202 template< class T > 00203 class ResettableGuard : public ClearableGuard< T > 00204 { 00205 private: 00206 ResettableGuard(ResettableGuard &); // not defined 00207 void operator =(ResettableGuard &); // not defined 00208 00209 protected: 00210 T* pResetT; 00211 public: 00214 ResettableGuard( T* pT_ ) : 00215 ClearableGuard<T>( pT_ ), 00216 pResetT( pT_ ) 00217 {} 00218 00221 ResettableGuard( T& rT ) : 00222 ClearableGuard<T>( rT ), 00223 pResetT( &rT ) 00224 {} 00225 00228 void reset() 00229 { 00230 if( pResetT ) 00231 { 00232 this->pT = pResetT; 00233 this->pT->acquire(); 00234 } 00235 } 00236 }; 00237 00238 typedef Guard<Mutex> MutexGuard; 00239 typedef ClearableGuard<Mutex> ClearableMutexGuard; 00240 typedef ResettableGuard< Mutex > ResettableMutexGuard; 00241 00245 class SolarMutex 00246 { 00247 public: 00250 virtual void SAL_CALL acquire() = 0; 00251 00254 virtual sal_Bool SAL_CALL tryToAcquire() = 0; 00255 00258 virtual void SAL_CALL release() = 0; 00259 00260 protected: 00261 SolarMutex() {} 00262 virtual ~SolarMutex() {} 00263 }; 00264 typedef osl::Guard< SolarMutex > SolarGuard; 00265 typedef osl::ClearableGuard< SolarMutex > ClearableSolarGuard; 00266 typedef osl::ResettableGuard< SolarMutex > ResettableSolarGuard; 00267 } 00268 00269 #endif /* __cplusplus */ 00270 #endif /* _OSL_MUTEX_HXX_ */ 00271 00272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */