Files
spark/src/lib/utils/Dispatcher.h
T

122 lines
4.0 KiB
C++

#pragma once
#ifndef NO_CPP0X
#include "lib/utils/bts_export.h"
#include "lib/utils/bts_no_copy.h"
#include <boost/asio/io_service.hpp>
#include <cstdint>
#include <functional>
#include <memory>
/*
* Dispatcher is a simple delegate based event queue built on boost::asio.
*
* Each dispatcher is owned by a single thread which is responsible for calling
* run() to dispatch events. The run call will not return until terminate() is
* called. It is safe to call terminate() from the same thread
*
* Sample use:
* Create a thread and get Dispatcher::current() for that thread. Pass that
* Dispatcher& to some other code. If that other code makes calls through the
* dispatcher post() (asyncronous) or invoke() (syncronous) the callback is
* guarunteed to happen on the Dispatcher's thread.
*
*
* Some use cases:
* 1) Short asyncronous work
* You can use Dispatcher::background() to get a handle to a common background
* thread (created on demand). This is useful for short tasks like asyncronous
* logging or timer events where you do not need to monopolize a thread of your
* own. You can also use post() with a delay to set up one time callbacks after
* a short time. Ex: Simple async logging #define ALOG(x)
* Dispatcher::background().post([=]() { LOG(Info, Something) << x; });
*
* 2) Use a DispatcherTimer to drive a WorkScheduler without tying up a thread
* during the idle times. Ex: Dispatcher& d = Dispatcher::background();
* WorkScheduler* s = new WorkScheduler(...);
* DispatcherTimer t(d, 100, [=]() { s->dispatch(); });
* t.start();
*
* Now the WorkScheduler will be invoked every 100ms but you are still free to
* use d.post([](){ ... }); to do asyncronous work between timer ticks. If your
* async task takes longer than the timer interval the timer callback will fire
* immediately when it is done. Simlilarly if the timer callback is still
* working when you call post() your call will be queued and fire when the timer
* routine finishes.
*
* 3) Controlling access to non-thread safe resources.
* If you have an object which is not thread safe you can give it a Dispatcher
* "owner". A thread can test object.owner().check_access() to see if it is the
* owning thread and if not serialize access by using
* object.owner().invoke([&](){ object.unsafe_method(); }; The unsafe_method
* might assert that access is only allowed from its owning thread.
*
*/
namespace bts {
namespace utils {
struct DispatcherImpl;
struct DispatcherTimerImpl;
class DispatcherTimer;
typedef std::function<void()> Action;
class BTS_EXPORT Dispatcher {
public:
Dispatcher();
~Dispatcher();
void run(size_t threads = 1);
void add_thread();
bool check_access();
void terminate();
bool is_running();
bool is_threadpool();
// Get/create the dispatcher for the current thread
static Dispatcher &current();
// Get a default background thread to dispatch work to
static Dispatcher &background();
// Create a new background dispatcher thread and return the dispatcher for it
static Dispatcher &get_named(const char *name);
static Dispatcher &create_new(const char *name);
// Perform an action on the dispatcher thread. Block until the action is
// complete.
void invoke(Action action);
// Queue an action on the dispatcher thread and return immediately.
void post(Action &&action, int64_t delay_ms = 0);
void post_copy(Action action, int64_t delay_ms = 0);
boost::asio::io_service *io_service();
private:
BTS_NO_COPY(Dispatcher);
friend struct DispatcherTimerImpl;
std::unique_ptr<DispatcherImpl> _me;
};
class BTS_EXPORT DispatcherTimer {
public:
DispatcherTimer(Dispatcher &dispatcher, int64_t time_ms, Action cb);
DispatcherTimer(DispatcherTimer &&other);
~DispatcherTimer();
void start();
void stop();
bool running();
void set_interval(int64_t time_ms);
void set_callback(Action cb);
private:
BTS_NO_COPY(DispatcherTimer);
std::unique_ptr<DispatcherTimerImpl> _me;
};
} // namespace utils
} // namespace bts
#endif