Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 635bf20 | 2017-03-09 21:57:34 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 25 | |
| 26 | #include "scheduler.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 27 | #include "global-io.hpp" |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 29 | #include <boost/thread/tss.hpp> |
| 30 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 31 | namespace nfd { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 32 | namespace scheduler { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 34 | static boost::thread_specific_ptr<Scheduler> g_scheduler; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 36 | Scheduler& |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 37 | getGlobalScheduler() |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 38 | { |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 39 | if (g_scheduler.get() == nullptr) { |
Alexander Afanasyev | 740f1ed | 2014-11-14 17:11:41 -0800 | [diff] [blame] | 40 | g_scheduler.reset(new Scheduler(*static_cast<boost::asio::io_service*>(nullptr))); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 41 | } |
Alexander Afanasyev | 5d57f97 | 2015-02-11 21:04:29 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 43 | return *g_scheduler; |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 46 | EventId |
Alexander Afanasyev | 635bf20 | 2017-03-09 21:57:34 +0000 | [diff] [blame] | 47 | schedule(time::nanoseconds after, const EventCallback& event) |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 48 | { |
| 49 | return getGlobalScheduler().scheduleEvent(after, event); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | cancel(const EventId& eventId) |
| 54 | { |
| 55 | getGlobalScheduler().cancelEvent(eventId); |
| 56 | } |
| 57 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 58 | void |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 59 | resetGlobalScheduler() |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 60 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 61 | g_scheduler.reset(); |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 64 | ScopedEventId::ScopedEventId() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | ScopedEventId::ScopedEventId(const EventId& event) |
| 69 | : m_event(event) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | ScopedEventId::ScopedEventId(ScopedEventId&& other) |
| 74 | : m_event(other.m_event) |
| 75 | { |
| 76 | other.release(); |
| 77 | } |
| 78 | |
| 79 | ScopedEventId& |
| 80 | ScopedEventId::operator=(const EventId& event) |
| 81 | { |
| 82 | if (m_event != event) { |
| 83 | scheduler::cancel(m_event); |
| 84 | m_event = event; |
| 85 | } |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | ScopedEventId::~ScopedEventId() |
| 90 | { |
| 91 | scheduler::cancel(m_event); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | ScopedEventId::cancel() |
| 96 | { |
| 97 | scheduler::cancel(m_event); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | ScopedEventId::release() |
| 102 | { |
| 103 | m_event.reset(); |
| 104 | } |
| 105 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 106 | } // namespace scheduler |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 107 | } // namespace nfd |