Skip to content

Commit 8b8606b

Browse files
committed
Add start function for separating object allocation from thread initialization
Allows threads to started separately from when they are declared, avoiding the need to dynamically allocate threads at runtime.
1 parent 1ae994f commit 8b8606b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

core/mbed-rtos/rtos/Thread.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,22 @@
2626

2727
namespace rtos {
2828

29+
Thread::Thread() {
30+
_tid = NULL;
31+
}
32+
2933
Thread::Thread(void (*task)(void const *argument), void *argument,
3034
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
35+
_tid = NULL;
36+
start(task, argument, priority, stack_size, stack_pointer);
37+
}
38+
39+
osStatus Thread::start(void (*task)(void const *argument), void *argument,
40+
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
41+
if (_tid != NULL) {
42+
return osErrorResource;
43+
}
44+
3145
#ifdef __MBED_CMSIS_RTOS_CM
3246
_thread_def.pthread = task;
3347
_thread_def.tpriority = priority;
@@ -48,6 +62,11 @@ Thread::Thread(void (*task)(void const *argument), void *argument,
4862
}
4963
#endif
5064
_tid = osThreadCreate(&_thread_def, argument);
65+
if (_tid == NULL) {
66+
return osErrorResource;
67+
}
68+
69+
return osOK;
5170
}
5271

5372
osStatus Thread::terminate() {

core/mbed-rtos/rtos/Thread.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ namespace rtos {
3030
/** The Thread class allow defining, creating, and controlling thread functions in the system. */
3131
class Thread {
3232
public:
33+
/** Allocate a new thread without starting execution
34+
*/
35+
Thread();
36+
3337
/** Create a new thread, and start it executing the specified function.
3438
@param task function to be executed by this thread.
3539
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
@@ -42,6 +46,19 @@ class Thread {
4246
uint32_t stack_size=DEFAULT_STACK_SIZE,
4347
unsigned char *stack_pointer=NULL);
4448

49+
/** Starts a thread executing the specified function.
50+
@param task function to be executed by this thread.
51+
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
52+
@param priority initial priority of the thread function. (default: osPriorityNormal).
53+
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
54+
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
55+
@return status code that indicates the execution status of the function.
56+
*/
57+
osStatus start(void (*task)(void const *argument), void *argument=NULL,
58+
osPriority priority=osPriorityNormal,
59+
uint32_t stack_size=DEFAULT_STACK_SIZE,
60+
unsigned char *stack_pointer=NULL);
61+
4562
/** Terminate execution of a thread and remove it from Active Threads
4663
@return status code that indicates the execution status of the function.
4764
*/

0 commit comments

Comments
 (0)