15 #include "threadPosixImpl.h"
16 #include "selectThreadImpl.h"
18 #ifdef THREAD_POSIX_IMPL
21 #include "pointerTo.h"
22 #include "config_pipeline.h"
26 #include "config_express.h"
30 pthread_key_t ThreadPosixImpl::_pt_ptr_index = 0;
31 bool ThreadPosixImpl::_got_pt_ptr_index =
false;
40 if (thread_cat->is_debug()) {
42 <<
"Deleting thread " << _parent_obj->get_name() <<
"\n";
48 pthread_detach(_thread);
62 void ThreadPosixImpl::
72 bool ThreadPosixImpl::
73 start(ThreadPriority priority,
bool joinable) {
75 if (thread_cat->is_debug()) {
76 thread_cat.debug() <<
"Starting " << *_parent_obj <<
"\n";
79 nassertd(_status == S_new) {
85 _status = S_start_called;
88 if (!_got_pt_ptr_index) {
93 pthread_attr_init(&attr);
96 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
100 int result = pthread_attr_setstacksize(&attr, thread_stack_size);
102 thread_cat->warning()
103 <<
"Unable to set stack size.\n";
108 result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
110 thread_cat->warning()
111 <<
"Unable to set system scope.\n";
114 struct sched_param param;
115 int current_policy = SCHED_OTHER;
116 result = pthread_attr_setschedpolicy(&attr, current_policy);
118 thread_cat->warning()
119 <<
"Unable to set scheduling policy.\n";
126 param.sched_priority = sched_get_priority_min(current_policy);
127 result = pthread_attr_setschedparam(&attr, ¶m);
132 param.sched_priority = sched_get_priority_max(current_policy);
133 result = pthread_attr_setschedparam(&attr, ¶m);
142 thread_cat->warning()
143 <<
"Unable to specify thread priority.\n";
149 result = pthread_create(&_thread, &attr, &root_func, (
void *)
this);
151 pthread_attr_destroy(&attr);
157 unref_delete(_parent_obj);
174 void ThreadPosixImpl::
180 pthread_join(_thread, &return_val);
192 string ThreadPosixImpl::
193 get_unique_id()
const {
195 strm << getpid() <<
"." << _thread;
205 void *ThreadPosixImpl::
206 root_func(
void *data) {
207 TAU_REGISTER_THREAD();
211 ThreadPosixImpl *
self = (ThreadPosixImpl *)data;
212 int result = pthread_setspecific(_pt_ptr_index, self->_parent_obj);
213 nassertr(result == 0, NULL);
216 self->_mutex.acquire();
217 nassertd(self->_status == S_start_called) {
218 self->_mutex.release();
222 self->_status = S_running;
223 self->_mutex.release();
228 JavaVM *jvm = get_java_vm();
230 if (jvm == NULL || jvm->AttachCurrentThread(&env, NULL) != 0) {
232 <<
"Failed to attach Java VM to thread "
233 <<
self->_parent_obj->get_name() <<
"!\n";
238 self->_parent_obj->thread_main();
240 if (thread_cat->is_debug()) {
242 <<
"Terminating thread " <<
self->_parent_obj->get_name()
243 <<
", count = " <<
self->_parent_obj->get_ref_count() <<
"\n";
247 self->_mutex.acquire();
248 nassertd(self->_status == S_running) {
249 self->_mutex.release();
252 self->_status = S_finished;
253 self->_mutex.release();
258 jvm->DetachCurrentThread();
265 unref_delete(self->_parent_obj);
277 void ThreadPosixImpl::
278 init_pt_ptr_index() {
279 nassertv(!_got_pt_ptr_index);
281 int result = pthread_key_create(&_pt_ptr_index, NULL);
284 <<
"Unable to associate Thread pointers with threads.\n";
288 _got_pt_ptr_index =
true;
293 result = pthread_setspecific(_pt_ptr_index, main_thread_obj);
294 nassertv(result == 0);
297 #endif // THREAD_POSIX_IMPL
static Thread * get_main_thread()
Returns a pointer to the "main" Thread object–this is the Thread that started the whole process...
A thread; that is, a lightweight process.