Skip to content

Commit d15cd78

Browse files
committed
Add Thread::join call for exiting threads without forceful termination
Allows multiple threads to join without forceful termination. Allows threads to synchronize on cleanup.
1 parent 8b8606b commit d15cd78

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

core/mbed-rtos/rtos/Thread.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Thread::Thread(void (*task)(void const *argument), void *argument,
3939
osStatus Thread::start(void (*task)(void const *argument), void *argument,
4040
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
4141
if (_tid != NULL) {
42-
return osErrorResource;
42+
return osErrorParameter;
4343
}
4444

4545
#ifdef __MBED_CMSIS_RTOS_CM
@@ -73,6 +73,20 @@ osStatus Thread::terminate() {
7373
return osThreadTerminate(_tid);
7474
}
7575

76+
osStatus Thread::join() {
77+
while (true) {
78+
uint8_t state = get_state();
79+
if (state == Thread::Inactive || state == osErrorParameter) {
80+
return osOK;
81+
}
82+
83+
osStatus status = yield();
84+
if (status != osOK) {
85+
return status;
86+
}
87+
}
88+
}
89+
7690
osStatus Thread::set_priority(osPriority priority) {
7791
return osThreadSetPriority(_tid, priority);
7892
}

core/mbed-rtos/rtos/Thread.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class Thread {
5959
uint32_t stack_size=DEFAULT_STACK_SIZE,
6060
unsigned char *stack_pointer=NULL);
6161

62+
/** Wait for thread to terminate
63+
@return status code that indicates the execution status of the function.
64+
*/
65+
osStatus join();
66+
6267
/** Terminate execution of a thread and remove it from Active Threads
6368
@return status code that indicates the execution status of the function.
6469
*/

0 commit comments

Comments
 (0)