[beast: 7/20] SFI: provide and use this_thread_*() for name/getpid/gettid/online_cpus



commit 1f2d3b60b1866a7320d8ae85f40e4e9fde32b2fb
Author: Tim Janik <timj gnu org>
Date:   Sun Sep 17 03:01:36 2017 +0200

    SFI: provide and use this_thread_*() for name/getpid/gettid/online_cpus
    
    Signed-off-by: Tim Janik <timj gnu org>

 sfi/bcore.hh    |    1 -
 sfi/entropy.cc  |    2 +-
 sfi/platform.cc |   52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 sfi/platform.hh |    9 ++++++++-
 4 files changed, 57 insertions(+), 7 deletions(-)
---
diff --git a/sfi/bcore.hh b/sfi/bcore.hh
index 3265a82..157d693 100644
--- a/sfi/bcore.hh
+++ b/sfi/bcore.hh
@@ -36,7 +36,6 @@ using   Rapicorn::Res;
 using   Rapicorn::ThreadInfo;
 using   Rapicorn::cpu_info;
 using   Rapicorn::AsyncBlockingQueue;
-namespace ThisThread = Rapicorn::ThisThread;
 
 // == Diagnostics ==
 template<class... Args> String      string_format        (const char *format, const Args &...args) 
BSE_PRINTF (1, 0);
diff --git a/sfi/entropy.cc b/sfi/entropy.cc
index 45dc7fb..a57b4cf 100644
--- a/sfi/entropy.cc
+++ b/sfi/entropy.cc
@@ -285,7 +285,7 @@ runtime_entropy (KeccakRng &pool)
   hash_time (stamp++);  *uintp++ = timestamp_benchmark();
   hash_time (stamp++);  get_rdrand (uintp, 8); uintp += 8;
   hash_time (stamp++);  get_arc4random (uintp, 8); uintp += 8;
-  hash_time (stamp++);  *uintp++ = ThisThread::thread_pid();
+  hash_time (stamp++);  *uintp++ = this_thread_gettid();
   hash_time (stamp++);  *uintp++ = getuid();
   hash_time (stamp++);  *uintp++ = geteuid();
   hash_time (stamp++);  *uintp++ = getgid();
diff --git a/sfi/platform.cc b/sfi/platform.cc
index 8a73179..7dc10a4 100644
--- a/sfi/platform.cc
+++ b/sfi/platform.cc
@@ -2,16 +2,17 @@
 #include "platform.hh"
 #include "path.hh"
 #include <unistd.h>
+#include <cstring>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <execinfo.h>           // _EXECINFO_H
+#include <sys/syscall.h>        // SYS_gettid
 #if defined __APPLE__
 #include <mach-o/dyld.h>        // _NSGetExecutablePath
 #endif // __APPLE__
 #ifdef  _WIN32                  // includes _WIN64
 #include <windows.h>
 #endif  // _WIN32
-#include <cstring>
-#include <sys/wait.h>
-#include <sys/time.h>
-#include <execinfo.h>           // _EXECINFO_H
 
 namespace Bse {
 
@@ -320,6 +321,49 @@ TaskStatus::string ()
                    utime * 0.001, stime * 0.001, cutime * 0.001, cstime * 0.001);
 }
 
+// == Thread Info ==
+void
+this_thread_set_name (const String &name16chars)
+{
+  pthread_setname_np (pthread_self(), name16chars.c_str());
+}
+
+String
+this_thread_get_name ()
+{
+  char buffer[1024] = { 0, };
+  pthread_getname_np (pthread_self(), buffer, sizeof (buffer) - 1);
+  buffer[sizeof (buffer) - 1] = 0;
+  return buffer;
+}
+
+int
+this_thread_getpid ()
+{
+  return getpid();
+}
+
+int
+this_thread_gettid ()
+{
+  int tid = -1;
+#ifdef  __linux__       // SYS_gettid is present on linux >= 2.4.20
+  tid = syscall (SYS_gettid);
+#endif
+  if (tid < 0)
+    tid = this_thread_getpid();
+  return tid;
+}
+
+int
+this_thread_online_cpus ()
+{
+  static int cpus = 0;
+  if (!cpus)
+    cpus = sysconf (_SC_NPROCESSORS_ONLN);
+  return cpus;
+}
+
 // == Early Startup ctors ==
 namespace {
 struct EarlyStartup {
diff --git a/sfi/platform.hh b/sfi/platform.hh
index a43f399..4fba0df 100644
--- a/sfi/platform.hh
+++ b/sfi/platform.hh
@@ -23,7 +23,7 @@ String      program_cwd           ();                   ///< The current working
 std::string executable_name       ();                   ///< Retrieve the name part of executable_path().
 std::string executable_path       ();                   ///< Retrieve the path to the currently running 
executable.
 
-// == thread info ==
+// == Thread Status ==
 /// Acquire information about a task (process or thread) at runtime.
 struct TaskStatus {
   enum State { UNKNOWN = '?', RUNNING = 'R', SLEEPING = 'S', DISKWAIT = 'D', STOPPED = 'T', PAGING = 'W', 
ZOMBIE = 'Z', DEBUG = 'X', };
@@ -44,6 +44,13 @@ struct TaskStatus {
   String        string     ();  ///< Retrieve string representation of the status information.
 };
 
+// == Thread Info ==
+void   this_thread_set_name    (const String &name16chars);
+String this_thread_get_name    ();
+int    this_thread_getpid      ();
+int    this_thread_gettid      ();
+int    this_thread_online_cpus ();
+
 // == Debugging Aids ==
 extern inline void breakpoint               () BSE_ALWAYS_INLINE;       ///< Cause a debugging breakpoint, 
for development only.
 extern int       (*backtrace_pointers)      (void **buffer, int size);  ///< Capture stack frames for a 
backtrace (on __GLIBC__).


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]