Skip to content

Commit f68cdcb

Browse files
committed
Merge pull request #1802 from theotherjimmy/callback-rtos
Adopt Callback class in rtos Threads
2 parents 7643399 + e079c2e commit f68cdcb

File tree

2 files changed

+95
-21
lines changed

2 files changed

+95
-21
lines changed

rtos/rtos/Thread.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
#include "Thread.h"
2323

24-
#include "mbed_error.h"
24+
#include "mbed.h"
2525
#include "rtos_idle.h"
2626

2727
// rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
@@ -32,25 +32,23 @@ 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;
4143
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
4244
#endif
4345
}
4446

45-
Thread::Thread(void (*task)(void const *argument), void *argument,
46-
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer):
47-
_tid(0), _dynamic_stack(stack_pointer == NULL) {
48-
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
49-
_thread_def.tpriority = priority;
50-
_thread_def.stacksize = stack_size;
51-
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
52-
#endif
53-
switch (start(task, argument)) {
47+
void Thread::constructor(Callback<void()> task,
48+
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
49+
constructor(priority, stack_size, stack_pointer);
50+
51+
switch (start(task)) {
5452
case osErrorResource:
5553
error("OS ran out of threads!\n");
5654
break;
@@ -64,13 +62,13 @@ Thread::Thread(void (*task)(void const *argument), void *argument,
6462
}
6563
}
6664

67-
osStatus Thread::start(void (*task)(void const *argument), void *argument) {
68-
if (_tid != NULL) {
65+
osStatus Thread::start(Callback<void()> task) {
66+
if (_tid != 0) {
6967
return osErrorParameter;
7068
}
7169

7270
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
73-
_thread_def.pthread = task;
71+
_thread_def.pthread = (void (*)(const void *))Callback<void()>::thunk;
7472
if (_thread_def.stack_pointer == NULL) {
7573
_thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
7674
if (_thread_def.stack_pointer == NULL)
@@ -82,7 +80,8 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
8280
_thread_def.stack_pointer[i] = 0xE25A2EA5;
8381
}
8482
#endif
85-
_tid = osThreadCreate(&_thread_def, argument);
83+
_task = task;
84+
_tid = osThreadCreate(&_thread_def, &_task);
8685
if (_tid == NULL) {
8786
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
8887
return osErrorResource;

rtos/rtos/Thread.h

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <stdint.h>
2626
#include "cmsis_os.h"
27+
#include "Callback.h"
2728

2829
namespace rtos {
2930

@@ -37,7 +38,9 @@ class Thread {
3738
*/
3839
Thread(osPriority priority=osPriorityNormal,
3940
uint32_t stack_size=DEFAULT_STACK_SIZE,
40-
unsigned char *stack_pointer=NULL);
41+
unsigned char *stack_pointer=NULL) {
42+
constructor(priority, stack_size, stack_pointer);
43+
}
4144

4245
/** Create a new thread, and start it executing the specified function.
4346
@param task function to be executed by this thread.
@@ -46,17 +49,78 @@ class Thread {
4649
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
4750
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
4851
*/
52+
Thread(mbed::Callback<void()> task,
53+
osPriority priority=osPriorityNormal,
54+
uint32_t stack_size=DEFAULT_STACK_SIZE,
55+
unsigned char *stack_pointer=NULL) {
56+
constructor(task, priority, stack_size, stack_pointer);
57+
}
58+
59+
/** Create a new thread, and start it executing the specified function.
60+
@param obj argument to task.
61+
@param method function to be executed by this thread.
62+
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
63+
@param priority initial priority of the thread function. (default: osPriorityNormal).
64+
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
65+
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
66+
*/
67+
template <typename T>
68+
Thread(T *obj, void (T::*method)(),
69+
osPriority priority=osPriorityNormal,
70+
uint32_t stack_size=DEFAULT_STACK_SIZE,
71+
unsigned char *stack_pointer=NULL) {
72+
constructor(mbed::Callback<void()>(obj, method),
73+
priority, stack_size, stack_pointer);
74+
}
75+
76+
/** Create a new thread, and start it executing the specified function.
77+
@param obj argument to task.
78+
@param method function to be executed by this thread.
79+
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
80+
@param priority initial priority of the thread function. (default: osPriorityNormal).
81+
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
82+
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
83+
*/
84+
template <typename T>
85+
Thread(T *obj, void (*method)(T *),
86+
osPriority priority=osPriorityNormal,
87+
uint32_t stack_size=DEFAULT_STACK_SIZE,
88+
unsigned char *stack_pointer=NULL) {
89+
constructor(mbed::Callback<void()>(obj, method),
90+
priority, stack_size, stack_pointer);
91+
}
92+
93+
/** Create a new thread, and start it executing the specified function.
94+
Provided for backwards compatibility
95+
@param task function to be executed by this thread.
96+
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
97+
@param priority initial priority of the thread function. (default: osPriorityNormal).
98+
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
99+
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
100+
*/
49101
Thread(void (*task)(void const *argument), void *argument=NULL,
50102
osPriority priority=osPriorityNormal,
51103
uint32_t stack_size=DEFAULT_STACK_SIZE,
52-
unsigned char *stack_pointer=NULL);
104+
unsigned char *stack_pointer=NULL) {
105+
constructor(mbed::Callback<void()>(argument, (void (*)(void *))task),
106+
priority, stack_size, stack_pointer);
107+
}
53108

54109
/** Starts a thread executing the specified function.
55110
@param task function to be executed by this thread.
56-
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
57111
@return status code that indicates the execution status of the function.
58112
*/
59-
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+
}
60124

61125
/** Wait for thread to terminate
62126
@return status code that indicates the execution status of the function.
@@ -165,6 +229,17 @@ class Thread {
165229
virtual ~Thread();
166230

167231
private:
232+
// Required to share definitions without
233+
// delegated constructors
234+
void constructor(osPriority priority=osPriorityNormal,
235+
uint32_t stack_size=DEFAULT_STACK_SIZE,
236+
unsigned char *stack_pointer=NULL);
237+
void constructor(mbed::Callback<void()> task,
238+
osPriority priority=osPriorityNormal,
239+
uint32_t stack_size=DEFAULT_STACK_SIZE,
240+
unsigned char *stack_pointer=NULL);
241+
242+
mbed::Callback<void()> _task;
168243
osThreadId _tid;
169244
osThreadDef_t _thread_def;
170245
bool _dynamic_stack;

0 commit comments

Comments
 (0)