-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from evoskuil/master
Add thread_priority::highest, comments.
- Loading branch information
Showing
3 changed files
with
32 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ namespace network { | |
|
||
enum class thread_priority | ||
{ | ||
highest, | ||
high, | ||
normal, | ||
low, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,18 +22,24 @@ | |
|
||
#ifdef HAVE_MSC | ||
#include <windows.h> | ||
// #define THREAD_PRIORITY_IDLE -15 | ||
// #define THREAD_PRIORITY_LOWEST -2 | ||
// #define THREAD_PRIORITY_BELOW_NORMAL -1 | ||
// #define THREAD_PRIORITY_NORMAL 0 | ||
// #define THREAD_PRIORITY_ABOVE_NORMAL 1 | ||
// #define THREAD_PRIORITY_HIGHEST 2 | ||
// #define THREAD_PRIORITY_TIME_CRITICAL 15 | ||
// #define THREAD_PRIORITY_ERROR_RETURN (MAXLONG) | ||
#else | ||
#include <unistd.h> | ||
#include <pthread.h> | ||
#include <sys/resource.h> | ||
#include <sys/types.h> | ||
#ifndef PRIO_MAX | ||
#define PRIO_MAX 20 | ||
#endif | ||
#define THREAD_PRIORITY_ABOVE_NORMAL (-2) | ||
#define THREAD_PRIORITY_NORMAL 0 | ||
#define THREAD_PRIORITY_BELOW_NORMAL 2 | ||
#define THREAD_PRIORITY_LOWEST PRIO_MAX | ||
#define THREAD_PRIORITY_HIGHEST -20 | ||
#define THREAD_PRIORITY_ABOVE_NORMAL -2 | ||
#define THREAD_PRIORITY_NORMAL 0 | ||
#define THREAD_PRIORITY_BELOW_NORMAL 2 | ||
#define THREAD_PRIORITY_LOWEST 20 | ||
#endif | ||
|
||
namespace libbitcoin { | ||
|
@@ -50,22 +56,36 @@ static int get_priority(thread_priority priority) NOEXCEPT | |
return THREAD_PRIORITY_BELOW_NORMAL; | ||
case thread_priority::high: | ||
return THREAD_PRIORITY_ABOVE_NORMAL; | ||
case thread_priority::highest: | ||
return THREAD_PRIORITY_HIGHEST; | ||
default: | ||
case thread_priority::normal: | ||
return THREAD_PRIORITY_NORMAL; | ||
} | ||
} | ||
|
||
// Set the thread priority (or process if thread priority is not available). | ||
// Set the thread priority. | ||
// TODO: handle error conditions. | ||
// TODO: handle potential lack of PRIO_THREAD | ||
// TODO: use proper non-win32 priority levels. | ||
// TODO: Linux: pthread_setschedprio() | ||
// TOOD: macOS: somethign else. | ||
void set_priority(thread_priority priority) NOEXCEPT | ||
{ | ||
const auto prioritization = get_priority(priority); | ||
|
||
#if defined(HAVE_MSC) | ||
// learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ | ||
// nf-processthreadsapi-getthreadpriority | ||
SetThreadPriority(GetCurrentThread(), prioritization); | ||
|
||
#elif defined(PRIO_THREAD) | ||
// lore.kernel.org/lkml/[email protected]/ | ||
setpriority(PRIO_THREAD, pthread_self(), prioritization); | ||
|
||
#else | ||
// BUGBUG: This will set all threads in the process. | ||
// man7.org/linux/man-pages/man3/pthread_self.3.html | ||
setpriority(PRIO_PROCESS, getpid(), prioritization); | ||
#endif | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters