9 static const char rcsid[] __attribute__((used)) =
"$Id: Task.cpp 1653 2016-02-28 19:54:59Z sella $";
19 using namespace sella::util;
21 Task::Task() : timer({0, 0}), priority(0),
function(NULL),
object(NULL) {
25 Task::Task(Task::callback &callback,
void *
object,
int priority) :
34 Task::Task(
const Task &other) :
36 priority(other.priority),
37 function(other.function),
42 Task::Task(
const Task &&other) :
44 priority(other.priority),
45 function(std::move(other.function)),
54 Task& Task::operator=(
const Task &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;
66 Task& Task::operator=(
const Task &&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;
78 void Task::clear(
void) {
86 Task& Task::setTimer(
const std::chrono::system_clock::time_point &time) {
87 this->timer = Time::chronototv(time);
92 Task& Task::setTimer(timeval &timer) {
98 Task& Task::setTimer(time_t &timer) {
99 this->timer = Time::sectotv(timer);
104 Task& Task::setDelay(
const std::chrono::system_clock::duration &duration) {
105 timeval now = { 0, 0 }, delay = Time::chronototv(duration);
107 if (gettimeofday(&now, NULL) == 0) {
108 timeradd(&now, &delay, &(this->timer));
114 Task& Task::setDelay(time_t sec, suseconds_t usec) {
115 timeval now = { 0, 0 }, delay = { sec + usec / 1000000, usec % 1000000 };
117 if (gettimeofday(&now, NULL) == 0) {
118 timeradd(&now, &delay, &(this->timer));
124 Task& Task::setNoDelay(
void) {
125 timerclear(&(this->timer));
130 Task& Task::setPriority(
int priority) {
131 this->priority = priority;
136 Task& Task::setCallback(callback &
function,
void *
object) {
137 this->
function =
function;
138 this->
object = object;
144 Task& Task::setObject(
void *
object) {
145 this->
object = object;
150 Task& Task::setData(std::vector<void*> &data) {
156 Task& Task::addData(
void *data) {
157 this->data.push_back(data);
162 const timeval& Task::getTimer(
void)
const {
166 const timeval Task::getDelay(
void)
const {
167 static const timeval zero = { 0, 0 };
168 timeval delay = { 0, 0 };
170 if (gettimeofday(&delay, NULL) == 0) {
171 if (timercmp(&delay, &(this->timer), <)) {
172 timersub(&timer, &delay, &delay);
174 if (timercmp(&delay, &zero, <)) {
185 uint64_t Task::getDelayMsec(
void)
const {
186 return Time::tvtomsec(getDelay());
189 uint64_t Task::getDelayUsec(
void)
const {
190 return Time::tvtousec(getDelay());
193 int Task::getPriority(
void)
const {
197 const Task::callback& Task::getCallback(
void)
const {
201 void* Task::getObject(
void)
const {
205 std::vector<void*> Task::getData(
void)
const {
209 void* Task::getData(
size_t pos)
const throw (std::out_of_range) {
213 bool Task::isReady(
void)
const {
216 gettimeofday(&now, NULL);
218 if (timercmp(&now, &timer, >=)) {
225 void Task::execute(
void) {
231 const Task::shared Task::shared_ptr(
void) throw () {
232 return std::make_shared<Task>();
235 const Task::shared Task::shared_ptr(Task::callback &callback,
void *
object,
int priority) {
236 return std::make_shared<Task>(callback, object, priority);
239 const timeval Task::getDelay(
const shared task, timeval delay) {
241 return task->getDelay();
247 bool Task::compareTimer(
const shared task, timeval now) {
249 timeval timer = task->getTimer();
251 if (!timerisset(&now)) {
252 gettimeofday(&now, NULL);
255 if (timercmp(&now, &timer, >=)) {