809 // http://sourceware.org/bugzilla/show_bug.cgi?id=11787.
810 //
811 // As a workaround, we call a private but assumed-stable glibc function,
812 // __pthread_get_minstack() to obtain the minstack size and derive the
813 // static TLS size from it. We then increase the user requested stack
814 // size by this TLS size.
815 //
816 // Due to compatibility concerns, this size adjustment is opt-in and
817 // controlled via AdjustStackSizeForTLS.
818 typedef size_t (*GetMinStack)(const pthread_attr_t *attr);
819
820 GetMinStack _get_minstack_func = NULL;
821
822 static void get_minstack_init() {
823 _get_minstack_func =
824 (GetMinStack)dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
825 log_info(os, thread)("Lookup of __pthread_get_minstack %s",
826 _get_minstack_func == NULL ? "failed" : "succeeded");
827 }
828
829 // Returns the size of the static TLS area glibc puts on thread stacks.
830 static size_t get_static_tls_area_size(const pthread_attr_t *attr) {
831 size_t tls_size = 0;
832 if (_get_minstack_func != NULL) {
833 // Obtain the pthread minstack size by calling __pthread_get_minstack.
834 size_t minstack_size = _get_minstack_func(attr);
835
836 // Remove non-TLS area size included in minstack size returned
837 // by __pthread_get_minstack() to get the static TLS size.
838 // In glibc before 2.27, minstack size includes guard_size.
839 // In glibc 2.27 and later, guard_size is automatically added
840 // to the stack size by pthread_create and is no longer included
841 // in minstack size. In both cases, the guard_size is taken into
842 // account, so there is no need to adjust the result for that.
843 //
844 // Although __pthread_get_minstack() is a private glibc function,
845 // it is expected to have a stable behavior across future glibc
846 // versions while glibc still allocats the static TLS blocks off
847 // the stack. Following is glibc 2.28 __pthread_get_minstack():
848 //
849 // size_t
850 // __pthread_get_minstack (const pthread_attr_t *attr)
851 // {
852 // return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
853 // }
854 //
855 //
856 // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN'
857 // if check is done for precaution.
858 if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN) {
859 tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN;
860 }
861 }
862
863 log_info(os, thread)("Stack size adjustment for TLS is " SIZE_FORMAT,
864 tls_size);
865 return tls_size;
866 }
867
868 bool os::create_thread(Thread* thread, ThreadType thr_type,
869 size_t req_stack_size) {
870 assert(thread->osthread() == NULL, "caller responsible");
871
872 // Allocate the OSThread object
873 OSThread* osthread = new OSThread(NULL, NULL);
874 if (osthread == NULL) {
875 return false;
876 }
877
878 // set the correct thread state
879 osthread->set_thread_type(thr_type);
|
809 // http://sourceware.org/bugzilla/show_bug.cgi?id=11787.
810 //
811 // As a workaround, we call a private but assumed-stable glibc function,
812 // __pthread_get_minstack() to obtain the minstack size and derive the
813 // static TLS size from it. We then increase the user requested stack
814 // size by this TLS size.
815 //
816 // Due to compatibility concerns, this size adjustment is opt-in and
817 // controlled via AdjustStackSizeForTLS.
818 typedef size_t (*GetMinStack)(const pthread_attr_t *attr);
819
820 GetMinStack _get_minstack_func = NULL;
821
822 static void get_minstack_init() {
823 _get_minstack_func =
824 (GetMinStack)dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
825 log_info(os, thread)("Lookup of __pthread_get_minstack %s",
826 _get_minstack_func == NULL ? "failed" : "succeeded");
827 }
828
829 static bool tls_size_inited = false;
830 static size_t tls_size = 0;
831 // Returns the size of the static TLS area glibc puts on thread stacks.
832 static size_t get_static_tls_area_size(const pthread_attr_t *attr) {
833 if (!tls_size_inited) {
834 tls_size_inited = true;
835
836 if (_get_minstack_func != NULL) {
837 // Obtain the pthread minstack size by calling __pthread_get_minstack.
838 size_t minstack_size = _get_minstack_func(attr);
839
840 // Remove non-TLS area size included in minstack size returned
841 // by __pthread_get_minstack() to get the static TLS size.
842 // In glibc before 2.27, minstack size includes guard_size.
843 // In glibc 2.27 and later, guard_size is automatically added
844 // to the stack size by pthread_create and is no longer included
845 // in minstack size. In both cases, the guard_size is taken into
846 // account, so there is no need to adjust the result for that.
847 //
848 // Although __pthread_get_minstack() is a private glibc function,
849 // it is expected to have a stable behavior across future glibc
850 // versions while glibc still allocats the static TLS blocks off
851 // the stack. Following is glibc 2.28 __pthread_get_minstack():
852 //
853 // size_t
854 // __pthread_get_minstack (const pthread_attr_t *attr)
855 // {
856 // return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
857 // }
858 //
859 //
860 // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN'
861 // if check is done for precaution.
862 if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN) {
863 tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN;
864 }
865 }
866 }
867
868 log_info(os, thread)("Stack size adjustment for TLS is " SIZE_FORMAT,
869 tls_size);
870 return tls_size;
871 }
872
873 bool os::create_thread(Thread* thread, ThreadType thr_type,
874 size_t req_stack_size) {
875 assert(thread->osthread() == NULL, "caller responsible");
876
877 // Allocate the OSThread object
878 OSThread* osthread = new OSThread(NULL, NULL);
879 if (osthread == NULL) {
880 return false;
881 }
882
883 // set the correct thread state
884 osthread->set_thread_type(thr_type);
|