Skip to content

Commit cde78ec

Browse files
committed
Merge pull request #94 from geky/callback
Improve FunctionPointer class
2 parents b7cdc20 + dd6a24b commit cde78ec

File tree

18 files changed

+1344
-303
lines changed

18 files changed

+1344
-303
lines changed

core/mbed-rtos/rtos/Thread.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,34 @@
2121
*/
2222
#include "Thread.h"
2323

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

2727
namespace rtos {
2828

29-
Thread::Thread(osPriority priority,
30-
uint32_t stack_size, unsigned char *stack_pointer):
31-
_tid(NULL), _dynamic_stack(stack_pointer == NULL) {
29+
void Thread::constructor(osPriority priority,
30+
uint32_t stack_size, unsigned char *stack_pointer) {
31+
_tid = NULL;
32+
_dynamic_stack = (stack_pointer == NULL);
33+
3234
#ifdef __MBED_CMSIS_RTOS_CM
3335
_thread_def.tpriority = priority;
3436
_thread_def.stacksize = stack_size;
3537
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
3638
#endif
3739
}
3840

39-
Thread::Thread(void (*task)(void const *argument), void *argument,
40-
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer):
41-
_tid(NULL), _dynamic_stack(stack_pointer == NULL) {
41+
void Thread::constructor(Callback<void()> task,
42+
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
43+
_tid = NULL;
44+
_dynamic_stack = (stack_pointer == NULL);
45+
4246
#ifdef __MBED_CMSIS_RTOS_CM
4347
_thread_def.tpriority = priority;
4448
_thread_def.stacksize = stack_size;
4549
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
4650
#endif
47-
switch(start(task, argument)) {
51+
switch(start(task)) {
4852
case osErrorResource:
4953
error("OS ran out of threads!\n");
5054
break;
@@ -58,13 +62,13 @@ Thread::Thread(void (*task)(void const *argument), void *argument,
5862
}
5963
}
6064

61-
osStatus Thread::start(void (*task)(void const *argument), void *argument) {
65+
osStatus Thread::start(Callback<void()> task) {
6266
if (_tid != NULL) {
6367
return osErrorParameter;
6468
}
6569

6670
#ifdef __MBED_CMSIS_RTOS_CM
67-
_thread_def.pthread = task;
71+
_thread_def.pthread = (void (*)(const void *))Callback<void()>::thunk;
6872
if (_thread_def.stack_pointer == NULL) {
6973
_thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
7074
if (_thread_def.stack_pointer == NULL)
@@ -76,7 +80,8 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
7680
_thread_def.stack_pointer[i] = 0xE25A2EA5;
7781
}
7882
#endif
79-
_tid = osThreadCreate(&_thread_def, argument);
83+
_task = task;
84+
_tid = osThreadCreate(&_thread_def, &_task);
8085
if (_tid == NULL) {
8186
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
8287
return osErrorResource;

core/mbed-rtos/rtos/Thread.h

+77-4
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,26 +38,87 @@ 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.
44-
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
4547
@param priority initial priority of the thread function. (default: osPriorityNormal).
4648
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
4749
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
4850
*/
51+
Thread(mbed::Callback<void()> task,
52+
osPriority priority=osPriorityNormal,
53+
uint32_t stack_size=DEFAULT_STACK_SIZE,
54+
unsigned char *stack_pointer=NULL) {
55+
constructor(task, priority, stack_size, stack_pointer);
56+
}
57+
58+
/** Create a new thread, and start it executing the specified function.
59+
@param obj argument to task.
60+
@param method function to be executed by this thread.
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 priority initial priority of the thread function. (default: osPriorityNormal).
78+
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
79+
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
80+
*/
81+
template <typename T>
82+
Thread(T *obj, void (*method)(T *),
83+
osPriority priority=osPriorityNormal,
84+
uint32_t stack_size=DEFAULT_STACK_SIZE,
85+
unsigned char *stack_pointer=NULL) {
86+
constructor(mbed::Callback<void()>(obj, method),
87+
priority, stack_size, stack_pointer);
88+
}
89+
90+
/** Create a new thread, and start it executing the specified function.
91+
Provided for backwards compatibility
92+
@param task function to be executed by this thread.
93+
@param argument pointer that is passed to the thread function as start argument. (default: NULL).
94+
@param priority initial priority OF the thread function. (default: osPriorityNormal).
95+
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
96+
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
97+
*/
4998
Thread(void (*task)(void const *argument), void *argument=NULL,
5099
osPriority priority=osPriorityNormal,
51100
uint32_t stack_size=DEFAULT_STACK_SIZE,
52-
unsigned char *stack_pointer=NULL);
101+
unsigned char *stack_pointer=NULL) {
102+
constructor(mbed::Callback<void()>(argument, (void (*)(void *))task),
103+
priority, stack_size, stack_pointer);
104+
}
53105

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

61123
/** Wait for thread to terminate
62124
@return status code that indicates the execution status of the function.
@@ -165,6 +227,17 @@ class Thread {
165227
virtual ~Thread();
166228

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

0 commit comments

Comments
 (0)