Skip to content

Commit 8a37762

Browse files
committed
Smaller Thread class with clearer error messages.
1 parent ef291e7 commit 8a37762

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

core/mbed-rtos/rtos/Thread.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,35 @@
2727
namespace rtos {
2828

2929
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
3537
}
3638

3739
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+
}
4459
}
4560

4661
osStatus Thread::start(void (*task)(void const *argument), void *argument) {
@@ -50,26 +65,22 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
5065

5166
#ifdef __MBED_CMSIS_RTOS_CM
5267
_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)];
5970
if (_thread_def.stack_pointer == NULL)
60-
error("Error allocating the stack memory\n");
71+
return osErrorNoMemory;
6172
}
62-
73+
6374
//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++) {
6576
_thread_def.stack_pointer[i] = 0xE25A2EA5;
6677
}
6778
#endif
6879
_tid = osThreadCreate(&_thread_def, argument);
6980
if (_tid == NULL) {
81+
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
7082
return osErrorResource;
7183
}
72-
7384
return osOK;
7485
}
7586

@@ -195,7 +206,7 @@ void Thread::attach_idle_hook(void (*fptr)(void)) {
195206
Thread::~Thread() {
196207
terminate();
197208
#ifdef __MBED_CMSIS_RTOS_CM
198-
if (_stack_pointer == NULL) {
209+
if (_dynamic_stack) {
199210
delete[] (_thread_def.stack_pointer);
200211
}
201212
#endif

core/mbed-rtos/rtos/Thread.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ class Thread {
167167
private:
168168
osThreadId _tid;
169169
osThreadDef_t _thread_def;
170-
171-
osPriority _priority;
172-
uint32_t _stack_size;
173-
unsigned char *_stack_pointer;
170+
bool _dynamic_stack;
174171
};
175172

176173
}

0 commit comments

Comments
 (0)