Skip to content

Commit 7b2319c

Browse files
author
dave
committed
#25 example updated
1 parent f3aa7dd commit 7b2319c

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/reentrantLocking/reentrantLocking.ino

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
#include <TaskManagerIO.h>
33
#include <ReentrantYieldingLock.h>
44

5-
// this is the global lock that can only be
5+
// this is the global lock that that we'll use to protect the variable below myVar.
66
ReentrantYieldingLock myLock;
77
volatile int myVar = 0;
88

9+
//
10+
// A simple log function for writing to serial
11+
//
912
void log(const char* str, int val) {
1013
Serial.print(millis());
1114
Serial.print(' ');
1215
Serial.println(str);
1316
}
1417

18+
//
19+
// here we define a nested function that is called from the task, it locks again. This test that the reentrant
20+
// functionality is working
21+
//
1522
void nestedFunction() {
1623
TaskMgrLock locker(myLock);
1724
log("in nested function", myVar);
@@ -20,18 +27,25 @@ void nestedFunction() {
2027
void setup() {
2128
Serial.begin(115200);
2229

30+
// start a task that locks the bus, calls a nested function and yields time back to task manager.
2331
taskManager.scheduleFixedRate(1000, [] {
2432
TaskMgrLock locker(myLock);
33+
int myVarAtStart = myVar;
2534
log("start task function", myVar);
2635

2736
// the nested function will lock again, which is fine because it's in the same task
2837
nestedFunction();
2938

30-
// now we release taskmanager to run other tasks, the will not be able to take our lock
39+
// now we release task manager to run other tasks, the will not be able to take our lock
3140
taskManager.yieldForMicros(millisToMicros(500));
3241

33-
// now we have the context back lock again
42+
// now we have the context back call the nested lock again
3443
nestedFunction();
44+
45+
if(myVar != myVarAtStart) {
46+
log("ERROR in locking ", myVarAtStart);
47+
}
48+
3549
log("exit task function", myVar);
3650
});
3751

0 commit comments

Comments
 (0)