19 worker::worker(uint8_t
id, std::unique_ptr<loop_t>& loop) : m_id(id), m_term(false), m_leader(false) {
25 struct ev_loop* loop_raw =
nullptr;
26 if (
nullptr != loop) {
29 ev_set_userdata(
m_loop->ptr,
this);
32 loop_raw = ev_default_loop(0);
54 while (
m_work_cond.wait_for(lock, std::chrono::milliseconds(100)) == std::cv_status::timeout && !
m_leader &&
62 ev_loop(
m_loop->ptr, EVLOOP_ONESHOT);
74 bool cont =
event.task->handle_event(
this,
event.
revents);
77 event.task->notify_error(
this);
82 event.task->reset_error();
83 event.task->start_watcher(
this);
86 event.task->finish(
this);
99 ev_unloop(
m_loop->ptr, EVUNLOOP_ALL);
100 ev_loop_destroy(
m_loop->ptr);
108 assert(
nullptr != worker);
110 if (
nullptr != tfq) {
111 std::queue<task_func_t> qcpy;
113 std::lock_guard<std::mutex> lock(tfq->
mutex);
114 tfq->
queue.swap(qcpy);
117 while (!qcpy.empty()) {
void tasks_async_callback(struct ev_loop *loop, ev_async *w, int events)
Callback for async events.
bool error() const
Return true if an error occured.
std::condition_variable m_work_cond
ev_async m_signal_watcher
bool auto_delete() const
Returns true if auto deletion is active.
std::atomic< bool > m_leader
std::queue< task_func_t > queue
std::queue< event > m_events_queue
std::atomic< bool > m_term
static thread_local worker * m_worker_ptr
std::unique_ptr< loop_t > m_loop
bool exec_in_worker_ctx(task_func_t f)
void mark_free()
Let the dispatcher know that this thread is free to become a leader and take on some work...
std::string get_string() const
void run()
Main method of the thread.
std::unique_ptr< std::thread > m_thread
worker(uint8_t id, std::unique_ptr< loop_t > &loop)
void promote_leader()
Find a free worker and promote it to become the next leader.