Skip to content

Commit 8bdeaa3

Browse files
committed
Re-clone network.hpp from libbitcoin-network.
1 parent 6eda08f commit 8bdeaa3

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

include/bitcoin/protocol/network.hpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,29 @@
2424
// config::authority and config::endpoint are also cloned from network.
2525

2626
#include <memory>
27+
#include <thread>
2728
#include <bitcoin/system.hpp>
2829

2930
#ifdef HAVE_MSC
3031
#include <windows.h>
32+
// #define THREAD_PRIORITY_IDLE -15
33+
// #define THREAD_PRIORITY_LOWEST -2
34+
// #define THREAD_PRIORITY_BELOW_NORMAL -1
35+
// #define THREAD_PRIORITY_NORMAL 0
36+
// #define THREAD_PRIORITY_ABOVE_NORMAL 1
37+
// #define THREAD_PRIORITY_HIGHEST 2
38+
// #define THREAD_PRIORITY_TIME_CRITICAL 15
39+
// #define THREAD_PRIORITY_ERROR_RETURN (MAXLONG)
3140
#else
3241
#include <unistd.h>
3342
#include <pthread.h>
3443
#include <sys/resource.h>
3544
#include <sys/types.h>
36-
#ifndef PRIO_MAX
37-
#define PRIO_MAX 20
38-
#endif
39-
#define THREAD_PRIORITY_ABOVE_NORMAL (-2)
40-
#define THREAD_PRIORITY_NORMAL 0
41-
#define THREAD_PRIORITY_BELOW_NORMAL 2
42-
#define THREAD_PRIORITY_LOWEST PRIO_MAX
45+
#define THREAD_PRIORITY_HIGHEST -20
46+
#define THREAD_PRIORITY_ABOVE_NORMAL -2
47+
#define THREAD_PRIORITY_NORMAL 0
48+
#define THREAD_PRIORITY_BELOW_NORMAL 2
49+
#define THREAD_PRIORITY_LOWEST 20
4350
#endif
4451

4552
namespace libbitcoin {
@@ -62,6 +69,7 @@ class enable_shared_from_base
6269

6370
enum class thread_priority
6471
{
72+
highest,
6573
high,
6674
normal,
6775
low,
@@ -79,26 +87,43 @@ inline int get_priority(thread_priority priority) NOEXCEPT
7987
return THREAD_PRIORITY_BELOW_NORMAL;
8088
case thread_priority::high:
8189
return THREAD_PRIORITY_ABOVE_NORMAL;
90+
case thread_priority::highest:
91+
return THREAD_PRIORITY_HIGHEST;
8292
default:
8393
case thread_priority::normal:
8494
return THREAD_PRIORITY_NORMAL;
8595
}
8696
}
8797

88-
// Set the thread priority (or process if thread priority is not available).
98+
// Set the thread priority.
99+
// TODO: handle error conditions.
100+
// TODO: handle potential lack of PRIO_THREAD
101+
// TODO: use proper non-win32 priority levels.
102+
// TODO: Linux: pthread_setschedprio()
103+
// TOOD: macOS: somethign else.
89104
inline void set_priority(thread_priority priority) NOEXCEPT
90105
{
91106
const auto prioritization = get_priority(priority);
92107

93-
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
94108
#if defined(HAVE_MSC)
109+
// learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/
110+
// nf-processthreadsapi-getthreadpriority
95111
SetThreadPriority(GetCurrentThread(), prioritization);
112+
96113
#elif defined(PRIO_THREAD)
114+
// lore.kernel.org/lkml/[email protected]/
97115
setpriority(PRIO_THREAD, pthread_self(), prioritization);
116+
98117
#else
118+
// BUGBUG: This will set all threads in the process.
119+
// man7.org/linux/man-pages/man3/pthread_self.3.html
99120
setpriority(PRIO_PROCESS, getpid(), prioritization);
100121
#endif
101-
BC_POP_WARNING()
122+
}
123+
124+
inline size_t cores() NOEXCEPT
125+
{
126+
return std::max(std::thread::hardware_concurrency(), 1_u32);
102127
}
103128

104129
} // namespace protocol

0 commit comments

Comments
 (0)