blob: 155c6bac30764ff253c57f422495736361df39e7 [file] [log] [blame]
Alexander Afanasyev45fba082012-03-12 18:05:24 -07001/* -*- 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 Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev45fba082012-03-12 18:05:24 -070020 * 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 Afanasyev181d7e52012-04-09 13:54:11 -070031#define TIME_SECONDS(number) boost::posix_time::seconds (number)
Alexander Afanasyev3a229132012-04-25 15:07:26 -070032#define TIME_MILLISECONDS(number) boost::posix_time::milliseconds(number)
33#define TIME_NOW boost::get_system_time ()
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070034typedef boost::posix_time::time_duration TimeDuration;
Alexander Afanasyev3a229132012-04-25 15:07:26 -070035typedef boost::system_time TimeAbsolute;
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070036
Alexander Afanasyev45fba082012-03-12 18:05:24 -070037namespace Sync {
38
39/**
40 * @ingroup sync
41 * @brief General purpose event scheduler
42 *
43 * This class internally runs a thread and events can be scheduled by specifying an absolute or relative time of the event
44 */
45class Scheduler
46{
47public:
48 /**
49 * @brief Default constructor. Thread will be created
50 */
51 Scheduler ();
52 /**
53 * @brief Destructor. Thread will be nicely stopped
54 */
55 ~Scheduler ();
56
57 /**
Alexander Afanasyev45fba082012-03-12 18:05:24 -070058 * @brief Schedule an event at relative time 'reltime'
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070059 * @param reltime Relative time
Alexander Afanasyev45fba082012-03-12 18:05:24 -070060 * @param event function to be called at the time
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070061 * @param label Label for the event
Alexander Afanasyev45fba082012-03-12 18:05:24 -070062 */
63 void
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070064 schedule (const TimeDuration &reltime, Event event, uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070065
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070066 /**
67 * @brief Cancel all events for the label
68 * @param label Label of the event that needs to be cancelled
69 */
70 void
71 cancel (uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070072
73#ifdef _DEBUG
74 size_t
75 getEventsSize ()
76 {
77 boost::lock_guard<boost::mutex> lock (m_eventsMutex);
78 return m_events.size ();
79 }
80#endif
81
82private:
83 void
84 threadLoop ();
85
86private:
87 EventsContainer m_events;
88 boost::condition_variable m_eventsCondition;
89 boost::mutex m_eventsMutex;
90
91 boost::thread m_thread;
92 bool m_threadRunning;
93};
94
95}
96
97#endif // SYNC_SCHEDULER_H