Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread_priority::highest, comments. #442

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/bitcoin/network/async/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace network {

enum class thread_priority
{
highest,
high,
normal,
low,
Expand Down
36 changes: 28 additions & 8 deletions src/async/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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/1220278355.3866.21.camel@localhost.localdomain/
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
}
Expand Down
3 changes: 3 additions & 0 deletions test/async/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ BOOST_AUTO_TEST_CASE(thread__set_thread_priorites__all__set_as_expected)
// Save so we can restore at the end of this test case.
const int save = get_thread_priority_test();

set_priority(thread_priority::highest);
BOOST_REQUIRE_EQUAL(THREAD_PRIORITY_HIGHEST, get_thread_priority_test());
set_priority(thread_priority::high);
BOOST_REQUIRE_EQUAL(THREAD_PRIORITY_ABOVE_NORMAL, get_thread_priority_test());
set_priority(thread_priority::normal);
Expand All @@ -87,6 +89,7 @@ BOOST_AUTO_TEST_CASE(thread__set_thread_priorites__all__set_as_expected)
const int save = get_thread_priority_test();

// Haven't had any luck matching the set and get priority calls as in win.
BOOST_REQUIRE_NO_THROW(set_priority(thread_priority::highest));
BOOST_REQUIRE_NO_THROW(set_priority(thread_priority::high));
BOOST_REQUIRE_NO_THROW(set_priority(thread_priority::normal));
BOOST_REQUIRE_NO_THROW(set_priority(thread_priority::low));
Expand Down
Loading