Skip to content
Bitcoin Roots

v29.4-roots.2 · Commit 2: [PATCH 02/21] runtime: preserve shared operator controls

src/util/ioprio.cpp

Before: /dev/null · After: src/util/ioprio.cpp

File metadata
new file mode 100644
index 0000000000000000000000000000000000000000..f5d761706eb4831856a96206d22d58ceb0ac06c1

Download original patch · Release notes

Diff rows 1–79 of 79. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/util/ioprio.cpp ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) 2016 The Bitcoin Core developers
2
+ // Distributed under the MIT software license, see the accompanying
3
+ // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
+
5
+ #include <bitcoin-build-config.h> // IWYU pragma: keep
6
+
7
+ #include <util/ioprio.h>
8
+
9
+ #if HAVE_IOPRIO_SYSCALL
10
+
11
+ #ifndef _GNU_SOURCE
12
+ #define _GNU_SOURCE
13
+ #endif
14
+ #include <unistd.h>
15
+ #include <sys/syscall.h>
16
+
17
+ #ifndef IOPRIO_WHO_PROCESS
18
+ #define IOPRIO_WHO_PROCESS 1
19
+ #endif
20
+ #ifndef IOPRIO_CLASS_IDLE
21
+ #define IOPRIO_CLASS_IDLE 3
22
+ #endif
23
+ #ifndef IOPRIO_CLASS_SHIFT
24
+ #define IOPRIO_CLASS_SHIFT 13
25
+ #endif
26
+
27
+ int ioprio_get() {
28
+ return syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, 0);
29
+ }
30
+
31
+ int ioprio_set(const int ioprio) {
32
+ return syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, ioprio);
33
+ }
34
+
35
+ int ioprio_set_idle() {
36
+ return ioprio_set(7 | (IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT));
37
+ }
38
+
39
+ #elif HAVE_IOPOLICY
40
+
41
+ #include <sys/resource.h>
42
+
43
+ int ioprio_get() {
44
+ return getiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD);
45
+ }
46
+
47
+ int ioprio_set(const int ioprio) {
48
+ return setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, ioprio);
49
+ }
50
+
51
+ int ioprio_set_idle() {
52
+ return ioprio_set(IOPOL_UTILITY);
53
+ }
54
+
55
+ #endif
56
+
57
+
58
+ #if HAVE_WINDOWS_IOPRIO
59
+
60
+ #include <windows.h>
61
+ #include <io.h>
62
+ #include <cstddef>
63
+ #include <cstdint>
64
+ #include <cstdio>
65
+
66
+ bool ioprio_set_file_idle(FILE * const F) {
67
+ FILE_IO_PRIORITY_HINT_INFO priorityHint = {
68
+ .PriorityHint = IoPriorityHintLow,
69
+ };
70
+ intptr_t osfhandle = _get_osfhandle(_fileno(F));
71
+ if (osfhandle == (intptr_t)INVALID_HANDLE_VALUE || osfhandle == (intptr_t)-2) {
72
+ return false;
73
+ }
74
+ HANDLE hFile = (HANDLE)osfhandle;
75
+
76
+ return SetFileInformationByHandle(hFile, FileIoPriorityHintInfo, &priorityHint, sizeof(priorityHint));
77
+ }
78
+
79
+ #endif