Thu, 03 Oct 2013 12:39:58 +0400
8022616: u4 should not be used as a type for thread_id
Summary: Usage of u4 as a type for thread_id cause a compilation error on platform, where thread_id is a pointer
Reviewed-by: sla, sspitsyn, minqi
1.1 --- a/src/os/bsd/vm/osThread_bsd.hpp Thu Oct 03 04:42:57 2013 +0200 1.2 +++ b/src/os/bsd/vm/osThread_bsd.hpp Thu Oct 03 12:39:58 2013 +0400 1.3 @@ -42,7 +42,7 @@ 1.4 #ifdef __APPLE__ 1.5 typedef thread_t thread_id_t; 1.6 #else 1.7 - typedef pthread_t thread_id_t; 1.8 + typedef pid_t thread_id_t; 1.9 #endif 1.10 1.11 // _pthread_id is the pthread id, which is used by library calls
2.1 --- a/src/os/bsd/vm/os_bsd.cpp Thu Oct 03 04:42:57 2013 +0200 2.2 +++ b/src/os/bsd/vm/os_bsd.cpp Thu Oct 03 12:39:58 2013 +0400 2.3 @@ -691,18 +691,12 @@ 2.4 return NULL; 2.5 } 2.6 2.7 + osthread->set_thread_id(os::Bsd::gettid()); 2.8 + 2.9 #ifdef __APPLE__ 2.10 - // thread_id is mach thread on macos, which pthreads graciously caches and provides for us 2.11 - mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self()); 2.12 - guarantee(thread_id != 0, "thread id missing from pthreads"); 2.13 - osthread->set_thread_id(thread_id); 2.14 - 2.15 - uint64_t unique_thread_id = locate_unique_thread_id(thread_id); 2.16 + uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id()); 2.17 guarantee(unique_thread_id != 0, "unique thread id was not found"); 2.18 osthread->set_unique_thread_id(unique_thread_id); 2.19 -#else 2.20 - // thread_id is pthread_id on BSD 2.21 - osthread->set_thread_id(::pthread_self()); 2.22 #endif 2.23 // initialize signal mask for this thread 2.24 os::Bsd::hotspot_sigmask(thread); 2.25 @@ -859,18 +853,13 @@ 2.26 return false; 2.27 } 2.28 2.29 + osthread->set_thread_id(os::Bsd::gettid()); 2.30 + 2.31 // Store pthread info into the OSThread 2.32 #ifdef __APPLE__ 2.33 - // thread_id is mach thread on macos, which pthreads graciously caches and provides for us 2.34 - mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self()); 2.35 - guarantee(thread_id != 0, "just checking"); 2.36 - osthread->set_thread_id(thread_id); 2.37 - 2.38 - uint64_t unique_thread_id = locate_unique_thread_id(thread_id); 2.39 + uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id()); 2.40 guarantee(unique_thread_id != 0, "just checking"); 2.41 osthread->set_unique_thread_id(unique_thread_id); 2.42 -#else 2.43 - osthread->set_thread_id(::pthread_self()); 2.44 #endif 2.45 osthread->set_pthread_id(::pthread_self()); 2.46 2.47 @@ -1137,6 +1126,30 @@ 2.48 return n; 2.49 } 2.50 2.51 +// Information of current thread in variety of formats 2.52 +pid_t os::Bsd::gettid() { 2.53 + int retval = -1; 2.54 + 2.55 +#ifdef __APPLE__ //XNU kernel 2.56 + // despite the fact mach port is actually not a thread id use it 2.57 + // instead of syscall(SYS_thread_selfid) as it certainly fits to u4 2.58 + retval = ::pthread_mach_thread_np(::pthread_self()); 2.59 + guarantee(retval != 0, "just checking"); 2.60 + return retval; 2.61 + 2.62 +#elif __FreeBSD__ 2.63 + retval = syscall(SYS_thr_self); 2.64 +#elif __OpenBSD__ 2.65 + retval = syscall(SYS_getthrid); 2.66 +#elif __NetBSD__ 2.67 + retval = (pid_t) syscall(SYS__lwp_self); 2.68 +#endif 2.69 + 2.70 + if (retval == -1) { 2.71 + return getpid(); 2.72 + } 2.73 +} 2.74 + 2.75 intx os::current_thread_id() { 2.76 #ifdef __APPLE__ 2.77 return (intx)::pthread_mach_thread_np(::pthread_self()); 2.78 @@ -1144,6 +1157,7 @@ 2.79 return (intx)::pthread_self(); 2.80 #endif 2.81 } 2.82 + 2.83 int os::current_process_id() { 2.84 2.85 // Under the old bsd thread library, bsd gives each thread
3.1 --- a/src/os/bsd/vm/os_bsd.hpp Thu Oct 03 04:42:57 2013 +0200 3.2 +++ b/src/os/bsd/vm/os_bsd.hpp Thu Oct 03 12:39:58 2013 +0400 3.3 @@ -84,6 +84,7 @@ 3.4 static void hotspot_sigmask(Thread* thread); 3.5 3.6 static bool is_initial_thread(void); 3.7 + static pid_t gettid(); 3.8 3.9 static int page_size(void) { return _page_size; } 3.10 static void set_page_size(int val) { _page_size = val; }