]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/thread: Prune a little more
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:48 +0000 (11:23 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:48 +0000 (11:23 -0700)
Remove automatic VThread_SetName for unmanaged threads.

Other minor removals and tweaks:
- modify "safe" name in VThreadBase to use gettid or
  pthread_threadid_np if available
- Move lib/thread name computation to child thread to remove dependency
  on VThreadID in parent.
- Convert VThreadFreeID to VThreadFree (and avoid a lookup)
- Move VThreadBase_ForgetSelf to VThreadDestroyThread to
  make VThreadHostDestroyThread straightforward.
- The 'priority' field had become write-only after earlier cleanups
  to replace AdjustThreadPriority with SetThreadPriority. Remove it,
  and some dead comments mentioning it.
- Remove unused 'signalNestCount' atomic.
- Remove two unimplemented VThreadInt prototypes.
- Remove a vacuously-true ASSERT in VThreadHostExitThread()

open-vm-tools/lib/include/vthreadBase.h
open-vm-tools/lib/misc/vthreadBase.c

index 5b1e6f423e0e00fdbef1fbd067fd866e910118c1..ff14cc3334dc1a6316fe21950b33e5cc25952907 100644 (file)
@@ -39,7 +39,6 @@
 #if !defined VMM
 
 #if !defined WIN32
-#define VTHREAD_USE_PTHREAD 1
 #include <signal.h>
 #endif
 
index 30e6080ce0ff02dbcad2a2dcb460c52fe418d4a3..ca2e79b22ee779f278585392da5910e97426de5c 100644 (file)
@@ -81,7 +81,7 @@
 #if defined _WIN32
 #  include <windows.h>
 #else
-#  if defined(sun) && !defined(_XOPEN_SOURCE)
+#  if defined(__sun__) && !defined(_XOPEN_SOURCE)
      /*
       * Solaris headers don't define constants we need unless
       * the Posix standard is somewhat modern.  Most of our builds
@@ -89,6 +89,9 @@
       */
 #    define _XOPEN_SOURCE 500
 #  endif
+#  if defined __linux__
+#    include <sys/syscall.h>   // for gettid(2)
+#  endif
 #  include <pthread.h>
 #  include <signal.h>
 #  include <errno.h>
@@ -678,6 +681,56 @@ VThreadBase_CurID(void)
 }
 
 
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * VThreadBaseNativeID --
+ *
+ *      Native thread-id function.
+ *
+ *      Most OSes tend to use 'small' numbers (32-bit in practice),
+ *      these IDs are more readable in log files than e.g. a pointer.
+ *
+ *      TODO: merge with Util_GetCurrentThreadId()
+ *
+ * Results:
+ *      Some sort of ID.
+ *
+ * Side effects:
+ *      None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+static uint64
+VThreadBaseNativeID(void)
+{
+#if defined(_WIN32)
+   return GetCurrentThreadId();
+#elif defined(__APPLE__)
+   /* Available as of 10.6 */
+   uint64 hostTid;
+   pthread_threadid_np(NULL /* current thread */, &hostTid);
+   return hostTid;
+#elif defined(__ANDROID__)
+   return gettid();
+#elif defined(VMX86_SERVER) || defined(__linux__)
+   return syscall(__NR_gettid);  // pid_t, 32-bit, is the LWP
+#elif defined(__sun__)
+   return pthread_self();  // uint_t, Solaris uses LWP as pthread_t
+#elif defined(__FreeBSD__)
+#  if 0
+   // Requires FreeBSD 9, we currently use FreeBSD 6.
+   return pthread_getthreadid_np();   // must include <pthread_np.h>
+#  endif
+   return (uint64)(uintptr_t)(void *)pthread_self();
+#else
+   // pthread_self() is available, but cannot be safely cast...
+#  error "Unknown platform"
+#endif
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  *
@@ -705,19 +758,8 @@ VThreadBaseSafeName(char *buf,   // OUT: new name
     * function (that can ASSERT or Panic), as the Panic handler is
     * very likey to query the thread name and end up right back here.
     */
-   uintptr_t hostTid;
-
-#if defined(_WIN32)
-   hostTid = GetCurrentThreadId();
-#elif defined(__linux__)
-   hostTid = pthread_self();
-#elif defined(__APPLE__)
-   hostTid = (uintptr_t)(void*)pthread_self();
-#else
-   hostTid = 0;
-#endif
    snprintf(buf, len - 1 /* keep buffer NUL-terminated */,
-            "host-%"FMTPD"u", hostTid);
+            "host-%"FMT64"u", VThreadBaseNativeID());
 }
 
 
@@ -790,8 +832,7 @@ VThreadBase_CurName(void)
  * VThreadBase_InitWithTLS --
  *
  *      (Atomic) VThreadBase initialization, using caller-managed memory.
- *      Gets the VThreadID and thread name from within that caller-managed
- *      memory, so both must be populated.
+ *      Gets the VThreadID from within that caller-managed memory.
  *
  *      Always "succeeds" in initialization; the return value is useful
  *      as an indication of whether this was the first initialization.
@@ -1150,7 +1191,6 @@ static void
 VThreadBaseSimpleNoID(void)
 {
    VThreadID newID;
-   char newName[VTHREADBASE_MAX_NAME];
    Bool reused = FALSE;
    Bool result;
    void *newNative = VThreadBaseGetNative();
@@ -1209,10 +1249,8 @@ VThreadBaseSimpleNoID(void)
    /* ID picked.  Now do the important stuff. */
    base = Util_SafeCalloc(1, sizeof *base);
    base->id = newID;
-   Str_Sprintf(newName, sizeof newName, "vthread-%" FMT64 "u", newID);
 
    result = VThreadBase_InitWithTLS(base);
-   VThreadBase_SetName(newName);
    ASSERT(result);
 
    if (vmx86_debug && reused) {