blob: 110c4dfcc52f3ec6ecce1ea676e666bcd6252440 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
17INIT_LOGGER ("Scheduler.PeriodicTask");
18
19PeriodicTask::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
26void
27PeriodicTask::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
42void
43PeriodicTask::reset()
44{
45 m_invoked = false;
46 double interval = m_generator->nextInterval();
47 setTv(interval);
48}