blob: fd310eb206e03b9e632329fc4bc2df7295d429be [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev635bf202017-03-09 21:57:34 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev5d57f972015-02-11 21:04:29 -08004 * 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 Afanasyev5d57f972015-02-11 21:04:29 -080029#include <boost/thread/tss.hpp>
30
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Junxiao Shic041ca32014-02-25 20:01:15 -070032namespace scheduler {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080033
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080034static boost::thread_specific_ptr<Scheduler> g_scheduler;
Junxiao Shic041ca32014-02-25 20:01:15 -070035
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080036Scheduler&
Junxiao Shic041ca32014-02-25 20:01:15 -070037getGlobalScheduler()
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080038{
Alexander Afanasyevbf9945d2014-11-14 17:11:41 -080039 static ndn::DummyIoService io;
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080040 if (g_scheduler.get() == nullptr) {
Alexander Afanasyevbf9945d2014-11-14 17:11:41 -080041 g_scheduler.reset(new Scheduler(io));
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080042 }
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080043
Junxiao Shic041ca32014-02-25 20:01:15 -070044 return *g_scheduler;
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080045}
46
Junxiao Shi98e29f42014-03-31 10:27:26 -070047EventId
Alexander Afanasyev635bf202017-03-09 21:57:34 +000048schedule(time::nanoseconds after, const EventCallback& event)
Junxiao Shi98e29f42014-03-31 10:27:26 -070049{
50 return getGlobalScheduler().scheduleEvent(after, event);
51}
52
53void
54cancel(const EventId& eventId)
55{
56 getGlobalScheduler().cancelEvent(eventId);
57}
58
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080059void
Junxiao Shic041ca32014-02-25 20:01:15 -070060resetGlobalScheduler()
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080061{
Junxiao Shic041ca32014-02-25 20:01:15 -070062 g_scheduler.reset();
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080063}
64
Junxiao Shi27533da2014-12-15 10:56:20 -070065ScopedEventId::ScopedEventId()
66{
67}
68
69ScopedEventId::ScopedEventId(const EventId& event)
70 : m_event(event)
71{
72}
73
74ScopedEventId::ScopedEventId(ScopedEventId&& other)
75 : m_event(other.m_event)
76{
77 other.release();
78}
79
80ScopedEventId&
81ScopedEventId::operator=(const EventId& event)
82{
83 if (m_event != event) {
84 scheduler::cancel(m_event);
85 m_event = event;
86 }
87 return *this;
88}
89
90ScopedEventId::~ScopedEventId()
91{
92 scheduler::cancel(m_event);
93}
94
95void
96ScopedEventId::cancel()
97{
98 scheduler::cancel(m_event);
99}
100
101void
102ScopedEventId::release()
103{
104 m_event.reset();
105}
106
Junxiao Shic041ca32014-02-25 20:01:15 -0700107} // namespace scheduler
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800108} // namespace nfd