blob: 0979f971a5c4948c43932244349eb4ad2a2294e1 [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi27533da2014-12-15 10:56:20 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi27533da2014-12-15 10:56:20 -070024 */
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080025
26#include "scheduler.hpp"
Junxiao Shi98e29f42014-03-31 10:27:26 -070027#include "global-io.hpp"
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shic041ca32014-02-25 20:01:15 -070030namespace scheduler {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080031
Junxiao Shic041ca32014-02-25 20:01:15 -070032static shared_ptr<Scheduler> g_scheduler;
33
Junxiao Shi98e29f42014-03-31 10:27:26 -070034inline Scheduler&
Junxiao Shic041ca32014-02-25 20:01:15 -070035getGlobalScheduler()
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080036{
Junxiao Shic041ca32014-02-25 20:01:15 -070037 if (!static_cast<bool>(g_scheduler)) {
Alexander Afanasyevf6980282014-05-13 18:28:40 -070038 g_scheduler = make_shared<Scheduler>(ref(getGlobalIoService()));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080039 }
Junxiao Shic041ca32014-02-25 20:01:15 -070040 return *g_scheduler;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080041}
42
Junxiao Shi98e29f42014-03-31 10:27:26 -070043EventId
44schedule(const time::nanoseconds& after, const Scheduler::Event& event)
45{
46 return getGlobalScheduler().scheduleEvent(after, event);
47}
48
49void
50cancel(const EventId& eventId)
51{
52 getGlobalScheduler().cancelEvent(eventId);
53}
54
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080055void
Junxiao Shic041ca32014-02-25 20:01:15 -070056resetGlobalScheduler()
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080057{
Junxiao Shic041ca32014-02-25 20:01:15 -070058 g_scheduler.reset();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080059}
60
Junxiao Shi27533da2014-12-15 10:56:20 -070061ScopedEventId::ScopedEventId()
62{
63}
64
65ScopedEventId::ScopedEventId(const EventId& event)
66 : m_event(event)
67{
68}
69
70ScopedEventId::ScopedEventId(ScopedEventId&& other)
71 : m_event(other.m_event)
72{
73 other.release();
74}
75
76ScopedEventId&
77ScopedEventId::operator=(const EventId& event)
78{
79 if (m_event != event) {
80 scheduler::cancel(m_event);
81 m_event = event;
82 }
83 return *this;
84}
85
86ScopedEventId::~ScopedEventId()
87{
88 scheduler::cancel(m_event);
89}
90
91void
92ScopedEventId::cancel()
93{
94 scheduler::cancel(m_event);
95}
96
97void
98ScopedEventId::release()
99{
100 m_event.reset();
101}
102
Junxiao Shic041ca32014-02-25 20:01:15 -0700103} // namespace scheduler
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800104} // namespace nfd