Panda3D
|
00001 // Filename: threadSimpleImpl.I 00002 // Created by: drose (18Jun07) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: ThreadSimpleImpl::get_current_thread 00018 // Access: Public, Static 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE Thread *ThreadSimpleImpl:: 00022 get_current_thread() { 00023 return ThreadSimpleManager::get_global_ptr()->get_current_thread()->_parent_obj; 00024 } 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: ThreadSimpleImpl::is_same_system_thread 00028 // Access: Public 00029 // Description: Returns true if we are still running within the same 00030 // OS-level thread that this thread begin in, or false 00031 // if this appears to be running in a different thread. 00032 //////////////////////////////////////////////////////////////////// 00033 INLINE bool ThreadSimpleImpl:: 00034 is_same_system_thread() const { 00035 #ifdef HAVE_POSIX_THREADS 00036 return pthread_equal(_posix_system_thread_id, pthread_self()); 00037 #endif 00038 #ifdef WIN32 00039 return (_win32_system_thread_id == GetCurrentThreadId()); 00040 #endif 00041 return true; 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: ThreadSimpleImpl::bind_thread 00046 // Access: Public, Static 00047 // Description: Associates the indicated Thread object with the 00048 // currently-executing thread. You should not call this 00049 // directly; use Thread::bind_thread() instead. 00050 //////////////////////////////////////////////////////////////////// 00051 INLINE void ThreadSimpleImpl:: 00052 bind_thread(Thread *) { 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function: ThreadSimpleImpl::is_threading_supported 00057 // Access: Public, Static 00058 // Description: 00059 //////////////////////////////////////////////////////////////////// 00060 INLINE bool ThreadSimpleImpl:: 00061 is_threading_supported() { 00062 return true; 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: ThreadSimpleImpl::is_true_threads 00067 // Access: Public, Static 00068 // Description: 00069 //////////////////////////////////////////////////////////////////// 00070 INLINE bool ThreadSimpleImpl:: 00071 is_true_threads() { 00072 return (is_os_threads != 0); 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: ThreadSimpleImpl::is_simple_threads 00077 // Access: Public, Static 00078 // Description: 00079 //////////////////////////////////////////////////////////////////// 00080 INLINE bool ThreadSimpleImpl:: 00081 is_simple_threads() { 00082 return true; 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: ThreadSimpleImpl::sleep 00087 // Access: Public, Static 00088 // Description: 00089 //////////////////////////////////////////////////////////////////// 00090 INLINE void ThreadSimpleImpl:: 00091 sleep(double seconds) { 00092 ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr(); 00093 if (manager->is_same_system_thread()) { 00094 ThreadSimpleImpl *thread = manager->get_current_thread(); 00095 thread->sleep_this(seconds); 00096 } else { 00097 manager->system_sleep(seconds); 00098 } 00099 } 00100 00101 //////////////////////////////////////////////////////////////////// 00102 // Function: ThreadSimpleImpl::yield 00103 // Access: Public, Static 00104 // Description: 00105 //////////////////////////////////////////////////////////////////// 00106 INLINE void ThreadSimpleImpl:: 00107 yield() { 00108 ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr(); 00109 if (manager->is_same_system_thread()) { 00110 ThreadSimpleImpl *thread = manager->get_current_thread(); 00111 thread->yield_this(true); 00112 } else { 00113 manager->system_yield(); 00114 } 00115 } 00116 00117 //////////////////////////////////////////////////////////////////// 00118 // Function: ThreadSimpleImpl::consider_yield 00119 // Access: Public, Static 00120 // Description: 00121 //////////////////////////////////////////////////////////////////// 00122 INLINE void ThreadSimpleImpl:: 00123 consider_yield() { 00124 ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr(); 00125 if (manager->is_same_system_thread()) { 00126 ThreadSimpleImpl *thread = manager->get_current_thread(); 00127 thread->consider_yield_this(); 00128 } 00129 } 00130 00131 //////////////////////////////////////////////////////////////////// 00132 // Function: ThreadSimpleImpl::consider_yield_this 00133 // Access: Public 00134 // Description: 00135 //////////////////////////////////////////////////////////////////// 00136 INLINE void ThreadSimpleImpl:: 00137 consider_yield_this() { 00138 double now = _manager->get_current_time(); 00139 if (now >= _stop_time) { 00140 yield_this(false); 00141 } 00142 } 00143 00144 //////////////////////////////////////////////////////////////////// 00145 // Function: ThreadSimpleImpl::get_wake_time 00146 // Access: Public 00147 // Description: 00148 //////////////////////////////////////////////////////////////////// 00149 INLINE double ThreadSimpleImpl:: 00150 get_wake_time() const { 00151 return _wake_time; 00152 } 00153 00154 //////////////////////////////////////////////////////////////////// 00155 // Function: ThreadSimpleImpl::write_status 00156 // Access: Public, Static 00157 // Description: Writes a list of threads running and threads blocked. 00158 //////////////////////////////////////////////////////////////////// 00159 void ThreadSimpleImpl:: 00160 write_status(ostream &out) { 00161 ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr(); 00162 manager->write_status(out); 00163 }