csutil/threading/mutex.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2006 by Marten Svanfeldt 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_CSUTIL_THREADING_MUTEX_H__ 00020 #define __CS_CSUTIL_THREADING_MUTEX_H__ 00021 00022 #include "csutil/noncopyable.h" 00023 00030 // Include implementation specific versions 00031 #if defined(CS_PLATFORM_WIN32) 00032 # include "csutil/threading/win32_mutex.h" 00033 #elif defined(CS_PLATFORM_UNIX) || \ 00034 defined(CS_PLATFORM_MACOSX) 00035 # include "csutil/threading/pthread_mutex.h" 00036 #else 00037 #error "No threading implementation for your platform" 00038 #endif 00039 00040 namespace CS 00041 { 00042 namespace Threading 00043 { 00044 00054 template<typename BaseMutex> 00055 class MutexImpl : public BaseMutex, 00056 private CS::NonCopyable 00057 { 00058 public: 00062 MutexImpl () 00063 { 00064 BaseMutex::Initialize (); 00065 } 00066 00070 ~MutexImpl () 00071 { 00072 BaseMutex::Destroy (); 00073 } 00074 00080 bool Lock () 00081 { 00082 return BaseMutex::Lock (); 00083 } 00084 00090 bool TryLock () 00091 { 00092 return BaseMutex::TryLock (); 00093 } 00094 00098 void Unlock () 00099 { 00100 BaseMutex::Unlock (); 00101 } 00102 00106 bool IsLocked () 00107 { 00108 return BaseMutex::IsLocked (); 00109 } 00110 protected: 00111 friend class ConditionBase; 00112 }; 00113 00117 typedef MutexImpl<Implementation::MutexBase> Mutex; 00118 00124 typedef MutexImpl<Implementation::RecursiveMutexBase> RecursiveMutex; 00125 00126 00142 template<typename T> 00143 class ScopedLock 00144 { 00145 public: 00146 ScopedLock (T& lockObj) 00147 : lockObj (lockObj) 00148 { 00149 lockObj.Lock (); 00150 } 00151 00152 ~ScopedLock () 00153 { 00154 if (lockObj.IsLocked ()) 00155 { 00156 lockObj.Unlock (); 00157 } 00158 } 00159 00160 private: 00161 T& lockObj; 00162 }; 00163 00164 // Standard lock 00165 typedef ScopedLock<Mutex> MutexScopedLock; 00166 typedef ScopedLock<RecursiveMutex> RecursiveMutexScopedLock; 00167 } 00168 } 00169 00170 #endif
Generated for Crystal Space 1.2 by doxygen 1.4.7