Skip to content

Fix thread self termination #2643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions TESTS/mbedmicro-rtos-mbed/threads/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ void increment_with_murder(counter_t* counter) {
(*counter)++;
}

void self_terminate(Thread *self) {
self->terminate();
// Code should not get here
TEST_ASSERT(0);
}

// Tests that spawn tasks in different configurations
template <void (*F)(counter_t *)>
void test_single_thread() {
Expand Down Expand Up @@ -97,6 +103,13 @@ void test_serial_threads() {
TEST_ASSERT_EQUAL(counter, N);
}

void test_self_terminate() {
Thread *thread = new Thread(osPriorityNormal, STACK_SIZE);
thread->start(thread, self_terminate);
thread->join();
delete thread;
}

utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(40, "default_auto");
return verbose_test_setup_handler(number_of_cases);
Expand All @@ -123,6 +136,8 @@ Case cases[] = {
Case("Testing single thread with murder", test_single_thread<increment_with_murder>),
Case("Testing parallel threads with murder", test_parallel_threads<3, increment_with_murder>),
Case("Testing serial threads with murder", test_serial_threads<10, increment_with_murder>),

Case("Testing thread self terminate", test_self_terminate),
};

Specification specification(test_setup, cases);
Expand Down
17 changes: 14 additions & 3 deletions rtos/rtos/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ osStatus Thread::terminate() {
osStatus ret;
_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;

// Wake threads joining the terminated thread
_join_sem.release();
ret = osThreadTerminate(local_id);

_mutex.unlock();
return ret;
Expand All @@ -116,6 +119,14 @@ osStatus Thread::join() {
if (ret < 0) {
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
_join_sem.release();
return osOK;
Expand Down