Skip to content
Snippets Groups Projects
Commit af330426 authored by Jan Hák's avatar Jan Hák Committed by Daniel Salzman
Browse files

workers: add worker_pool_wait_cb() and extend worker_pool_status()

parent f7e7b44e
No related branches found
No related tags found
1 merge request!1301Systemd integration improvements
/* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -191,7 +191,7 @@ void worker_pool_join(worker_pool_t *pool)
dt_join(pool->threads);
}
void worker_pool_wait(worker_pool_t *pool)
void worker_pool_wait_cb(worker_pool_t *pool, wait_callback_t cb)
{
if (!pool) {
return;
......@@ -199,11 +199,19 @@ void worker_pool_wait(worker_pool_t *pool)
pthread_mutex_lock(&pool->lock);
while (!EMPTY_LIST(pool->tasks.list) || pool->running > 0) {
if (cb != NULL) {
cb(pool);
}
pthread_cond_wait(&pool->wake, &pool->lock);
}
pthread_mutex_unlock(&pool->lock);
}
void worker_pool_wait(worker_pool_t *pool)
{
worker_pool_wait_cb(pool, NULL);
}
void worker_pool_assign(worker_pool_t *pool, struct task *task)
{
if (!pool || !task) {
......@@ -228,15 +236,19 @@ void worker_pool_clear(worker_pool_t *pool)
pthread_mutex_unlock(&pool->lock);
}
void worker_pool_status(worker_pool_t *pool, int *running, int *queued)
void worker_pool_status(worker_pool_t *pool, bool locked, int *running, int *queued)
{
if (!pool) {
*running = *queued = 0;
return;
}
pthread_mutex_lock(&pool->lock);
if (!locked) {
pthread_mutex_lock(&pool->lock);
}
*running = pool->running;
*queued = worker_queue_length(&pool->tasks);
pthread_mutex_unlock(&pool->lock);
if (!locked) {
pthread_mutex_unlock(&pool->lock);
}
}
/* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -16,11 +16,15 @@
#pragma once
#include <stdbool.h>
#include "knot/worker/queue.h"
struct worker_pool;
typedef struct worker_pool worker_pool_t;
typedef void(*wait_callback_t)(worker_pool_t *);
/*!
* \brief Initialize worker pool.
*
......@@ -62,10 +66,15 @@ void worker_pool_join(worker_pool_t *pool);
/*!
* \brief Wait till the number of pending tasks is zero.
*
*/
void worker_pool_wait(worker_pool_t *pool);
/*!
* \brief Wait till the number of pending tasks is zero. Callback emitted on
* thread wakeup can be specified.
*/
void worker_pool_wait_cb(worker_pool_t *pool, wait_callback_t cb);
/*!
* \brief Assign a task to be performed by a worker in the pool.
*/
......@@ -78,5 +87,7 @@ void worker_pool_clear(worker_pool_t *pool);
/*!
* \brief Obtain info regarding how the pool is busy.
*
* \note Locked means if the mutex `pool->lock` is locked.
*/
void worker_pool_status(worker_pool_t *pool, int *running, int *queued);
void worker_pool_status(worker_pool_t *pool, bool locked, int *running, int *queued);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment