Skip to content

Commit 6fd9154

Browse files
committed
Add task terminate hook
Add an RTX hook which gets called when a thread terminates. Add the function Thread::attach_terminate_hook() to allow users to attach a hook to this event at runtime.
1 parent c319296 commit 6fd9154

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

rtos/rtos/Thread.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030

3131
extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
3232

33+
34+
static void (*terminate_hook)(osThreadId id) = 0;
35+
extern "C" void thread_terminate_hook(osThreadId id)
36+
{
37+
if (terminate_hook != (void (*)(osThreadId))NULL) {
38+
terminate_hook(id);
39+
}
40+
}
41+
3342
namespace rtos {
3443

3544
void Thread::constructor(osPriority priority,
@@ -336,6 +345,10 @@ void Thread::attach_idle_hook(void (*fptr)(void)) {
336345
rtos_attach_idle_hook(fptr);
337346
}
338347

348+
void Thread::attach_terminate_hook(void (*fptr)(osThreadId id)) {
349+
terminate_hook = fptr;
350+
}
351+
339352
Thread::~Thread() {
340353
// terminate is thread safe
341354
terminate();

rtos/rtos/Thread.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ class Thread {
320320
*/
321321
static void attach_idle_hook(void (*fptr)(void));
322322

323+
/** Attach a function to be called when a task is killed
324+
@param fptr pointer to the function to be called
325+
*/
326+
static void attach_terminate_hook(void (*fptr)(osThreadId id));
327+
323328
virtual ~Thread();
324329

325330
private:

rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ void os_error (uint32_t err_code) {
320320
for (;;);
321321
}
322322

323+
/*----------------------------------------------------------------------------
324+
* RTX Hooks
325+
*---------------------------------------------------------------------------*/
326+
extern void thread_terminate_hook(osThreadId id);
327+
328+
void sysThreadTerminate(osThreadId id) {
329+
thread_terminate_hook(id);
330+
}
323331

324332
/*----------------------------------------------------------------------------
325333
* RTX Configuration Functions

rtos/rtx/TARGET_CORTEX_A/rt_CMSIS.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,10 @@ extern osMessageQId osMessageQId_osTimerMessageQ;
479479

480480
extern U32 IRQNestLevel; /* Indicates whether inside an ISR, and the depth of nesting. 0 = not in ISR. */
481481

482-
// Thread creation and destruction mutex
482+
// Thread creation and destruction
483483
osMutexDef(osThreadMutex);
484484
osMutexId osMutexId_osThreadMutex;
485+
void sysThreadTerminate(osThreadId id);
485486

486487
// ==== Helper Functions ====
487488

@@ -956,6 +957,7 @@ osStatus osThreadTerminate (osThreadId thread_id) {
956957
osStatus status;
957958
if (__exceptional_mode()) return osErrorISR; // Not allowed in ISR
958959
osMutexWait(osMutexId_osThreadMutex, osWaitForever);
960+
sysThreadTerminate(thread_id);
959961
// Thread mutex must be held when a thread is created or terminated
960962
status = __svcThreadTerminate(thread_id);
961963
osMutexRelease(osMutexId_osThreadMutex);
@@ -983,11 +985,14 @@ osPriority osThreadGetPriority (osThreadId thread_id) {
983985
/// INTERNAL - Not Public
984986
/// Auto Terminate Thread on exit (used implicitly when thread exists)
985987
__NO_RETURN void osThreadExit (void) {
988+
osThreadId id;
986989
// Thread mutex must be held when a thread is created or terminated
987990
// Note - the mutex will be released automatically by the os when
988991
// the thread is terminated
989992
osMutexWait(osMutexId_osThreadMutex, osWaitForever);
990-
__svcThreadTerminate(__svcThreadGetId());
993+
id = __svcThreadGetId();
994+
sysThreadTerminate(id);
995+
__svcThreadTerminate(id);
991996
for (;;); // Should never come here
992997
}
993998

rtos/rtx/TARGET_CORTEX_M/RTX_Conf_CM.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ void sysThreadError(osStatus status) {
407407
}
408408
}
409409

410+
/*----------------------------------------------------------------------------
411+
* RTX Hooks
412+
*---------------------------------------------------------------------------*/
413+
extern void thread_terminate_hook(osThreadId id);
414+
415+
void sysThreadTerminate(osThreadId id) {
416+
thread_terminate_hook(id);
417+
}
418+
410419
/*----------------------------------------------------------------------------
411420
* RTX Configuration Functions
412421
*---------------------------------------------------------------------------*/

rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,10 @@ extern osThreadId osThreadId_osTimerThread;
392392
extern const osMessageQDef_t os_messageQ_def_osTimerMessageQ;
393393
extern osMessageQId osMessageQId_osTimerMessageQ;
394394

395-
// Thread creation and destruction mutex
395+
// Thread creation and destruction
396396
osMutexDef(osThreadMutex);
397397
osMutexId osMutexId_osThreadMutex;
398+
void sysThreadTerminate(osThreadId id);
398399

399400
// ==== Helper Functions ====
400401

@@ -897,6 +898,7 @@ osStatus osThreadTerminate (osThreadId thread_id) {
897898
return osErrorISR; // Not allowed in ISR
898899
}
899900
osMutexWait(osMutexId_osThreadMutex, osWaitForever);
901+
sysThreadTerminate(thread_id);
900902
// Thread mutex must be held when a thread is created or terminated
901903
status = __svcThreadTerminate(thread_id);
902904
osMutexRelease(osMutexId_osThreadMutex);
@@ -930,11 +932,14 @@ osPriority osThreadGetPriority (osThreadId thread_id) {
930932
/// INTERNAL - Not Public
931933
/// Auto Terminate Thread on exit (used implicitly when thread exists)
932934
__NO_RETURN void osThreadExit (void) {
935+
osThreadId id;
933936
// Thread mutex must be held when a thread is created or terminated
934937
// Note - the mutex will be released automatically by the os when
935938
// the thread is terminated
936939
osMutexWait(osMutexId_osThreadMutex, osWaitForever);
937-
__svcThreadTerminate(__svcThreadGetId());
940+
id = __svcThreadGetId();
941+
sysThreadTerminate(id);
942+
__svcThreadTerminate(id);
938943
for (;;); // Should never come here
939944
}
940945

0 commit comments

Comments
 (0)