From 658ce96ddb728ae9128a01928036b012aa5017ee Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Wed, 24 Feb 2016 15:45:33 +0000 Subject: Implemented delete handler. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@8943 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/STM32/NASA-OSAL-STM32F407-DISCOVERY/chconf.h | 4 +++- os/common/abstractions/nasa_osal/src/osapi.c | 28 +++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/demos/STM32/NASA-OSAL-STM32F407-DISCOVERY/chconf.h b/demos/STM32/NASA-OSAL-STM32F407-DISCOVERY/chconf.h index 2ddded71e..731b07adb 100644 --- a/demos/STM32/NASA-OSAL-STM32F407-DISCOVERY/chconf.h +++ b/demos/STM32/NASA-OSAL-STM32F407-DISCOVERY/chconf.h @@ -420,7 +420,8 @@ * @details User fields added to the end of the @p thread_t structure. */ #define CH_CFG_THREAD_EXTRA_FIELDS \ - /* Add threads custom fields here.*/ + /* Add threads custom fields here.*/ \ + void *osal_delete_handler; /** * @brief Threads initialization hook. @@ -431,6 +432,7 @@ */ #define CH_CFG_THREAD_INIT_HOOK(tp) { \ /* Add threads initialization code here.*/ \ + tp->osal_delete_handler = NULL; \ } /** diff --git a/os/common/abstractions/nasa_osal/src/osapi.c b/os/common/abstractions/nasa_osal/src/osapi.c index 9d02cd05e..775dffe26 100644 --- a/os/common/abstractions/nasa_osal/src/osapi.c +++ b/os/common/abstractions/nasa_osal/src/osapi.c @@ -80,6 +80,11 @@ /* Module local types. */ /*===========================================================================*/ +/** + * @brief Generic function pointer type. + */ +typedef void (*funcptr_t)(void); + /** * @brief Type of OSAL timer. */ @@ -1301,7 +1306,7 @@ int32 OS_TaskCreate(uint32 *task_id, /** * @brief Installs a deletion handler. - * @note It is not currently implemented. + * @note It is implemented as hooks in chconf.h. * * @param[in] function_pointer the handler function * @return An error code. @@ -1310,14 +1315,16 @@ int32 OS_TaskCreate(uint32 *task_id, */ int32 OS_TaskInstallDeleteHandler(void *function_pointer) { - (void)function_pointer; + chThdGetSelfX()->osal_delete_handler = function_pointer; - return OS_ERR_NOT_IMPLEMENTED; + return OS_SUCCESS; } /** * @brief Task delete. - * @note It is not currently implemented. + * @note Limitation, it does not actually kill the thread, it just sets a + * flag in the thread that has then to terminate volountarly. The + * flag can be checked using @p chThdShouldTerminateX(). * * @param[in] task_id the task id * @return An error code. @@ -1335,13 +1342,18 @@ int32 OS_TaskDelete(uint32 task_id) { /* Asking for thread termination.*/ chThdTerminate(tp); - /* Releasing the thread reference.*/ - chThdRelease(tp); - /* Waiting for termination.*/ chThdWait(tp); - return OS_ERR_NOT_IMPLEMENTED; + /* Calling the delete handler, if defined.*/ + if (tp->osal_delete_handler != NULL) { + ((funcptr_t)(tp->osal_delete_handler))(); + } + + /* Releasing the thread reference.*/ + chThdRelease(tp); + + return OS_SUCCESS; } /** -- cgit v1.2.3