(tpool.c) Code style nits

This commit is contained in:
twinaphex 2019-12-18 18:11:18 +01:00
parent 9a7a8efb39
commit 3e8e6d35d6

View File

@ -31,14 +31,16 @@
* waiting for the pool to process it. * waiting for the pool to process it.
* *
* It is a singly linked list acting as a FIFO queue. */ * It is a singly linked list acting as a FIFO queue. */
struct tpool_work { struct tpool_work
{
thread_func_t func; /* Function to be called. */ thread_func_t func; /* Function to be called. */
void *arg; /* Data to be passed to func. */ void *arg; /* Data to be passed to func. */
struct tpool_work *next; /* Next work item in the queue. */ struct tpool_work *next; /* Next work item in the queue. */
}; };
typedef struct tpool_work tpool_work_t; typedef struct tpool_work tpool_work_t;
struct tpool { struct tpool
{
tpool_work_t *work_first; /* First work item in the work queue. */ tpool_work_t *work_first; /* First work item in the work queue. */
tpool_work_t *work_last; /* Last work item in the work queue. */ tpool_work_t *work_last; /* Last work item in the work queue. */
slock_t *work_mutex; /* Mutex protecting inserting and removing work from the work queue. */ slock_t *work_mutex; /* Mutex protecting inserting and removing work from the work queue. */
@ -54,21 +56,20 @@ static tpool_work_t *tpool_work_create(thread_func_t func, void *arg)
{ {
tpool_work_t *work; tpool_work_t *work;
if (func == NULL) if (!func)
return NULL; return NULL;
work = calloc(1, sizeof(*work)); work = (tpool_work_t*)calloc(1, sizeof(*work));
work->func = func; work->func = func;
work->arg = arg; work->arg = arg;
work->next = NULL; work->next = NULL;
return work; return work;
} }
static void tpool_work_destroy(tpool_work_t *work) static void tpool_work_destroy(tpool_work_t *work)
{ {
if (work == NULL) if (work)
return; free(work);
free(work);
} }
/* Pull the first work item out of the queue. */ /* Pull the first work item out of the queue. */
@ -76,20 +77,20 @@ static tpool_work_t *tpool_work_get(tpool_t *tp)
{ {
tpool_work_t *work; tpool_work_t *work;
if (tp == NULL) if (!tp)
return NULL; return NULL;
work = tp->work_first; work = tp->work_first;
if (work == NULL) if (!work)
return NULL; return NULL;
if (work->next == NULL) if (!work->next)
{ {
tp->work_first = NULL; tp->work_first = NULL;
tp->work_last = NULL; tp->work_last = NULL;
} else {
tp->work_first = work->next;
} }
else
tp->work_first = work->next;
return work; return work;
} }
@ -108,7 +109,7 @@ static void tpool_worker(void *arg)
/* If there is no work in the queue wait in the conditional until /* If there is no work in the queue wait in the conditional until
* there is work to take. */ * there is work to take. */
if (tp->work_first == NULL) if (!tp->work_first)
scond_wait(tp->work_cond, tp->work_mutex); scond_wait(tp->work_cond, tp->work_mutex);
/* Try to pull work from the queue. */ /* Try to pull work from the queue. */
@ -127,7 +128,7 @@ static void tpool_worker(void *arg)
* not true there is work processing the thread is considered working * not true there is work processing the thread is considered working
* because it's not waiting in the conditional. Pedantic but... * because it's not waiting in the conditional. Pedantic but...
*/ */
if (work != NULL) if (work)
{ {
work->func(work->arg); work->func(work->arg);
tpool_work_destroy(work); tpool_work_destroy(work);
@ -159,15 +160,15 @@ tpool_t *tpool_create(size_t num)
if (num == 0) if (num == 0)
num = 2; num = 2;
tp = calloc(1, sizeof(*tp)); tp = (tpool_t*)calloc(1, sizeof(*tp));
tp->thread_cnt = num; tp->thread_cnt = num;
tp->work_mutex = slock_new(); tp->work_mutex = slock_new();
tp->work_cond = scond_new(); tp->work_cond = scond_new();
tp->working_cond = scond_new(); tp->working_cond = scond_new();
tp->work_first = NULL; tp->work_first = NULL;
tp->work_last = NULL; tp->work_last = NULL;
/* Create the requested number of thread and detach them. */ /* Create the requested number of thread and detach them. */
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
@ -184,18 +185,19 @@ void tpool_destroy(tpool_t *tp)
tpool_work_t *work; tpool_work_t *work;
tpool_work_t *work2; tpool_work_t *work2;
if (tp == NULL) if (!tp)
return; return;
/* Take all work out of the queue and destroy it. */ /* Take all work out of the queue and destroy it. */
slock_lock(tp->work_mutex); slock_lock(tp->work_mutex);
work = tp->work_first; work = tp->work_first;
while (work != NULL) while (work)
{ {
work2 = work->next; work2 = work->next;
tpool_work_destroy(work); tpool_work_destroy(work);
work = work2; work = work2;
} }
/* Tell the worker threads to stop. */ /* Tell the worker threads to stop. */
tp->stop = true; tp->stop = true;
scond_broadcast(tp->work_cond); scond_broadcast(tp->work_cond);
@ -215,18 +217,18 @@ bool tpool_add_work(tpool_t *tp, thread_func_t func, void *arg)
{ {
tpool_work_t *work; tpool_work_t *work;
if (tp == NULL) if (!tp)
return false; return false;
work = tpool_work_create(func, arg); work = tpool_work_create(func, arg);
if (work == NULL) if (!work)
return false; return false;
slock_lock(tp->work_mutex); slock_lock(tp->work_mutex);
if (tp->work_first == NULL) if (!tp->work_first)
{ {
tp->work_first = work; tp->work_first = work;
tp->work_last = tp->work_first; tp->work_last = tp->work_first;
} }
else else
{ {
@ -242,23 +244,19 @@ bool tpool_add_work(tpool_t *tp, thread_func_t func, void *arg)
void tpool_wait(tpool_t *tp) void tpool_wait(tpool_t *tp)
{ {
if (tp == NULL) if (!tp)
return; return;
slock_lock(tp->work_mutex); slock_lock(tp->work_mutex);
while (true) while (true)
{ {
/* working_cond is dual use. It signals when we're not stopping but the /* working_cond is dual use. It signals when we're not stopping but the
* working_cnt is 0 indicating there isn't any work processing. If we * working_cnt is 0 indicating there isn't any work processing. If we
* are stopping it will trigger when there aren't any threads running. */ * are stopping it will trigger when there aren't any threads running. */
if ((!tp->stop && tp->working_cnt != 0) || (tp->stop && tp->thread_cnt != 0)) if ((!tp->stop && tp->working_cnt != 0) || (tp->stop && tp->thread_cnt != 0))
{
scond_wait(tp->working_cond, tp->work_mutex); scond_wait(tp->working_cond, tp->work_mutex);
}
else else
{
break; break;
}
} }
slock_unlock(tp->work_mutex); slock_unlock(tp->work_mutex);
} }