SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
IoContext.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_comm_IoContext_h
7 #define swcdb_core_comm_IoContext_h
8 
9 
10 #include "swcdb/core/Logger.h"
12 
13 
14 namespace SWC { namespace Comm {
15 // Forward Declaration
16 class IoContext;
17 typedef std::shared_ptr<IoContext> IoContextPtr;
18 }}
19 
21 
22 
23 
24 namespace SWC {
25 
26 
32 namespace Comm {
33 
34 
35 class IoContext final : public std::enable_shared_from_this<IoContext> {
36  public:
37 
38  typedef asio::thread_pool::executor_type Executor;
39  typedef asio::executor_work_guard<Executor> ExecutorWorkGuard;
40 
41  static IoContextPtr make(std::string&& _name, int32_t size) {
42  return IoContextPtr(new IoContext(std::move(_name), size));
43  }
44 
46  static uint32_t
47  get_number_of_threads(bool relative, int32_t size) noexcept {
48  if(relative) {
49  uint32_t sz = size * std::thread::hardware_concurrency();
50  if(sz > 0) return sz;
51  }
52  return size;
53  }
54 
55  static IoContextPtr make(std::string&& _name, bool relative, int32_t size) {
56  return IoContextPtr(
57  new IoContext(
58  std::move(_name),
59  get_number_of_threads(relative, size)
60  )
61  );
62  }
63 
65  const std::string name;
66  asio::thread_pool pool;
67  std::unique_ptr<asio::signal_set> signals;
68 
69 
70  IoContext(std::string&& _name, int32_t size);
71 
72  ~IoContext() noexcept { }
73 
75  int32_t get_size() const noexcept {
76  return m_size; // asio::query(executor(), asio::execution::occupancy);
77  }
78 
80  Executor executor() noexcept {
81  return pool.get_executor();
82  }
83 
84 #pragma GCC diagnostic push
85 #pragma GCC diagnostic ignored "-Wnull-dereference"
86 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
87  template <typename T_Handler>
89  void post(T_Handler&& handler) {
90  asio::post(pool, std::move(handler));
91  }
92 #pragma GCC diagnostic pop
93 
94  void set_signals();
95 
97  PeriodicTimer::Call_t&& call);
98 
99  void stop();
100 
101  private:
102  int32_t m_size;
104 
105 
106 };
107 
108 } //namespace Comm
109 
110 
111 
112 namespace Env {
113 
114 class IoCtx final {
115  public:
116 
118  static void init(int32_t size) {
119  m_env.reset(new IoCtx(size));
120  }
121 
123  static void init(bool relative, int32_t size) {
124  m_env.reset(new IoCtx(relative, size));
125  }
126 
128  static bool ok() noexcept {
129  return bool(m_env);
130  }
131 
133  SWC_ASSERT(ok());
134  return m_env->m_io;
135  }
136 
137  template <typename T_Handler>
139  static void post(T_Handler&& handler) {
140  m_env->m_io->post(std::move(handler));
141  }
142 
144  static bool stopping() noexcept {
145  return !m_env->m_io->running;
146  }
147 
148  static void reset() noexcept {
149  m_env = nullptr;
150  }
151 
153  IoCtx(int32_t size)
154  : m_io(Comm::IoContext::make("Env", size)) {
155  }
156 
158  IoCtx(bool relative, int32_t size)
159  : m_io(Comm::IoContext::make("Env", relative, size)) {
160  }
161 
162  ~IoCtx() noexcept { }
163 
164  private:
166  inline static std::shared_ptr<IoCtx> m_env = nullptr;
167 };
168 
169 } // namespace Env
170 
171 
172 } // namespace SWC
173 
174 
175 
176 #ifdef SWC_IMPL_SOURCE
179 #endif
180 
181 #endif // swcdb_core_comm_IoContext_h
SWC::Comm::IoContext::running
Core::AtomicBool running
Definition: IoContext.h:64
Logger.h
SWC::Core::AtomicBase< bool >
PeriodicTimer.cc
SWC::Comm::IoContext::name
const std::string name
Definition: IoContext.h:65
SWC::Env::IoCtx::post
static SWC_CAN_INLINE void post(T_Handler &&handler)
Definition: IoContext.h:139
SWC::Env::IoCtx::m_env
static std::shared_ptr< IoCtx > m_env
Definition: IoContext.h:166
SWC::Comm::IoContext::m_size
int32_t m_size
Definition: IoContext.h:102
SWC::Comm::IoContext::get_size
SWC_CAN_INLINE int32_t get_size() const noexcept
Definition: IoContext.h:75
SWC::Env::IoCtx::ok
static SWC_CAN_INLINE bool ok() noexcept
Definition: IoContext.h:128
SWC::Env::IoCtx::m_io
Comm::IoContextPtr m_io
Definition: IoContext.h:165
SWC::Comm::IoContextPtr
std::shared_ptr< IoContext > IoContextPtr
Definition: IoContext.h:16
SWC::Comm::PeriodicTimers
Definition: PeriodicTimer.h:45
SWC::Comm::PeriodicTimer::Call_t
const std::function< void()> Call_t
Definition: PeriodicTimer.h:18
SWC::Comm::IoContext::post
SWC_CAN_INLINE void post(T_Handler &&handler)
Definition: IoContext.h:89
SWC::Env::IoCtx::~IoCtx
~IoCtx() noexcept
Definition: IoContext.h:162
SWC::Env::IoCtx
Definition: IoContext.h:114
SWC::Comm::IoContext::get_number_of_threads
static SWC_CAN_INLINE uint32_t get_number_of_threads(bool relative, int32_t size) noexcept
Definition: IoContext.h:47
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Comm::IoContext
Definition: IoContext.h:35
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
PeriodicTimer.h
SWC::Comm::IoContext::m_periodic_timers
PeriodicTimers m_periodic_timers
Definition: IoContext.h:103
IoContext.cc
SWC::Env::IoCtx::init
static SWC_CAN_INLINE void init(int32_t size)
Definition: IoContext.h:118
SWC::Comm::IoContext::signals
std::unique_ptr< asio::signal_set > signals
Definition: IoContext.h:67
SWC::Comm::IoContext::~IoContext
~IoContext() noexcept
Definition: IoContext.h:72
SWC::Env::IoCtx::init
static SWC_CAN_INLINE void init(bool relative, int32_t size)
Definition: IoContext.h:123
asio_wrap.h
SWC::Env::IoCtx::IoCtx
SWC_CAN_INLINE IoCtx(bool relative, int32_t size)
Definition: IoContext.h:158
SWC::Env::IoCtx::IoCtx
SWC_CAN_INLINE IoCtx(int32_t size)
Definition: IoContext.h:153
SWC::Comm::IoContext::executor
SWC_CAN_INLINE Executor executor() noexcept
Definition: IoContext.h:80
SWC::Comm::IoContext::IoContext
IoContext(std::string &&_name, int32_t size)
Definition: IoContext.cc:12
SWC::Comm::IoContext::Executor
asio::thread_pool::executor_type Executor
Definition: IoContext.h:38
SWC::Env::IoCtx::io
static Comm::IoContextPtr io()
Definition: IoContext.h:132
SWC::Env::IoCtx::reset
static void reset() noexcept
Definition: IoContext.h:148
SWC::Comm::IoContext::set_periodic_timer
void set_periodic_timer(const Config::Property::Value_int32_g::Ptr ms, PeriodicTimer::Call_t &&call)
Definition: IoContext.cc:24
SWC::Comm::IoContext::make
static IoContextPtr make(std::string &&_name, bool relative, int32_t size)
Definition: IoContext.h:55
SWC::Comm::IoContext::ExecutorWorkGuard
asio::executor_work_guard< Executor > ExecutorWorkGuard
Definition: IoContext.h:39
SWC_ASSERT
#define SWC_ASSERT(_e_)
Definition: Exception.h:165
SWC::Comm::IoContext::make
static IoContextPtr make(std::string &&_name, int32_t size)
Definition: IoContext.h:41
SWC::Comm::IoContext::set_signals
void set_signals()
Definition: IoContext.cc:20
SWC::Env::IoCtx::stopping
static SWC_CAN_INLINE bool stopping() noexcept
Definition: IoContext.h:144
SWC::Config::Property::Value_int32_g
Definition: Property.h:586
SWC::Comm::IoContext::stop
void stop()
Definition: IoContext.cc:30
SWC::Comm::IoContext::pool
asio::thread_pool pool
Definition: IoContext.h:66