blob: 53b40afc3ef431eb31731c0bf6c3955ec8b30123 [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
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 /**
Alexander Afanasyev45fba082012-03-12 18:05:24 -070052 * @brief Schedule an event at relative time 'reltime'
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070053 * @param reltime Relative time
Alexander Afanasyev45fba082012-03-12 18:05:24 -070054 * @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::posix_time::time_duration &reltime, Event event, uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070059
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070060 /**
61 * @brief Cancel all events for the label
62 * @param label Label of the event that needs to be cancelled
63 */
64 void
65 cancel (uint32_t label);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070066
67#ifdef _DEBUG
68 size_t
69 getEventsSize ()
70 {
71 boost::lock_guard<boost::mutex> lock (m_eventsMutex);
72 return m_events.size ();
73 }
74#endif
75
76private:
77 void
78 threadLoop ();
79
80private:
81 EventsContainer m_events;
82 boost::condition_variable m_eventsCondition;
83 boost::mutex m_eventsMutex;
84
85 boost::thread m_thread;
86 bool m_threadRunning;
87};
88
89}
90
91#endif // SYNC_SCHEDULER_H