OpenSDN source code
task_timer_api.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <string>
7 #include "base/timer.h"
8 
9 #include "task_map.h"
10 #include "task_timer_map.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 #include "task_timer_api.h"
16 #ifdef __cplusplus
17 }
18 #endif
19 
20 extern bool task_timer_callback(void *agent_timer_map);
21 extern bool task_timer_cleanup(void *data);
22 
24 {
25  tp->cleanup_timer = NULL;
27 }
28 
30 {
31  TaskMap *agent_task = (TaskMap *)tp->agent_task;
32 
33  Timer * cleanup_timer;
34  cleanup_timer = TimerManager::CreateTimer(agent_task->io_,
35  "Cleanup Timer",
37  GetTaskId(agent_task->name_),
38  agent_task->instance_, true);
39  tp->cleanup_timer = (void *)cleanup_timer;
40  cleanup_timer->Start(0, boost::bind(task_timer_cleanup, tp));
41 }
42 
44  flag_t flags, task_timer *parent,
45  timer_callback tjob, void *data)
46 {
47  task_timer *timer = (task_timer *)malloc(sizeof(task_timer));
48  if (!timer) {
49  return NULL;
50  }
51 
52  TaskMap *agent_task = (TaskMap *)tp->agent_task;
53  TimerMap *timer_map = new TimerMap();
54 
55  timer_map->timer_name_ = name;
56  timer_map->timer_ = timer;
57  timer_map->agent_timer_ = TimerManager::CreateTimer(agent_task->io_,
58  timer_map->timer_name_,
60  GetTaskId(agent_task->name_),
61  agent_task->instance_, false);
62 
63  timer->tp = tp;
64  timer->callback = tjob;
65  timer->agent_timer_map = timer_map;
66  timer->tdata = data;
67 
68  return timer;
69 }
70 
72 {
73  TimerMap *timer_map = (TimerMap *)timer->agent_timer_map;
74 
75  if (!TimerManager::DeleteTimer(timer_map->agent_timer_)) {
77  &timer->deleted_entry);
79  return;
80  }
81 
82  delete timer_map;
83 
84  free(timer);
85 
86  return;
87 }
88 
90 {
91  return;
92 }
93 
95  task_timer *timer, utime_t *offset, u_int jitter)
96 {
97  TimerMap *timer_map = (TimerMap *)timer->agent_timer_map;
98 
99  Timer *agent_timer = timer_map->agent_timer_;
100 
101  timer->timeout = offset->ut_sec*1000 + offset->ut_usec/1000;
102 
103  task_timer_reset(timer);
104  // If running in the timer context, Agent Timer cannot be started
105  // again. Will inform Agent Timer to restart based on the 'oneshot'
106  // and 'rescheduled' flags in the function 'task_timer_callback'.
107  if (agent_timer->fired()) {
108  agent_timer->Reschedule(timer->timeout);
109  timer->rescheduled = TRUE;
110  } else {
111  agent_timer->Start(timer->timeout,
112  boost::bind(task_timer_callback, timer_map));
113  }
114 
115  timer->oneshot = TRUE;
116 
117  return;
118 }
119 
121 {
122  TimerMap *timer_map = (TimerMap *)timer->agent_timer_map;
123  Timer *agent_timer = (Timer *)timer_map->agent_timer_;
124 
125  agent_timer->Cancel();
126 
127  return;
128 }
129 
131 {
132  return NULL;
133 }
134 
136 {
137  return timer->tdata;
138 }
139 
140 void task_timer_utime_left(task_timer *timer, utime_t *remaining)
141 {
142  TimerMap *timer_map = (TimerMap *)timer->agent_timer_map;
143  Timer *agent_timer = (Timer *)timer_map->agent_timer_;
144 
145  int elapsed = timer->timeout - agent_timer->GetElapsedTime();
146 
147  remaining->ut_usec = elapsed*1000;
148  remaining->ut_sec = 0;
149 
150  return;
151 }
152 
154 {
155  TimerMap *timer_map = (TimerMap *)timer->agent_timer_map;
156  Timer *agent_timer = (Timer *)timer_map->agent_timer_;
157 
158  return agent_timer->running() ? TRUE : FALSE;
159 }
160 
161 bool task_timer_callback(void *agent_timer_map)
162 {
163  TimerMap *timer_map = (TimerMap *)agent_timer_map;
164  task_timer *timer = (task_timer *)timer_map->timer_;
165 
166  timer->callback(timer, 0);
167  bool reschedule = false;
168  if (timer->rescheduled == TRUE) {
169  reschedule = true;
170  timer->rescheduled = FALSE;
171  }
172  if (!reschedule) {
173  reschedule = (!timer->oneshot ? true : false);
174  }
175 
176  return reschedule;
177 }
178 
179 bool task_timer_cleanup(void *data)
180 {
181  task *tp = (task *)data;
182  task_timer *timer = NULL;
183  task_thread *thread_ptr = NULL;
184 
185  while (TRUE) {
186  thread_ptr = thread_circular_top(&tp->deleted_timers);
187  if (!thread_ptr) break;
188 
189  timer = task_timer_list_entry(thread_ptr);
190  thread_remove(thread_ptr);
191  TimerMap *timer_map = (TimerMap *)timer->agent_timer_map;
192  if (!TimerManager::DeleteTimer(timer_map->agent_timer_)) {
193  assert(0);
194  }
195  delete timer_map;
196 
197  free(timer);
198  }
199 
200  tp->cleanup_timer = NULL;
201  return false;
202 }
203 
const std::string & name_
Definition: task_map.h:20
int instance_
Definition: task_map.h:21
boost::asio::io_context & io_
Definition: task_map.h:22
static TaskScheduler * GetInstance()
Definition: task.cc:574
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
static Timer * CreateTimer(boost::asio::io_context &service, const std::string &name, int task_id=Timer::GetTimerTaskId(), int task_instance=Timer::GetTimerInstanceId(), bool delete_on_completion=false)
Definition: timer.cc:201
std::string timer_name_
void * timer_
Timer * agent_timer_
Definition: timer.h:58
bool Cancel()
Definition: timer.cc:150
bool running() const
Definition: timer.h:90
#define TRUE
Definition: mcast_common.h:16
#define FALSE
Definition: mcast_common.h:15
unsigned long flag_t
Definition: mcast_common.h:13
Definition: task_int.h:11
task_thread deleted_timers
Definition: task_int.h:14
void * cleanup_timer
Definition: task_int.h:13
void * agent_task
Definition: task_int.h:12
boolean oneshot
boolean rescheduled
void * agent_timer_map
task_thread deleted_entry
timer_callback callback
time_t ut_sec
time_t ut_usec
void thread_remove(task_thread *node)
task_thread * thread_circular_top(task_thread *head)
void thread_new_circular_thread(task_thread *head)
void thread_circular_add_top(task_thread *head, task_thread *node)
void task_timer_cleanup_deleted(task *tp)
void task_timer_init(task *tp)
void task_timer_uset_alt_root_auto_parent_oneshot(task_timer_root *root, task_timer *timer, utime_t *offset, u_int jitter)
void task_timer_smear_auto_parent_timers(task_timer_root *root)
void * task_timer_data(task_timer *timer)
void task_timer_utime_left(task_timer *timer, utime_t *remaining)
void task_timer_reset(task_timer *timer)
bool task_timer_cleanup(void *data)
boolean task_timer_running(task_timer *timer)
bool task_timer_callback(void *agent_timer_map)
task_timer_root * task_timer_get_auto_parent_root(void)
task_timer * task_timer_create_idle_leaf(task *tp, const char *name, flag_t flags, task_timer *parent, timer_callback tjob, void *data)
void task_timer_delete(task_timer *timer)
void(* timer_callback)(task_timer *, time_t)