Smaller Thread class with clearer error messages.

Niklas Hauser 2016-05-10 14:02:58 +01:00
parent ef291e79d9
commit 8a37762866
2 changed files with 34 additions and 26 deletions

View File

@ -27,20 +27,35 @@
namespace rtos { namespace rtos {
Thread::Thread(osPriority priority, Thread::Thread(osPriority priority,
uint32_t stack_size, unsigned char *stack_pointer) { uint32_t stack_size, unsigned char *stack_pointer):
_tid = NULL; _tid(NULL), _dynamic_stack(stack_pointer == NULL) {
_priority = priority; #ifdef __MBED_CMSIS_RTOS_CM
_stack_size = stack_size; _thread_def.tpriority = priority;
_stack_pointer = stack_pointer; _thread_def.stacksize = stack_size;
_thread_def.stack_pointer = (uint32_t*)stack_pointer;
#endif
} }
Thread::Thread(void (*task)(void const *argument), void *argument, Thread::Thread(void (*task)(void const *argument), void *argument,
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { osPriority priority, uint32_t stack_size, unsigned char *stack_pointer):
_tid = NULL; _tid(NULL), _dynamic_stack(stack_pointer == NULL) {
_priority = priority; #ifdef __MBED_CMSIS_RTOS_CM
_stack_size = stack_size; _thread_def.tpriority = priority;
_stack_pointer = stack_pointer; _thread_def.stacksize = stack_size;
start(task, argument); _thread_def.stack_pointer = (uint32_t*)stack_pointer;
#endif
switch(start(task, argument)) {
case osErrorResource:
error("OS ran out of threads!\n");
break;
case osErrorParameter:
error("Thread already running!\n");
break;
case osErrorNoMemory:
error("Error allocating the stack memory\n");
default:
break;
}
} }
osStatus Thread::start(void (*task)(void const *argument), void *argument) { osStatus Thread::start(void (*task)(void const *argument), void *argument) {
@ -50,26 +65,22 @@ osStatus Thread::start(void (*task)(void const *argument), void *argument) {
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
_thread_def.pthread = task; _thread_def.pthread = task;
_thread_def.tpriority = _priority; if (_thread_def.stack_pointer == NULL) {
_thread_def.stacksize = _stack_size; _thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
if (_stack_pointer != NULL) {
_thread_def.stack_pointer = (uint32_t*)_stack_pointer;
} else {
_thread_def.stack_pointer = new uint32_t[_stack_size/sizeof(uint32_t)];
if (_thread_def.stack_pointer == NULL) if (_thread_def.stack_pointer == NULL)
error("Error allocating the stack memory\n"); return osErrorNoMemory;
} }
//Fill the stack with a magic word for maximum usage checking //Fill the stack with a magic word for maximum usage checking
for (uint32_t i = 0; i < (_stack_size / sizeof(uint32_t)); i++) { for (uint32_t i = 0; i < (_thread_def.stacksize / sizeof(uint32_t)); i++) {
_thread_def.stack_pointer[i] = 0xE25A2EA5; _thread_def.stack_pointer[i] = 0xE25A2EA5;
} }
#endif #endif
_tid = osThreadCreate(&_thread_def, argument); _tid = osThreadCreate(&_thread_def, argument);
if (_tid == NULL) { if (_tid == NULL) {
if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
return osErrorResource; return osErrorResource;
} }
return osOK; return osOK;
} }
@ -195,7 +206,7 @@ void Thread::attach_idle_hook(void (*fptr)(void)) {
Thread::~Thread() { Thread::~Thread() {
terminate(); terminate();
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
if (_stack_pointer == NULL) { if (_dynamic_stack) {
delete[] (_thread_def.stack_pointer); delete[] (_thread_def.stack_pointer);
} }
#endif #endif

View File

@ -167,10 +167,7 @@ public:
private: private:
osThreadId _tid; osThreadId _tid;
osThreadDef_t _thread_def; osThreadDef_t _thread_def;
bool _dynamic_stack;
osPriority _priority;
uint32_t _stack_size;
unsigned char *_stack_pointer;
}; };
} }