ublk-cpp v0.0
Loading...
Searching...
No Matches
io_loop.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <condy.hpp>
10#include <sys/prctl.h>
11#include <sys/resource.h>
12#include <thread>
13
14namespace ublk {
15namespace detail {
16
17class IoLoop {
18public:
19 IoLoop(const condy::RuntimeOptions &options, size_t nr_files,
20 size_t nr_buffers, cpu_set_t cpuset)
21 : runtime_(options) {
22 if (nr_files) {
23 int r = runtime_.fd_table().init(nr_files);
24 if (r < 0) {
25 throw std::system_error(-r, std::generic_category(),
26 "fd_table init");
27 }
28 }
29 if (nr_buffers) {
30 int r = runtime_.buffer_table().init(nr_buffers);
31 if (r < 0) {
32 throw std::system_error(-r, std::generic_category(),
33 "buffer_table init");
34 }
35 }
36 thread_ = std::jthread([this, cpuset] { run_(cpuset); });
37 }
38
39 IoLoop(const IoLoop &) = delete;
40 IoLoop &operator=(const IoLoop &) = delete;
41 IoLoop(IoLoop &&) = delete;
42 IoLoop &operator=(IoLoop &&) = delete;
43
44 ~IoLoop() { runtime_.allow_exit(); }
45
46public:
47 auto get_scheduler() noexcept { return condy::get_scheduler(runtime_); }
48
49 auto &runtime() noexcept { return runtime_; }
50
51private:
52 void run_(cpu_set_t cpuset) {
53 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
54 setpriority(PRIO_PROCESS, 0, -20);
55#if defined(PR_SET_IO_FLUSHER)
56 prctl(PR_SET_IO_FLUSHER, 1, 0, 0, 0);
57#endif
58 runtime_.run();
59 }
60
61private:
62 condy::Runtime runtime_;
63 std::jthread thread_;
64};
65
66} // namespace detail
67} // namespace ublk
The main namespace of the ublk-cpp library.
Definition ublk.hpp:21