8022616: u4 should not be used as a type for thread_id
authordsamersoff
Thu, 03 Oct 2013 12:39:58 +0400
changeset 20377 393ee12eccc8
parent 20376 b24f0afe8735
child 20378 a296c8e1c5d2
child 20379 a4c59d30d67d
child 20381 ed1047a3ac21
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
hotspot/src/os/bsd/vm/osThread_bsd.hpp
hotspot/src/os/bsd/vm/os_bsd.cpp
hotspot/src/os/bsd/vm/os_bsd.hpp
--- a/hotspot/src/os/bsd/vm/osThread_bsd.hpp	Thu Oct 03 04:42:57 2013 +0200
+++ b/hotspot/src/os/bsd/vm/osThread_bsd.hpp	Thu Oct 03 12:39:58 2013 +0400
@@ -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
--- a/hotspot/src/os/bsd/vm/os_bsd.cpp	Thu Oct 03 04:42:57 2013 +0200
+++ b/hotspot/src/os/bsd/vm/os_bsd.cpp	Thu Oct 03 12:39:58 2013 +0400
@@ -691,18 +691,12 @@
     return NULL;
   }
 
+  osthread->set_thread_id(os::Bsd::gettid());
+
 #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);
-
-  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, "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 @@
     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 @@
   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 @@
   return (intx)::pthread_self();
 #endif
 }
+
 int os::current_process_id() {
 
   // Under the old bsd thread library, bsd gives each thread
--- a/hotspot/src/os/bsd/vm/os_bsd.hpp	Thu Oct 03 04:42:57 2013 +0200
+++ b/hotspot/src/os/bsd/vm/os_bsd.hpp	Thu Oct 03 12:39:58 2013 +0400
@@ -84,6 +84,7 @@
   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; }