Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 10 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #include "periodic-task.h" |
| 14 | #include "logging.h" |
| 15 | #include <utility> |
| 16 | |
| 17 | INIT_LOGGER ("Scheduler.PeriodicTask"); |
| 18 | |
| 19 | PeriodicTask::PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, |
| 20 | IntervalGeneratorPtr generator) |
| 21 | : Task(callback, tag, scheduler) |
| 22 | , m_generator(generator) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | PeriodicTask::run() |
| 28 | { |
| 29 | if (!m_invoked) |
| 30 | { |
| 31 | m_invoked = true; |
| 32 | m_callback(); |
| 33 | |
| 34 | if (m_invoked) |
| 35 | { |
| 36 | // m_invoked getting back if it is rescheduled inside the callback |
| 37 | m_scheduler->rescheduleTask(m_tag); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | PeriodicTask::reset() |
| 44 | { |
| 45 | m_invoked = false; |
| 46 | double interval = m_generator->nextInterval(); |
| 47 | setTv(interval); |
| 48 | } |