libutil++  1.9.3
 All Classes Functions Variables
Task.cpp
1 /*
2 ** libutil++
3 ** $Id: Task.cpp 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: Task.cpp 1653 2016-02-28 19:54:59Z sella $";
10 
11 #include "Task.h"
12 #include "Time.h"
13 
14 #include <stdio.h>
15 #include <signal.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 
19 using namespace sella::util;
20 
21 Task::Task() : timer({0, 0}), priority(0), function(NULL), object(NULL) {
22  timerclear(&timer);
23 }
24 
25 Task::Task(Task::callback &callback, void *object, int priority) :
26  timer({ 0, 0 }),
27  priority(priority),
28  function(callback),
29  object(object)
30 {
31  timerclear(&timer);
32 }
33 
34 Task::Task(const Task &other) :
35  timer(other.timer),
36  priority(other.priority),
37  function(other.function),
38  object(other.object),
39  data(other.data)
40 { }
41 
42 Task::Task(const Task &&other) :
43  timer(other.timer),
44  priority(other.priority),
45  function(std::move(other.function)),
46  object(other.object),
47  data(other.data)
48 { }
49 
50 Task::~Task() {
51  function = NULL;
52 }
53 
54 Task& Task::operator=(const Task &other) {
55  if (this != &other) {
56  this->timer = other.timer;
57  this->priority = other.priority;
58  this->function = other.function;
59  this->object = other.object;
60  this->data = other.data;
61  }
62 
63  return *this;
64 }
65 
66 Task& Task::operator=(const Task &&other) {
67  if (this != &other) {
68  this->timer = other.timer;
69  this->priority = other.priority;
70  this->function = std::move(other.function);
71  this->object = other.object;
72  this->data = other.data;
73  }
74 
75  return *this;
76 }
77 
78 void Task::clear(void) {
79  timerclear(&timer);
80  priority = 0;
81  function = NULL;
82  object = NULL;
83  data.clear();
84 }
85 
86 Task& Task::setTimer(const std::chrono::system_clock::time_point &time) {
87  this->timer = Time::chronototv(time);
88 
89  return *this;
90 }
91 
92 Task& Task::setTimer(timeval &timer) {
93  this->timer = timer;
94 
95  return *this;
96 }
97 
98 Task& Task::setTimer(time_t &timer) {
99  this->timer = Time::sectotv(timer);
100 
101  return *this;
102 }
103 
104 Task& Task::setDelay(const std::chrono::system_clock::duration &duration) {
105  timeval now = { 0, 0 }, delay = Time::chronototv(duration);
106 
107  if (gettimeofday(&now, NULL) == 0) {
108  timeradd(&now, &delay, &(this->timer));
109  }
110 
111  return *this;
112 }
113 
114 Task& Task::setDelay(time_t sec, suseconds_t usec) {
115  timeval now = { 0, 0 }, delay = { sec + usec / 1000000, usec % 1000000 };
116 
117  if (gettimeofday(&now, NULL) == 0) {
118  timeradd(&now, &delay, &(this->timer));
119  }
120 
121  return *this;
122 }
123 
124 Task& Task::setNoDelay(void) {
125  timerclear(&(this->timer));
126 
127  return *this;
128 }
129 
130 Task& Task::setPriority(int priority) {
131  this->priority = priority;
132 
133  return *this;
134 }
135 
136 Task& Task::setCallback(callback &function, void *object) {
137  this->function = function;
138  this->object = object;
139  this->data = data;
140 
141  return *this;
142 }
143 
144 Task& Task::setObject(void *object) {
145  this->object = object;
146 
147  return *this;
148 }
149 
150 Task& Task::setData(std::vector<void*> &data) {
151  this->data = data;
152 
153  return *this;
154 }
155 
156 Task& Task::addData(void *data) {
157  this->data.push_back(data);
158 
159  return *this;
160 }
161 
162 const timeval& Task::getTimer(void) const {
163  return timer;
164 }
165 
166 const timeval Task::getDelay(void) const {
167  static const timeval zero = { 0, 0 };
168  timeval delay = { 0, 0 };
169 
170  if (gettimeofday(&delay, NULL) == 0) {
171  if (timercmp(&delay, &(this->timer), <)) {
172  timersub(&timer, &delay, &delay);
173 
174  if (timercmp(&delay, &zero, <)) {
175  timerclear(&delay);
176  }
177  } else {
178  timerclear(&delay);
179  }
180  }
181 
182  return delay;
183 }
184 
185 uint64_t Task::getDelayMsec(void) const {
186  return Time::tvtomsec(getDelay());
187 }
188 
189 uint64_t Task::getDelayUsec(void) const {
190  return Time::tvtousec(getDelay());
191 }
192 
193 int Task::getPriority(void) const {
194  return priority;
195 }
196 
197 const Task::callback& Task::getCallback(void) const {
198  return function;
199 }
200 
201 void* Task::getObject(void) const {
202  return object;
203 }
204 
205 std::vector<void*> Task::getData(void) const {
206  return data;
207 }
208 
209 void* Task::getData(size_t pos) const throw (std::out_of_range) {
210  return data.at(pos);
211 }
212 
213 bool Task::isReady(void) const {
214  timeval now;
215 
216  gettimeofday(&now, NULL);
217 
218  if (timercmp(&now, &timer, >=)) {
219  return true;
220  }
221 
222  return false;
223 }
224 
225 void Task::execute(void) {
226  if (function) {
227  function();
228  }
229 }
230 
231 const Task::shared Task::shared_ptr(void) throw () {
232  return std::make_shared<Task>();
233 }
234 
235 const Task::shared Task::shared_ptr(Task::callback &callback, void *object, int priority) {
236  return std::make_shared<Task>(callback, object, priority);
237 }
238 
239 const timeval Task::getDelay(const shared task, timeval delay) {
240  if (task) {
241  return task->getDelay();
242  }
243 
244  return delay;
245 }
246 
247 bool Task::compareTimer(const shared task, timeval now) {
248  if (task) {
249  timeval timer = task->getTimer();
250 
251  if (!timerisset(&now)) {
252  gettimeofday(&now, NULL);
253  }
254 
255  if (timercmp(&now, &timer, >=)) {
256  return true;
257  }
258  }
259 
260  return false;
261 }
262 
263 /*
264 ** vim: noet ts=3 sw=3
265 */