blob: c7ebe0946a33ee88b097317bde2fa46a7b1d7399 [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)
32typedef boost::posix_time::time_duration TimeDuration;
33
Alexander Afanasyev45fba082012-03-12 18:05:24 -070034namespace 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 */
42class Scheduler
43{
44public:
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 Afanasyev45fba082012-03-12 18:05:24 -070055 * @brief Schedule an event at relative time 'reltime'
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070056 * @param reltime Relative time
Alexander Afanasyev45fba082012-03-12 18:05:24 -070057 * @param event function to be called at the time
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070058 * @param label Label for the event
Alexander Afanasyev45fba082012-03-12 18:05:24 -070059 */
60 void
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070061 schedule (const TimeDuration &reltime, Event event, uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070062
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070063 /**
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 Afanasyev45fba082012-03-12 18:05:24 -070069
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
79private:
80 void
81 threadLoop ();
82
83private:
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