Skip to content

Commit f39232c

Browse files
committed
- Fixing Thread
1 parent 5c90734 commit f39232c

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

src/ESPressio_IThread.hpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace ESPressio {
2626
*/
2727
class IThread {
2828
public:
29+
// Type Defs
30+
31+
typedef std::function<void(IThread*)> ThreadCallback;
32+
typedef std::function<void(IThread*, ThreadState, ThreadState)> ThreadStateChangeCallback;
33+
2934
// Methods
3035

3136
/// `Initialize` is invoked automatically for all Threads when the `ThreadManager` is initialized in your `main()` (or `setup()` for MCU projects) function.
@@ -77,20 +82,23 @@ namespace ESPressio {
7782

7883
// Callback Getters
7984

85+
/// `GetOnDestroy` returns the callback to be invoked when the Thread is destroyed.
86+
virtual ThreadCallback GetOnDestroy() = 0;
87+
8088
/// `GetOnInitialized` returns the callback to be invoked when the Thread is initialized.
81-
virtual std::function<void(IThread*)> GetOnInitialize() = 0;
89+
virtual ThreadCallback GetOnInitialize() = 0;
8290

8391
/// `GetOnStarted` returns the callback to be invoked when the Thread is started.
84-
virtual std::function<void(IThread*)> GetOnStart() = 0;
92+
virtual ThreadCallback GetOnStart() = 0;
8593

8694
/// `GetOnPaused` returns the callback to be invoked when the Thread is paused.
87-
virtual std::function<void(IThread*)> GetOnPause() = 0;
95+
virtual ThreadCallback GetOnPause() = 0;
8896

8997
/// `GetOnTerminated` returns the callback to be invoked when the Thread is terminated.
90-
virtual std::function<void(IThread*)> GetOnTerminate() = 0;
98+
virtual ThreadCallback GetOnTerminate() = 0;
9199

92100
/// `GetOnStateChange` returns the callback to be invoked when the Thread's state changes.
93-
virtual std::function<void(IThread*, ThreadState, ThreadState)> GetOnStateChange() = 0;
101+
virtual ThreadStateChangeCallback GetOnStateChange() = 0;
94102

95103
// Setters
96104

@@ -108,25 +116,29 @@ namespace ESPressio {
108116

109117
// Callback Setters
110118

119+
/// `SetOnDestroy` sets the callback to be invoked when the Thread is destroyed.
120+
/// The callback function takes `IThread*` and ideally named `sender`.
121+
virtual void SetOnDestroy(ThreadCallback) = 0;
122+
111123
/// `SetOnInitialized` sets the callback to be invoked when the Thread is initialized.
112124
/// The callback function takes `IThread*` and ideally named `sender`.
113-
virtual void SetOnInitialize(std::function<void(IThread*)>) = 0;
125+
virtual void SetOnInitialize(ThreadCallback) = 0;
114126

115127
/// `SetOnStarted` sets the callback to be invoked when the Thread is started.
116128
/// The callback function takes `IThread*` and ideally named `sender`.
117-
virtual void SetOnStart(std::function<void(IThread*)>) = 0;
129+
virtual void SetOnStart(ThreadCallback) = 0;
118130

119131
/// `SetOnPaused` sets the callback to be invoked when the Thread is paused.
120132
/// The callback function takes `IThread*` and ideally named `sender`.
121-
virtual void SetOnPause(std::function<void(IThread*)>) = 0;
133+
virtual void SetOnPause(ThreadCallback) = 0;
122134

123135
/// `SetOnTerminated` sets the callback to be invoked when the Thread is terminated.
124136
/// The callback function takes `IThread*` and ideally named `sender`.
125-
virtual void SetOnTerminate(std::function<void(IThread*)>) = 0;
137+
virtual void SetOnTerminate(ThreadCallback) = 0;
126138

127139
/// `SetOnStateChange` sets the callback to be invoked when the Thread's state changes.
128140
/// The callback function takes `IThread*` and ideally named `sender`, `ThreadState` for the previous state and `ThreadState` for the new state.
129-
virtual void SetOnStateChange(std::function<void(IThread*, ThreadState, ThreadState)>) = 0;
141+
virtual void SetOnStateChange(ThreadStateChangeCallback) = 0;
130142
};
131143

132144
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include "ESPressio_Thread.hpp"
4+
#include "ESPressio_ThreadManager.hpp"
5+
6+
#ifndef ESPRESSIO_THREAD_GARBAGE_COLLECTOR_STACK_SIZE
7+
#define ESPRESSIO_THREAD_GARBAGE_COLLECTOR_STACK_SIZE 2000 // You are encouraged to determine the appropriate stack size for your application, and define it in your platformio.ini file.
8+
#endif
9+
10+
namespace ESPressio {
11+
12+
namespace Threads {
13+
14+
class IThreadGarbageCollector {
15+
public:
16+
virtual void CleanUp() = 0;
17+
};
18+
19+
}
20+
}

src/ESPressio_Thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ESPressio {
1414
}
1515

1616
Thread::~Thread() {
17-
DoNotifyDestroy();
17+
if (_onDestroy() != nullptr) { _onDestroy(); }
1818
SetThreadState(ThreadState::Destroyed);
1919
_deleteTask();
2020
}

src/ESPressio_Thread.hpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace ESPressio {
3939
ReadWriteMutex<UBaseType_t> _priority = ReadWriteMutex<UBaseType_t>(2);
4040
ReadWriteMutex<BaseType_t> _coreID = ReadWriteMutex<BaseType_t>(0);
4141
// Callbacks
42+
TOnThreadEvent _onDestroy = nullptr;
4243
TOnThreadEvent _onInitialize = nullptr;
4344
TOnThreadEvent _onStart = nullptr;
4445
TOnThreadEvent _onPause = nullptr;
@@ -205,23 +206,27 @@ namespace ESPressio {
205206

206207
// Callback Getters
207208

208-
std::function<void(IThread*)> GetOnInitialize() {
209+
TOnThreadEvent GetOnDestroy() {
210+
return _onDestroy;
211+
}
212+
213+
TOnThreadEvent GetOnInitialize() {
209214
return _onInitialize;
210215
}
211216

212-
std::function<void(IThread*)> GetOnStart() {
217+
TOnThreadEvent GetOnStart() {
213218
return _onStart;
214219
}
215220

216-
std::function<void(IThread*)> GetOnPause() {
221+
TOnThreadEvent GetOnPause() {
217222
return _onPause;
218223
}
219224

220-
std::function<void(IThread*)> GetOnTerminate() {
225+
TOnThreadEvent GetOnTerminate() {
221226
return _onTerminate;
222227
}
223228

224-
std::function<void(IThread*, ThreadState, ThreadState)> GetOnStateChange() {
229+
TOnThreadStateChangeEvent GetOnStateChange() {
225230
return _onStateChange;
226231
}
227232

@@ -249,23 +254,27 @@ namespace ESPressio {
249254

250255
// Callback Setters
251256

252-
void SetOnInitialize(std::function<void(IThread*)> value) {
257+
void SetOnDestroy(TOnThreadEvent value) {
258+
_onDestroy = value;
259+
}
260+
261+
void SetOnInitialize(TOnThreadEvent value) {
253262
_onInitialize = value;
254263
}
255264

256-
void SetOnStart(std::function<void(IThread*)> value) {
265+
void SetOnStart(TOnThreadEvent value) {
257266
_onStart = value;
258267
}
259268

260-
void SetOnPause(std::function<void(IThread*)> value) {
269+
void SetOnPause(TOnThreadEvent value) {
261270
_onPause = value;
262271
}
263272

264-
void SetOnTerminate(std::function<void(IThread*)> value) {
273+
void SetOnTerminate(TOnThreadEvent value) {
265274
_onTerminate = value;
266275
}
267276

268-
void SetOnStateChange(std::function<void(IThread*, ThreadState, ThreadState)> value) {
277+
void SetOnStateChange(TOnThreadStateChangeEvent value) {
269278
_onStateChange = value;
270279
}
271280
};

src/ESPressio_ThreadGarbageCollector.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "ESPressio_Thread.hpp"
44
#include "ESPressio_ThreadManager.hpp"
5+
#include "ESPressio_IThreadGarbageCollector.hpp"
56

67
#ifndef ESPRESSIO_THREAD_GARBAGE_COLLECTOR_STACK_SIZE
78
#define ESPRESSIO_THREAD_GARBAGE_COLLECTOR_STACK_SIZE 2000 // You are encouraged to determine the appropriate stack size for your application, and define it in your platformio.ini file.
@@ -11,7 +12,7 @@ namespace ESPressio {
1112

1213
namespace Threads {
1314

14-
class ThreadGarbageCollector : public Thread {
15+
class ThreadGarbageCollector : public Thread, public IThreadGarbageCollector {
1516
private:
1617
// A semaphore for our loop to wait on (so that it doesn't consume CPU cycles when there's nothing to do).
1718
SemaphoreHandle_t _semaphore = xSemaphoreCreateBinary();
@@ -34,7 +35,7 @@ namespace ESPressio {
3435
return instance;
3536
}
3637

37-
void CleanUp() {
38+
void CleanUp() override {
3839
// Signal the semaphore to wake up the thread.
3940
xSemaphoreGive(_semaphore);
4041
}

0 commit comments

Comments
 (0)