Add Thread::join call for exiting threads without forceful termination

Allows multiple threads to join without forceful termination. Allows
threads to synchronize on cleanup.
Christopher Haster 2016-04-22 01:12:40 -05:00
parent 8b8606b98d
commit d15cd7826a
2 changed files with 20 additions and 1 deletions

View File

@ -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);
}

View File

@ -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.
*/