2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-11-04 09:37:03 +00:00
|
|
|
* @file thread_pool.c
|
2006-07-10 14:24:04 +00:00
|
|
|
*
|
2005-11-25 13:42:58 +00:00
|
|
|
* @brief Implementation of thread_pool_t.
|
2006-07-10 14:24:04 +00:00
|
|
|
*
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-11-03 13:34:07 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* 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 the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
2005-11-10 09:17:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2005-11-11 13:31:52 +00:00
|
|
|
|
|
|
|
#include "thread_pool.h"
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
#include <daemon.h>
|
2007-04-10 06:01:03 +00:00
|
|
|
#include <processing/job_queue.h>
|
2005-11-03 17:26:30 +00:00
|
|
|
|
2005-12-06 16:36:42 +00:00
|
|
|
|
2005-11-24 11:30:19 +00:00
|
|
|
typedef struct private_thread_pool_t private_thread_pool_t;
|
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* @brief Private data of thread_pool_t class.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2005-11-24 11:30:19 +00:00
|
|
|
struct private_thread_pool_t {
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Public thread_pool_t interface.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
|
|
|
thread_pool_t public;
|
2005-12-06 12:27:09 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Number of running threads.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
u_int pool_size;
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2006-10-18 11:46:13 +00:00
|
|
|
* Number of threads waiting for work
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
u_int idle_threads;
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-10 09:17:23 +00:00
|
|
|
/**
|
2006-10-18 11:46:13 +00:00
|
|
|
* Array of thread ids.
|
2005-11-10 09:17:23 +00:00
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
pthread_t *threads;
|
2005-11-10 15:06:52 +00:00
|
|
|
} ;
|
2005-11-03 13:34:07 +00:00
|
|
|
|
2005-11-10 15:06:52 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of private_thread_pool_t.process_jobs.
|
2005-11-10 15:06:52 +00:00
|
|
|
*/
|
2005-11-25 13:42:58 +00:00
|
|
|
static void process_jobs(private_thread_pool_t *this)
|
2005-11-03 13:34:07 +00:00
|
|
|
{
|
2005-12-05 12:21:38 +00:00
|
|
|
job_t *job;
|
2006-05-31 14:23:15 +00:00
|
|
|
status_t status;
|
2005-12-05 12:21:38 +00:00
|
|
|
|
2005-11-04 13:20:11 +00:00
|
|
|
/* cancellation disabled by default */
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
2005-11-26 15:44:40 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_JOB, "worker thread running, thread_ID: %06u",
|
2006-10-18 11:46:13 +00:00
|
|
|
(int)pthread_self());
|
2006-05-31 14:23:15 +00:00
|
|
|
|
|
|
|
while (TRUE)
|
2006-05-23 08:01:49 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
/* TODO: should be atomic, but is not mission critical */
|
2006-10-18 11:46:13 +00:00
|
|
|
this->idle_threads++;
|
2005-11-29 10:25:07 +00:00
|
|
|
job = charon->job_queue->get(charon->job_queue);
|
2006-10-18 11:46:13 +00:00
|
|
|
this->idle_threads--;
|
2005-12-05 12:21:38 +00:00
|
|
|
|
2006-05-31 14:23:15 +00:00
|
|
|
status = job->execute(job);
|
2006-05-23 08:01:49 +00:00
|
|
|
|
2006-05-31 14:23:15 +00:00
|
|
|
if (status == DESTROY_ME)
|
2005-12-06 12:27:09 +00:00
|
|
|
{
|
2006-05-31 14:23:15 +00:00
|
|
|
job->destroy(job);
|
2005-12-06 12:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of thread_pool_t.get_pool_size.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
static u_int get_pool_size(private_thread_pool_t *this)
|
2005-11-03 13:34:07 +00:00
|
|
|
{
|
2005-11-10 15:06:52 +00:00
|
|
|
return this->pool_size;
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/**
|
|
|
|
* Implementation of thread_pool_t.get_idle_threads.
|
|
|
|
*/
|
|
|
|
static u_int get_idle_threads(private_thread_pool_t *this)
|
|
|
|
{
|
|
|
|
return this->idle_threads;
|
|
|
|
}
|
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of thread_pool_t.destroy.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
static void destroy(private_thread_pool_t *this)
|
2005-11-03 13:34:07 +00:00
|
|
|
{
|
|
|
|
int current;
|
|
|
|
/* flag thread for termination */
|
2006-10-18 11:46:13 +00:00
|
|
|
for (current = 0; current < this->pool_size; current++)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_JOB, "cancelling worker thread #%d", current+1);
|
2005-11-03 13:34:07 +00:00
|
|
|
pthread_cancel(this->threads[current]);
|
2005-11-04 09:37:03 +00:00
|
|
|
}
|
2005-11-03 13:34:07 +00:00
|
|
|
|
|
|
|
/* wait for all threads */
|
|
|
|
for (current = 0; current < this->pool_size; current++) {
|
2006-04-10 08:07:38 +00:00
|
|
|
if (pthread_join(this->threads[current], NULL) == 0)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_JOB, "worker thread #%d terminated", current+1);
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_JOB, "could not terminate worker thread #%d", current+1);
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-31 14:23:15 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/* free mem */
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->threads);
|
|
|
|
free(this);
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
2005-11-10 15:06:52 +00:00
|
|
|
/*
|
2005-12-02 13:20:20 +00:00
|
|
|
* Described in header.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
|
|
|
thread_pool_t *thread_pool_create(size_t pool_size)
|
|
|
|
{
|
|
|
|
int current;
|
2006-04-10 08:07:38 +00:00
|
|
|
private_thread_pool_t *this = malloc_thing(private_thread_pool_t);
|
2005-11-03 13:34:07 +00:00
|
|
|
|
|
|
|
/* fill in public fields */
|
2005-11-28 20:29:47 +00:00
|
|
|
this->public.destroy = (void(*)(thread_pool_t*))destroy;
|
2006-10-18 11:46:13 +00:00
|
|
|
this->public.get_pool_size = (u_int(*)(thread_pool_t*))get_pool_size;
|
|
|
|
this->public.get_idle_threads = (u_int(*)(thread_pool_t*))get_idle_threads;
|
2005-11-03 13:34:07 +00:00
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/* initialize member */
|
2005-11-03 13:34:07 +00:00
|
|
|
this->pool_size = pool_size;
|
2006-10-18 11:46:13 +00:00
|
|
|
this->idle_threads = 0;
|
2006-04-10 08:07:38 +00:00
|
|
|
this->threads = malloc(sizeof(pthread_t) * pool_size);
|
2005-11-04 13:20:11 +00:00
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/* try to create as many threads as possible, up to pool_size */
|
2006-10-18 11:46:13 +00:00
|
|
|
for (current = 0; current < pool_size; current++)
|
2005-11-10 15:06:52 +00:00
|
|
|
{
|
2006-10-18 11:46:13 +00:00
|
|
|
if (pthread_create(&(this->threads[current]), NULL,
|
2006-07-10 14:24:04 +00:00
|
|
|
(void*(*)(void*))process_jobs, this) == 0)
|
2005-11-10 15:06:52 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_JOB, "created worker thread #%d", current+1);
|
2005-11-10 15:06:52 +00:00
|
|
|
}
|
2005-11-26 15:44:40 +00:00
|
|
|
else
|
2005-11-10 09:17:23 +00:00
|
|
|
{
|
2005-11-10 15:06:52 +00:00
|
|
|
/* creation failed, is it the first one? */
|
2006-10-18 11:46:13 +00:00
|
|
|
if (current == 0)
|
2005-11-10 09:17:23 +00:00
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->threads);
|
|
|
|
free(this);
|
2006-10-18 11:46:13 +00:00
|
|
|
charon->kill(charon, "could not create any worker threads");
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
/* not all threads could be created, but at least one :-/ */
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_JOB, "could only create %d from requested %d threads!",
|
2006-10-18 11:46:13 +00:00
|
|
|
current, pool_size);
|
2005-11-03 13:34:07 +00:00
|
|
|
this->pool_size = current;
|
2006-10-18 11:46:13 +00:00
|
|
|
break;
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
2006-07-10 14:24:04 +00:00
|
|
|
}
|
2005-11-03 13:34:07 +00:00
|
|
|
return (thread_pool_t*)this;
|
|
|
|
}
|