Skip to content

Commit 48232be

Browse files
author
Cruz Monrreal
authored
Merge pull request #7872 from kjbracey-arm/thisthread
Add ThisThread namespace and deprecate static Thread methods
2 parents 0edce1d + 0ddd1d9 commit 48232be

File tree

10 files changed

+775
-279
lines changed

10 files changed

+775
-279
lines changed

TESTS/mbedmicro-rtos-mbed/threads/main.cpp

Lines changed: 99 additions & 96 deletions
Large diffs are not rendered by default.

rtos/Kernel.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
#include "rtos/Kernel.h"
2424

2525
#include "mbed.h"
26+
#include "rtos/rtos_idle.h"
27+
#include "rtos/rtos_handlers.h"
2628

2729
namespace rtos {
2830

29-
uint64_t Kernel::get_ms_count() {
31+
uint64_t Kernel::get_ms_count()
32+
{
3033
// CMSIS-RTOS 2.1.0 and 2.1.1 differ in the time type. We assume
3134
// our header at least matches the implementation, so we don't try looking
3235
// at the run-time version report. (There's no compile-time version report)
@@ -36,7 +39,7 @@ uint64_t Kernel::get_ms_count() {
3639
// 2.1.x who knows? We assume could go back to uint64_t
3740
if (sizeof osKernelGetTickCount() == sizeof(uint64_t)) {
3841
return osKernelGetTickCount();
39-
} else /* assume 32-bit */ {
42+
} else { /* assume 32-bit */
4043
// Based on suggestion in CMSIS-RTOS 2.1.1 docs, but with reentrancy
4144
// protection for the tick memory. We use critical section rather than a
4245
// mutex, as hopefully this method can be callable from interrupt later -
@@ -60,4 +63,14 @@ uint64_t Kernel::get_ms_count() {
6063
}
6164
}
6265

66+
void Kernel::attach_idle_hook(void (*fptr)(void))
67+
{
68+
rtos_attach_idle_hook(fptr);
69+
}
70+
71+
void Kernel::attach_thread_terminate_hook(void (*fptr)(osThreadId_t id))
72+
{
73+
rtos_attach_thread_terminate_hook(fptr);
74+
}
75+
6376
}

rtos/Kernel.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define KERNEL_H
2424

2525
#include <stdint.h>
26+
#include "cmsis_os2.h"
2627

2728
namespace rtos {
2829
/** \addtogroup rtos */
@@ -43,6 +44,20 @@ namespace Kernel {
4344
*/
4445
uint64_t get_ms_count();
4546

47+
/** Attach a function to be called by the RTOS idle task
48+
@param fptr pointer to the function to be called
49+
50+
@note You may call this function from ISR context.
51+
*/
52+
void attach_idle_hook(void (*fptr)(void));
53+
54+
/** Attach a function to be called when a task is killed
55+
@param fptr pointer to the function to be called
56+
57+
@note You may call this function from ISR context.
58+
*/
59+
void attach_thread_terminate_hook(void (*fptr)(osThreadId_t id));
60+
4661
} // namespace Kernel
4762

4863
} // namespace rtos

rtos/TARGET_CORTEX/mbed_rtx_handlers.c

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mbed_error.h"
2222
#include "mbed_interface.h"
2323
#include "RTX_Config.h"
24+
#include "rtos/rtos_handlers.h"
2425

2526
#ifdef RTE_Compiler_EventRecorder
2627
#include "EventRecorder.h" // Keil::Compiler:Event Recorder
@@ -30,45 +31,58 @@
3031
#endif
3132

3233
extern void rtos_idle_loop(void);
33-
extern void thread_terminate_hook(osThreadId_t id);
3434

35-
__NO_RETURN void osRtxIdleThread (void *argument)
35+
static void (*terminate_hook)(osThreadId_t id);
36+
37+
static void thread_terminate_hook(osThreadId_t id)
38+
{
39+
if (terminate_hook) {
40+
terminate_hook(id);
41+
}
42+
}
43+
44+
void rtos_attach_thread_terminate_hook(void (*fptr)(osThreadId_t id))
45+
{
46+
terminate_hook = fptr;
47+
}
48+
49+
__NO_RETURN void osRtxIdleThread(void *argument)
3650
{
3751
for (;;) {
38-
rtos_idle_loop();
52+
rtos_idle_loop();
3953
}
4054
}
4155

42-
__NO_RETURN uint32_t osRtxErrorNotify (uint32_t code, void *object_id)
56+
__NO_RETURN uint32_t osRtxErrorNotify(uint32_t code, void *object_id)
4357
{
4458
osThreadId_t tid = osThreadGetId();
4559

4660
switch (code) {
47-
case osRtxErrorStackUnderflow:
48-
// Stack underflow detected for thread (thread_id=object_id)
49-
// Note: "overflow" is printed instead of "underflow" due to end user familiarity with overflow errors
50-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_STACK_OVERFLOW), "CMSIS-RTOS error: Stack overflow", code);
51-
break;
52-
case osRtxErrorISRQueueOverflow:
53-
// ISR Queue overflow detected when inserting object (object_id)
54-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_ISR_QUEUE_OVERFLOW), "CMSIS-RTOS error: ISR Queue overflow", code);
55-
break;
56-
case osRtxErrorTimerQueueOverflow:
57-
// User Timer Callback Queue overflow detected for timer (timer_id=object_id)
58-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_TIMER_QUEUE_OVERFLOW), "CMSIS-RTOS error: User Timer Callback Queue overflow", code);
59-
break;
60-
case osRtxErrorClibSpace:
61-
// Standard C/C++ library libspace not available: increase OS_THREAD_LIBSPACE_NUM
62-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_CLIB_SPACE_UNAVAILABLE), "CMSIS-RTOS error: STD C/C++ library libspace not available", code);
63-
break;
64-
case osRtxErrorClibMutex:
65-
// Standard C/C++ library mutex initialization failed
66-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_CLIB_MUTEX_INIT_FAILURE), "CMSIS-RTOS error: STD C/C++ library mutex initialization failed", code);
67-
break;
68-
default:
69-
//Unknown error flagged from kernel
70-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_UNKNOWN), "CMSIS-RTOS error: Unknown", code);
71-
break;
61+
case osRtxErrorStackUnderflow:
62+
// Stack underflow detected for thread (thread_id=object_id)
63+
// Note: "overflow" is printed instead of "underflow" due to end user familiarity with overflow errors
64+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_STACK_OVERFLOW), "CMSIS-RTOS error: Stack overflow", code);
65+
break;
66+
case osRtxErrorISRQueueOverflow:
67+
// ISR Queue overflow detected when inserting object (object_id)
68+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_ISR_QUEUE_OVERFLOW), "CMSIS-RTOS error: ISR Queue overflow", code);
69+
break;
70+
case osRtxErrorTimerQueueOverflow:
71+
// User Timer Callback Queue overflow detected for timer (timer_id=object_id)
72+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_TIMER_QUEUE_OVERFLOW), "CMSIS-RTOS error: User Timer Callback Queue overflow", code);
73+
break;
74+
case osRtxErrorClibSpace:
75+
// Standard C/C++ library libspace not available: increase OS_THREAD_LIBSPACE_NUM
76+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_CLIB_SPACE_UNAVAILABLE), "CMSIS-RTOS error: STD C/C++ library libspace not available", code);
77+
break;
78+
case osRtxErrorClibMutex:
79+
// Standard C/C++ library mutex initialization failed
80+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_CLIB_MUTEX_INIT_FAILURE), "CMSIS-RTOS error: STD C/C++ library mutex initialization failed", code);
81+
break;
82+
default:
83+
//Unknown error flagged from kernel
84+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_UNKNOWN), "CMSIS-RTOS error: Unknown", code);
85+
break;
7286
}
7387

7488
/* That shouldn't be reached */
@@ -77,52 +91,52 @@ __NO_RETURN uint32_t osRtxErrorNotify (uint32_t code, void *object_id)
7791

7892
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
7993

80-
static const char* error_msg(int32_t status)
94+
static const char *error_msg(int32_t status)
8195
{
8296
switch (status) {
83-
case osError:
84-
return "Unspecified RTOS error";
85-
case osErrorTimeout:
86-
return "Operation not completed within the timeout period";
87-
case osErrorResource:
88-
return "Resource not available";
89-
case osErrorParameter:
90-
return "Parameter error";
91-
case osErrorNoMemory:
92-
return "System is out of memory";
93-
case osErrorISR:
94-
return "Not allowed in ISR context";
95-
default:
96-
return "Unknown";
97+
case osError:
98+
return "Unspecified RTOS error";
99+
case osErrorTimeout:
100+
return "Operation not completed within the timeout period";
101+
case osErrorResource:
102+
return "Resource not available";
103+
case osErrorParameter:
104+
return "Parameter error";
105+
case osErrorNoMemory:
106+
return "System is out of memory";
107+
case osErrorISR:
108+
return "Not allowed in ISR context";
109+
default:
110+
return "Unknown";
97111
}
98112
}
99113

100-
void EvrRtxKernelError (int32_t status)
114+
void EvrRtxKernelError(int32_t status)
101115
{
102116
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_EVENT), error_msg(status), status);
103117
}
104118

105-
void EvrRtxThreadError (osThreadId_t thread_id, int32_t status)
119+
void EvrRtxThreadError(osThreadId_t thread_id, int32_t status)
106120
{
107121
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_THREAD_EVENT), error_msg(status), thread_id);
108122
}
109123

110-
void EvrRtxTimerError (osTimerId_t timer_id, int32_t status)
124+
void EvrRtxTimerError(osTimerId_t timer_id, int32_t status)
111125
{
112126
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_TIMER_EVENT), error_msg(status), timer_id);
113127
}
114128

115-
void EvrRtxEventFlagsError (osEventFlagsId_t ef_id, int32_t status)
129+
void EvrRtxEventFlagsError(osEventFlagsId_t ef_id, int32_t status)
116130
{
117131
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT), error_msg(status), ef_id);
118132
}
119133

120-
void EvrRtxMutexError (osMutexId_t mutex_id, int32_t status)
134+
void EvrRtxMutexError(osMutexId_t mutex_id, int32_t status)
121135
{
122136
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_MUTEX_EVENT), error_msg(status), mutex_id);
123137
}
124138

125-
void EvrRtxSemaphoreError (osSemaphoreId_t semaphore_id, int32_t status)
139+
void EvrRtxSemaphoreError(osSemaphoreId_t semaphore_id, int32_t status)
126140
{
127141
// Ignore semaphore overflow, the count will saturate with a returned error
128142
if (status == osRtxErrorSemaphoreCountLimit) {
@@ -132,20 +146,20 @@ void EvrRtxSemaphoreError (osSemaphoreId_t semaphore_id, int32_t status)
132146
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT), error_msg(status), semaphore_id);
133147
}
134148

135-
void EvrRtxMemoryPoolError (osMemoryPoolId_t mp_id, int32_t status)
149+
void EvrRtxMemoryPoolError(osMemoryPoolId_t mp_id, int32_t status)
136150
{
137151
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT), error_msg(status), mp_id);
138152
}
139153

140-
void EvrRtxMessageQueueError (osMessageQueueId_t mq_id, int32_t status)
154+
void EvrRtxMessageQueueError(osMessageQueueId_t mq_id, int32_t status)
141155
{
142156
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT), error_msg(status), mq_id);
143157
}
144158

145159
#endif
146160

147161
// RTX hook which gets called when a thread terminates, using the event function to call hook
148-
void EvrRtxThreadExit (void)
162+
void EvrRtxThreadExit(void)
149163
{
150164
osThreadId_t thread_id = osThreadGetId();
151165
thread_terminate_hook(thread_id);
@@ -154,7 +168,7 @@ void EvrRtxThreadExit (void)
154168
#endif
155169
}
156170

157-
void EvrRtxThreadTerminate (osThreadId_t thread_id)
171+
void EvrRtxThreadTerminate(osThreadId_t thread_id)
158172
{
159173
thread_terminate_hook(thread_id);
160174
#if (!defined(EVR_RTX_DISABLE) && (OS_EVR_THREAD != 0) && !defined(EVR_RTX_THREAD_TERMINATE_DISABLE) && defined(RTE_Compiler_EventRecorder))

0 commit comments

Comments
 (0)