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

Kadir Cetinkaya via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 08:17:11 PDT 2019


kadircet updated this revision to Diff 190264.
kadircet marked an inline comment as done.
kadircet added a comment.

- Return true on success.
- Explain intended behavior of the function.


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59130/new/

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();
 }
+
+bool llvm::set_thread_priority(ThreadPriority Priority) {
+  return 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,20 @@
 #endif
 #endif
 }
+
+bool llvm::set_thread_priority(ThreadPriority Priority) {
+#if defined(__linux__) && defined(SCHED_IDLE)
+  // Some *really* old glibcs are missing SCHED_IDLE.
+  sched_param priority;
+  priority.sched_priority = 0;
+  return !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
+  return !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,19 @@
   /// 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,
+  };
+  /// If priority is Background tries to lower current threads priority such
+  /// that it does not affect foreground tasks significantly. Can be used for
+  /// long-running, latency-insensitive tasks to make sure cpu is not hogged by
+  /// this task.
+  /// If the priority is default tries to restore current threads priority to
+  /// default scheduling priority.
+  /// Returns true on success.
+  bool set_thread_priority(ThreadPriority Priority);
 }
 
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59130.190264.patch
Type: text/x-patch
Size: 2421 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190312/0cc4c0b5/attachment.bin>


More information about the llvm-commits mailing list