blob: ac1eef9640ea109b9e7faaec9cf8b74a54ba9319 [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
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
31namespace Sync {
32
33/**
34 * @ingroup sync
35 * @brief General purpose event scheduler
36 *
37 * This class internally runs a thread and events can be scheduled by specifying an absolute or relative time of the event
38 */
39class Scheduler
40{
41public:
42 /**
43 * @brief Default constructor. Thread will be created
44 */
45 Scheduler ();
46 /**
47 * @brief Destructor. Thread will be nicely stopped
48 */
49 ~Scheduler ();
50
51 /**
52 * @brief Schedule an event at absolute time 'abstime'
53 * @param abstime Absolute time
54 * @param event function to be called at the time
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070055 * @param label Label for the event
Alexander Afanasyev45fba082012-03-12 18:05:24 -070056 */
57 void
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070058 schedule (const boost::system_time &abstime, Event event, uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070059
60 /**
61 * @brief Schedule an event at relative time 'reltime'
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070062 * @param reltime Relative time
Alexander Afanasyev45fba082012-03-12 18:05:24 -070063 * @param event function to be called at the time
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070064 * @param label Label for the event
Alexander Afanasyev45fba082012-03-12 18:05:24 -070065 */
66 void
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070067 schedule (const boost::posix_time::time_duration &reltime, Event event, uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070068
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070069 /**
70 * @brief Cancel all events for the label
71 * @param label Label of the event that needs to be cancelled
72 */
73 void
74 cancel (uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070075
76#ifdef _DEBUG
77 size_t
78 getEventsSize ()
79 {
80 boost::lock_guard<boost::mutex> lock (m_eventsMutex);
81 return m_events.size ();
82 }
83#endif
84
85private:
86 void
87 threadLoop ();
88
89private:
90 EventsContainer m_events;
91 boost::condition_variable m_eventsCondition;
92 boost::mutex m_eventsMutex;
93
94 boost::thread m_thread;
95 bool m_threadRunning;
96};
97
98}
99
100#endif // SYNC_SCHEDULER_H