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.
66ReentrantYieldingLock myLock;
77volatile int myVar = 0 ;
88
9+ //
10+ // A simple log function for writing to serial
11+ //
912void 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+ //
1522void nestedFunction () {
1623 TaskMgrLock locker (myLock);
1724 log (" in nested function" , myVar);
@@ -20,18 +27,25 @@ void nestedFunction() {
2027void 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