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 | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 24 | #include "sync-log.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 | { |
| 57 | unique_lock<mutex> lock (m_eventsMutex); |
| 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 | { |
| 87 | lock_guard<mutex> lock (m_eventsMutex); |
| 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 | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 111 | Scheduler::schedule (const boost::posix_time::time_duration &reltime, Event event, uint32_t label) |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 112 | { |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame^] | 113 | { |
| 114 | lock_guard<mutex> lock (m_eventsMutex); |
| 115 | m_events.insert (LogicEvent (boost::get_system_time () + reltime, event, label)); |
| 116 | } |
| 117 | m_eventsCondition.notify_one (); |
| 118 | m_thread.interrupt (); // interrupt sleep, if currently sleeping |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 121 | void |
| 122 | Scheduler::cancel (uint32_t label) |
| 123 | { |
| 124 | { |
| 125 | // cout << "Canceling label " << label << " size: " << m_events.size () << endl; |
| 126 | lock_guard<mutex> lock (m_eventsMutex); |
| 127 | m_events.get<byLabel> ().erase (label); |
| 128 | // cout << "Canceled label " << label << " size: " << m_events.size () << endl; |
| 129 | } |
| 130 | m_eventsCondition.notify_one (); |
| 131 | m_thread.interrupt (); // interrupt sleep, if currently sleeping |
| 132 | } |
| 133 | |
| 134 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 135 | } // Sync |