00001 // Filename: thread.I 00002 // Created by: drose (08Aug02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: Thread::Constructor 00022 // Access: Public 00023 // Description: Creates a new Thread object, but does not 00024 // immediately start executing it. This gives the 00025 // caller a chance to store it in a PT(Thread) object, 00026 // if desired, before the thread gets a chance to 00027 // terminate and destruct itself. 00028 // 00029 // Call start() to begin thread execution. 00030 //////////////////////////////////////////////////////////////////// 00031 INLINE Thread:: 00032 Thread(const string &name) : _name(name), _impl(this) { 00033 _started = false; 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: Thread::Copy Constructor 00038 // Access: Private 00039 // Description: Do not attempt to copy threads. 00040 //////////////////////////////////////////////////////////////////// 00041 INLINE Thread:: 00042 Thread(const Thread ©) : _impl(this) { 00043 nassertv(false); 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: Thread::Copy Assignment Operator 00048 // Access: Private 00049 // Description: Do not attempt to copy threads. 00050 //////////////////////////////////////////////////////////////////// 00051 INLINE void Thread:: 00052 operator = (const Thread ©) { 00053 nassertv(false); 00054 } 00055 00056 //////////////////////////////////////////////////////////////////// 00057 // Function: Thread::get_name 00058 // Access: Public 00059 // Description: Returns the name of the thread. 00060 //////////////////////////////////////////////////////////////////// 00061 INLINE const string &Thread:: 00062 get_name() const { 00063 return _name; 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: Thread::start 00068 // Access: Public 00069 // Description: Starts the thread executing. It is only valid to 00070 // call this once. 00071 // 00072 // The thread will begin executing its thread_main() 00073 // function, and will terminate when thread_main() 00074 // returns. 00075 // 00076 // priority is intended as a hint to the relative 00077 // importance of this thread, and global should be set 00078 // true if the thread will perform a lot of blocking 00079 // I/O, or false otherwise (see the NSPR documentation 00080 // on global vs. local threads for more on this). Both 00081 // of these parameters may be ignored by the thread 00082 // implementation. 00083 // 00084 // joinable should be set true if you intend to call 00085 // join() to wait for the thread to terminate, or false 00086 // if you don't care and you will never call join(). 00087 // 00088 // The return value is true if the thread is 00089 // successfully started, false otherwise. 00090 //////////////////////////////////////////////////////////////////// 00091 INLINE bool Thread:: 00092 start(ThreadPriority priority, bool global, bool joinable) { 00093 nassertr(!_started, false); 00094 _started = _impl.start(priority, global, joinable); 00095 return _started; 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: Thread::join 00100 // Access: Public 00101 // Description: Blocks the calling process until the thread 00102 // terminates. If the thread has already terminated, 00103 // this returns immediately. 00104 //////////////////////////////////////////////////////////////////// 00105 INLINE void Thread:: 00106 join() { 00107 _impl.join(); 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: Thread::prepare_for_exit 00112 // Access: Public 00113 // Description: Should be called by the main thread just before 00114 // exiting the program, this blocks until any remaining 00115 // thread cleanup has finished. 00116 //////////////////////////////////////////////////////////////////// 00117 INLINE void Thread:: 00118 prepare_for_exit() { 00119 ThreadImpl::prepare_for_exit(); 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: Thread::get_current_thread 00124 // Access: Public, Static 00125 // Description: Returns a pointer to the currently-executing Thread 00126 // object, or NULL if the main thread (or some system 00127 // thread other than one started from the Panda 00128 // interface) is currently executing. 00129 //////////////////////////////////////////////////////////////////// 00130 INLINE Thread *Thread:: 00131 get_current_thread() { 00132 return ThreadImpl::get_current_thread(); 00133 } 00134 00135 //////////////////////////////////////////////////////////////////// 00136 // Function: Thread::is_threading_supported 00137 // Access: Public, Static 00138 // Description: Returns true if a real threading library is available 00139 // that supports threads, or false if no threading 00140 // library is available (and Thread::start() will always 00141 // fail). 00142 //////////////////////////////////////////////////////////////////// 00143 INLINE bool Thread:: 00144 is_threading_supported() { 00145 return ThreadImpl::is_threading_supported(); 00146 } 00147 00148 //////////////////////////////////////////////////////////////////// 00149 // Function: Thread::sleep 00150 // Access: Public, Static 00151 // Description: Suspends the current thread for at least the 00152 // indicated amount of time. It might be suspended for 00153 // longer. 00154 //////////////////////////////////////////////////////////////////// 00155 INLINE void Thread:: 00156 sleep(double seconds) { 00157 ThreadImpl::sleep(seconds); 00158 } 00159 00160 INLINE ostream & 00161 operator << (ostream &out, const Thread &thread) { 00162 thread.output(out); 00163 return out; 00164 }