00001 //******************************************************************************* 00002 // 00003 // ******* *** *** * 00004 // * * * * 00005 // * * * ***** 00006 // * * *** * * ** * ** *** 00007 // * * * * * * * **** * * * 00008 // * * * * * * * * * * * 00009 // * *** *** * ** ** ** * * 00010 // * 00011 //******************************************************************************* 00012 // see http://sourceforge.net/projects/tcsystem/ for details. 00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 00014 //******************************************************************************* 00015 // 00016 // TCSystem is the legal property of its developers. 00017 // Please refer to the COPYRIGHT file distributed with this source distribution. 00018 // 00019 // This library is free software; you can redistribute it and/or 00020 // modify it under the terms of the GNU Lesser General Public 00021 // License as published by the Free Software Foundation; either 00022 // version 2.1 of the License, or (at your option) any later version. 00023 // 00024 // This library is distributed in the hope that it will be useful, 00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00027 // Lesser General Public License for more details. 00028 // 00029 // You should have received a copy of the GNU Lesser General Public 00030 // License along with this library; if not, write to the Free Software 00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00032 //******************************************************************************* 00033 // $Id: TCMTLockable.h 870 2008-07-21 14:15:33Z the_____tiger $ 00034 //******************************************************************************* 00035 00036 #ifndef _TC_MT_LOCKABLE_H_ 00037 #define _TC_MT_LOCKABLE_H_ 00038 00039 #include "TCNonCopyable.h" 00040 #include "TCMTFactory.h" 00041 00042 namespace TC 00043 { 00044 namespace MT 00045 { 00060 template <class CLASS_TO_LOCK> 00061 class ObjectLevelLockable: protected NonCopyAble 00062 { 00063 public: 00064 ObjectLevelLockable() 00065 { 00066 m_mutex = Factory::CreateMutex(); 00067 } 00068 ~ObjectLevelLockable() 00069 { 00070 m_mutex = MutexPtr(); 00071 } 00072 00074 bool Lock() const 00075 { 00076 return m_mutex->Lock(); 00077 } 00078 00080 bool UnLock() const 00081 { 00082 return m_mutex->UnLock(); 00083 } 00084 00086 typedef LockerPtr< const CLASS_TO_LOCK* > Locker; 00087 00088 private: 00089 mutable MutexPtr m_mutex; 00090 }; 00091 00096 template <class CLASS_TO_LOCK> 00097 class ClassLevelLockable: protected NonCopyAble 00098 { 00099 public: 00105 class Locker: protected NonCopyAble 00106 { 00107 public: 00108 Locker() 00109 { 00110 m_mutex->Lock(); 00111 } 00112 ~Locker() 00113 { 00114 m_mutex->UnLock(); 00115 } 00116 }; 00117 00119 bool Lock() 00120 { 00121 return m_mutex->Lock(); 00122 } 00123 00125 bool UnLock() 00126 { 00127 return m_mutex->UnLock(); 00128 } 00129 00130 private: 00131 static MutexPtr m_mutex; 00132 00133 friend class Locker; 00134 }; 00135 00136 template<class CLASS_TO_LOCK> 00137 MutexPtr ClassLevelLockable<CLASS_TO_LOCK>::m_mutex = Factory::CreateMutex(); 00138 00142 } // namespace MT 00143 } // namespace TC 00144 00145 #endif // _TC_MT_LOCKABLE_H_