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_THREAD_H_ 00026 #define _UT_THREAD_H_ 00027 00028 00029 //================================================================================================== 00030 //=== Project headers 00031 //================================================================================================== 00032 #include "UTLists.h" 00033 #include "UTMutex.h" 00034 00035 00036 //================================================================================================== 00037 //=== Types and constants 00038 //================================================================================================== 00039 00063 typedef uint8 thread_priority_t; 00064 00065 const thread_priority_t eTHREADPRI_idle = 1; 00066 const thread_priority_t eTHREADPRI_very_low_min = 2; 00067 const thread_priority_t eTHREADPRI_very_low = 13; 00068 const thread_priority_t eTHREADPRI_low_min = 25; 00069 const thread_priority_t eTHREADPRI_low = 37; 00070 const thread_priority_t eTHREADPRI_normal_min = 50; 00071 const thread_priority_t eTHREADPRI_normal = 62; 00072 const thread_priority_t eTHREADPRI_above_normal_min = 75; 00073 const thread_priority_t eTHREADPRI_above_normal = 87; 00074 const thread_priority_t eTHREADPRI_gui_min = 100; 00075 const thread_priority_t eTHREADPRI_gui = 112; 00076 const thread_priority_t eTHREADPRI_above_gui_min = 125; 00077 const thread_priority_t eTHREADPRI_above_gui = 137; 00078 const thread_priority_t eTHREADPRI_high_min = 150; 00079 const thread_priority_t eTHREADPRI_high = 167; 00080 const thread_priority_t eTHREADPRI_very_high_min = 175; 00081 const thread_priority_t eTHREADPRI_very_high = 187; 00082 const thread_priority_t eTHREADPRI_time_critical_min = 200; 00083 const thread_priority_t eTHREADPRI_real_time_min = 225; 00084 const thread_priority_t eTHREADPRI_kernel_real_time_min = 240; 00085 00086 00087 //================================================================================================== 00088 //=== Forward name declarations 00089 //================================================================================================== 00090 struct PlatformThreadInfo_t; 00091 class MessageLoop_t; 00092 00093 00094 //================================================================================================== 00095 //=== Global functions 00096 //================================================================================================== 00097 00099 extern UT_EXPORT thread_id_t current_thread_id(); 00100 00101 00102 //================================================================================================== 00103 class UT_EXPORT Thread_t 00104 //================================================================================================== 00105 : private LinkedListNode_t<Thread_t> 00107 { 00108 //---------------------------------------------------------------------------------------------- 00109 public: 00110 //---------------------------------------------------------------------------------------------- 00111 Thread_t(thread_priority_t priority = eTHREADPRI_normal); 00119 00120 virtual ~Thread_t(); 00127 00128 virtual void Start(int32 stack_size = 0); 00136 00137 void SetPriority(thread_priority_t priority); 00141 00142 inline thread_id_t ThreadId() const; 00145 00146 inline thread_priority_t Priority() const; 00149 00150 virtual void WaitForThreadToExit(); 00156 00157 virtual void Main() = 0; 00161 00162 static void RegisterMainThread(Thread_t* main_thread); 00166 00167 static thread_id_t MainThreadId(); 00170 00171 inline bool IsMainThread() const; 00174 00175 inline static int CountThreads(); 00178 00179 static inline bool IsThreadingSafe(); 00182 00183 // \cond DOXYGEN_DOCUMENT_NEVER 00184 static void ThreadingIsSafe(bool safe); 00185 // Internal to UT library, public because it's called from 00186 // main 00187 // \endcond 00188 00189 //---------------------------------------------------------------------------------------------- 00190 private: 00191 //---------------------------------------------------------------------------------------------- 00192 void SetPriorityInternal(); 00193 // Sets the thread priority to m_priority, ms_mutex must be 00194 // locked, this must be in ms_threads. 00195 00196 //---------------------------------------------------------------------------------------------- 00197 private: 00198 //---------------------------------------------------------------------------------------------- 00199 thread_id_t m_thread_id; 00200 thread_priority_t m_priority; 00201 PlatformThreadInfo_t* m_info; 00202 bool m_thread_running; 00203 00204 static Mutex_t ms_mutex; 00205 static bool ms_threading_is_safe; 00206 static LinkedList_t<Thread_t> ms_threads; 00207 static Thread_t* ms_main_thread; 00208 00209 friend class LinkedList_t<Thread_t>; 00210 friend class LinkedListNode_t<Thread_t>; 00211 friend class PhysMemCache_t; 00212 friend class SystemTimeManager_t; 00213 }; 00214 00215 00216 00217 00218 //================================================================================================== 00219 //================================================================================================== 00220 //=== 00221 //=== Inline function implementations: class Thread_t 00222 //=== 00223 //================================================================================================== 00224 //================================================================================================== 00225 inline thread_id_t 00226 Thread_t::ThreadId() const 00227 { 00228 return m_thread_id; 00229 } 00230 00231 00232 inline thread_priority_t 00233 Thread_t::Priority() const 00234 { 00235 return m_priority; 00236 } 00237 00238 00239 inline bool 00240 Thread_t::IsMainThread() const 00241 { 00242 return (ms_main_thread == this); 00243 } 00244 00245 00246 inline int 00247 Thread_t::CountThreads() 00248 { 00249 return ms_threads.CountItems(); 00250 } 00251 00252 00253 inline bool 00254 Thread_t::IsThreadingSafe() 00255 { 00256 return ms_threading_is_safe; 00257 } 00258 00259 00260 #endif // _UT_THREAD_H_