From 96f62f340e08cf4923407b884c57dfe0f153bb48 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Sun, 3 May 2020 15:13:07 +0200 Subject: [PATCH] Simplify task.c to use handles as callbacks --- components/task/include/task/task.h | 10 +++--- components/task/task.c | 47 +++-------------------------- 2 files changed, 9 insertions(+), 48 deletions(-) diff --git a/components/task/include/task/task.h b/components/task/include/task/task.h index b7763516ac..1bb95e4290 100644 --- a/components/task/include/task/task.h +++ b/components/task/include/task/task.h @@ -13,9 +13,9 @@ typedef enum { TASK_PRIORITY_COUNT } task_prio_t; -typedef uint32_t task_handle_t; typedef intptr_t task_param_t; - +typedef void (*task_callback_t)(task_param_t param, task_prio_t prio); +typedef task_callback_t task_handle_t; /* * Signals are a 32-bit number of the form header:14; count:18. The header * is just a fixed fingerprint and the count is allocated serially by the @@ -27,9 +27,9 @@ bool task_post(task_prio_t priority, task_handle_t handle, task_param_t param); #define task_post_medium(handle,param) task_post(TASK_PRIORITY_MEDIUM, handle, param) #define task_post_high(handle,param) task_post(TASK_PRIORITY_HIGH, handle, param) -typedef void (*task_callback_t)(task_param_t param, task_prio_t prio); - -task_handle_t task_get_id(task_callback_t t); +inline task_handle_t task_get_id(task_callback_t t) { + return (task_handle_t)t; +} /* Init, must be called before any posting happens */ void task_init (void); diff --git a/components/task/task.c b/components/task/task.c index 9fa69e6fae..62fb836243 100644 --- a/components/task/task.c +++ b/components/task/task.c @@ -9,20 +9,11 @@ #include "freertos/queue.h" #include "freertos/semphr.h" -#define TASK_HANDLE_MONIKER 0x68680000 -#define TASK_HANDLE_MASK 0xFFF80000 -#define TASK_HANDLE_UNMASK (~TASK_HANDLE_MASK) #define TASK_HANDLE_ALLOCATION_BRICK 4 // must be a power of 2 -#define CHECK(p,v,msg) if (!(p)) { NODE_DBG ( msg ); return (v); } - -#ifndef NODE_DBG -# define NODE_DBG(...) do{}while(0) -#endif - typedef struct { - task_handle_t sig; + task_handle_t handle; task_param_t par; } task_event_t; @@ -35,30 +26,9 @@ static xQueueHandle task_Q[TASK_PRIORITY_COUNT]; * we use a binary semaphore to unblock the pump whenever something is posted */ static xSemaphoreHandle pending; -static task_callback_t *task_func; -static int task_count; - - -task_handle_t task_get_id(task_callback_t t) { - if ( (task_count & (TASK_HANDLE_ALLOCATION_BRICK - 1)) == 0 ) { - /* With a brick size of 4 this branch is taken at 0, 4, 8 ... and the new size is +4 */ - task_func =(task_callback_t *)realloc( - task_func, - sizeof(task_callback_t)*(task_count+TASK_HANDLE_ALLOCATION_BRICK)); - - CHECK(task_func, 0 , "Malloc failure in task_get_id"); - memset (task_func+task_count, 0, sizeof(task_callback_t)*TASK_HANDLE_ALLOCATION_BRICK); - } - - task_func[task_count] = t; - return TASK_HANDLE_MONIKER | task_count++; -} - - bool IRAM_ATTR task_post (task_prio_t priority, task_handle_t handle, task_param_t param) { - if (priority >= TASK_PRIORITY_COUNT || - (handle & TASK_HANDLE_MASK) != TASK_HANDLE_MONIKER) + if (priority >= TASK_PRIORITY_COUNT) return false; task_event_t ev = { handle, param }; @@ -86,17 +56,8 @@ static bool next_event (task_event_t *ev, task_prio_t *prio) static void dispatch (task_event_t *e, uint8_t prio) { - task_handle_t handle = e->sig; - if ( (handle & TASK_HANDLE_MASK) == TASK_HANDLE_MONIKER) { - uint16_t entry = (handle & TASK_HANDLE_UNMASK); - if ( task_func && entry < task_count ){ - /* call the registered task handler with the specified parameter and priority */ - task_func[entry](e->par, prio); - return; - } - } - /* Invalid signals are ignored */ - NODE_DBG ( "Invalid signal issued: %08x", handle); + task_callback_t callback = (task_callback_t)e->handle; + callback(e->par, prio); }