libtasks Documentation  1.6
timer_task.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/timer_task.h>
10 #include <tasks/logging.h>
11 #include <unistd.h>
12 
13 namespace tasks {
14 
15 timer_task::timer_task(double after, double repeat) : m_after(after), m_repeat(repeat) {
16  tdbg(get_string() << ": ctor" << std::endl);
17  std::unique_ptr<ev_timer> timer(new ev_timer);
18  m_timer = std::move(timer);
19  ev_timer_init(m_timer.get(), tasks_event_callback<ev_timer*>, after, repeat);
20  m_timer->data = this;
21 }
22 
23 timer_task::~timer_task() { tdbg(get_string() << ": dtor" << std::endl); }
24 
26  worker->exec_in_worker_ctx([this](struct ev_loop* loop) {
27  if (!ev_is_active(m_timer.get())) {
28  tdbg(get_string() << ": starting watcher" << std::endl);
29  ev_timer_start(loop, m_timer.get());
30  }
31  });
32 }
33 
35  worker->exec_in_worker_ctx([this](struct ev_loop* loop) {
36  if (ev_is_active(m_timer.get())) {
37  tdbg(get_string() << ": stopping watcher" << std::endl);
38  ev_timer_stop(loop, m_timer.get());
39  }
40  });
41 }
42 
43 } // tasks
virtual ~timer_task()
Definition: timer_task.cpp:23
#define tdbg(m)
Definition: logging.h:54
void stop_watcher(worker *worker)
Deactivate the underlying watcher.
Definition: timer_task.cpp:34
std::unique_ptr< ev_timer > m_timer
Definition: timer_task.h:42
void start_watcher(worker *worker)
Activate the underlying watcher to listen for I/O or timer events.
Definition: timer_task.cpp:25
double after() const
Definition: timer_task.h:33
double repeat() const
Definition: timer_task.h:35
bool exec_in_worker_ctx(task_func_t f)
Definition: worker.h:89
std::string get_string() const
Definition: timer_task.h:25
timer_task(double after, double repeat)
Definition: timer_task.cpp:15