Skip to content

Commit 5aa3db9

Browse files
committed
#9 - Added Optional Callbacks for Thread (interfaces in IThread
1 parent 5ff47f8 commit 5aa3db9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/ESPressio_IThread.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ namespace ESPressio {
9191
/// `GetOnDestroying` returns the callback to be invoked when the Thread is being destroyed.
9292
virtual std::function<void(IThread*)> GetOnDestroy() = 0;
9393

94+
/// `GetOnStateChange` returns the callback to be invoked when the Thread's state changes.
95+
virtual std::function<void(IThread*, ThreadState, ThreadState)> GetOnStateChange() = 0;
96+
9497
// Setters
9598

9699
/// `SetCoreID` sets the ID of the Core the Thread should run on.
@@ -126,6 +129,10 @@ namespace ESPressio {
126129
/// `SetOnDestroying` sets the callback to be invoked when the Thread is being destroyed.
127130
/// The callback function takes `IThread*` and ideally named `sender`.
128131
virtual void SetOnDestroy(std::function<void(IThread*)>) = 0;
132+
133+
/// `SetOnStateChange` sets the callback to be invoked when the Thread's state changes.
134+
/// The callback function takes `IThread*` and ideally named `sender`, `ThreadState` for the previous state and `ThreadState` for the new state.
135+
virtual void SetOnStateChange(std::function<void(IThread*, ThreadState, ThreadState)>) = 0;
129136
};
130137

131138
}

src/ESPressio_Thread.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace ESPressio {
2626

2727
/// `TOnThreadEvent` is a function type that can be used to handle Thread events.
2828
using TOnThreadEvent = std::function<void(IThread*)>;
29+
using TOnThreadStateChangeEvent = std::function<void(IThread*, ThreadState, ThreadState)>;
2930

3031
// Members
3132
uint8_t _threadID; // This is idempotent so doesn't need a `Mutex` wrapper.
@@ -42,6 +43,7 @@ namespace ESPressio {
4243
TOnThreadEvent _onPause = nullptr;
4344
TOnThreadEvent _onTerminate = nullptr;
4445
TOnThreadEvent _onDestroy = nullptr;
46+
TOnThreadStateChangeEvent _onStateChange = nullptr;
4547

4648
// Methods
4749
void _loop() {
@@ -79,7 +81,9 @@ namespace ESPressio {
7981
// Setters (Internal)
8082

8183
void SetThreadState(ThreadState state) {
84+
ThreadState oldState = _threadState.Get();
8285
_threadState.Set(state);
86+
if (_onStateChange != nullptr) { _onStateChange(this, oldState, state); }
8387
}
8488
public:
8589

@@ -189,6 +193,10 @@ namespace ESPressio {
189193
return _onDestroy;
190194
}
191195

196+
std::function<void(IThread*, ThreadState, ThreadState)> GetOnStateChange() {
197+
return _onStateChange;
198+
}
199+
192200
// Setters
193201

194202
void SetCoreID(BaseType_t value) {
@@ -232,6 +240,10 @@ namespace ESPressio {
232240
void SetOnDestroy(std::function<void(IThread*)> value) {
233241
_onDestroy = value;
234242
}
243+
244+
void SetOnStateChange(std::function<void(IThread*, ThreadState, ThreadState)> value) {
245+
_onStateChange = value;
246+
}
235247
};
236248

237249
}

0 commit comments

Comments
 (0)