2010-12-08 06:19:06 +00:00
|
|
|
/*
|
2012-12-13 15:39:07 +00:00
|
|
|
* virthreadpool.c: a generic thread pool implementation
|
2010-12-08 06:19:06 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Hu Tao
|
|
|
|
* Copyright (C) 2010 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-12-08 06:19:06 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hu Tao <hutao@cn.fujitsu.com>
|
|
|
|
* Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-12-13 15:39:07 +00:00
|
|
|
#include "virthreadpool.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
typedef struct _virThreadPoolJob virThreadPoolJob;
|
|
|
|
typedef virThreadPoolJob *virThreadPoolJobPtr;
|
|
|
|
|
|
|
|
struct _virThreadPoolJob {
|
2011-08-12 12:04:31 +00:00
|
|
|
virThreadPoolJobPtr prev;
|
2010-12-08 06:19:06 +00:00
|
|
|
virThreadPoolJobPtr next;
|
2011-08-12 12:04:31 +00:00
|
|
|
unsigned int priority;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _virThreadPoolJobList virThreadPoolJobList;
|
|
|
|
typedef virThreadPoolJobList *virThreadPoolJobListPtr;
|
|
|
|
|
|
|
|
struct _virThreadPoolJobList {
|
|
|
|
virThreadPoolJobPtr head;
|
2011-08-12 12:04:31 +00:00
|
|
|
virThreadPoolJobPtr tail;
|
|
|
|
virThreadPoolJobPtr firstPrio;
|
2010-12-08 06:19:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct _virThreadPool {
|
|
|
|
bool quit;
|
|
|
|
|
|
|
|
virThreadPoolJobFunc jobFunc;
|
|
|
|
void *jobOpaque;
|
|
|
|
virThreadPoolJobList jobList;
|
2011-08-12 12:04:31 +00:00
|
|
|
size_t jobQueueDepth;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
virMutex mutex;
|
|
|
|
virCond cond;
|
|
|
|
virCond quit_cond;
|
|
|
|
|
|
|
|
size_t maxWorkers;
|
2012-08-09 10:48:17 +00:00
|
|
|
size_t minWorkers;
|
2010-12-08 06:19:06 +00:00
|
|
|
size_t freeWorkers;
|
|
|
|
size_t nWorkers;
|
|
|
|
virThreadPtr workers;
|
2011-08-12 12:04:31 +00:00
|
|
|
|
|
|
|
size_t nPrioWorkers;
|
|
|
|
virThreadPtr prioWorkers;
|
|
|
|
virCond prioCond;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct virThreadPoolWorkerData {
|
|
|
|
virThreadPoolPtr pool;
|
|
|
|
virCondPtr cond;
|
|
|
|
bool priority;
|
2010-12-08 06:19:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void virThreadPoolWorker(void *opaque)
|
|
|
|
{
|
2011-08-12 12:04:31 +00:00
|
|
|
struct virThreadPoolWorkerData *data = opaque;
|
|
|
|
virThreadPoolPtr pool = data->pool;
|
|
|
|
virCondPtr cond = data->cond;
|
|
|
|
bool priority = data->priority;
|
|
|
|
virThreadPoolJobPtr job = NULL;
|
|
|
|
|
|
|
|
VIR_FREE(data);
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
virMutexLock(&pool->mutex);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
while (!pool->quit &&
|
2011-08-12 12:04:31 +00:00
|
|
|
((!priority && !pool->jobList.head) ||
|
|
|
|
(priority && !pool->jobList.firstPrio))) {
|
|
|
|
if (!priority)
|
|
|
|
pool->freeWorkers++;
|
|
|
|
if (virCondWait(cond, &pool->mutex) < 0) {
|
|
|
|
if (!priority)
|
|
|
|
pool->freeWorkers--;
|
2010-12-08 06:19:06 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2011-08-12 12:04:31 +00:00
|
|
|
if (!priority)
|
|
|
|
pool->freeWorkers--;
|
2010-12-08 06:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->quit)
|
|
|
|
break;
|
|
|
|
|
2011-08-12 12:04:31 +00:00
|
|
|
if (priority) {
|
|
|
|
job = pool->jobList.firstPrio;
|
|
|
|
} else {
|
|
|
|
job = pool->jobList.head;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job == pool->jobList.firstPrio) {
|
|
|
|
virThreadPoolJobPtr tmp = job->next;
|
|
|
|
while (tmp) {
|
|
|
|
if (tmp->priority) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
pool->jobList.firstPrio = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job->prev)
|
|
|
|
job->prev->next = job->next;
|
|
|
|
else
|
|
|
|
pool->jobList.head = job->next;
|
|
|
|
if (job->next)
|
|
|
|
job->next->prev = job->prev;
|
|
|
|
else
|
|
|
|
pool->jobList.tail = job->prev;
|
|
|
|
|
|
|
|
pool->jobQueueDepth--;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
virMutexUnlock(&pool->mutex);
|
|
|
|
(pool->jobFunc)(job->data, pool->jobOpaque);
|
|
|
|
VIR_FREE(job);
|
|
|
|
virMutexLock(&pool->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2011-08-12 12:04:31 +00:00
|
|
|
if (priority)
|
|
|
|
pool->nPrioWorkers--;
|
|
|
|
else
|
|
|
|
pool->nWorkers--;
|
|
|
|
if (pool->nWorkers == 0 && pool->nPrioWorkers==0)
|
2010-12-08 06:19:06 +00:00
|
|
|
virCondSignal(&pool->quit_cond);
|
|
|
|
virMutexUnlock(&pool->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
virThreadPoolPtr virThreadPoolNew(size_t minWorkers,
|
|
|
|
size_t maxWorkers,
|
2011-08-12 12:04:31 +00:00
|
|
|
size_t prioWorkers,
|
2010-12-08 06:19:06 +00:00
|
|
|
virThreadPoolJobFunc func,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virThreadPoolPtr pool;
|
|
|
|
size_t i;
|
2011-08-12 12:04:31 +00:00
|
|
|
struct virThreadPoolWorkerData *data = NULL;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
if (minWorkers > maxWorkers)
|
|
|
|
minWorkers = maxWorkers;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(pool) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-12 12:04:31 +00:00
|
|
|
pool->jobList.tail = pool->jobList.head = NULL;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
pool->jobFunc = func;
|
|
|
|
pool->jobOpaque = opaque;
|
|
|
|
|
|
|
|
if (virMutexInit(&pool->mutex) < 0)
|
|
|
|
goto error;
|
|
|
|
if (virCondInit(&pool->cond) < 0)
|
|
|
|
goto error;
|
|
|
|
if (virCondInit(&pool->quit_cond) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(pool->workers, minWorkers) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2012-08-09 10:48:17 +00:00
|
|
|
pool->minWorkers = minWorkers;
|
2010-12-08 06:19:06 +00:00
|
|
|
pool->maxWorkers = maxWorkers;
|
2012-08-09 10:48:17 +00:00
|
|
|
|
2010-12-08 06:19:06 +00:00
|
|
|
for (i = 0; i < minWorkers; i++) {
|
2011-08-12 12:04:31 +00:00
|
|
|
if (VIR_ALLOC(data) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
data->pool = pool;
|
|
|
|
data->cond = &pool->cond;
|
|
|
|
|
2010-12-08 06:19:06 +00:00
|
|
|
if (virThreadCreate(&pool->workers[i],
|
|
|
|
true,
|
|
|
|
virThreadPoolWorker,
|
2011-08-12 12:04:31 +00:00
|
|
|
data) < 0) {
|
2010-12-08 06:19:06 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
pool->nWorkers++;
|
|
|
|
}
|
|
|
|
|
2011-08-12 12:04:31 +00:00
|
|
|
if (prioWorkers) {
|
|
|
|
if (virCondInit(&pool->prioCond) < 0)
|
|
|
|
goto error;
|
|
|
|
if (VIR_ALLOC_N(pool->prioWorkers, prioWorkers) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (i = 0; i < prioWorkers; i++) {
|
|
|
|
if (VIR_ALLOC(data) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
data->pool = pool;
|
|
|
|
data->cond = &pool->prioCond;
|
|
|
|
data->priority = true;
|
|
|
|
|
|
|
|
if (virThreadCreate(&pool->prioWorkers[i],
|
|
|
|
true,
|
|
|
|
virThreadPoolWorker,
|
|
|
|
data) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
pool->nPrioWorkers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 06:19:06 +00:00
|
|
|
return pool;
|
|
|
|
|
|
|
|
error:
|
2011-08-12 12:04:31 +00:00
|
|
|
VIR_FREE(data);
|
2010-12-08 06:19:06 +00:00
|
|
|
virThreadPoolFree(pool);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void virThreadPoolFree(virThreadPoolPtr pool)
|
|
|
|
{
|
|
|
|
virThreadPoolJobPtr job;
|
2011-08-12 12:04:31 +00:00
|
|
|
bool priority = false;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
if (!pool)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virMutexLock(&pool->mutex);
|
|
|
|
pool->quit = true;
|
2011-08-12 12:04:31 +00:00
|
|
|
if (pool->nWorkers > 0)
|
2010-12-08 06:19:06 +00:00
|
|
|
virCondBroadcast(&pool->cond);
|
2011-08-12 12:04:31 +00:00
|
|
|
if (pool->nPrioWorkers > 0) {
|
|
|
|
priority = true;
|
|
|
|
virCondBroadcast(&pool->prioCond);
|
2010-12-08 06:19:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 16:23:47 +00:00
|
|
|
while (pool->nWorkers > 0 || pool->nPrioWorkers > 0)
|
2011-12-09 14:12:37 +00:00
|
|
|
ignore_value(virCondWait(&pool->quit_cond, &pool->mutex));
|
2011-08-12 12:04:31 +00:00
|
|
|
|
2010-12-08 06:19:06 +00:00
|
|
|
while ((job = pool->jobList.head)) {
|
|
|
|
pool->jobList.head = pool->jobList.head->next;
|
|
|
|
VIR_FREE(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(pool->workers);
|
|
|
|
virMutexUnlock(&pool->mutex);
|
|
|
|
virMutexDestroy(&pool->mutex);
|
2013-02-07 14:03:17 +00:00
|
|
|
virCondDestroy(&pool->quit_cond);
|
|
|
|
virCondDestroy(&pool->cond);
|
2011-08-12 12:04:31 +00:00
|
|
|
if (priority) {
|
|
|
|
VIR_FREE(pool->prioWorkers);
|
2013-02-07 14:03:17 +00:00
|
|
|
virCondDestroy(&pool->prioCond);
|
2011-08-12 12:04:31 +00:00
|
|
|
}
|
2010-12-08 06:19:06 +00:00
|
|
|
VIR_FREE(pool);
|
|
|
|
}
|
|
|
|
|
2012-08-09 10:48:17 +00:00
|
|
|
|
|
|
|
size_t virThreadPoolGetMinWorkers(virThreadPoolPtr pool)
|
|
|
|
{
|
|
|
|
return pool->minWorkers;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t virThreadPoolGetMaxWorkers(virThreadPoolPtr pool)
|
|
|
|
{
|
|
|
|
return pool->maxWorkers;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t virThreadPoolGetPriorityWorkers(virThreadPoolPtr pool)
|
|
|
|
{
|
|
|
|
return pool->nPrioWorkers;
|
|
|
|
}
|
|
|
|
|
2011-08-12 12:04:31 +00:00
|
|
|
/*
|
|
|
|
* @priority - job priority
|
|
|
|
* Return: 0 on success, -1 otherwise
|
|
|
|
*/
|
2010-12-08 06:19:06 +00:00
|
|
|
int virThreadPoolSendJob(virThreadPoolPtr pool,
|
2011-08-12 12:04:31 +00:00
|
|
|
unsigned int priority,
|
2010-12-08 06:19:06 +00:00
|
|
|
void *jobData)
|
|
|
|
{
|
|
|
|
virThreadPoolJobPtr job;
|
2011-09-07 12:08:14 +00:00
|
|
|
struct virThreadPoolWorkerData *data = NULL;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
virMutexLock(&pool->mutex);
|
|
|
|
if (pool->quit)
|
|
|
|
goto error;
|
|
|
|
|
2011-08-12 12:04:31 +00:00
|
|
|
if (pool->freeWorkers - pool->jobQueueDepth <= 0 &&
|
2010-12-08 06:19:06 +00:00
|
|
|
pool->nWorkers < pool->maxWorkers) {
|
|
|
|
if (VIR_EXPAND_N(pool->workers, pool->nWorkers, 1) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-09-07 12:08:14 +00:00
|
|
|
if (VIR_ALLOC(data) < 0) {
|
|
|
|
pool->nWorkers--;
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->pool = pool;
|
|
|
|
data->cond = &pool->cond;
|
|
|
|
|
2010-12-08 06:19:06 +00:00
|
|
|
if (virThreadCreate(&pool->workers[pool->nWorkers - 1],
|
|
|
|
true,
|
|
|
|
virThreadPoolWorker,
|
2011-09-07 12:08:14 +00:00
|
|
|
data) < 0) {
|
|
|
|
VIR_FREE(data);
|
2010-12-08 06:19:06 +00:00
|
|
|
pool->nWorkers--;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_ALLOC(job) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
job->data = jobData;
|
2011-08-12 12:04:31 +00:00
|
|
|
job->priority = priority;
|
|
|
|
|
|
|
|
job->prev = pool->jobList.tail;
|
|
|
|
if (pool->jobList.tail)
|
|
|
|
pool->jobList.tail->next = job;
|
|
|
|
pool->jobList.tail = job;
|
|
|
|
|
|
|
|
if (!pool->jobList.head)
|
|
|
|
pool->jobList.head = job;
|
|
|
|
|
|
|
|
if (priority && !pool->jobList.firstPrio)
|
|
|
|
pool->jobList.firstPrio = job;
|
|
|
|
|
|
|
|
pool->jobQueueDepth++;
|
2010-12-08 06:19:06 +00:00
|
|
|
|
|
|
|
virCondSignal(&pool->cond);
|
2011-08-12 12:04:31 +00:00
|
|
|
if (priority)
|
|
|
|
virCondSignal(&pool->prioCond);
|
2010-12-08 06:19:06 +00:00
|
|
|
|
2011-08-12 12:04:31 +00:00
|
|
|
virMutexUnlock(&pool->mutex);
|
2010-12-08 06:19:06 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virMutexUnlock(&pool->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|