Skip to content

Commit bf3034e

Browse files
pnndrafacchinm
authored andcommitted
restored stack size and added better synchronization primitives
1 parent c3bdd06 commit bf3034e

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

Diff for: Arduino_Threads.h

+55-20
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,71 @@ class Shared // template definition
3838

3939
class ArduinoThreads {
4040
private:
41-
rtos::EventFlags *GlobalEvents;
42-
rtos::EventFlags ThreadEvents;
41+
static rtos::EventFlags globalEvents;
42+
uint32_t startFlags;
43+
uint32_t stopFlags;
44+
uint32_t loopDelay;
4345
virtual void setup(void) {};
4446
virtual void loop(void) {};
45-
void execute() {
47+
void execute() {
4648
setup();
47-
if (GlobalEvents!=NULL) GlobalEvents->wait_all(LoopStart);
48-
while ( (GlobalEvents==NULL || !(GlobalEvents->get()&LoopStop) ) &&
49-
!(ThreadEvents.get()&LoopStop) ) {
50-
loop();
51-
}
52-
}
53-
rtos::Thread t;
49+
// if startFlags have been passed then wait until all the flags are set
50+
// before starting the loop. this is used to synchronize loops from multiple
51+
// sketches.
52+
globalEvents.wait_all(startFlags);
53+
54+
// if stopFlags have been passed stop when all the flags are set
55+
// otherwise loop forever
56+
while ( 1 ) {
57+
loop();
58+
// on exit clear the flags that have forced us to stop.
59+
// note that if two groups of sketches stop on common flags
60+
// the first group will clear them so the second group may never
61+
// exit
62+
if (stopFlags!=0) {
63+
if ((globalEvents.get()&stopFlags)!=stopFlags) {
64+
globalEvents.clear(stopFlags);
65+
return;
66+
}
67+
if ((rtos::ThisThread::flags_get()&stopFlags)!=stopFlags) {
68+
rtos::ThisThread::flags_clear(stopFlags);
69+
return;
70+
}
71+
}
72+
// sleep for the time we've been asked to insert between loops
73+
rtos::ThisThread::sleep_for(loopDelay);
74+
}
75+
}
76+
rtos::Thread *t;
5477
public:
55-
typedef enum {
56-
LoopStart= (1<<0),
57-
LoopStop= (1<<1)
58-
} Events;
59-
void start(rtos::EventFlags *event = NULL) {
60-
GlobalEvents = event;
61-
t.start(mbed::callback(this, &ArduinoThreads::execute));
78+
// start this sketch
79+
void start(int stacksize = 4096, uint32_t startFlags=0, uint32_t stopFlags=0) {
80+
this->startFlags = startFlags;
81+
this->stopFlags = stopFlags;
82+
loopDelay=0;
83+
t = new rtos::Thread(osPriorityNormal, stacksize, nullptr, _macroToString(tabname));
84+
t->start(mbed::callback(this, &ArduinoThreads::execute));
6285
}
86+
// kill this sketch
6387
void terminate() {
64-
t.terminate();
88+
t->terminate();
6589
}
66-
void stop() {
67-
ThreadEvents.set(LoopStop);
90+
// send an event to all sketches at the same time
91+
static void broadcastEvent(uint32_t event) {
92+
globalEvents.set(event);
93+
}
94+
// send an event only to this sketch
95+
void sendEvent(uint32_t event) {
96+
t->flags_set(event);
97+
}
98+
// set the rate at which loop function will be called
99+
void setLoopDelay(uint32_t delay) {
100+
loopDelay = delay;
68101
}
69102
};
70103

104+
rtos::EventFlags ArduinoThreads::globalEvents;
105+
71106
#define THD_ENTER(tabname) class CONCAT(tabname, Class) : public ArduinoThreads { \
72107
private:
73108

0 commit comments

Comments
 (0)