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.
Christopher Haster 2016-04-22 01:03:53 -05:00
parent 1ae994fb62
commit 8b8606b98d
2 changed files with 36 additions and 0 deletions

View File

@ -26,8 +26,22 @@
namespace rtos { namespace rtos {
Thread::Thread() {
_tid = NULL;
}
Thread::Thread(void (*task)(void const *argument), void *argument, Thread::Thread(void (*task)(void const *argument), void *argument,
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
_tid = NULL;
start(task, argument, priority, stack_size, stack_pointer);
}
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;
}
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
_thread_def.pthread = task; _thread_def.pthread = task;
_thread_def.tpriority = priority; _thread_def.tpriority = priority;
@ -48,6 +62,11 @@ Thread::Thread(void (*task)(void const *argument), void *argument,
} }
#endif #endif
_tid = osThreadCreate(&_thread_def, argument); _tid = osThreadCreate(&_thread_def, argument);
if (_tid == NULL) {
return osErrorResource;
}
return osOK;
} }
osStatus Thread::terminate() { osStatus Thread::terminate() {

View File

@ -30,6 +30,10 @@ namespace rtos {
/** The Thread class allow defining, creating, and controlling thread functions in the system. */ /** The Thread class allow defining, creating, and controlling thread functions in the system. */
class Thread { class Thread {
public: public:
/** Allocate a new thread without starting execution
*/
Thread();
/** Create a new thread, and start it executing the specified function. /** Create a new thread, and start it executing the specified function.
@param task function to be executed by this thread. @param task function to be executed by this thread.
@param argument pointer that is passed to the thread function as start argument. (default: NULL). @param argument pointer that is passed to the thread function as start argument. (default: NULL).
@ -42,6 +46,19 @@ public:
uint32_t stack_size=DEFAULT_STACK_SIZE, uint32_t stack_size=DEFAULT_STACK_SIZE,
unsigned char *stack_pointer=NULL); unsigned char *stack_pointer=NULL);
/** Starts a thread executing the specified function.
@param task function to be executed by this thread.
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
@param priority initial priority of the thread function. (default: osPriorityNormal).
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
@return status code that indicates the execution status of the function.
*/
osStatus start(void (*task)(void const *argument), void *argument=NULL,
osPriority priority=osPriorityNormal,
uint32_t stack_size=DEFAULT_STACK_SIZE,
unsigned char *stack_pointer=NULL);
/** Terminate execution of a thread and remove it from Active Threads /** Terminate execution of a thread and remove it from Active Threads
@return status code that indicates the execution status of the function. @return status code that indicates the execution status of the function.
*/ */