27
27
namespace rtos {
28
28
29
29
Thread::Thread (osPriority priority,
30
- uint32_t stack_size, unsigned char *stack_pointer) {
31
- _tid = NULL ;
32
- _priority = priority;
33
- _stack_size = stack_size;
34
- _stack_pointer = stack_pointer;
30
+ uint32_t stack_size, unsigned char *stack_pointer):
31
+ _tid (NULL ), _dynamic_stack(stack_pointer == NULL ) {
32
+ #ifdef __MBED_CMSIS_RTOS_CM
33
+ _thread_def.tpriority = priority;
34
+ _thread_def.stacksize = stack_size;
35
+ _thread_def.stack_pointer = (uint32_t *)stack_pointer;
36
+ #endif
35
37
}
36
38
37
39
Thread::Thread (void (*task)(void const *argument), void *argument,
38
- osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
39
- _tid = NULL ;
40
- _priority = priority;
41
- _stack_size = stack_size;
42
- _stack_pointer = stack_pointer;
43
- start (task, argument);
40
+ osPriority priority, uint32_t stack_size, unsigned char *stack_pointer):
41
+ _tid (NULL ), _dynamic_stack(stack_pointer == NULL ) {
42
+ #ifdef __MBED_CMSIS_RTOS_CM
43
+ _thread_def.tpriority = priority;
44
+ _thread_def.stacksize = stack_size;
45
+ _thread_def.stack_pointer = (uint32_t *)stack_pointer;
46
+ #endif
47
+ switch (start (task, argument)) {
48
+ case osErrorResource:
49
+ error (" OS ran out of threads!\n " );
50
+ break ;
51
+ case osErrorParameter:
52
+ error (" Thread already running!\n " );
53
+ break ;
54
+ case osErrorNoMemory:
55
+ error (" Error allocating the stack memory\n " );
56
+ default :
57
+ break ;
58
+ }
44
59
}
45
60
46
61
osStatus Thread::start (void (*task)(void const *argument), void *argument) {
@@ -50,26 +65,22 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
50
65
51
66
#ifdef __MBED_CMSIS_RTOS_CM
52
67
_thread_def.pthread = task;
53
- _thread_def.tpriority = _priority;
54
- _thread_def.stacksize = _stack_size;
55
- if (_stack_pointer != NULL ) {
56
- _thread_def.stack_pointer = (uint32_t *)_stack_pointer;
57
- } else {
58
- _thread_def.stack_pointer = new uint32_t [_stack_size/sizeof (uint32_t )];
68
+ if (_thread_def.stack_pointer == NULL ) {
69
+ _thread_def.stack_pointer = new uint32_t [_thread_def.stacksize /sizeof (uint32_t )];
59
70
if (_thread_def.stack_pointer == NULL )
60
- error ( " Error allocating the stack memory \n " ) ;
71
+ return osErrorNoMemory ;
61
72
}
62
-
73
+
63
74
// Fill the stack with a magic word for maximum usage checking
64
- for (uint32_t i = 0 ; i < (_stack_size / sizeof (uint32_t )); i++) {
75
+ for (uint32_t i = 0 ; i < (_thread_def. stacksize / sizeof (uint32_t )); i++) {
65
76
_thread_def.stack_pointer [i] = 0xE25A2EA5 ;
66
77
}
67
78
#endif
68
79
_tid = osThreadCreate (&_thread_def, argument);
69
80
if (_tid == NULL ) {
81
+ if (_dynamic_stack) delete[] (_thread_def.stack_pointer );
70
82
return osErrorResource;
71
83
}
72
-
73
84
return osOK;
74
85
}
75
86
@@ -195,7 +206,7 @@ void Thread::attach_idle_hook(void (*fptr)(void)) {
195
206
Thread::~Thread () {
196
207
terminate ();
197
208
#ifdef __MBED_CMSIS_RTOS_CM
198
- if (_stack_pointer == NULL ) {
209
+ if (_dynamic_stack ) {
199
210
delete[] (_thread_def.stack_pointer );
200
211
}
201
212
#endif
0 commit comments