@@ -15,27 +15,174 @@ class Scheduler
1515public:
1616 Scheduler ();
1717 ~Scheduler ();
18+
19+ /* ************** Methods to Perform Actions on Processes ****************/
20+ // Note: These can also be called directly on the Process object
21+ // example process.add()
22+
23+ /* *
24+ * Add a new process to the scheduler chain
25+ * If it is already added, do nothing
26+ * This will trigger the Processes setup() method
27+ *
28+ * @return: True on success
29+ */
1830 bool add (Process &process, bool enableIfNot = false );
31+
32+
33+ /* *
34+ * Disable a process
35+ * If it is not part of chain or already disabled, do nothing
36+ * This will trigger the Processes onDisable() method
37+ *
38+ * @return: True on success
39+ */
1940 bool disable (Process &process);
41+
42+
43+ /* *
44+ * Enable a process
45+ * If it is not part of chain or already enabled, do nothing
46+ * This will trigger the Processes onEnable() method
47+ *
48+ * @return: True on success
49+ */
2050 bool enable (Process &process);
51+
52+
53+ /* *
54+ * Remove a process from the scheduling chain
55+ * If it is not part of chain, do nothing
56+ * This will trigger the Processes cleanup() method
57+ * Note: If it is currently enabled, disable() will automatically be called first
58+ *
59+ * @return: True on success
60+ */
2161 bool destroy (Process &process);
62+
63+ /* *
64+ * Same as calling destroy() and add() with less overhead
65+ *
66+ * @return: True on success
67+ */
2268 bool restart (Process &process);
23- bool halt ();
2469
70+
71+ /* *
72+ * Get the id of process
73+ *
74+ * @return: the unique id, otherwise 0 if not added to a scheduler
75+ */
2576 uint8_t getID (Process &process);
77+
78+
79+ /* *
80+ * Determine if the process is currently enabled
81+ *
82+ * @return: bool
83+ */
84+ bool isEnabled (Process &process);
85+
86+
87+ /* *
88+ * Determine if the scheduler is currently in the middle of servicing this process
89+ *
90+ * @return: bool
91+ */
2692 bool isRunningProcess (Process &process);
93+
94+
95+ /* *
96+ * Determine if the process is currently destroyed (not part of the scheduler chain)
97+ *
98+ * @return: bool
99+ */
27100 bool isNotDestroyed (Process &process);
28- bool isEnabled (Process &process);
29101
102+ /* ********************** End Processes Methods ********************/
103+
104+
105+ /* *
106+ * Destroy all processes then put the processor into a low power sleep state
107+ *
108+ * @return: True on success
109+ */
110+ bool halt ();
111+
112+
113+ /* *
114+ * Get the currently running process
115+ *
116+ * @return: a pointer to the process, NULL on no process currently being run
117+ */
30118 static Process *getActive ();
119+
120+ /* *
121+ * Get a pointer to the process with id
122+ *
123+ * @return: a pointer to the process, NULL on no process with that id
124+ */
31125 Process *findProcById (uint8_t id);
126+
127+ /* *
128+ * Get the number of processes in the scheduler chain at priority levels
129+ * If priority = ALL_PRIORITY_LEVELS, count at all priority levels
130+ * If enabledOnly = true, only the enabled processes will be counted
131+ *
132+ * @return: a pointer to the process, NULL on no process with that id
133+ */
32134 uint8_t countProcesses (int priority = ALL_PRIORITY_LEVELS, bool enabledOnly = true );
135+
136+ /* *
137+ * Get the internal timestamp the scheduler is using to track time
138+ * Either the same as millis() or micros() depending on _MICROS_PRECISION
139+ * @return: uint32_t
140+ */
33141 static uint32_t getCurrTS ();
34142
143+ /* *
144+ * Run one pass through the scheduler, call this repeatedly in your void loop()
145+ *
146+ * @return: The number of processes serviced in that pass
147+ */
35148 int run ();
36- protected:
37149
38- // Inner queue object class
150+
151+ // Enable this option in config.h to track time statistics on processes
152+ #ifdef _PROCESS_STATISTICS
153+ /* *
154+ * This will update the Process.getLoadPercent() method
155+ * It will estimate the % CPU time for all processes
156+ * Note: This will simply add this task to the scheduler job queue
157+ * The update will not happen until the scheduler gets a chance to process the request
158+ *
159+ * @return: True on success
160+ */
161+ bool updateStats ();
162+
163+ #endif
164+
165+ // Enable this option to allow processes to raise and catch custom exceptions
166+ // Behind the scenes this is using setjmp and longjmp
167+ #ifdef _PROCESS_EXCEPTION_HANDLING
168+ /* *
169+ * Raise Exception with code e inside a Process service routine
170+ * Execution will stop immediatley, and the processes handleException() will be called
171+ * Note: DO NOT CALL THIS FROM OUTSIDE A PROCESS SERVICE ROUTINE
172+ * @return: True on success
173+ */
174+ void raiseException (int e);
175+
176+ static jmp_buf _env; // have to do this to access it from ISR
177+ #endif
178+
179+ // //////// YOU CAN IGNORE ALL THE PROTECTED/PRIVATE METHODS BELOW HERE /////////
180+
181+ protected:
182+ #ifdef _PROCESS_EXCEPTION_HANDLING
183+ virtual void handleException (Process &process, int e) { };
184+ #endif
185+ // Inner queue object class to queue scheduler jobs
39186 class QueableOperation
40187 {
41188 public:
@@ -66,6 +213,19 @@ class Scheduler
66213 const uint8_t _operation;
67214 };
68215
216+
217+ #ifdef _PROCESS_EXCEPTION_HANDLING
218+ // handle the return from setjmp()
219+ bool jmpHandler (int e);
220+ #endif
221+
222+ #ifdef _PROCESS_STATISTICS
223+ // Actually do the update that was requested in updateStats()
224+ void procUpdateStats ();
225+ // This will handle overflow condiitions on the statistics
226+ void handleHistOverFlow (uint8_t div);
227+ #endif
228+
69229 // Methods that process queued operations
70230 void procDisable (Process &process);
71231 void procEnable (Process &process);
@@ -74,15 +234,17 @@ class Scheduler
74234 void procRestart (Process &process);
75235 void procHalt ();
76236
237+ // Process the scheduler job queue
77238 void processQueue ();
239+
78240 // Linked list methods
79241 bool appendNode (Process &node); // true on success
80242 bool removeNode (Process &node); // true on success
81243 bool findNode (Process &node); // True if node exists in list
82244 Process *findPrevNode (Process &node);
83245
84246
85- static Process *_active;
247+ static Process *_active; // needs to be static for access in ISR
86248 uint8_t _lastID;
87249 RingBuf *_queue;
88250
@@ -100,25 +262,6 @@ class Scheduler
100262 void reOrderProcs(ProcPriority level);
101263*/
102264
103- #ifdef _PROCESS_STATISTICS
104- public:
105- bool updateStats ();
106- private:
107- void procUpdateStats ();
108- void handleHistOverFlow (uint8_t div);
109-
110- #endif
111-
112-
113- #ifdef _PROCESS_EXCEPTION_HANDLING
114- public:
115- void raiseException (int e);
116- virtual void handleException (Process &process, int e) { };
117- static jmp_buf _env; // have to do this to access it from ISR
118- protected:
119- bool jmpHandler (int e);
120- #endif
121-
122265};
123266
124267#endif
0 commit comments