17 #ifdef THREAD_POSIX_IMPL    28 static JavaVM *java_vm = 
nullptr;
    31 pthread_key_t ThreadPosixImpl::_pt_ptr_index = 0;
    32 bool ThreadPosixImpl::_got_pt_ptr_index = 
false;
    39   if (thread_cat->is_debug()) {
    41       << 
"Deleting thread " << _parent_obj->get_name() << 
"\n";
    47     pthread_detach(_thread);
    58 void ThreadPosixImpl::
    66 bool ThreadPosixImpl::
    67 start(ThreadPriority priority, 
bool joinable) {
    69   if (thread_cat->is_debug()) {
    70     thread_cat.debug() << 
"Starting " << *_parent_obj << 
"\n";
    73   nassertd(_status == S_new) {
    79   _status = S_start_called;
    82   if (!_got_pt_ptr_index) {
    87   pthread_attr_init(&attr);
    90     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    94   int result = pthread_attr_setstacksize(&attr, thread_stack_size);
    97       << 
"Unable to set stack size.\n";
   102   result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
   104     thread_cat->warning()
   105       << 
"Unable to set system scope.\n";
   108   struct sched_param param;
   109   int current_policy = SCHED_OTHER;
   110   result = pthread_attr_setschedpolicy(&attr, current_policy);
   112     thread_cat->warning()
   113       << 
"Unable to set scheduling policy.\n";
   120     param.sched_priority = sched_get_priority_min(current_policy);
   121     result = pthread_attr_setschedparam(&attr, ¶m);
   126     param.sched_priority = sched_get_priority_max(current_policy);
   127     result = pthread_attr_setschedparam(&attr, ¶m);
   136     thread_cat->warning()
   137       << 
"Unable to specify thread priority.\n";
   143   result = pthread_create(&_thread, &attr, &root_func, (
void *)
this);
   145   pthread_attr_destroy(&attr);
   164 void ThreadPosixImpl::
   170     pthread_join(_thread, &return_val);
   180 std::string ThreadPosixImpl::
   181 get_unique_id()
 const {
   182   std::ostringstream strm;
   183   strm << getpid() << 
"." << _thread;
   193 bool ThreadPosixImpl::
   196   std::string thread_name = _parent_obj->get_name();
   197   JavaVMAttachArgs args;
   198   args.version = JNI_VERSION_1_2;
   199   args.name = thread_name.c_str();
   200   args.group = 
nullptr;
   201   if (java_vm->AttachCurrentThread(&env, &args) != 0) {
   203       << 
"Failed to attach Java VM to thread "   204       << _parent_obj->get_name() << 
"!\n";
   216 void ThreadPosixImpl::
   219   nassertv(thread != 
nullptr);
   224   if (java_vm->GetEnv((
void **)&env, JNI_VERSION_1_4) == JNI_OK) {
   225     nassertv(thread->_impl._jni_env == 
nullptr || thread->_impl._jni_env == env);
   226     thread->_impl._jni_env = env;
   229       << 
"Called bind_java_thread() on thread "   230       << *thread << 
", which is not attached to Java VM!\n";
   238 void *ThreadPosixImpl::
   239 root_func(
void *data) {
   240   TAU_REGISTER_THREAD();
   244     ThreadPosixImpl *
self = (ThreadPosixImpl *)data;
   245     int result = pthread_setspecific(_pt_ptr_index, self->_parent_obj);
   246     nassertr(result == 0, 
nullptr);
   250       nassertd(self->_status == S_start_called) {
   251         self->_mutex.unlock();
   255       self->_status = S_running;
   256       self->_mutex.unlock();
   261     self->attach_java_vm();
   264     self->_parent_obj->thread_main();
   266     if (thread_cat->is_debug()) {
   268         << 
"Terminating thread " << 
self->_parent_obj->get_name()
   269         << 
", count = " << 
self->_parent_obj->get_ref_count() << 
"\n";
   274       nassertd(self->_status == S_running) {
   275         self->_mutex.unlock();
   278       self->_status = S_finished;
   279       self->_mutex.unlock();
   284     if (self->_jni_env != 
nullptr) {
   285       java_vm->DetachCurrentThread();
   286       self->_jni_env = 
nullptr;
   303 void ThreadPosixImpl::
   304 init_pt_ptr_index() {
   305   nassertv(!_got_pt_ptr_index);
   307   int result = pthread_key_create(&_pt_ptr_index, 
nullptr);
   310       << 
"Unable to associate Thread pointers with threads.\n";
   314   _got_pt_ptr_index = 
true;
   319   result = pthread_setspecific(_pt_ptr_index, main_thread_obj);
   320   nassertv(result == 0);
   327 jint JNI_OnLoad(JavaVM *jvm, 
void *reserved) {
   331   ThreadPosixImpl::bind_java_thread();
   332   return JNI_VERSION_1_4;
   336 #endif  // THREAD_POSIX_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.
 
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
 
get_current_thread
Returns a pointer to the currently-executing Thread object.
 
get_main_thread
Returns a pointer to the "main" Thread object–this is the Thread that started the whole process.
 
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
 
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
 
A thread; that is, a lightweight process.
 
void unref_delete(RefCountType *ptr)
This global helper function will unref the given ReferenceCount object, and if the reference count re...