Fix Thread class synchronization

Prevent osTheadTerminate from being called on an already terminated
thread. Also make sure the thread termination process is properly
synchronized.
pull/4389/head
Russ Butler 2017-06-01 17:59:44 -05:00 committed by Russ Butler
parent 96bd943ea2
commit cabc1e0911
1 changed files with 6 additions and 6 deletions

View File

@ -103,7 +103,7 @@ osStatus Thread::start(Callback<void()> task) {
} }
osStatus Thread::terminate() { osStatus Thread::terminate() {
osStatus_t ret; osStatus_t ret = osOK;
_mutex.lock(); _mutex.lock();
// Set the Thread's tid to NULL and // Set the Thread's tid to NULL and
@ -112,11 +112,11 @@ osStatus Thread::terminate() {
osThreadId_t local_id = _tid; osThreadId_t local_id = _tid;
_join_sem.release(); _join_sem.release();
_tid = (osThreadId_t)NULL; _tid = (osThreadId_t)NULL;
_finished = true; if (!_finished) {
_finished = true;
ret = osThreadTerminate(local_id);
}
_mutex.unlock(); _mutex.unlock();
ret = osThreadTerminate(local_id);
return ret; return ret;
} }
@ -352,8 +352,8 @@ void Thread::_thunk(void * thread_ptr)
t->_mutex.lock(); t->_mutex.lock();
t->_tid = (osThreadId)NULL; t->_tid = (osThreadId)NULL;
t->_finished = true; t->_finished = true;
t->_mutex.unlock();
t->_join_sem.release(); t->_join_sem.release();
// rtos will release the mutex automatically
} }
} }