andrewm@0
|
1 /*
|
andrewm@0
|
2 TouchKeys: multi-touch musical keyboard control software
|
andrewm@0
|
3 Copyright (c) 2013 Andrew McPherson
|
andrewm@0
|
4
|
andrewm@0
|
5 This program is free software: you can redistribute it and/or modify
|
andrewm@0
|
6 it under the terms of the GNU General Public License as published by
|
andrewm@0
|
7 the Free Software Foundation, either version 3 of the License, or
|
andrewm@0
|
8 (at your option) any later version.
|
andrewm@0
|
9
|
andrewm@0
|
10 This program is distributed in the hope that it will be useful,
|
andrewm@0
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
andrewm@0
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
andrewm@0
|
13 GNU General Public License for more details.
|
andrewm@0
|
14
|
andrewm@0
|
15 You should have received a copy of the GNU General Public License
|
andrewm@0
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
andrewm@0
|
17
|
andrewm@0
|
18 =====================================================================
|
andrewm@0
|
19
|
andrewm@0
|
20 Scheduler.h: allows actions to be scheduled at future times. Runs a
|
andrewm@0
|
21 thread in which these actions are executed.
|
andrewm@0
|
22 */
|
andrewm@0
|
23
|
andrewm@0
|
24 #ifndef KEYCONTROL_SCHEDULER_H
|
andrewm@0
|
25 #define KEYCONTROL_SCHEDULER_H
|
andrewm@0
|
26
|
andrewm@0
|
27 #include <iostream>
|
andrewm@0
|
28 #include <map>
|
andrewm@0
|
29 #include <boost/bind.hpp>
|
andrewm@0
|
30 #include <boost/function.hpp>
|
andrewm@0
|
31 #include "../JuceLibraryCode/JuceHeader.h"
|
andrewm@0
|
32 #include "Types.h"
|
andrewm@0
|
33
|
andrewm@0
|
34 /*
|
andrewm@0
|
35 * Scheduler
|
andrewm@0
|
36 *
|
andrewm@0
|
37 * This class allows function calls to be scheduled for arbitrary points in the future.
|
andrewm@0
|
38 * It maintains a list of future events, ordered by timestamp. A dedicated thread scans the
|
andrewm@0
|
39 * list, and when it is time for an event to occur, the thread wakes up, executes it, deletes
|
andrewm@0
|
40 * it from the list, and goes back to sleep.
|
andrewm@0
|
41 */
|
andrewm@0
|
42
|
andrewm@0
|
43 class Scheduler : public Thread {
|
andrewm@0
|
44 public:
|
andrewm@0
|
45 typedef boost::function<timestamp_type ()> action;
|
andrewm@0
|
46
|
andrewm@0
|
47 private:
|
andrewm@0
|
48 static const timestamp_diff_type kAllowableAdvanceExecutionTime;
|
andrewm@0
|
49
|
andrewm@0
|
50 public:
|
andrewm@0
|
51 // ***** Constructor *****
|
andrewm@0
|
52 //
|
andrewm@0
|
53 // Note: This class is not copy-constructable.
|
andrewm@0
|
54
|
andrewm@0
|
55 Scheduler(String threadName = "Scheduler") : Thread(threadName), waitableEvent_(true), isRunning_(false) {}
|
andrewm@0
|
56
|
andrewm@0
|
57 // ***** Destructor *****
|
andrewm@0
|
58
|
andrewm@0
|
59 ~Scheduler() noexcept { stop(); }
|
andrewm@0
|
60
|
andrewm@0
|
61 // ***** Timer Methods *****
|
andrewm@0
|
62 //
|
andrewm@0
|
63 // These start and stop the thread that handles the scheduling of events.
|
andrewm@0
|
64
|
andrewm@0
|
65 void start(timestamp_type where = 0);
|
andrewm@0
|
66 void stop();
|
andrewm@0
|
67
|
andrewm@0
|
68 bool isRunning() { return isRunning_; }
|
andrewm@0
|
69 timestamp_type currentTimestamp();
|
andrewm@0
|
70
|
andrewm@0
|
71 // ***** Event Management Methods *****
|
andrewm@0
|
72 //
|
andrewm@0
|
73 // This interface provides the ability to schedule and unschedule events for
|
andrewm@0
|
74 // future times.
|
andrewm@0
|
75
|
andrewm@0
|
76 void schedule(void *who, action func, timestamp_type timestamp);
|
andrewm@0
|
77 void unschedule(void *who, timestamp_type timestamp = 0);
|
andrewm@0
|
78 void clear();
|
andrewm@0
|
79
|
andrewm@0
|
80 //static void staticRunLoop(Scheduler* sch, timestamp_type starting_timestamp) { sch->runLoop(starting_timestamp); }
|
andrewm@0
|
81
|
andrewm@0
|
82 // The main Thread run loop
|
andrewm@0
|
83 void run();
|
andrewm@0
|
84
|
andrewm@0
|
85 private:
|
andrewm@0
|
86 // These variables keep track of the status of the separate thread running the events
|
andrewm@0
|
87 CriticalSection eventMutex_;
|
andrewm@0
|
88 WaitableEvent waitableEvent_;
|
andrewm@0
|
89 timestamp_type startingTimestamp_;
|
andrewm@0
|
90 bool isRunning_;
|
andrewm@0
|
91
|
andrewm@0
|
92 // Collection of future events to execute
|
andrewm@0
|
93 //boost::posix_time::ptime startTime_;
|
andrewm@0
|
94 double startTimeMilliseconds_;
|
andrewm@0
|
95 std::multimap<timestamp_type, std::pair<void*, action> > events_;
|
andrewm@0
|
96 };
|
andrewm@0
|
97
|
andrewm@0
|
98
|
andrewm@0
|
99 #endif /* KEYCONTROL_SCHEDULER_H */ |