645 // create new thread
646
647 // Thread start routine for all newly created threads
648 static void *java_start(Thread *thread) {
649 // Try to randomize the cache line index of hot stack frames.
650 // This helps when threads of the same stack traces evict each other's
651 // cache lines. The threads can be either from the same JVM instance, or
652 // from different JVM instances. The benefit is especially true for
653 // processors with hyperthreading technology.
654 static int counter = 0;
655 int pid = os::current_process_id();
656 alloca(((pid ^ counter++) & 7) * 128);
657
658 thread->initialize_thread_current();
659
660 OSThread* osthread = thread->osthread();
661 Monitor* sync = osthread->startThread_lock();
662
663 osthread->set_thread_id(os::current_thread_id());
664
665 if (UseNUMA) {
666 int lgrp_id = os::numa_get_group_id();
667 if (lgrp_id != -1) {
668 thread->set_lgrp_id(lgrp_id);
669 }
670 }
671 // initialize signal mask for this thread
672 os::Linux::hotspot_sigmask(thread);
673
674 // initialize floating point control register
675 os::Linux::init_thread_fpu_state();
676
677 // handshaking with parent thread
678 {
679 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
680
681 // notify parent thread
682 osthread->set_state(INITIALIZED);
683 sync->notify_all();
684
685 // wait until os::start_thread()
686 while (osthread->get_state() == INITIALIZED) {
687 sync->wait(Mutex::_no_safepoint_check_flag);
688 }
689 }
690
691 // call one more level start routine
692 thread->run();
693
694 return 0;
695 }
696
697 bool os::create_thread(Thread* thread, ThreadType thr_type,
698 size_t stack_size) {
699 assert(thread->osthread() == NULL, "caller responsible");
700
701 // Allocate the OSThread object
702 OSThread* osthread = new OSThread(NULL, NULL);
703 if (osthread == NULL) {
704 return false;
705 }
706
707 // set the correct thread state
708 osthread->set_thread_type(thr_type);
709
710 // Initial state is ALLOCATED but not INITIALIZED
711 osthread->set_state(ALLOCATED);
712
713 thread->set_osthread(osthread);
739 case os::pgc_thread:
740 case os::cgc_thread:
741 case os::watcher_thread:
742 if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
743 break;
744 }
745 }
746
747 stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);
748 pthread_attr_setstacksize(&attr, stack_size);
749
750 // glibc guard page
751 pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
752
753 ThreadState state;
754
755 {
756 pthread_t tid;
757 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
758
759 pthread_attr_destroy(&attr);
760
761 if (ret != 0) {
762 if (PrintMiscellaneous && (Verbose || WizardMode)) {
763 perror("pthread_create()");
764 }
765 // Need to clean up stuff we've allocated so far
766 thread->set_osthread(NULL);
767 delete osthread;
768 return false;
769 }
770
771 // Store pthread info into the OSThread
772 osthread->set_pthread_id(tid);
773
774 // Wait until child thread is either initialized or aborted
775 {
776 Monitor* sync_with_child = osthread->startThread_lock();
777 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
778 while ((state = osthread->get_state()) == ALLOCATED) {
779 sync_with_child->wait(Mutex::_no_safepoint_check_flag);
780 }
781 }
782 }
783
784 // Aborted due to thread limit being reached
840 // the entire stack region to avoid SEGV in stack banging.
841 // It is also useful to get around the heap-stack-gap problem on SuSE
842 // kernel (see 4821821 for details). We first expand stack to the top
843 // of yellow zone, then enable stack yellow zone (order is significant,
844 // enabling yellow zone first will crash JVM on SuSE Linux), so there
845 // is no gap between the last two virtual memory regions.
846
847 JavaThread *jt = (JavaThread *)thread;
848 address addr = jt->stack_reserved_zone_base();
849 assert(addr != NULL, "initialization problem?");
850 assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
851
852 osthread->set_expanding_stack();
853 os::Linux::manually_expand_stack(jt, addr);
854 osthread->clear_expanding_stack();
855 }
856
857 // initialize signal mask for this thread
858 // and save the caller's signal mask
859 os::Linux::hotspot_sigmask(thread);
860
861 return true;
862 }
863
864 void os::pd_start_thread(Thread* thread) {
865 OSThread * osthread = thread->osthread();
866 assert(osthread->get_state() != INITIALIZED, "just checking");
867 Monitor* sync_with_child = osthread->startThread_lock();
868 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
869 sync_with_child->notify();
870 }
871
872 // Free Linux resources related to the OSThread
873 void os::free_thread(OSThread* osthread) {
874 assert(osthread != NULL, "osthread not set");
875
876 if (Thread::current()->osthread() == osthread) {
877 // Restore caller's signal mask
878 sigset_t sigmask = osthread->caller_sigmask();
879 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
|
645 // create new thread
646
647 // Thread start routine for all newly created threads
648 static void *java_start(Thread *thread) {
649 // Try to randomize the cache line index of hot stack frames.
650 // This helps when threads of the same stack traces evict each other's
651 // cache lines. The threads can be either from the same JVM instance, or
652 // from different JVM instances. The benefit is especially true for
653 // processors with hyperthreading technology.
654 static int counter = 0;
655 int pid = os::current_process_id();
656 alloca(((pid ^ counter++) & 7) * 128);
657
658 thread->initialize_thread_current();
659
660 OSThread* osthread = thread->osthread();
661 Monitor* sync = osthread->startThread_lock();
662
663 osthread->set_thread_id(os::current_thread_id());
664
665 log_debug(os)("Thread is alive (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
666 os::current_thread_id(), (uintx) pthread_self());
667
668 if (UseNUMA) {
669 int lgrp_id = os::numa_get_group_id();
670 if (lgrp_id != -1) {
671 thread->set_lgrp_id(lgrp_id);
672 }
673 }
674 // initialize signal mask for this thread
675 os::Linux::hotspot_sigmask(thread);
676
677 // initialize floating point control register
678 os::Linux::init_thread_fpu_state();
679
680 // handshaking with parent thread
681 {
682 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
683
684 // notify parent thread
685 osthread->set_state(INITIALIZED);
686 sync->notify_all();
687
688 // wait until os::start_thread()
689 while (osthread->get_state() == INITIALIZED) {
690 sync->wait(Mutex::_no_safepoint_check_flag);
691 }
692 }
693
694 // call one more level start routine
695 thread->run();
696
697 log_debug(os)("Thread finished (tid " UINTX_FORMAT ", pthread id " UINTX_FORMAT ").",
698 os::current_thread_id(), (uintx) pthread_self());
699
700 return 0;
701 }
702
703 bool os::create_thread(Thread* thread, ThreadType thr_type,
704 size_t stack_size) {
705 assert(thread->osthread() == NULL, "caller responsible");
706
707 // Allocate the OSThread object
708 OSThread* osthread = new OSThread(NULL, NULL);
709 if (osthread == NULL) {
710 return false;
711 }
712
713 // set the correct thread state
714 osthread->set_thread_type(thr_type);
715
716 // Initial state is ALLOCATED but not INITIALIZED
717 osthread->set_state(ALLOCATED);
718
719 thread->set_osthread(osthread);
745 case os::pgc_thread:
746 case os::cgc_thread:
747 case os::watcher_thread:
748 if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
749 break;
750 }
751 }
752
753 stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);
754 pthread_attr_setstacksize(&attr, stack_size);
755
756 // glibc guard page
757 pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
758
759 ThreadState state;
760
761 {
762 pthread_t tid;
763 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
764
765 LogHandle(os) log;
766 if (log.is_debug()) {
767 char buf[64];
768 if (ret == 0) {
769 log.debug("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
770 (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
771 } else {
772 log.warning("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
773 os::errno_name(errno), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
774 }
775 }
776
777 pthread_attr_destroy(&attr);
778
779 if (ret != 0) {
780 // Need to clean up stuff we've allocated so far
781 thread->set_osthread(NULL);
782 delete osthread;
783 return false;
784 }
785
786 // Store pthread info into the OSThread
787 osthread->set_pthread_id(tid);
788
789 // Wait until child thread is either initialized or aborted
790 {
791 Monitor* sync_with_child = osthread->startThread_lock();
792 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
793 while ((state = osthread->get_state()) == ALLOCATED) {
794 sync_with_child->wait(Mutex::_no_safepoint_check_flag);
795 }
796 }
797 }
798
799 // Aborted due to thread limit being reached
855 // the entire stack region to avoid SEGV in stack banging.
856 // It is also useful to get around the heap-stack-gap problem on SuSE
857 // kernel (see 4821821 for details). We first expand stack to the top
858 // of yellow zone, then enable stack yellow zone (order is significant,
859 // enabling yellow zone first will crash JVM on SuSE Linux), so there
860 // is no gap between the last two virtual memory regions.
861
862 JavaThread *jt = (JavaThread *)thread;
863 address addr = jt->stack_reserved_zone_base();
864 assert(addr != NULL, "initialization problem?");
865 assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
866
867 osthread->set_expanding_stack();
868 os::Linux::manually_expand_stack(jt, addr);
869 osthread->clear_expanding_stack();
870 }
871
872 // initialize signal mask for this thread
873 // and save the caller's signal mask
874 os::Linux::hotspot_sigmask(thread);
875
876 log_debug(os)("Thread attached (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
877 os::current_thread_id(), (uintx) pthread_self());
878
879 return true;
880 }
881
882 void os::pd_start_thread(Thread* thread) {
883 OSThread * osthread = thread->osthread();
884 assert(osthread->get_state() != INITIALIZED, "just checking");
885 Monitor* sync_with_child = osthread->startThread_lock();
886 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
887 sync_with_child->notify();
888 }
889
890 // Free Linux resources related to the OSThread
891 void os::free_thread(OSThread* osthread) {
892 assert(osthread != NULL, "osthread not set");
893
894 if (Thread::current()->osthread() == osthread) {
895 // Restore caller's signal mask
896 sigset_t sigmask = osthread->caller_sigmask();
897 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
|