[PATCH] D59130: [llvm][Support] Provide interface to set thread priorities

Kadir Cetinkaya via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 05:26:45 PST 2019


kadircet created this revision.
kadircet added reviewers: gribozavr, ioeric.
Herald added subscribers: llvm-commits, kristina, jfb, krytarowski.
Herald added a project: LLVM.

We have a multi-platform thread priority setting function(last piece
landed with D58683 <https://reviews.llvm.org/D58683>), I wanted to make this available to all llvm community,
there seem to be other users of such functionality with portability fixmes:
lib/Support/CrashRecoveryContext.cpp
tools/clang/tools/libclang/CIndex.cpp


Repository:
  rL LLVM

https://reviews.llvm.org/D59130

Files:
  include/llvm/Support/Threading.h
  lib/Support/Unix/Threading.inc
  lib/Support/Windows/Threading.inc


Index: lib/Support/Windows/Threading.inc
===================================================================
--- lib/Support/Windows/Threading.inc
+++ lib/Support/Windows/Threading.inc
@@ -106,3 +106,10 @@
   // value.
   Name.clear();
 }
+
+void llvm::set_thread_priority(ThreadPriority Priority) {
+  SetThreadPriority(GetCurrentThread(),
+                    Priority == ThreadPriority::Background
+                        ? THREAD_MODE_BACKGROUND_BEGIN
+                        : THREAD_MODE_BACKGROUND_END);
+}
Index: lib/Support/Unix/Threading.inc
===================================================================
--- lib/Support/Unix/Threading.inc
+++ lib/Support/Unix/Threading.inc
@@ -217,3 +217,19 @@
 #endif
 #endif
 }
+
+void llvm::set_thread_priority(ThreadPriority Priority) {
+  // Some *really* old glibcs are missing SCHED_IDLE.
+#if defined(__linux__) && defined(SCHED_IDLE)
+  sched_param priority;
+  priority.sched_priority = 0;
+  pthread_setschedparam(
+      pthread_self(),
+      Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
+      &priority);
+#elif defined(__APPLE__)
+  // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html
+  setpriority(PRIO_DARWIN_THREAD, 0,
+              Priority == ThreadPriority::Background ? PRIO_DARWIN_BG : 0);
+#endif
+}
Index: include/llvm/Support/Threading.h
===================================================================
--- include/llvm/Support/Threading.h
+++ include/llvm/Support/Threading.h
@@ -167,6 +167,12 @@
   /// purposes, and as with setting a thread's name no indication of whether
   /// the operation succeeded or failed is returned.
   void get_thread_name(SmallVectorImpl<char> &Name);
+
+  enum class ThreadPriority {
+    Background = 0,
+    Default = 1,
+  };
+  void set_thread_priority(ThreadPriority Priority);
 }
 
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59130.189848.patch
Type: text/x-patch
Size: 1904 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190308/1fd3bcab/attachment.bin>


More information about the llvm-commits mailing list