@@ -143,75 +143,84 @@ int Scheduler::run()
143143 {
144144 processQueue ();
145145
146- _active = _pLevels[pLevel].next ;
147- if (!_active)
146+ Process *tmp = _pLevels[pLevel].head ;
147+
148+ // No processes at this priority level
149+ if (!tmp)
148150 continue ;
149151
150- // ///////// Run the correct process /////////
152+ // find the highest priority process that needs to run
151153 uint32_t start = getCurrTS ();
154+ Process *torun = NULL ;
155+
156+ // Search for the best process
157+ while (tmp) {
158+ if (tmp->needsServicing (start)) {
159+ if (torun) { // Compare which one needs to run more
160+ _active = Process::runWhich (torun, tmp);
161+ } else { // torun is NULL so this is the best one to run
162+ torun = tmp;
163+ }
164+ }
165+ tmp = tmp->getNext ();
166+ }
152167
153- if (_active->needsServicing (start))
154- {
155- bool force = _active->forceSet (); // Store whether it was a forced iteraiton
156- _active->willService (start);
168+ // No ready process found at this priority level
169+ if (!torun)
170+ continue ;
171+
172+ // ///////// Run the correct process /////////
173+ _active = torun;
174+ start = getCurrTS (); // update
175+ bool force = _active->forceSet (); // Store whether it was a forced iteraiton
176+ _active->willService (start);
157177
158178#ifdef _PROCESS_EXCEPTION_HANDLING
159- int ret = setjmp (_env);
179+ int ret = setjmp (_env);
160180
161- #ifdef _PROCESS_TIMEOUT_INTERRUPTS
162- ENABLE_SCHEDULER_ISR ();
163- #endif
164- if (!ret) {
165- _active->service ();
166- } else {
167- jmpHandler (ret);
168- }
169- #else
181+ // Enable the interrupts
182+ #ifdef _PROCESS_TIMEOUT_INTERRUPTS
183+ ENABLE_SCHEDULER_ISR ();
184+ #endif
185+
186+ if (!ret) {
170187 _active->service ();
188+ } else {
189+ jmpHandler (ret);
190+ }
191+ #else
192+ _active->service ();
171193#endif
172194
195+ // Disable the interrupts after the process returned
173196#ifdef _PROCESS_TIMEOUT_INTERRUPTS
174197 DISABLE_SCHEDULER_ISR ();
175198#endif
199+ _active = NULL ; // done!
200+ // ////////////////////END PROCESS SERVICING//////////////////////
176201
177202#ifdef _PROCESS_STATISTICS
178- uint32_t runTime = getCurrTS () - start;
179- // Make sure no overflow happens
180- if (_active->statsWillOverflow (1 , runTime))
181- handleHistOverFlow (HISTORY_DIV_FACTOR);
203+ uint32_t runTime = getCurrTS () - start;
204+ // Make sure no overflow happens
205+ if (_active->statsWillOverflow (1 , runTime))
206+ handleHistOverFlow (HISTORY_DIV_FACTOR);
182207
183- _active->setHistIterations (_active->getHistIterations ()+1 );
184- _active->setHistRuntime (_active->getHistRunTime ()+runTime);
208+ _active->setHistIterations (_active->getHistIterations ()+1 );
209+ _active->setHistRuntime (_active->getHistRunTime ()+runTime);
185210
186211#endif
187- // Is it time to disable?
188- if (_active->wasServiced (force)) {
189- disable (*_active);
190- }
191-
192- count++; // incr counter
212+ // Is it time to disable?
213+ if (_active->wasServiced (force)) {
214+ disable (*_active);
193215 }
194- // ////////////////////END PROCESS SERVICING//////////////////////
195- delay (0 ); // For esp8266
196216
197- // Determine what to do next ///
198- if (!_active->hasNext ()) {
199- #ifdef _PROCESS_REORDERING
200- if (++_pLevels[pLevel].passes >= _PROCESS_REORDERING_AGGRESSIVENESS) {
201- reOrderProcs ((ProcPriority)pLevel);
202- ++_pLevels[pLevel].passes = 0 ;
203- }
204- #endif
205- _pLevels[pLevel].next = _pLevels[pLevel].head ; // Set next to first
206- } else {
207- _pLevels[pLevel].next = _active->getNext (); // Set next and break
208- _active = NULL ;
209- break ;
210- }
211-
212- _active = NULL ;
217+ count++; // incr counter
218+ processQueue ();
219+ delay (0 ); // For esp8266
220+ break ; // We found the process and serviced it, so were done
213221 }
214- processQueue ();
222+ delay (0 ); // For esp8266
223+
215224 return count;
216225}
217226
@@ -434,9 +443,15 @@ void Scheduler::handleHistOverFlow(uint8_t div)
434443 longjmp (_env, e);
435444 }
436445
437- void Scheduler::handleException (Process & process, int e)
446+ void Scheduler::handleException (Process * process, int e)
438447 {
439- process.restart ();
448+ // Exception came from process
449+ if (process) {
450+ process.restart ();
451+ } else {
452+ // Exception came from scheduler
453+
454+ }
440455 }
441456
442457
@@ -457,7 +472,7 @@ void Scheduler::handleHistOverFlow(uint8_t div)
457472
458473 default :
459474 if (!_active->handleException (e))
460- handleException (* _active, e);
475+ handleException (_active, e);
461476 break ;
462477
463478 }
0 commit comments