Skip to content

Commit e079c2e

Browse files
committed
Added support for Callback to Thread lifetime
1 parent 0180125 commit e079c2e

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

rtos/rtos/Thread.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
3232

3333
namespace rtos {
3434

35-
Thread::Thread(osPriority priority,
36-
uint32_t stack_size, unsigned char *stack_pointer):
37-
_tid(0), _dynamic_stack(stack_pointer == NULL) {
35+
void Thread::constructor(osPriority priority,
36+
uint32_t stack_size, unsigned char *stack_pointer) {
37+
_tid = 0;
38+
_dynamic_stack = (stack_pointer == NULL);
39+
3840
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
3941
_thread_def.tpriority = priority;
4042
_thread_def.stacksize = stack_size;
@@ -44,16 +46,9 @@ Thread::Thread(osPriority priority,
4446

4547
void Thread::constructor(Callback<void()> task,
4648
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
47-
_tid = 0;
48-
_dynamic_stack = (stack_pointer == NULL);
49+
constructor(priority, stack_size, stack_pointer);
4950

50-
_task = task;
51-
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
52-
_thread_def.tpriority = priority;
53-
_thread_def.stacksize = stack_size;
54-
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
55-
#endif
56-
switch (start((void (*)(const void *))Callback<void()>::thunk, &_task)) {
51+
switch (start(task)) {
5752
case osErrorResource:
5853
error("OS ran out of threads!\n");
5954
break;
@@ -67,13 +62,13 @@ void Thread::constructor(Callback<void()> task,
6762
}
6863
}
6964

70-
osStatus Thread::start(void (*task)(void const *argument), void *argument) {
71-
if (_tid != NULL) {
65+
osStatus Thread::start(Callback<void()> task) {
66+
if (_tid != 0) {
7267
return osErrorParameter;
7368
}
7469

7570
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
76-
_thread_def.pthread = task;
71+
_thread_def.pthread = (void (*)(const void *))Callback<void()>::thunk;
7772
if (_thread_def.stack_pointer == NULL) {
7873
_thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
7974
if (_thread_def.stack_pointer == NULL)
@@ -85,6 +80,7 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
8580
_thread_def.stack_pointer[i] = 0xE25A2EA5;
8681
}
8782
#endif
83+
_task = task;
8884
_tid = osThreadCreate(&_thread_def, &_task);
8985
if (_tid == NULL) {
9086
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);

rtos/rtos/Thread.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class Thread {
3838
*/
3939
Thread(osPriority priority=osPriorityNormal,
4040
uint32_t stack_size=DEFAULT_STACK_SIZE,
41-
unsigned char *stack_pointer=NULL);
41+
unsigned char *stack_pointer=NULL) {
42+
constructor(priority, stack_size, stack_pointer);
43+
}
4244

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

107109
/** Starts a thread executing the specified function.
108110
@param task function to be executed by this thread.
109-
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
110111
@return status code that indicates the execution status of the function.
111112
*/
112-
osStatus start(void (*task)(void const *argument), void *argument=NULL);
113+
osStatus start(mbed::Callback<void()> task);
114+
115+
/** Starts a thread executing the specified function.
116+
@param obj argument to task
117+
@param method function to be executed by this thread.
118+
@return status code that indicates the execution status of the function.
119+
*/
120+
template <typename T, typename M>
121+
osStatus start(T *obj, M method) {
122+
return start(mbed::Callback<void()>(obj, method));
123+
}
113124

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

0 commit comments

Comments
 (0)