Panda3D
 All Classes Functions Variables Enumerations
threadSimpleImpl.I
1 // Filename: threadSimpleImpl.I
2 // Created by: drose (18Jun07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: ThreadSimpleImpl::get_current_thread
18 // Access: Public, Static
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE Thread *ThreadSimpleImpl::
22 get_current_thread() {
23  return ThreadSimpleManager::get_global_ptr()->get_current_thread()->_parent_obj;
24 }
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: ThreadSimpleImpl::is_same_system_thread
28 // Access: Public
29 // Description: Returns true if we are still running within the same
30 // OS-level thread that this thread begin in, or false
31 // if this appears to be running in a different thread.
32 ////////////////////////////////////////////////////////////////////
33 INLINE bool ThreadSimpleImpl::
34 is_same_system_thread() const {
35 #ifdef HAVE_POSIX_THREADS
36  return pthread_equal(_posix_system_thread_id, pthread_self());
37 #endif
38 #ifdef WIN32
39  return (_win32_system_thread_id == GetCurrentThreadId());
40 #endif
41  return true;
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: ThreadSimpleImpl::bind_thread
46 // Access: Public, Static
47 // Description: Associates the indicated Thread object with the
48 // currently-executing thread. You should not call this
49 // directly; use Thread::bind_thread() instead.
50 ////////////////////////////////////////////////////////////////////
51 INLINE void ThreadSimpleImpl::
52 bind_thread(Thread *) {
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: ThreadSimpleImpl::is_threading_supported
57 // Access: Public, Static
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 INLINE bool ThreadSimpleImpl::
61 is_threading_supported() {
62  return true;
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: ThreadSimpleImpl::is_true_threads
67 // Access: Public, Static
68 // Description:
69 ////////////////////////////////////////////////////////////////////
70 INLINE bool ThreadSimpleImpl::
71 is_true_threads() {
72  return (is_os_threads != 0);
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: ThreadSimpleImpl::is_simple_threads
77 // Access: Public, Static
78 // Description:
79 ////////////////////////////////////////////////////////////////////
80 INLINE bool ThreadSimpleImpl::
81 is_simple_threads() {
82  return true;
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: ThreadSimpleImpl::sleep
87 // Access: Public, Static
88 // Description:
89 ////////////////////////////////////////////////////////////////////
90 INLINE void ThreadSimpleImpl::
91 sleep(double seconds) {
92  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
93  if (manager->is_same_system_thread()) {
94  ThreadSimpleImpl *thread = manager->get_current_thread();
95  thread->sleep_this(seconds);
96  } else {
97  manager->system_sleep(seconds);
98  }
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: ThreadSimpleImpl::yield
103 // Access: Public, Static
104 // Description:
105 ////////////////////////////////////////////////////////////////////
106 INLINE void ThreadSimpleImpl::
107 yield() {
108  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
109  if (manager->is_same_system_thread()) {
110  ThreadSimpleImpl *thread = manager->get_current_thread();
111  thread->yield_this(true);
112  } else {
113  manager->system_yield();
114  }
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: ThreadSimpleImpl::consider_yield
119 // Access: Public, Static
120 // Description:
121 ////////////////////////////////////////////////////////////////////
122 INLINE void ThreadSimpleImpl::
123 consider_yield() {
124  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
125  if (manager->is_same_system_thread()) {
126  ThreadSimpleImpl *thread = manager->get_current_thread();
127  thread->consider_yield_this();
128  }
129 }
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: ThreadSimpleImpl::consider_yield_this
133 // Access: Public
134 // Description:
135 ////////////////////////////////////////////////////////////////////
136 INLINE void ThreadSimpleImpl::
137 consider_yield_this() {
138  double now = _manager->get_current_time();
139  if (now >= _stop_time) {
140  yield_this(false);
141  }
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: ThreadSimpleImpl::get_wake_time
146 // Access: Public
147 // Description:
148 ////////////////////////////////////////////////////////////////////
149 INLINE double ThreadSimpleImpl::
150 get_wake_time() const {
151  return _wake_time;
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: ThreadSimpleImpl::write_status
156 // Access: Public, Static
157 // Description: Writes a list of threads running and threads blocked.
158 ////////////////////////////////////////////////////////////////////
159 void ThreadSimpleImpl::
160 write_status(ostream &out) {
161  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
162  manager->write_status(out);
163 }
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
Definition: thread.I:145
A thread; that is, a lightweight process.
Definition: thread.h:51