From 797c7f09f85a55fb530f3d4cf3b05924470b1198 Mon Sep 17 00:00:00 2001 From: Guillaume DELACOURT Date: Tue, 23 May 2023 16:40:46 +0200 Subject: [PATCH] When setting thread priority, always set the value to zero when the policy is not compatible. Also, only call nice when the priority is set to a strictly positive value. --- doc/zmq_ctx_set.txt | 3 ++- src/thread.cpp | 17 ++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/zmq_ctx_set.txt b/doc/zmq_ctx_set.txt index 11feb159..6544e00e 100644 --- a/doc/zmq_ctx_set.txt +++ b/doc/zmq_ctx_set.txt @@ -66,7 +66,8 @@ The 'ZMQ_THREAD_PRIORITY' argument sets scheduling priority for internal context's thread pool. This option is not available on windows. Supported values for this option depend on chosen scheduling policy. On Linux, when the scheduler policy is SCHED_OTHER, SCHED_IDLE or SCHED_BATCH, the OS scheduler -will not use the thread priority but rather the thread "nice value"; in such cases +will not use the thread priority but rather the thread "nice value"; in such cases, +if 'ZMQ_THREAD_PRIORITY' is set to a strictly positive value, the system call "nice" will be used to set the nice value to -20 (max priority) instead of adjusting the thread priority (which must be zero for those scheduling policies). Details can be found in sched.h file, or at http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html. diff --git a/src/thread.cpp b/src/thread.cpp index 29885392..e013a778 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -292,14 +292,12 @@ void zmq::thread_t:: bool use_nice_instead_priority = (policy != SCHED_FIFO) && (policy != SCHED_RR); - if (_thread_priority != ZMQ_THREAD_PRIORITY_DFLT) { - if (use_nice_instead_priority) - param.sched_priority = - 0; // this is the only supported priority for most scheduling policies - else - param.sched_priority = - _thread_priority; // user should provide a value between 1 and 99 - } + if (use_nice_instead_priority) + param.sched_priority = + 0; // this is the only supported priority for most scheduling policies + else if (_thread_priority != ZMQ_THREAD_PRIORITY_DFLT) + param.sched_priority = + _thread_priority; // user should provide a value between 1 and 99 #ifdef __NetBSD__ if (policy == SCHED_OTHER) @@ -318,7 +316,8 @@ void zmq::thread_t:: #if !defined ZMQ_HAVE_VXWORKS if (use_nice_instead_priority - && _thread_priority != ZMQ_THREAD_PRIORITY_DFLT) { + && _thread_priority != ZMQ_THREAD_PRIORITY_DFLT + && _thread_priority > 0) { // assume the user wants to decrease the thread's nice value // i.e., increase the chance of this thread being scheduled: try setting that to // maximum priority.