Merge pull request #6228 from liquid-helium/thread-pool-priority

Enabled setting thread poll priorities.
This commit is contained in:
He Liu 2022-01-19 11:00:45 -08:00 committed by GitHub
commit fd2f553d78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 10 deletions

View File

@ -36,8 +36,7 @@ class ThreadPool final : public IThreadPool, public ReferenceCounted<ThreadPool>
~Thread() { ASSERT_ABORT(!userObject); }
void run() {
deprioritizeThread();
setThreadPriority(pool->priority());
threadUserObject = userObject;
try {
userObject->init();
@ -62,6 +61,7 @@ class ThreadPool final : public IThreadPool, public ReferenceCounted<ThreadPool>
enum Mode { Run = 0, Shutdown = 2 };
volatile int mode;
int stackSize;
int pri;
struct ActionWrapper {
PThreadAction action;
@ -82,7 +82,7 @@ class ThreadPool final : public IThreadPool, public ReferenceCounted<ThreadPool>
};
public:
ThreadPool(int stackSize) : dontstop(ios), mode(Run), stackSize(stackSize) {}
ThreadPool(int stackSize, int pri) : dontstop(ios), mode(Run), stackSize(stackSize), pri(pri) {}
~ThreadPool() override {}
Future<Void> stop(Error const& e = success()) override {
if (mode == Shutdown)
@ -111,10 +111,11 @@ public:
threads.back()->handle = g_network->startThread(start, threads.back(), stackSize, name);
}
void post(PThreadAction action) override { ios.post(ActionWrapper(action)); }
int priority() const { return pri; }
};
Reference<IThreadPool> createGenericThreadPool(int stackSize) {
return Reference<IThreadPool>(new ThreadPool(stackSize));
Reference<IThreadPool> createGenericThreadPool(int stackSize, int pri) {
return Reference<IThreadPool>(new ThreadPool(stackSize, pri));
}
thread_local IThreadPoolReceiver* ThreadPool::Thread::threadUserObject;

View File

@ -141,7 +141,7 @@ private:
PromiseStream<T> promiseStream;
};
Reference<IThreadPool> createGenericThreadPool(int stackSize = 0);
Reference<IThreadPool> createGenericThreadPool(int stackSize = 0, int pri = 10);
class DummyThreadPool final : public IThreadPool, ReferenceCounted<DummyThreadPool> {
public:

View File

@ -2772,10 +2772,10 @@ void waitThread(THREAD_HANDLE thread) {
#endif
}
void deprioritizeThread() {
void setThreadPriority(int pri) {
#ifdef __linux__
int tid = syscall(SYS_gettid);
setpriority(PRIO_PROCESS, tid, 10);
setpriority(PRIO_PROCESS, tid, pri);
#elif defined(_WIN32)
#endif
}

View File

@ -187,8 +187,8 @@ THREAD_HANDLE startThread(void*(func)(void*), void* arg, int stackSize = 0, cons
void waitThread(THREAD_HANDLE thread);
// Linux-only for now. Set thread priority "low"
void deprioritizeThread();
// Linux-only for now. Set thread priority.
void setThreadPriority(int pri);
#define DEBUG_DETERMINISM 0