@@ -32,34 +32,82 @@ extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
32
32
33
33
namespace rtos {
34
34
35
+ Thread::Thread (osPriority priority,
36
+ uint32_t stack_size, unsigned char *stack_pointer):
37
+ _tid (0 ), _dynamic_stack(stack_pointer == NULL ) {
38
+ #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
39
+ _thread_def.tpriority = priority;
40
+ _thread_def.stacksize = stack_size;
41
+ _thread_def.stack_pointer = (uint32_t *)stack_pointer;
42
+ #endif
43
+ }
44
+
35
45
Thread::Thread (void (*task)(void const *argument), void *argument,
36
- osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
46
+ osPriority priority, uint32_t stack_size, unsigned char *stack_pointer):
47
+ _tid (0 ), _dynamic_stack(stack_pointer == NULL ) {
37
48
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
38
- _thread_def.pthread = task;
39
49
_thread_def.tpriority = priority;
40
50
_thread_def.stacksize = stack_size;
41
- if (stack_pointer != NULL ) {
42
- _thread_def.stack_pointer = (uint32_t *)stack_pointer;
43
- _dynamic_stack = false ;
44
- } else {
45
- _thread_def.stack_pointer = new uint32_t [stack_size/sizeof (uint32_t )];
46
- if (_thread_def.stack_pointer == NULL )
51
+ _thread_def.stack_pointer = (uint32_t *)stack_pointer;
52
+ #endif
53
+ switch (start (task, argument)) {
54
+ case osErrorResource:
55
+ error (" OS ran out of threads!\n " );
56
+ break ;
57
+ case osErrorParameter:
58
+ error (" Thread already running!\n " );
59
+ break ;
60
+ case osErrorNoMemory:
47
61
error (" Error allocating the stack memory\n " );
48
- _dynamic_stack = true ;
62
+ default :
63
+ break ;
64
+ }
65
+ }
66
+
67
+ osStatus Thread::start (void (*task)(void const *argument), void *argument) {
68
+ if (_tid != NULL ) {
69
+ return osErrorParameter;
49
70
}
50
-
71
+
72
+ #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
73
+ _thread_def.pthread = task;
74
+ if (_thread_def.stack_pointer == NULL ) {
75
+ _thread_def.stack_pointer = new uint32_t [_thread_def.stacksize /sizeof (uint32_t )];
76
+ if (_thread_def.stack_pointer == NULL )
77
+ return osErrorNoMemory;
78
+ }
79
+
51
80
// Fill the stack with a magic word for maximum usage checking
52
- for (uint32_t i = 0 ; i < (stack_size / sizeof (uint32_t )); i++) {
81
+ for (uint32_t i = 0 ; i < (_thread_def. stacksize / sizeof (uint32_t )); i++) {
53
82
_thread_def.stack_pointer [i] = 0xE25A2EA5 ;
54
83
}
55
84
#endif
56
85
_tid = osThreadCreate (&_thread_def, argument);
86
+ if (_tid == NULL ) {
87
+ if (_dynamic_stack) delete[] (_thread_def.stack_pointer );
88
+ return osErrorResource;
89
+ }
90
+ return osOK;
57
91
}
58
92
59
93
osStatus Thread::terminate () {
60
94
return osThreadTerminate (_tid);
61
95
}
62
96
97
+ osStatus Thread::join () {
98
+ while (true ) {
99
+ uint8_t state = get_state ();
100
+ if (state == Thread::Inactive || state == osErrorParameter) {
101
+ return osOK;
102
+ }
103
+
104
+ osStatus status = yield ();
105
+ if (status != osOK) {
106
+ return status;
107
+ }
108
+ }
109
+ }
110
+
63
111
osStatus Thread::set_priority (osPriority priority) {
64
112
return osThreadSetPriority (_tid, priority);
65
113
}
0 commit comments