24
24
25
25
#include < stdint.h>
26
26
#include " cmsis_os.h"
27
+ #include " Callback.h"
27
28
28
29
namespace rtos {
29
30
@@ -37,7 +38,9 @@ class Thread {
37
38
*/
38
39
Thread (osPriority priority=osPriorityNormal,
39
40
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
+ }
41
44
42
45
/* * Create a new thread, and start it executing the specified function.
43
46
@param task function to be executed by this thread.
@@ -46,17 +49,78 @@ class Thread {
46
49
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
47
50
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
48
51
*/
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
+ */
49
101
Thread (void (*task)(void const *argument), void *argument=NULL ,
50
102
osPriority priority=osPriorityNormal,
51
103
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
+ }
53
108
54
109
/* * Starts a thread executing the specified function.
55
110
@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).
57
111
@return status code that indicates the execution status of the function.
58
112
*/
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
+ }
60
124
61
125
/* * Wait for thread to terminate
62
126
@return status code that indicates the execution status of the function.
@@ -165,6 +229,17 @@ class Thread {
165
229
virtual ~Thread ();
166
230
167
231
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;
168
243
osThreadId _tid;
169
244
osThreadDef_t _thread_def;
170
245
bool _dynamic_stack;
0 commit comments