Skip to content

Commit

Permalink
Simplify task.c to use handles as callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier committed May 3, 2020
1 parent 189fe96 commit 96f62f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 48 deletions.
10 changes: 5 additions & 5 deletions components/task/include/task/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
47 changes: 4 additions & 43 deletions components/task/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 };
Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit 96f62f3

Please sign in to comment.