From 1f08bcf0f894bf8d54774e97485332531e102083 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 29 Jan 2018 22:08:21 +0100 Subject: [PATCH] Add clear callback for workers. --- README.md | 4 +++- m-worker.h | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 637a6cc8..509ff372 100644 --- a/README.md +++ b/README.md @@ -3806,7 +3806,7 @@ A pool of worker. A synchronization point between workers. -#### void worker\_init(worker\_t worker, unsigned int numWorker, unsigned int extraQueue, void (*resetFunc)(void)) +#### void worker\_init(worker\_t worker[, unsigned int numWorker, unsigned int extraQueue, void (*resetFunc)(void), void (*clearFunc)(void) ]) Initialize the pool of workers 'worker' with 'numWorker' workers. if 'numWorker' is 0, then it will detect how many core is available on the @@ -3815,6 +3815,8 @@ Between each work order and before the first one, the function 'resetFunc' is called by the worker to reset its state (or NULL if no function to call). 'extraQueue' is the number of tasks which can be accepted by the work order queue in case if there is no worker available. +Before terminating, each worker will call clearFunc if the function is not NULL. + #### void worker\_clear(worker\_t worker) diff --git a/m-worker.h b/m-worker.h index 0f5b1eee..41352a0a 100644 --- a/m-worker.h +++ b/m-worker.h @@ -128,6 +128,9 @@ typedef struct worker_s { /* The global reset function */ void (*resetFunc_g)(void); + + /* The global clear function */ + void (*clearFunc_g)(void); } worker_t[1]; /* Return the number of CPU of the system */ @@ -187,10 +190,12 @@ workeri_thread(void *arg) g->resetFunc_g(); //printf ("Waiting for data (queue: %lu / %lu)\n", worker_queue_size(g->queue_g), worker_queue_capacity(g->queue_g)); worker_queue_pop(&w, g->queue_g); - if (w.block == NULL) return; + if (w.block == NULL) break; workeri_exec(&w); worker_queue_pop_release(g->queue_g); } + if (g->clearFunc_g != NULL) + g->clearFunc_g(); } /* Initialization of the worker module @@ -198,9 +203,10 @@ workeri_thread(void *arg) @numWorker: number of worker to create @extraQueue: number of extra work order we can get if all workers are full @resetFunc: function to reset the state of a worker between work orders (or NULL if none) + @clearFunc: function to clear the state of a worker before terminaning (or NULL if none) */ static inline void -worker_init(worker_t g, unsigned int numWorker, unsigned int extraQueue, void (*resetFunc)(void)) +worker_init(worker_t g, unsigned int numWorker, unsigned int extraQueue, void (*resetFunc)(void), void (*clearFunc)(void)) { if (numWorker == 0) numWorker = workeri_get_cpu_count()-1; @@ -211,12 +217,13 @@ worker_init(worker_t g, unsigned int numWorker, unsigned int extraQueue, void (* M_ASSERT_INIT (g->worker != NULL); g->numWorker_g = numWorker; g->resetFunc_g = resetFunc; + g->clearFunc_g = clearFunc; for(size_t i = 0; i < numWorker; i++) { m_thread_create(g->worker[i].id, workeri_thread, M_ASSIGN_CAST(void*, g)); } } // Define function with default values. -#define worker_init(...) worker_init(M_DEFAULT_ARGS(4, (0, 0, NULL), __VA_ARGS__)) +#define worker_init(...) worker_init(M_DEFAULT_ARGS(5, (0, 0, NULL, NULL), __VA_ARGS__)) /* Clear of the worker module */ static inline void