diff --git a/src/os/bsd/vm/osThread_bsd.hpp b/src/os/bsd/vm/osThread_bsd.hpp index fe903eb58d242c07eacd2d66203487d6ee7281ca..f4553821267b03329f58438c549e6323af80a59b 100644 --- a/src/os/bsd/vm/osThread_bsd.hpp +++ b/src/os/bsd/vm/osThread_bsd.hpp @@ -42,7 +42,7 @@ #ifdef __APPLE__ typedef thread_t thread_id_t; #else - typedef pthread_t thread_id_t; + typedef pid_t thread_id_t; #endif // _pthread_id is the pthread id, which is used by library calls diff --git a/src/os/bsd/vm/os_bsd.cpp b/src/os/bsd/vm/os_bsd.cpp index c075ec27c76fc03b3fb68987d0945814bfedc430..fe0ef89d99b1c24c160d896bf1369fde5097fc45 100644 --- a/src/os/bsd/vm/os_bsd.cpp +++ b/src/os/bsd/vm/os_bsd.cpp @@ -691,18 +691,12 @@ static void *java_start(Thread *thread) { return NULL; } -#ifdef __APPLE__ - // thread_id is mach thread on macos, which pthreads graciously caches and provides for us - mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self()); - guarantee(thread_id != 0, "thread id missing from pthreads"); - osthread->set_thread_id(thread_id); + osthread->set_thread_id(os::Bsd::gettid()); - uint64_t unique_thread_id = locate_unique_thread_id(thread_id); +#ifdef __APPLE__ + uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id()); guarantee(unique_thread_id != 0, "unique thread id was not found"); osthread->set_unique_thread_id(unique_thread_id); -#else - // thread_id is pthread_id on BSD - osthread->set_thread_id(::pthread_self()); #endif // initialize signal mask for this thread os::Bsd::hotspot_sigmask(thread); @@ -859,18 +853,13 @@ bool os::create_attached_thread(JavaThread* thread) { return false; } + osthread->set_thread_id(os::Bsd::gettid()); + // Store pthread info into the OSThread #ifdef __APPLE__ - // thread_id is mach thread on macos, which pthreads graciously caches and provides for us - mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self()); - guarantee(thread_id != 0, "just checking"); - osthread->set_thread_id(thread_id); - - uint64_t unique_thread_id = locate_unique_thread_id(thread_id); + uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id()); guarantee(unique_thread_id != 0, "just checking"); osthread->set_unique_thread_id(unique_thread_id); -#else - osthread->set_thread_id(::pthread_self()); #endif osthread->set_pthread_id(::pthread_self()); @@ -1137,6 +1126,30 @@ size_t os::lasterror(char *buf, size_t len) { return n; } +// Information of current thread in variety of formats +pid_t os::Bsd::gettid() { + int retval = -1; + +#ifdef __APPLE__ //XNU kernel + // despite the fact mach port is actually not a thread id use it + // instead of syscall(SYS_thread_selfid) as it certainly fits to u4 + retval = ::pthread_mach_thread_np(::pthread_self()); + guarantee(retval != 0, "just checking"); + return retval; + +#elif __FreeBSD__ + retval = syscall(SYS_thr_self); +#elif __OpenBSD__ + retval = syscall(SYS_getthrid); +#elif __NetBSD__ + retval = (pid_t) syscall(SYS__lwp_self); +#endif + + if (retval == -1) { + return getpid(); + } +} + intx os::current_thread_id() { #ifdef __APPLE__ return (intx)::pthread_mach_thread_np(::pthread_self()); @@ -1144,6 +1157,7 @@ intx os::current_thread_id() { return (intx)::pthread_self(); #endif } + int os::current_process_id() { // Under the old bsd thread library, bsd gives each thread diff --git a/src/os/bsd/vm/os_bsd.hpp b/src/os/bsd/vm/os_bsd.hpp index f18bb88637f2d2ff2cf63715943114bea7892f4f..a906a30e97fd888a3d35f88a7beb67aa9e95b860 100644 --- a/src/os/bsd/vm/os_bsd.hpp +++ b/src/os/bsd/vm/os_bsd.hpp @@ -84,6 +84,7 @@ class Bsd { static void hotspot_sigmask(Thread* thread); static bool is_initial_thread(void); + static pid_t gettid(); static int page_size(void) { return _page_size; } static void set_page_size(int val) { _page_size = val; }