Skip to content

Commit 8722eee

Browse files
committed
Test of strict priorities
1 parent 2cbbca1 commit 8722eee

5 files changed

Lines changed: 97 additions & 53 deletions

File tree

src/ProcessScheduler/Includes.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ typedef enum ProcessWarning
3535

3636
#define OVERSCHEDULED_NO_WARNING 0
3737

38-
// PICK INT VALUES PEOPLE UNLIKLEY TO USE
38+
// Only can use negative values
39+
typedef SchedulerException
40+
{
41+
ProcTimeout = -1000,
42+
ProcYield,
43+
};
3944
#define LONGJMP_ISR_CODE -1000
4045
#define LONGJMP_YIELD_CODE -1001
4146

src/ProcessScheduler/Process.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@
8484
}
8585

8686

87+
// both must need servicing
88+
Process *Process::runWhich(Process *p1, Process *p2)
89+
{
90+
// All things being equal pick yes
91+
92+
// Compare forces
93+
if (p1->forceSet() || p2->forceSet())
94+
return p1->forceSet() ? p1 : p2;
95+
96+
// whichever one is more behind goes first
97+
return (p1->timeToNextRun() <= p2->timeToNextRun()) ? p1 : p2;
98+
99+
}
100+
101+
87102

88103
/*********** PROTECTED *************/
89104

src/ProcessScheduler/Process.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class Process
3131
bool destroy();
3232
bool restart();
3333

34+
35+
/*
36+
* Give both processes that need to run p1 and p2
37+
* return the process that should run first
38+
*
39+
* @return: p1 or p2
40+
*/
41+
static Process *Process::runWhich(Process *p1, Process *p2);
42+
3443
///////////////////// GETTERS /////////////////////////
3544

3645
// These methods are also the same as calling calling scheduler.method(process)

src/ProcessScheduler/Scheduler.cpp

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ProcessScheduler/Scheduler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class Scheduler
184184
* Handle uncaught Process exceptions from Process process with Exception code e
185185
* By default just restart it
186186
*/
187-
virtual void handleException(Process &process, int e);
187+
virtual void handleException(Process *process, int e);
188188
#endif
189189
// Inner queue object class to queue scheduler jobs
190190
class QueableOperation

0 commit comments

Comments
 (0)