From d15cd7826ade05f28499c84fb4f5c5ae2da9fcc4 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 22 Apr 2016 01:12:40 -0500 Subject: [PATCH] Add Thread::join call for exiting threads without forceful termination Allows multiple threads to join without forceful termination. Allows threads to synchronize on cleanup. --- core/mbed-rtos/rtos/Thread.cpp | 16 +++++++++++++++- core/mbed-rtos/rtos/Thread.h | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/mbed-rtos/rtos/Thread.cpp b/core/mbed-rtos/rtos/Thread.cpp index 5ebb72d005..82ce688119 100644 --- a/core/mbed-rtos/rtos/Thread.cpp +++ b/core/mbed-rtos/rtos/Thread.cpp @@ -39,7 +39,7 @@ Thread::Thread(void (*task)(void const *argument), void *argument, osStatus Thread::start(void (*task)(void const *argument), void *argument, osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { if (_tid != NULL) { - return osErrorResource; + return osErrorParameter; } #ifdef __MBED_CMSIS_RTOS_CM @@ -73,6 +73,20 @@ osStatus Thread::terminate() { return osThreadTerminate(_tid); } +osStatus Thread::join() { + while (true) { + uint8_t state = get_state(); + if (state == Thread::Inactive || state == osErrorParameter) { + return osOK; + } + + osStatus status = yield(); + if (status != osOK) { + return status; + } + } +} + osStatus Thread::set_priority(osPriority priority) { return osThreadSetPriority(_tid, priority); } diff --git a/core/mbed-rtos/rtos/Thread.h b/core/mbed-rtos/rtos/Thread.h index 0da6764a00..c172f88930 100644 --- a/core/mbed-rtos/rtos/Thread.h +++ b/core/mbed-rtos/rtos/Thread.h @@ -59,6 +59,11 @@ public: uint32_t stack_size=DEFAULT_STACK_SIZE, unsigned char *stack_pointer=NULL); + /** Wait for thread to terminate + @return status code that indicates the execution status of the function. + */ + osStatus join(); + /** Terminate execution of a thread and remove it from Active Threads @return status code that indicates the execution status of the function. */