7 #include "eventdispatcher.h" 15 #include "eventloopinterface.h" 23 void dispatchEvent() {
24 std::queue<std::function<void()>> eventList;
26 std::lock_guard<std::mutex> lock(mutex_);
28 std::swap(eventList, eventList_);
30 while (!eventList.empty()) {
31 auto functor = std::move(eventList.front());
38 mutable std::mutex mutex_;
39 std::queue<std::function<void()>> eventList_;
40 std::unique_ptr<EventSourceAsync> asyncEvent_;
47 EventDispatcher::~EventDispatcher() =
default;
51 std::lock_guard<std::mutex> lock(d->mutex_);
52 d->asyncEvent_ =
event->addAsyncEvent([d](
EventSource *) {
61 std::lock_guard<std::mutex> lock(d->mutex_);
62 d->asyncEvent_.reset();
68 std::lock_guard<std::mutex> lock(d->mutex_);
71 if (!d->asyncEvent_) {
74 d->eventList_.push(std::move(functor));
76 d->asyncEvent_->send();
81 std::lock_guard<std::mutex> lock(d->mutex_);
EventDispatcher()
Construct a new event dispatcher.
Simple file system related API for checking file status.
EventLoop * eventLoop() const
Return the currently attached event loop.
Utility class to handle unix file decriptor.
void attach(EventLoop *event)
Attach EventDispatcher to an EventLoop.
void detach()
Detach event dispatcher from event loop, must be called from the same thread from event loop...
void schedule(std::function< void()> functor)
A thread-safe function to schedule a functor to be call from event loop.