Skip to content

Commit d6a7ab3

Browse files
committed
Inheritence corrections for ESPressio Base
1 parent e4d4c11 commit d6a7ab3

File tree

3 files changed

+74
-34
lines changed

3 files changed

+74
-34
lines changed

src/ESPressio_Thread.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,19 @@ namespace ESPressio {
99

1010
// Define the Constructor and Destructor of `Thread` here
1111
Thread::Thread() : _threadID(0) {
12-
_threadID = ThreadManager::GetInstance()->GetThreadCount();
12+
_threadID = ThreadManager::GetInstance()->GetThreadCount() + 1;
1313
SetCoreID(ThreadManager::GetInstance()->AddThread(this));
1414
}
1515

1616
Thread::~Thread() {
1717
DoNotifyDestroy();
1818
SetThreadState(ThreadState::Destroyed);
19-
ThreadManager::GetInstance()->RemoveThread(this);
20-
if (_taskHandle != nullptr) { vTaskDelete(_taskHandle); }
19+
_deleteTask();
2120
}
2221

23-
// Define the Terminate method of `Thread` here
24-
void Thread::Terminate() {
25-
SetThreadState(ThreadState::Terminated);
26-
if (_onTerminate != nullptr) { _onTerminate(this); }
22+
void Thread::GarbageCollect() {
2723
if (GetFreeOnTerminate()) { ThreadGarbageCollector::GetInstance()->CleanUp(); } // Automatically trigger the Garbage Collector
2824
}
29-
3025
}
3126

3227
}

src/ESPressio_Thread.hpp

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,26 @@ namespace ESPressio {
3434
ReadWriteMutex<ThreadState> _threadState = ReadWriteMutex<ThreadState>(ThreadState::Uninitialized);
3535
ReadWriteMutex<bool> _freeOnTerminate = ReadWriteMutex<bool>(false);
3636
ReadWriteMutex<bool> _startOnInitialize = ReadWriteMutex<bool>(true);
37-
TaskHandle_t _taskHandle = nullptr; // SHOULD be Atomic!
37+
TaskHandle_t _taskHandle = NULL; // SHOULD be Atomic!
3838
ReadWriteMutex<uint32_t> _stackSize = ReadWriteMutex<uint32_t>(ESPRESSIO_THREAD_DEFAULT_STACK_SIZE);
3939
ReadWriteMutex<UBaseType_t> _priority = ReadWriteMutex<UBaseType_t>(2);
4040
ReadWriteMutex<BaseType_t> _coreID = ReadWriteMutex<BaseType_t>(0);
4141
// Callbacks
4242
TOnThreadEvent _onInitialize = nullptr;
43-
TOnThreadEvent _onStarte = nullptr;
43+
TOnThreadEvent _onStart = nullptr;
4444
TOnThreadEvent _onPause = nullptr;
4545
TOnThreadEvent _onTerminate = nullptr;
4646
TOnThreadStateChangeEvent _onStateChange = nullptr;
4747

4848
// Methods
49+
void _deleteTask() {
50+
if (_taskHandle != NULL) {
51+
TaskHandle_t handle = _taskHandle;
52+
_taskHandle = NULL;
53+
vTaskDelete(handle);
54+
}
55+
}
56+
4957
void _loop() {
5058
for (;;) {
5159
switch (_threadState.Get()) {
@@ -58,9 +66,7 @@ namespace ESPressio {
5866
OnLoop();
5967
break;
6068
case ThreadState::Terminating:
61-
SetThreadState(ThreadState::Terminated);
6269
case ThreadState::Terminated:
63-
if (_taskHandle != nullptr) { vTaskDelete(_taskHandle); }
6470
return;
6571
}
6672
}
@@ -82,8 +88,27 @@ namespace ESPressio {
8288

8389
void SetThreadState(ThreadState state) {
8490
ThreadState oldState = _threadState.Get();
91+
if (oldState == state) { return; }
8592
_threadState.Set(state);
8693
if (_onStateChange != nullptr) { _onStateChange(this, oldState, state); }
94+
switch (state) {
95+
case ThreadState::Terminated:
96+
GarbageCollect();
97+
break;
98+
case ThreadState::Terminating:
99+
if (_onTerminate != nullptr) { _onTerminate(this); }
100+
break;
101+
case ThreadState::Paused:
102+
if (_onPause != nullptr) { _onPause(this); }
103+
break;
104+
case ThreadState::Running:
105+
if (_onStart != nullptr) { _onStart(this); }
106+
break;
107+
case ThreadState::Initialized:
108+
if (_onInitialize != nullptr) { _onInitialize(this); }
109+
break;
110+
111+
}
87112
}
88113
public:
89114

@@ -98,24 +123,29 @@ namespace ESPressio {
98123
~Thread();
99124

100125
// Methods
126+
void GarbageCollect();
127+
101128
void Initialize() {
102-
if (_taskHandle != nullptr) { vTaskDelete(_taskHandle); } // Delete any existing task handle if it's there!
103-
// Convert value of GetThreadID() to const char* for xTaskCreatePinnedToCore
104-
char threadIDStr[3];
105-
itoa(GetThreadID(), threadIDStr, 10);
106-
107-
xTaskCreatePinnedToCore(
108-
[](void* parameter) {
109-
Thread* instance = static_cast<Thread*>(parameter);
110-
instance->_loop();
111-
},
112-
threadIDStr, /* Name of the task. */
113-
GetStackSize(), /* Stack size of the task. */
114-
this, /* Parameter of the task (class instance). */
115-
GetPriority(), /* Priority of the task. */
116-
&_taskHandle, /* Task handle to keep track of the created task. */
117-
GetCoreID() /* Pin task to core 0. */
118-
);
129+
if (_taskHandle != NULL) { vTaskResume(_taskHandle); } // Resume existing Task if it exists...
130+
else { // ... or Create a new Task if it doesn't!
131+
String threadIDStr = "thread" + String(GetThreadID());
132+
xTaskCreatePinnedToCore(
133+
[](void* parameter) {
134+
Thread* instance = static_cast<Thread*>(parameter);
135+
if (instance != nullptr) {
136+
instance->_loop();
137+
instance->SetThreadState(ThreadState::Terminated);
138+
}
139+
vTaskSuspend(NULL);
140+
},
141+
threadIDStr.c_str(),
142+
GetStackSize(),
143+
this,
144+
GetPriority(),
145+
&_taskHandle,
146+
GetCoreID()
147+
);
148+
}
119149
OnInitialization(); // Invoke any custom initialization behaviour before we change the state of the Thread
120150
// Check if the state was changed to Terminating or Terminate during the OnInitialization() method
121151
if (GetThreadState() == ThreadState::Terminating || GetThreadState() == ThreadState::Terminated) {
@@ -126,14 +156,16 @@ namespace ESPressio {
126156
if (_onInitialize != nullptr) { _onInitialize(this); }
127157
}
128158

129-
void Terminate();
159+
void Terminate() {
160+
SetThreadState(ThreadState::Terminating);
161+
}
130162

131163
void Start() {
132164
if (GetThreadState() == ThreadState::Terminated) {
133165
Initialize();
134166
}
135167
SetThreadState(ThreadState::Running);
136-
if (_onStarte != nullptr) { _onStarte(this); }
168+
if (_onStart != nullptr) { _onStart(this); }
137169
}
138170

139171
void Pause() {
@@ -178,7 +210,7 @@ namespace ESPressio {
178210
}
179211

180212
std::function<void(IThread*)> GetOnStart() {
181-
return _onStarte;
213+
return _onStart;
182214
}
183215

184216
std::function<void(IThread*)> GetOnPause() {
@@ -222,7 +254,7 @@ namespace ESPressio {
222254
}
223255

224256
void SetOnStart(std::function<void(IThread*)> value) {
225-
_onStarte = value;
257+
_onStart = value;
226258
}
227259

228260
void SetOnPause(std::function<void(IThread*)> value) {

src/ESPressio_ThreadManager.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ namespace ESPressio {
5656
});
5757
}
5858

59+
/// Removes a Thread from the `ThreadManager` using its ID.
60+
void RemoveThread(uint8_t threadID) {
61+
_threads.WithWriteLock([threadID](std::vector<IThread*>& threads) {
62+
for (auto thread : threads) {
63+
if (thread->GetThreadID() == threadID) {
64+
threads.erase(std::remove(threads.begin(), threads.end(), thread), threads.end());
65+
break;
66+
}
67+
}
68+
});
69+
}
70+
5971
/// Iterates through all Threads in the `ThreadManager`.
6072
void ForEachThread(std::function<void(IThread*)> callback) {
6173
_threads.WithWriteLock([callback](std::vector<IThread*>& threads) {
@@ -94,7 +106,8 @@ namespace ESPressio {
94106
}
95107
// Now iterate deleteThreads and remove them from the threads list
96108
for (auto thread : deleteThreads) {
97-
free(thread);
109+
threads.erase(std::remove(threads.begin(), threads.end(), thread), threads.end());
110+
delete thread;
98111
}
99112
});
100113
}

0 commit comments

Comments
 (0)