Skip to content

Commit 0180125

Browse files
committed
Adopt Callback class in rtos Threads
1 parent 2db84da commit 0180125

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

rtos/rtos/Thread.cpp

Lines changed: 9 additions & 6 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
@@ -42,15 +42,18 @@ Thread::Thread(osPriority priority,
4242
#endif
4343
}
4444

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) {
45+
void Thread::constructor(Callback<void()> task,
46+
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
47+
_tid = 0;
48+
_dynamic_stack = (stack_pointer == NULL);
49+
50+
_task = task;
4851
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
4952
_thread_def.tpriority = priority;
5053
_thread_def.stacksize = stack_size;
5154
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
5255
#endif
53-
switch (start(task, argument)) {
56+
switch (start((void (*)(const void *))Callback<void()>::thunk, &_task)) {
5457
case osErrorResource:
5558
error("OS ran out of threads!\n");
5659
break;
@@ -82,7 +85,7 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
8285
_thread_def.stack_pointer[i] = 0xE25A2EA5;
8386
}
8487
#endif
85-
_tid = osThreadCreate(&_thread_def, argument);
88+
_tid = osThreadCreate(&_thread_def, &_task);
8689
if (_tid == NULL) {
8790
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
8891
return osErrorResource;

rtos/rtos/Thread.h

Lines changed: 62 additions & 1 deletion
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

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

54107
/** Starts a thread executing the specified function.
55108
@param task function to be executed by this thread.
@@ -165,6 +218,14 @@ class Thread {
165218
virtual ~Thread();
166219

167220
private:
221+
// Required to share definitions without
222+
// delegated constructors
223+
void constructor(mbed::Callback<void()> task,
224+
osPriority priority=osPriorityNormal,
225+
uint32_t stack_size=DEFAULT_STACK_SIZE,
226+
unsigned char *stack_pointer=NULL);
227+
228+
mbed::Callback<void()> _task;
168229
osThreadId _tid;
169230
osThreadDef_t _thread_def;
170231
bool _dynamic_stack;

0 commit comments

Comments
 (0)