libtasks Documentation  1.6
io_task_base.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #include <tasks/worker.h>
9 #include <tasks/io_task_base.h>
10 #include <tasks/logging.h>
11 
12 namespace tasks {
13 
14 io_task_base::io_task_base(int events) : m_events(events) {
15  tdbg(get_string() << ": ctor" << std::endl);
16  m_io.reset(new ev_io);
17  ev_init(m_io.get(), tasks_event_callback<ev_io*>);
18  m_io->data = this;
19 }
20 
22  tdbg(get_string() << ": init watcher with fd " << iob().fd() << std::endl);
23  ev_io_set(m_io.get(), iob().fd(), m_events);
24  m_watcher_initialized = true;
25 }
26 
27 void io_task_base::set_events(int events) {
28  if (m_events != events) {
29  tdbg(get_string() << ": setting events to " << events << std::endl);
30  m_events = events;
31  m_change_pending = true;
32  }
33 }
34 
36  assert(m_watcher_initialized);
37  worker->exec_in_worker_ctx([this](struct ev_loop* loop) {
38  if (!ev_is_active(m_io.get())) {
39  tdbg(get_string() << ": starting watcher" << std::endl);
40  ev_io_start(loop, m_io.get());
41  }
42  });
43 }
44 
46  assert(m_watcher_initialized);
47  worker->exec_in_worker_ctx([this](struct ev_loop* loop) {
48  if (ev_is_active(m_io.get())) {
49  tdbg(get_string() << ": stopping watcher" << std::endl);
50  ev_io_stop(loop, m_io.get());
51  }
52  });
53 }
54 
56  assert(m_watcher_initialized);
57  if (m_change_pending) {
58  worker->exec_in_worker_ctx([this](struct ev_loop* loop) {
59  tdbg(get_string() << ": updating watcher" << std::endl);
60  bool active = ev_is_active(m_io.get());
61  if (active) {
62  ev_io_stop(loop, m_io.get());
63  }
64  ev_io_set(m_io.get(), iob().fd(), m_events);
65  if (active) {
66  ev_io_start(loop, m_io.get());
67  }
68  });
69  m_change_pending = false;
70  }
71 }
72 
74  worker->exec_in_worker_ctx([this](struct ev_loop* loop) {
75  if (ev_is_active(watcher())) {
76  tdbg(get_string() << ": disposing io_task_base" << std::endl);
77  ev_io_stop(loop, watcher());
78  }
79  delete this;
80  });
81 }
82 
83 } // tasks
virtual void dispose(worker *worker)
Stop the watcher before being deleted.
#define tdbg(m)
Definition: logging.h:54
std::unique_ptr< ev_io > m_io
Definition: io_task_base.h:65
virtual void init_watcher()
Initialize the watcher.
int events() const
Return the monitored events.
Definition: io_task_base.h:38
int fd() const
Definition: io_base.h:22
virtual void update_watcher(worker *worker)
Udate a watcher in the context of the given worker.
bool exec_in_worker_ctx(task_func_t f)
Definition: worker.h:89
io_task_base(int events)
virtual void stop_watcher(worker *worker)
Stop a watcher in the context of the given worker.
virtual void start_watcher(worker *worker)
Start a watcher in the context of the given worker.
void set_events(int events)
Update the events the object monitors.
std::string get_string() const
Definition: io_task_base.h:31
virtual io_base & iob()=0
Return the io_base object.
ev_io * watcher() const
Return the io watcher object.
Definition: io_task_base.h:40