00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: Task.cpp 1230 2014-11-16 02:32:01Z sella $";
00010
00011 #include "Task.h"
00012 #include "Time.h"
00013
00014 #include <stdio.h>
00015 #include <signal.h>
00016 #include <stdlib.h>
00017 #include <unistd.h>
00018
00019 using namespace sella::util;
00020
00021 Task::Task() : timer({0, 0}), priority(0), function(NULL), object(NULL) {
00022 timerclear(&timer);
00023 }
00024
00025 Task::Task(Task::callback &callback, void *object, int priority) :
00026 timer({ 0, 0 }),
00027 priority(priority),
00028 function(callback),
00029 object(object)
00030 {
00031 timerclear(&timer);
00032 }
00033
00034 Task::Task(const Task &other) :
00035 timer(other.timer),
00036 priority(other.priority),
00037 function(other.function),
00038 object(other.object),
00039 data(other.data)
00040 { }
00041
00042 Task::Task(const Task &&other) :
00043 timer(other.timer),
00044 priority(other.priority),
00045 function(std::move(other.function)),
00046 object(other.object),
00047 data(other.data)
00048 { }
00049
00050 Task::~Task() { }
00051
00052 Task& Task::operator=(const Task &other) {
00053 if (this != &other) {
00054 this->timer = other.timer;
00055 this->priority = other.priority;
00056 this->function = other.function;
00057 this->object = other.object;
00058 this->data = other.data;
00059 }
00060
00061 return *this;
00062 }
00063
00064 Task& Task::operator=(const Task &&other) {
00065 if (this != &other) {
00066 this->timer = other.timer;
00067 this->priority = other.priority;
00068 this->function = std::move(other.function);
00069 this->object = other.object;
00070 this->data = other.data;
00071 }
00072
00073 return *this;
00074 }
00075
00076 void Task::clear(void) {
00077 timerclear(&timer);
00078 priority = 0;
00079 function = NULL;
00080 object = NULL;
00081 data.clear();
00082 }
00083
00084 Task& Task::setTimer(const std::chrono::system_clock::time_point &time) {
00085 this->timer = Time::chronototv(time);
00086
00087 return *this;
00088 }
00089
00090 Task& Task::setTimer(timeval &timer) {
00091 this->timer = timer;
00092
00093 return *this;
00094 }
00095
00096 Task& Task::setTimer(time_t &timer) {
00097 this->timer = Time::sectotv(timer);
00098
00099 return *this;
00100 }
00101
00102 Task& Task::setDelay(const std::chrono::system_clock::duration &duration) {
00103 timeval now = { 0, 0 }, delay = Time::chronototv(duration);
00104
00105 if (gettimeofday(&now, NULL) == 0) {
00106 timeradd(&now, &delay, &(this->timer));
00107 }
00108
00109 return *this;
00110 }
00111
00112 Task& Task::setDelay(time_t sec, suseconds_t usec) {
00113 timeval now = { 0, 0 }, delay = { sec + usec / 1000000, usec % 1000000 };
00114
00115 if (gettimeofday(&now, NULL) == 0) {
00116 timeradd(&now, &delay, &(this->timer));
00117 }
00118
00119 return *this;
00120 }
00121
00122 Task& Task::setNoDelay(void) {
00123 timerclear(&(this->timer));
00124
00125 return *this;
00126 }
00127
00128 Task& Task::setPriority(int priority) {
00129 this->priority = priority;
00130
00131 return *this;
00132 }
00133
00134 Task& Task::setCallback(callback &function, void *object) {
00135 this->function = function;
00136 this->object = object;
00137 this->data = data;
00138
00139 return *this;
00140 }
00141
00142 Task& Task::setObject(void *object) {
00143 this->object = object;
00144
00145 return *this;
00146 }
00147
00148 Task& Task::setData(std::vector<void*> &data) {
00149 this->data = data;
00150
00151 return *this;
00152 }
00153
00154 Task& Task::addData(void *data) {
00155 this->data.push_back(data);
00156
00157 return *this;
00158 }
00159
00160 const timeval& Task::getTimer(void) const {
00161 return timer;
00162 }
00163
00164 const timeval Task::getDelay(void) const {
00165 static const timeval zero = { 0, 0 };
00166 timeval delay = { 0, 0 };
00167
00168 if (gettimeofday(&delay, NULL) == 0) {
00169 if (timercmp(&delay, &(this->timer), <)) {
00170 timersub(&timer, &delay, &delay);
00171
00172 if (timercmp(&delay, &zero, <)) {
00173 timerclear(&delay);
00174 }
00175 } else {
00176 timerclear(&delay);
00177 }
00178 }
00179
00180 return delay;
00181 }
00182
00183 int Task::getPriority(void) const {
00184 return priority;
00185 }
00186
00187 const Task::callback& Task::getCallback(void) const {
00188 return function;
00189 }
00190
00191 void* Task::getObject(void) const {
00192 return object;
00193 }
00194
00195 std::vector<void*> Task::getData(void) const {
00196 return data;
00197 }
00198
00199 void* Task::getData(size_t pos) const throw (std::out_of_range) {
00200 return data.at(pos);
00201 }
00202
00203 void Task::execute(void) {
00204 if (function) {
00205 function();
00206 }
00207 }
00208
00209 const Task::shared Task::shared_ptr(void) throw () {
00210 return std::make_shared<Task>();
00211 }
00212
00213 const Task::shared Task::shared_ptr(Task::callback &callback, void *object, int priority) {
00214 return std::make_shared<Task>(callback, object, priority);
00215 }
00216
00217 const timeval Task::getDelay(const shared task, timeval delay) {
00218 if (task) {
00219 return task->getDelay();
00220 }
00221
00222 return delay;
00223 }
00224
00225 bool Task::compareTimer(const shared task, timeval now) {
00226 if (task) {
00227 timeval timer = task->getTimer();
00228
00229 if (!timerisset(&now)) {
00230 gettimeofday(&now, NULL);
00231 }
00232
00233 if (timercmp(&now, &timer, >=)) {
00234 return true;
00235 }
00236 }
00237
00238 return false;
00239 }
00240
00241
00242
00243