andrewm@0: /* andrewm@0: TouchKeys: multi-touch musical keyboard control software andrewm@0: Copyright (c) 2013 Andrew McPherson andrewm@0: andrewm@0: This program is free software: you can redistribute it and/or modify andrewm@0: it under the terms of the GNU General Public License as published by andrewm@0: the Free Software Foundation, either version 3 of the License, or andrewm@0: (at your option) any later version. andrewm@0: andrewm@0: This program is distributed in the hope that it will be useful, andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrewm@0: GNU General Public License for more details. andrewm@0: andrewm@0: You should have received a copy of the GNU General Public License andrewm@0: along with this program. If not, see . andrewm@0: andrewm@0: ===================================================================== andrewm@0: andrewm@0: Scheduler.h: allows actions to be scheduled at future times. Runs a andrewm@0: thread in which these actions are executed. andrewm@0: */ andrewm@0: andrewm@0: #ifndef KEYCONTROL_SCHEDULER_H andrewm@0: #define KEYCONTROL_SCHEDULER_H andrewm@0: andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include "../JuceLibraryCode/JuceHeader.h" andrewm@0: #include "Types.h" andrewm@0: andrewm@0: /* andrewm@0: * Scheduler andrewm@0: * andrewm@0: * This class allows function calls to be scheduled for arbitrary points in the future. andrewm@0: * It maintains a list of future events, ordered by timestamp. A dedicated thread scans the andrewm@0: * list, and when it is time for an event to occur, the thread wakes up, executes it, deletes andrewm@0: * it from the list, and goes back to sleep. andrewm@0: */ andrewm@0: andrewm@0: class Scheduler : public Thread { andrewm@0: public: andrewm@0: typedef boost::function action; andrewm@0: andrewm@0: private: andrewm@0: static const timestamp_diff_type kAllowableAdvanceExecutionTime; andrewm@0: andrewm@0: public: andrewm@0: // ***** Constructor ***** andrewm@0: // andrewm@0: // Note: This class is not copy-constructable. andrewm@0: andrewm@0: Scheduler(String threadName = "Scheduler") : Thread(threadName), waitableEvent_(true), isRunning_(false) {} andrewm@0: andrewm@0: // ***** Destructor ***** andrewm@0: andrewm@0: ~Scheduler() noexcept { stop(); } andrewm@0: andrewm@0: // ***** Timer Methods ***** andrewm@0: // andrewm@0: // These start and stop the thread that handles the scheduling of events. andrewm@0: andrewm@0: void start(timestamp_type where = 0); andrewm@0: void stop(); andrewm@0: andrewm@0: bool isRunning() { return isRunning_; } andrewm@0: timestamp_type currentTimestamp(); andrewm@0: andrewm@0: // ***** Event Management Methods ***** andrewm@0: // andrewm@0: // This interface provides the ability to schedule and unschedule events for andrewm@0: // future times. andrewm@0: andrewm@0: void schedule(void *who, action func, timestamp_type timestamp); andrewm@0: void unschedule(void *who, timestamp_type timestamp = 0); andrewm@0: void clear(); andrewm@0: andrewm@0: //static void staticRunLoop(Scheduler* sch, timestamp_type starting_timestamp) { sch->runLoop(starting_timestamp); } andrewm@0: andrewm@0: // The main Thread run loop andrewm@0: void run(); andrewm@0: andrewm@0: private: andrewm@0: // These variables keep track of the status of the separate thread running the events andrewm@0: CriticalSection eventMutex_; andrewm@0: WaitableEvent waitableEvent_; andrewm@0: timestamp_type startingTimestamp_; andrewm@0: bool isRunning_; andrewm@0: andrewm@0: // Collection of future events to execute andrewm@0: //boost::posix_time::ptime startTime_; andrewm@0: double startTimeMilliseconds_; andrewm@0: std::multimap > events_; andrewm@0: }; andrewm@0: andrewm@0: andrewm@0: #endif /* KEYCONTROL_SCHEDULER_H */