Properly handle a thread which terminates itself

In Thread::terminate() release the join semaphore before terminating
the thread. This allows the join semaphore to be properly signaled in
the case where a thread is terminating itself.
pull/2643/head
Russ Butler 2016-09-07 17:16:33 -05:00
parent da3b07dbd1
commit b49d7e4fb4
1 changed files with 14 additions and 3 deletions

View File

@ -101,11 +101,14 @@ osStatus Thread::terminate() {
osStatus ret; osStatus ret;
_mutex.lock(); _mutex.lock();
ret = osThreadTerminate(_tid); // Set the Thread's tid to NULL and
// release the semaphore before terminating
// since this thread could be terminating itself
osThreadId local_id = _tid;
_join_sem.release();
_tid = (osThreadId)NULL; _tid = (osThreadId)NULL;
// Wake threads joining the terminated thread ret = osThreadTerminate(local_id);
_join_sem.release();
_mutex.unlock(); _mutex.unlock();
return ret; return ret;
@ -116,6 +119,14 @@ osStatus Thread::join() {
if (ret < 0) { if (ret < 0) {
return osErrorOS; return osErrorOS;
} }
// The semaphore has been released so this thread is being
// terminated or has been terminated. Once the mutex has
// been locked it is ensured that the thread is deleted.
_mutex.lock();
MBED_ASSERT(NULL == _tid);
_mutex.unlock();
// Release sem so any other threads joining this thread wake up // Release sem so any other threads joining this thread wake up
_join_sem.release(); _join_sem.release();
return osOK; return osOK;