Skip to content

Commit cabc1e0

Browse files
committed
Fix Thread class synchronization
Prevent osTheadTerminate from being called on an already terminated thread. Also make sure the thread termination process is properly synchronized.
1 parent 96bd943 commit cabc1e0

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

rtos/Thread.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ osStatus Thread::start(Callback<void()> task) {
103103
}
104104

105105
osStatus Thread::terminate() {
106-
osStatus_t ret;
106+
osStatus_t ret = osOK;
107107
_mutex.lock();
108108

109109
// Set the Thread's tid to NULL and
@@ -112,11 +112,11 @@ osStatus Thread::terminate() {
112112
osThreadId_t local_id = _tid;
113113
_join_sem.release();
114114
_tid = (osThreadId_t)NULL;
115-
_finished = true;
115+
if (!_finished) {
116+
_finished = true;
117+
ret = osThreadTerminate(local_id);
118+
}
116119
_mutex.unlock();
117-
118-
ret = osThreadTerminate(local_id);
119-
120120
return ret;
121121
}
122122

@@ -352,8 +352,8 @@ void Thread::_thunk(void * thread_ptr)
352352
t->_mutex.lock();
353353
t->_tid = (osThreadId)NULL;
354354
t->_finished = true;
355-
t->_mutex.unlock();
356355
t->_join_sem.release();
356+
// rtos will release the mutex automatically
357357
}
358358

359359
}

0 commit comments

Comments
 (0)