Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "sync-scheduler.h" |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 24 | #include "sync-logging.h" |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace boost; |
| 27 | using namespace std; |
| 28 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 29 | INIT_LOGGER ("Scheduler"); |
| 30 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 31 | namespace Sync { |
| 32 | |
| 33 | Scheduler::Scheduler () |
| 34 | : m_threadRunning (true) |
| 35 | { |
| 36 | m_thread = thread (&Scheduler::threadLoop, this); |
| 37 | } |
| 38 | |
| 39 | Scheduler::~Scheduler () |
| 40 | { |
| 41 | m_threadRunning = false; |
| 42 | // cout << "Requested stop" << this_thread::get_id () << endl; |
| 43 | m_thread.interrupt (); |
| 44 | m_thread.join (); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | Scheduler::threadLoop () |
| 49 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 50 | _LOG_FUNCTION (this); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 51 | while (m_threadRunning) |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | boost::system_time nextTime; |
| 56 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame^] | 57 | boost::unique_lock<boost::mutex> lock (m_eventsMutex); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 58 | while (m_threadRunning && m_events.size () == 0) |
| 59 | { |
| 60 | m_eventsCondition.wait (lock); |
| 61 | // cout << "Got something" << endl; |
| 62 | } |
| 63 | |
| 64 | if (m_events.size () == 0) continue; |
| 65 | |
| 66 | nextTime = m_events.begin ()->time; |
| 67 | } |
| 68 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 69 | if (nextTime > get_system_time ()) |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 70 | { |
| 71 | this_thread::sleep (nextTime - get_system_time ()); |
| 72 | |
| 73 | // sleeping |
| 74 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 75 | if (nextTime > get_system_time ()) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 76 | { |
| 77 | // cout << "expected here" << endl; |
| 78 | continue; // something changes, try again |
| 79 | } |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!m_threadRunning) continue; |
| 83 | |
| 84 | Event event; |
| 85 | |
| 86 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame^] | 87 | boost::lock_guard<boost::mutex> lock (m_eventsMutex); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 89 | if (m_events.size () == 0) |
| 90 | { |
| 91 | // cout << "Here" << endl; |
| 92 | continue; |
| 93 | } |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 94 | |
| 95 | event = m_events.begin ()->event; |
| 96 | m_events.erase (m_events.begin ()); |
| 97 | } |
| 98 | |
| 99 | event (); // calling the event outside the locked mutex |
| 100 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 101 | catch (thread_interrupted &e) |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 102 | { |
| 103 | // cout << "interrupted: " << this_thread::get_id () << endl; |
| 104 | // do nothing |
| 105 | } |
| 106 | } |
| 107 | // cout << "Exited...\n"; |
| 108 | } |
| 109 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 110 | void |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 111 | Scheduler::schedule (const TimeDuration &reltime, Event event, uint32_t label) |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 112 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame^] | 113 | boost::lock_guard<boost::mutex> lock (m_eventsMutex); |
Alexander Afanasyev | 97b5d40 | 2012-06-07 10:30:12 -0700 | [diff] [blame] | 114 | m_events.insert (LogicEvent (boost::get_system_time () + reltime, event, label)); |
| 115 | |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 116 | m_eventsCondition.notify_one (); |
| 117 | m_thread.interrupt (); // interrupt sleep, if currently sleeping |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 120 | void |
| 121 | Scheduler::cancel (uint32_t label) |
| 122 | { |
Alexander Afanasyev | 97b5d40 | 2012-06-07 10:30:12 -0700 | [diff] [blame] | 123 | // cout << "Canceling label " << label << " size: " << m_events.size () << endl; |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame^] | 124 | boost::lock_guard<boost::mutex> lock (m_eventsMutex); |
Alexander Afanasyev | 97b5d40 | 2012-06-07 10:30:12 -0700 | [diff] [blame] | 125 | m_events.get<byLabel> ().erase (label); |
| 126 | // cout << "Canceled label " << label << " size: " << m_events.size () << endl; |
| 127 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 128 | m_eventsCondition.notify_one (); |
| 129 | m_thread.interrupt (); // interrupt sleep, if currently sleeping |
| 130 | } |
| 131 | |
| 132 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 133 | } // Sync |