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 | #ifndef SYNC_SCHEDULER_H |
| 24 | #define SYNC_SCHEDULER_H |
| 25 | |
| 26 | #include <boost/thread/thread.hpp> |
| 27 | #include <boost/thread/mutex.hpp> |
| 28 | |
| 29 | #include "sync-logic-event-container.h" |
| 30 | |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame^] | 31 | #define TIME_SECONDS(number) boost::posix_time::seconds (number) |
| 32 | typedef boost::posix_time::time_duration TimeDuration; |
| 33 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 34 | namespace Sync { |
| 35 | |
| 36 | /** |
| 37 | * @ingroup sync |
| 38 | * @brief General purpose event scheduler |
| 39 | * |
| 40 | * This class internally runs a thread and events can be scheduled by specifying an absolute or relative time of the event |
| 41 | */ |
| 42 | class Scheduler |
| 43 | { |
| 44 | public: |
| 45 | /** |
| 46 | * @brief Default constructor. Thread will be created |
| 47 | */ |
| 48 | Scheduler (); |
| 49 | /** |
| 50 | * @brief Destructor. Thread will be nicely stopped |
| 51 | */ |
| 52 | ~Scheduler (); |
| 53 | |
| 54 | /** |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 55 | * @brief Schedule an event at relative time 'reltime' |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 56 | * @param reltime Relative time |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 57 | * @param event function to be called at the time |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 58 | * @param label Label for the event |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 59 | */ |
| 60 | void |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame^] | 61 | schedule (const TimeDuration &reltime, Event event, uint32_t label); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @brief Cancel all events for the label |
| 65 | * @param label Label of the event that needs to be cancelled |
| 66 | */ |
| 67 | void |
| 68 | cancel (uint32_t label); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 69 | |
| 70 | #ifdef _DEBUG |
| 71 | size_t |
| 72 | getEventsSize () |
| 73 | { |
| 74 | boost::lock_guard<boost::mutex> lock (m_eventsMutex); |
| 75 | return m_events.size (); |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | private: |
| 80 | void |
| 81 | threadLoop (); |
| 82 | |
| 83 | private: |
| 84 | EventsContainer m_events; |
| 85 | boost::condition_variable m_eventsCondition; |
| 86 | boost::mutex m_eventsMutex; |
| 87 | |
| 88 | boost::thread m_thread; |
| 89 | bool m_threadRunning; |
| 90 | }; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | #endif // SYNC_SCHEDULER_H |