#ifndef NO_CPP0X #include "Dispatcher.h" #include #include "lib/utils/bts_assert.h" #include "../os/os.h" #include "defer.h" #include "lib/utils/bts_chrono.h" #include "lib/logging/logging.h" #include #include #include #include #include #include using namespace std; using namespace boost::asio; namespace bts { namespace utils { struct DispatcherImpl { public: DispatcherImpl() : thread(std::this_thread::get_id()), service(), work(), is_pool(false) { } std::thread::id thread; io_service service; unique_ptr work; bool is_pool; }; Dispatcher::Dispatcher() : _me(new DispatcherImpl()) { #ifdef _WIN32 _mm_mfence(); #else __sync_synchronize(); #endif } void wait_for_stop(io_service& service) { while(!service.stopped()) { std::this_thread::sleep_for(1ms); } } Dispatcher::~Dispatcher() { if (!_me->service.stopped()) { _me->work.reset(); _me->service.stop(); wait_for_stop(_me->service); } } void Dispatcher::run(size_t threads) { bts_assert_msg(_me && _me->work == nullptr, "Error dispatcher is already running."); _me->service.reset(); _me->work.reset(new io_service::work(_me->service)); DEFER(_me->work.reset()); // Start N-1 threads in addition to the current one for (size_t x = 1; x < threads; ++x) { add_thread(); } _me->service.run(); } void Dispatcher::add_thread() { _me->is_pool = true; boost::thread t([=]() { os::set_thread_name("DispatcherPool"); _me->service.run(); }); t.detach(); } bool Dispatcher::is_threadpool() { return _me->is_pool; } bool Dispatcher::is_running() { return _me->work != nullptr && !_me->service.stopped(); } void Dispatcher::terminate() { // allows run() to exit normally _me->work.reset(); // At this point this Dispatcher instance may be asyncronously deleted and is no longer safe to use. } bool Dispatcher::check_access() { return std::this_thread::get_id() == _me->thread; } boost::thread_specific_ptr _tl_dispatchers; Dispatcher& Dispatcher::current(bool set_name) { if (_tl_dispatchers.get() == nullptr) { if(set_name) os::set_thread_name("Dispatcher"); _tl_dispatchers.reset(new Dispatcher()); } return *(_tl_dispatchers.get()); } Dispatcher& Dispatcher::get_named(const char* name) { static std::mutex mutex; static std::map s_dispatchers; std::lock_guard lock(mutex); auto result = s_dispatchers.find(name); if (result != s_dispatchers.end()) { return *result->second; } Dispatcher& d = create_new(name); s_dispatchers[name] = &d; return d; } Dispatcher& Dispatcher::background() { static Dispatcher* s_dispatcher = nullptr; Dispatcher** p_s_dispatcher = &s_dispatcher; if (s_dispatcher == nullptr) { *p_s_dispatcher = &get_named("DispatcherBG"); } return *s_dispatcher; } Dispatcher& Dispatcher::create_new(const char* name) { Dispatcher* new_dispatcher = nullptr; mutex m; condition_variable c; // Take the lock before starting the thread to ensure c.notify_all() cannot happen before we wait on the condition unique_lock lock(m); boost::thread t([&]() { Dispatcher& temp = current(false); os::set_thread_name(name); try { temp.post([&]() { unique_lock lock(m); new_dispatcher = ¤t(); c.notify_all(); }); temp.run(); } catch(const std::exception& ex) { LOG(Gen,Error ,"Exception on dispatcher thread: {}" ,ex.what()); bts_assert_msg(false, ex.what()); } }); t.detach(); auto result = c.wait_for(lock, chrono::seconds(5));// releases the lock temporarily bts_assert_msg(result == cv_status::no_timeout, "Dispatcher failed to start."); bts_assert(new_dispatcher); // Could happen to to spurious wake return *new_dispatcher; } void Dispatcher::invoke(Action action) { bts_assert(!_me->service.stopped()); // If we're calling from the thread that owns this dispatcher // We can just call the function - to do otherwise would result in deadlock if (check_access()) { action(); return; } mutex m; condition_variable c; unique_lock lock(m); bool complete = false; _me->service.dispatch([&]() { action(); unique_lock lock(m); complete = true; c.notify_all(); }); while(!complete) { c.wait(lock); } } void Dispatcher::post(Action action, int64_t delay_ms) { if (delay_ms > 0) { shared_ptr timer = make_shared(_me->service); timer->expires_from_now(boost::posix_time::milliseconds(delay_ms)); timer->async_wait([timer, action] (const boost::system::error_code& err) { if (!err) action(); }); } else { _me->service.post(action); } } // // DispatcherTimer // struct DispatcherTimerImpl { DispatcherTimerImpl(Dispatcher& dispatcher, int64_t ms, Action& cb) : running(false), callback(cb), delay(ms), timer(new deadline_timer(dispatcher._me->service)) { } void schedule() { if (running) { timer->expires_from_now(boost::posix_time::milliseconds(delay)); timer->async_wait(std::bind(&DispatcherTimerImpl::execute, this, std::placeholders::_1)); } } void execute(const boost::system::error_code& error) { if (!error) { callback(); schedule(); } } bool running; Action callback; int64_t delay; unique_ptr timer; }; DispatcherTimer::DispatcherTimer(Dispatcher& dispatcher, int64_t time_ms, Action cb) : _me(new DispatcherTimerImpl(dispatcher, time_ms, cb)) { } DispatcherTimer::~DispatcherTimer() { } void DispatcherTimer::start() { _me->running = true; _me->schedule(); } void DispatcherTimer::stop() { _me->running = false; _me->timer->cancel(); } bool DispatcherTimer::running() { return _me->running; } void DispatcherTimer::set_interval(int64_t time_ms) { // This invalidates existing waits so restart _me->timer->expires_from_now(boost::posix_time::milliseconds(time_ms)); _me->schedule(); } void DispatcherTimer::set_callback(Action cb) { _me->callback = cb; } }}//namespace #endif