21 std::unique_lock<std::mutex> lock(
timer_->mutex_);
38 if (
ec_ && !
timer_->error_handler_.empty()) {
40 std::string(
ec_.category().name()),
43 restart =
timer_->handler_();
51 }
else if (
timer_->delete_on_completion_) {
62 std::scoped_lock lock(
timer_->mutex_);
64 if (
timer_->timer_task_ !=
this) {
65 assert(!
timer_->timer_task_);
68 timer_->timer_task_ = NULL;
73 return timer_->Description();
78 boost::system::error_code
ec_;
82 Timer::Timer(boost::asio::io_context &service,
const std::string &name,
83 int task_id,
int task_instance,
bool delete_on_completion)
92 task_instance_(task_instance),
94 delete_on_completion_(delete_on_completion) {
108 std::scoped_lock lock(
mutex_);
123 boost::system::error_code ec;
150 std::scoped_lock lock(
mutex_);
172 const boost::system::error_code &ec) {
173 std::scoped_lock lock(
mutex_);
180 if (ec && ec.value() == boost::asio::error::operation_aborted) {
201 boost::asio::io_context &service,
const std::string &name,
202 int task_id,
int task_instance,
bool delete_on_completion) {
203 Timer *timer =
new Timer(service, name, task_id, task_instance,
204 delete_on_completion);
210 std::scoped_lock lock(
mutex_);
222 if (!timer || timer->
fired())
return false;
227 std::scoped_lock lock(
mutex_);
235 std::scoped_lock lock(
mutex_);
238 #if __cplusplus >= 201103L
239 elapsed = std::chrono::nanoseconds(
impl_->expires_from_now()).count();
241 elapsed = boost::chrono::nanoseconds(
impl_->expires_from_now()).count();
244 elapsed =
time_ - elapsed / 1000000;
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
static TaskScheduler * GetInstance()
CancelReturnCode Cancel(Task *task)
Cancels a Task that can be in RUN/WAIT state. The caller needs to ensure that the task exists when Ca...
Task is a class to describe a computational task within OpenSDN control plane applications....
bool task_cancelled() const
Returns true if the task has been canceled.
static TimerSet timer_ref_
std::set< TimerPtr, TimerPtrCmp > TimerSet
static bool DeleteTimer(Timer *Timer)
static void AddTimer(Timer *Timer)
boost::intrusive_ptr< Timer > TimerPtr
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)
virtual std::string Description() const
Gives a description of the task.
void OnTaskCancel()
Called on task exit, if it is marked for cancellation. If the user wants to do any cleanup on task ca...
DISALLOW_COPY_AND_ASSIGN(TimerTask)
TimerTask(TimerPtr timer, boost::system::error_code ec)
boost::system::error_code ec_
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
bool IsDeleteOnCompletion() const
void SetState(TimerState s)
Timer(boost::asio::io_context &service, const std::string &name, int task_id, int task_instance, bool delete_on_completion=false)
boost::scoped_ptr< TimerImpl > impl_
int64_t GetElapsedTime() const
ErrorHandler error_handler_
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
boost::intrusive_ptr< Timer > TimerPtr
boost::function< bool(void)> Handler
void StartTimerTask(TimerPtr reference, int time, uint32_t seq_no, const boost::system::error_code &ec)
bool Reschedule(int time)
boost::function< void(std::string, std::string, std::string)> ErrorHandler
std::atomic< int > refcount_