629 pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
630 return true;
631 }
632 return false;
633 }
634
635 //////////////////////////////////////////////////////////////////////////////
636 // create new thread
637
638 // Thread start routine for all newly created threads
639 static void *java_start(Thread *thread) {
640 // Try to randomize the cache line index of hot stack frames.
641 // This helps when threads of the same stack traces evict each other's
642 // cache lines. The threads can be either from the same JVM instance, or
643 // from different JVM instances. The benefit is especially true for
644 // processors with hyperthreading technology.
645 static int counter = 0;
646 int pid = os::current_process_id();
647 alloca(((pid ^ counter++) & 7) * 128);
648
649 ThreadLocalStorage::set_thread(thread);
650
651 OSThread* osthread = thread->osthread();
652 Monitor* sync = osthread->startThread_lock();
653
654 osthread->set_thread_id(os::current_thread_id());
655
656 if (UseNUMA) {
657 int lgrp_id = os::numa_get_group_id();
658 if (lgrp_id != -1) {
659 thread->set_lgrp_id(lgrp_id);
660 }
661 }
662 // initialize signal mask for this thread
663 os::Linux::hotspot_sigmask(thread);
664
665 // initialize floating point control register
666 os::Linux::init_thread_fpu_state();
667
668 // handshaking with parent thread
669 {
854
855 void os::pd_start_thread(Thread* thread) {
856 OSThread * osthread = thread->osthread();
857 assert(osthread->get_state() != INITIALIZED, "just checking");
858 Monitor* sync_with_child = osthread->startThread_lock();
859 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
860 sync_with_child->notify();
861 }
862
863 // Free Linux resources related to the OSThread
864 void os::free_thread(OSThread* osthread) {
865 assert(osthread != NULL, "osthread not set");
866
867 if (Thread::current()->osthread() == osthread) {
868 // Restore caller's signal mask
869 sigset_t sigmask = osthread->caller_sigmask();
870 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
871 }
872
873 delete osthread;
874 }
875
876 //////////////////////////////////////////////////////////////////////////////
877 // thread local storage
878
879 // Restore the thread pointer if the destructor is called. This is in case
880 // someone from JNI code sets up a destructor with pthread_key_create to run
881 // detachCurrentThread on thread death. Unless we restore the thread pointer we
882 // will hang or crash. When detachCurrentThread is called the key will be set
883 // to null and we will not be called again. If detachCurrentThread is never
884 // called we could loop forever depending on the pthread implementation.
885 static void restore_thread_pointer(void* p) {
886 Thread* thread = (Thread*) p;
887 os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
888 }
889
890 int os::allocate_thread_local_storage() {
891 pthread_key_t key;
892 int rslt = pthread_key_create(&key, restore_thread_pointer);
893 assert(rslt == 0, "cannot allocate thread local storage");
894 return (int)key;
895 }
896
897 // Note: This is currently not used by VM, as we don't destroy TLS key
898 // on VM exit.
899 void os::free_thread_local_storage(int index) {
900 int rslt = pthread_key_delete((pthread_key_t)index);
901 assert(rslt == 0, "invalid index");
902 }
903
904 void os::thread_local_storage_at_put(int index, void* value) {
905 int rslt = pthread_setspecific((pthread_key_t)index, value);
906 assert(rslt == 0, "pthread_setspecific failed");
907 }
908
909 extern "C" Thread* get_thread() {
910 return ThreadLocalStorage::thread();
911 }
912
913 //////////////////////////////////////////////////////////////////////////////
914 // initial thread
915
916 // Check if current thread is the initial thread, similar to Solaris thr_main.
917 bool os::Linux::is_initial_thread(void) {
918 char dummy;
919 // If called before init complete, thread stack bottom will be null.
920 // Can be called if fatal error occurs before initialization.
921 if (initial_thread_stack_bottom() == NULL) return false;
922 assert(initial_thread_stack_bottom() != NULL &&
923 initial_thread_stack_size() != 0,
924 "os::init did not locate initial thread's stack region");
925 if ((address)&dummy >= initial_thread_stack_bottom() &&
926 (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size()) {
927 return true;
928 } else {
929 return false;
930 }
|
629 pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
630 return true;
631 }
632 return false;
633 }
634
635 //////////////////////////////////////////////////////////////////////////////
636 // create new thread
637
638 // Thread start routine for all newly created threads
639 static void *java_start(Thread *thread) {
640 // Try to randomize the cache line index of hot stack frames.
641 // This helps when threads of the same stack traces evict each other's
642 // cache lines. The threads can be either from the same JVM instance, or
643 // from different JVM instances. The benefit is especially true for
644 // processors with hyperthreading technology.
645 static int counter = 0;
646 int pid = os::current_process_id();
647 alloca(((pid ^ counter++) & 7) * 128);
648
649 thread->initialize_thread_current();
650
651 OSThread* osthread = thread->osthread();
652 Monitor* sync = osthread->startThread_lock();
653
654 osthread->set_thread_id(os::current_thread_id());
655
656 if (UseNUMA) {
657 int lgrp_id = os::numa_get_group_id();
658 if (lgrp_id != -1) {
659 thread->set_lgrp_id(lgrp_id);
660 }
661 }
662 // initialize signal mask for this thread
663 os::Linux::hotspot_sigmask(thread);
664
665 // initialize floating point control register
666 os::Linux::init_thread_fpu_state();
667
668 // handshaking with parent thread
669 {
854
855 void os::pd_start_thread(Thread* thread) {
856 OSThread * osthread = thread->osthread();
857 assert(osthread->get_state() != INITIALIZED, "just checking");
858 Monitor* sync_with_child = osthread->startThread_lock();
859 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
860 sync_with_child->notify();
861 }
862
863 // Free Linux resources related to the OSThread
864 void os::free_thread(OSThread* osthread) {
865 assert(osthread != NULL, "osthread not set");
866
867 if (Thread::current()->osthread() == osthread) {
868 // Restore caller's signal mask
869 sigset_t sigmask = osthread->caller_sigmask();
870 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
871 }
872
873 delete osthread;
874 }
875
876 //////////////////////////////////////////////////////////////////////////////
877 // initial thread
878
879 // Check if current thread is the initial thread, similar to Solaris thr_main.
880 bool os::Linux::is_initial_thread(void) {
881 char dummy;
882 // If called before init complete, thread stack bottom will be null.
883 // Can be called if fatal error occurs before initialization.
884 if (initial_thread_stack_bottom() == NULL) return false;
885 assert(initial_thread_stack_bottom() != NULL &&
886 initial_thread_stack_size() != 0,
887 "os::init did not locate initial thread's stack region");
888 if ((address)&dummy >= initial_thread_stack_bottom() &&
889 (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size()) {
890 return true;
891 } else {
892 return false;
893 }
|