Skip to content

Commit

Permalink
Added support for Callback to Thread lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
geky committed Jun 1, 2016
1 parent 0180125 commit e079c2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
26 changes: 11 additions & 15 deletions rtos/rtos/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);

namespace rtos {

Thread::Thread(osPriority priority,
uint32_t stack_size, unsigned char *stack_pointer):
_tid(0), _dynamic_stack(stack_pointer == NULL) {
void Thread::constructor(osPriority priority,
uint32_t stack_size, unsigned char *stack_pointer) {
_tid = 0;
_dynamic_stack = (stack_pointer == NULL);

#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
_thread_def.tpriority = priority;
_thread_def.stacksize = stack_size;
Expand All @@ -44,16 +46,9 @@ Thread::Thread(osPriority priority,

void Thread::constructor(Callback<void()> task,
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
_tid = 0;
_dynamic_stack = (stack_pointer == NULL);
constructor(priority, stack_size, stack_pointer);

_task = task;
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
_thread_def.tpriority = priority;
_thread_def.stacksize = stack_size;
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
#endif
switch (start((void (*)(const void *))Callback<void()>::thunk, &_task)) {
switch (start(task)) {
case osErrorResource:
error("OS ran out of threads!\n");
break;
Expand All @@ -67,13 +62,13 @@ void Thread::constructor(Callback<void()> task,
}
}

osStatus Thread::start(void (*task)(void const *argument), void *argument) {
if (_tid != NULL) {
osStatus Thread::start(Callback<void()> task) {
if (_tid != 0) {
return osErrorParameter;
}

#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
_thread_def.pthread = task;
_thread_def.pthread = (void (*)(const void *))Callback<void()>::thunk;
if (_thread_def.stack_pointer == NULL) {
_thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
if (_thread_def.stack_pointer == NULL)
Expand All @@ -85,6 +80,7 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
_thread_def.stack_pointer[i] = 0xE25A2EA5;
}
#endif
_task = task;
_tid = osThreadCreate(&_thread_def, &_task);
if (_tid == NULL) {
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
Expand Down
20 changes: 17 additions & 3 deletions rtos/rtos/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class Thread {
*/
Thread(osPriority priority=osPriorityNormal,
uint32_t stack_size=DEFAULT_STACK_SIZE,
unsigned char *stack_pointer=NULL);
unsigned char *stack_pointer=NULL) {
constructor(priority, stack_size, stack_pointer);
}

/** Create a new thread, and start it executing the specified function.
@param task function to be executed by this thread.
Expand Down Expand Up @@ -106,10 +108,19 @@ class Thread {

/** 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).
@return status code that indicates the execution status of the function.
*/
osStatus start(void (*task)(void const *argument), void *argument=NULL);
osStatus start(mbed::Callback<void()> task);

/** Starts a thread executing the specified function.
@param obj argument to task
@param method function to be executed by this thread.
@return status code that indicates the execution status of the function.
*/
template <typename T, typename M>
osStatus start(T *obj, M method) {
return start(mbed::Callback<void()>(obj, method));
}

/** Wait for thread to terminate
@return status code that indicates the execution status of the function.
Expand Down Expand Up @@ -220,6 +231,9 @@ class Thread {
private:
// Required to share definitions without
// delegated constructors
void constructor(osPriority priority=osPriorityNormal,
uint32_t stack_size=DEFAULT_STACK_SIZE,
unsigned char *stack_pointer=NULL);
void constructor(mbed::Callback<void()> task,
osPriority priority=osPriorityNormal,
uint32_t stack_size=DEFAULT_STACK_SIZE,
Expand Down

0 comments on commit e079c2e

Please sign in to comment.