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.cc
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 #include "swcdb/core/Exception.h"
8 
9 namespace SWC { namespace Comm {
10 
11 
12 IoContext::IoContext(std::string&& _name, int32_t size)
13  : running(true), name(std::move(_name)),
14  pool(size), signals(nullptr),
15  m_size(size), m_periodic_timers() {
16  SWC_LOGF(LOG_DEBUG, "Starting IO-ctx(%s) size=%d", name.c_str(), m_size);
17  SWC_ASSERT(m_size > 0);
18 }
19 
21  signals.reset(new asio::signal_set(executor() , SIGINT, SIGTERM));
22 }
23 
26  PeriodicTimer::Call_t&& call) {
27  m_periodic_timers.set(ms, std::move(call), shared_from_this());
28 }
29 
31  SWC_LOGF(LOG_DEBUG, "Stopping IO-ctx(%s)", name.c_str());
32 
34 
35  running.store(false);
36 
37  // hold on for IO to finish
38  bool untracked;
39  for(int i=0; i<10; ++i) {
40  untracked = asio::query(executor(), asio::execution::outstanding_work)
41  == asio::execution::outstanding_work.untracked;
42  if(untracked)
43  break;
44  SWC_LOGF(LOG_DEBUG, "Waiting for IO-ctx(%s)", name.c_str());
45  std::this_thread::sleep_for(std::chrono::milliseconds(500));
46  }
47  pool.stop();
48  pool.wait();
49 
50  SWC_LOGF(LOG_DEBUG, "Wait for IO-ctx(%s) finished %sgracefully",
51  name.c_str(), untracked ? "" : "not-");
52 }
53 
54 
55 }}
SWC::Comm::IoContext::running
Core::AtomicBool running
Definition: IoContext.h:64
SWC::Comm::IoContext::name
const std::string name
Definition: IoContext.h:65
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC::Comm::IoContext::m_size
int32_t m_size
Definition: IoContext.h:102
SWC::Comm::PeriodicTimer::Call_t
const std::function< void()> Call_t
Definition: PeriodicTimer.h:18
IoContext.h
SWC::Core::AtomicBase::store
constexpr SWC_CAN_INLINE void store(T v) noexcept
Definition: Atomic.h:37
SWC::LOG_DEBUG
@ LOG_DEBUG
Definition: Logger.h:36
Exception.h
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::PeriodicTimers::stop
void stop()
Definition: PeriodicTimer.cc:49
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC::Comm::IoContext::m_periodic_timers
PeriodicTimers m_periodic_timers
Definition: IoContext.h:103
SWC::Comm::IoContext::signals
std::unique_ptr< asio::signal_set > signals
Definition: IoContext.h:67
SWC::Comm::PeriodicTimers::set
void set(const Config::Property::Value_int32_g::Ptr ms, PeriodicTimer::Call_t &&call, const IoContextPtr &ioctx)
Definition: PeriodicTimer.cc:56
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::set_periodic_timer
void set_periodic_timer(const Config::Property::Value_int32_g::Ptr ms, PeriodicTimer::Call_t &&call)
Definition: IoContext.cc:24
SWC_ASSERT
#define SWC_ASSERT(_e_)
Definition: Exception.h:165
SWC::Comm::IoContext::set_signals
void set_signals()
Definition: IoContext.cc:20
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