16 #ifdef THREAD_SIMPLE_IMPL 22 ThreadSimpleImpl *
volatile ThreadSimpleImpl::_st_this;
24 int ThreadSimpleImpl::_next_unique_id = 1;
30 ThreadSimpleImpl(
Thread *parent_obj) :
31 _parent_obj(parent_obj)
33 _unique_id = _next_unique_id;
38 _priority = TP_normal;
39 _priority_weight = 1.0;
45 _context = alloc_thread_context();
50 _manager = ThreadSimpleManager::get_global_ptr();
52 #ifdef HAVE_POSIX_THREADS 53 _posix_system_thread_id = (pthread_t)-1;
56 _win32_system_thread_id = 0;
65 if (thread_cat->is_debug()) {
67 <<
"Deleting thread " << _parent_obj->get_name() <<
"\n";
69 nassertv(_status != TS_running);
71 free_thread_context(_context);
73 if (_stack !=
nullptr) {
74 memory_hook->
mmap_free(_stack, _stack_size);
76 _manager->remove_thread(
this);
83 void ThreadSimpleImpl::
86 _priority = TP_normal;
87 _priority_weight = _manager->_simple_thread_normal_weight;
89 #ifdef HAVE_POSIX_THREADS 90 _posix_system_thread_id = pthread_self();
93 _win32_system_thread_id = GetCurrentThreadId();
96 _manager->set_current_thread(
this);
102 bool ThreadSimpleImpl::
103 start(ThreadPriority priority,
bool joinable) {
104 if (thread_cat->is_debug()) {
105 thread_cat.debug() <<
"Starting " << *_parent_obj <<
"\n";
108 nassertr(_status == TS_new,
false);
110 nassertr(_stack ==
nullptr,
false);
112 if (needs_stack_prealloc) {
113 _stack = (
unsigned char *)memory_hook->
mmap_alloc(_stack_size,
true);
116 _joinable = joinable;
117 _status = TS_running;
118 _priority = priority;
122 _priority_weight = _manager->_simple_thread_low_weight;
126 _priority_weight = _manager->_simple_thread_normal_weight;
130 _priority_weight = _manager->_simple_thread_high_weight;
134 _priority_weight = _manager->_simple_thread_urgent_weight;
144 _python_state = thread_state_swap(
nullptr);
145 thread_state_swap(_python_state);
146 #endif // HAVE_PYTHON 148 init_thread_context(_context, _stack, _stack_size, st_begin_thread,
this);
150 _manager->enqueue_ready(
this,
false);
158 void ThreadSimpleImpl::
161 if (_status == TS_running) {
162 ThreadSimpleImpl *thread = _manager->get_current_thread();
163 if (thread !=
this) {
164 _joining_threads.push_back(thread);
165 _manager->next_context();
173 void ThreadSimpleImpl::
175 _manager->preempt(
this);
181 std::string ThreadSimpleImpl::
182 get_unique_id()
const {
183 std::ostringstream strm;
185 strm << GetCurrentProcessId();
189 strm <<
"." << _unique_id;
198 void ThreadSimpleImpl::
200 ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
201 manager->prepare_for_exit();
207 bool ThreadSimpleImpl::
209 return (is_os_threads != 0);
215 void ThreadSimpleImpl::
216 sleep_this(
double seconds) {
217 _manager->enqueue_sleep(
this, seconds);
218 _manager->next_context();
224 void ThreadSimpleImpl::
225 yield_this(
bool volunteer) {
226 if (thread_cat->is_debug() && volunteer) {
228 <<
"Force-yielding " << _parent_obj->get_name() <<
"\n";
230 _manager->enqueue_ready(
this,
true);
231 _manager->next_context();
237 void ThreadSimpleImpl::
238 st_begin_thread(
void *data) {
239 ThreadSimpleImpl *
self = (ThreadSimpleImpl *)data;
240 self->begin_thread();
246 void ThreadSimpleImpl::
249 thread_state_swap(_python_state);
250 #endif // HAVE_PYTHON 252 #ifdef HAVE_POSIX_THREADS 253 _posix_system_thread_id = pthread_self();
256 _win32_system_thread_id = GetCurrentThreadId();
261 _parent_obj->thread_main();
264 _status = TS_finished;
267 JoiningThreads::iterator jti;
268 for (jti = _joining_threads.begin(); jti != _joining_threads.end(); ++jti) {
269 _manager->enqueue_ready(*jti,
false);
271 _joining_threads.clear();
273 _manager->enqueue_finished(
this);
274 _manager->next_context();
281 #endif // THREAD_SIMPLE_IMPL PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void mmap_free(void *ptr, size_t size)
Frees a block of memory previously allocated via mmap_alloc().
size_t round_up_to_page_size(size_t size) const
Rounds the indicated size request up to the next larger multiple of page_size, to qualify it for a ca...
A thread; that is, a lightweight process.
virtual void * mmap_alloc(size_t size, bool allow_exec)
Allocates a raw page or pages of memory directly from the OS.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.