(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.
*
* 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. */
void *arg; /* Data to be passed to func. */
struct tpool_work *next; /* Next work item in the queue. */
};
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_last; /* Last work item in 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;
if (func == NULL)
if (!func)
return NULL;
work = calloc(1, sizeof(*work));
work = (tpool_work_t*)calloc(1, sizeof(*work));
work->func = func;
work->arg = arg;
work->arg = arg;
work->next = NULL;
return work;
}
static void tpool_work_destroy(tpool_work_t *work)
{
if (work == NULL)
return;
free(work);
if (work)
free(work);
}
/* 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;
if (tp == NULL)
if (!tp)
return NULL;
work = tp->work_first;
if (work == NULL)
if (!work)
return NULL;
if (work->next == NULL)
if (!work->next)
{
tp->work_first = NULL;
tp->work_last = NULL;
} else {
tp->work_first = work->next;
}
else
tp->work_first = work->next;
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
* there is work to take. */
if (tp->work_first == NULL)
if (!tp->work_first)
scond_wait(tp->work_cond, tp->work_mutex);
/* 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
* because it's not waiting in the conditional. Pedantic but...
*/
if (work != NULL)
if (work)
{
work->func(work->arg);
tpool_work_destroy(work);
@ -159,15 +160,15 @@ tpool_t *tpool_create(size_t num)
if (num == 0)
num = 2;
tp = calloc(1, sizeof(*tp));
tp->thread_cnt = num;
tp = (tpool_t*)calloc(1, sizeof(*tp));
tp->thread_cnt = num;
tp->work_mutex = slock_new();
tp->work_cond = scond_new();
tp->work_mutex = slock_new();
tp->work_cond = scond_new();
tp->working_cond = scond_new();
tp->work_first = NULL;
tp->work_last = NULL;
tp->work_first = NULL;
tp->work_last = NULL;
/* Create the requested number of thread and detach them. */
for (i = 0; i < num; i++)
@ -184,18 +185,19 @@ void tpool_destroy(tpool_t *tp)
tpool_work_t *work;
tpool_work_t *work2;
if (tp == NULL)
if (!tp)
return;
/* Take all work out of the queue and destroy it. */
slock_lock(tp->work_mutex);
work = tp->work_first;
while (work != NULL)
while (work)
{
work2 = work->next;
tpool_work_destroy(work);
work = work2;
}
/* Tell the worker threads to stop. */
tp->stop = true;
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;
if (tp == NULL)
if (!tp)
return false;
work = tpool_work_create(func, arg);
if (work == NULL)
if (!work)
return false;
slock_lock(tp->work_mutex);
if (tp->work_first == NULL)
if (!tp->work_first)
{
tp->work_first = work;
tp->work_last = tp->work_first;
tp->work_first = work;
tp->work_last = tp->work_first;
}
else
{
@ -242,23 +244,19 @@ bool tpool_add_work(tpool_t *tp, thread_func_t func, void *arg)
void tpool_wait(tpool_t *tp)
{
if (tp == NULL)
if (!tp)
return;
slock_lock(tp->work_mutex);
while (true)
{
/* 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
* are stopping it will trigger when there aren't any threads running. */
* 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. */
if ((!tp->stop && tp->working_cnt != 0) || (tp->stop && tp->thread_cnt != 0))
{
scond_wait(tp->working_cond, tp->work_mutex);
}
else
{
break;
}
}
slock_unlock(tp->work_mutex);
}