00001 //================================================================================================== 00002 // Copyright (C) 2010 Brian Tietz sdbtietz at yahoo dot com 00003 // 00004 // This program is free software; you can redistribute it and/or modify it under the terms of the 00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the 00006 // License. 00007 // 00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00010 // General Public License for more details. 00011 // 00012 // You should have received a copy of the GNU General Public License along with this program; if 00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00014 // 02110-1301, USA. 00015 // 00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com) 00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that 00018 // license as applied to the UT library in exchange for either substantial contributions to the 00019 // development of the UT library or other forms of compensation. Any such waiver must be 00020 // established in writing between the copyright holder and the commercial entity obtaining such a 00021 // waiver. 00022 //================================================================================================== 00023 00024 00025 #ifndef _UT_MUTEX_H_ 00026 #define _UT_MUTEX_H_ 00027 00028 00029 //================================================================================================== 00030 //=== Project headers 00031 //================================================================================================== 00032 #include "UTEvent.h" 00033 00034 00035 //================================================================================================== 00036 //=== Forward name declarations 00037 //================================================================================================== 00038 struct MutexPlatformInfo_t; 00039 00040 00041 //================================================================================================== 00042 class UT_EXPORT Mutex_t 00043 //================================================================================================== 00045 { 00046 //---------------------------------------------------------------------------------------------- 00047 public: 00048 //---------------------------------------------------------------------------------------------- 00049 Mutex_t(bool allow_recursive_lock = false); 00052 00053 ~Mutex_t(); 00055 00056 void Lock(); 00064 00065 void Unlock(); 00067 00068 inline thread_id_t LockedBy(); 00072 00073 //---------------------------------------------------------------------------------------------- 00074 private: 00075 //---------------------------------------------------------------------------------------------- 00076 MutexPlatformInfo_t* m_info; 00077 volatile thread_id_t m_owner; 00078 bool m_allow_recursive_lock; 00079 int m_locked_recursion_count; 00080 }; 00081 00082 00083 //================================================================================================== 00084 class UT_EXPORT MultiReaderMutex_t 00085 //================================================================================================== 00087 { 00088 //---------------------------------------------------------------------------------------------- 00089 public: 00090 //---------------------------------------------------------------------------------------------- 00091 MultiReaderMutex_t(); 00093 00094 ~MultiReaderMutex_t(); 00096 00097 void LockRead(); 00103 00104 void UnlockRead(); 00106 00107 void LockWrite(); 00113 00114 void UnlockWrite(); 00116 00117 //---------------------------------------------------------------------------------------------- 00118 private: 00119 //---------------------------------------------------------------------------------------------- 00120 inline void DebugMRMDiag( stringliteral* what, int value ) const; 00121 00122 //---------------------------------------------------------------------------------------------- 00123 private: 00124 //---------------------------------------------------------------------------------------------- 00125 Mutex_t m_state_mutex; 00126 volatile bool m_held_by_writer; 00127 volatile int m_held_by_readers; 00128 volatile int m_waiting_writers; 00129 volatile int m_waiting_readers; 00130 Event_t m_writer_wait_event; 00131 Event_t m_reader_wait_event; 00132 }; 00133 00134 00135 00136 00137 //================================================================================================== 00138 //================================================================================================== 00139 //=== 00140 //=== Inline function implementations: class Mutex_t 00141 //=== 00142 //================================================================================================== 00143 //================================================================================================== 00144 inline thread_id_t 00145 Mutex_t::LockedBy() 00146 { 00147 return m_owner; 00148 } 00149 00150 00151 #endif // _UT_MUTEX_H_