strongswan/Source/charon/threads/thread_pool.c

616 lines
19 KiB
C
Raw Normal View History

2005-11-03 13:34:07 +00:00
/**
2005-11-04 09:37:03 +00:00
* @file thread_pool.c
2005-11-03 13:34:07 +00:00
*
* @brief Implementation of thread_pool_t.
2005-11-03 13:34:07 +00:00
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* 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>
#include <string.h>
#include <errno.h>
#include "thread_pool.h"
2005-11-29 10:25:07 +00:00
#include <daemon.h>
2005-11-23 09:24:35 +00:00
#include <queues/job_queue.h>
#include <queues/jobs/delete_half_open_ike_sa_job.h>
#include <queues/jobs/delete_established_ike_sa_job.h>
#include <queues/jobs/incoming_packet_job.h>
#include <queues/jobs/initiate_ike_sa_job.h>
#include <queues/jobs/retransmit_request_job.h>
#include <encoding/payloads/notify_payload.h>
2005-11-23 09:24:35 +00:00
#include <utils/allocator.h>
#include <utils/logger.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 16:36:42 +00:00
* @brief Main processing function for worker threads.
*
* Gets a job from the job queue and calls corresponding
* function for processing.
*
2005-12-06 16:36:42 +00:00
* @param this calling object
*/
void (*process_jobs) (private_thread_pool_t *this);
/**
* @brief Process a INCOMING_PACKET job.
*
2005-12-06 16:36:42 +00:00
* @param this calling object
* @param job incoming_packet_job_t object
*/
void (*process_incoming_packet_job) (private_thread_pool_t *this, incoming_packet_job_t *job);
/**
* @brief Process a INITIATE_IKE_SA job.
*
2005-12-06 16:36:42 +00:00
* @param this calling object
* @param job initiate_ike_sa_job_t object
*/
void (*process_initiate_ike_sa_job) (private_thread_pool_t *this, initiate_ike_sa_job_t *job);
/**
* @brief Process a DELETE_HALF_OPEN_IKE_SA job.
*
2005-12-06 16:36:42 +00:00
* @param this calling object
* @param job delete__half_open_ike_sa_job_t object
*/
void (*process_delete_half_open_ike_sa_job) (private_thread_pool_t *this, delete_half_open_ike_sa_job_t *job);
/**
* @brief Process a DELETE_ESTABLISHED_IKE_SA job.
*
2005-12-06 16:36:42 +00:00
* @param this calling object
* @param job delete_established_ike_sa_job_t object
*/
void (*process_delete_established_ike_sa_job) (private_thread_pool_t *this, delete_established_ike_sa_job_t *job);
/**
* @brief Process a RETRANSMIT_REQUEST job.
*
2005-12-06 16:36:42 +00:00
* @param this calling object
* @param job retransmit_request_job_t object
*/
void (*process_retransmit_request_job) (private_thread_pool_t *this, retransmit_request_job_t *job);
/**
* Creates a job of type DELETE_HALF_OPEN_IKE_SA.
*
* This job is used to delete IKE_SA's which are still in state INITIATOR_INIT,
* RESPONDER_INIT, IKE_AUTH_REQUESTED, IKE_INIT_REQUESTED or IKE_INIT_RESPONDED.
*
* @param ike_sa_id ID of IKE_SA to delete
* @param delay Delay in ms after a half open IKE_SA gets deleted!
*/
void (*create_delete_half_open_ike_sa_job) (private_thread_pool_t *this,ike_sa_id_t *ike_sa_id, u_int32_t delay);
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
*/
2005-11-28 20:29:47 +00:00
size_t pool_size;
2005-11-03 13:34:07 +00:00
/**
2005-12-06 16:36:42 +00:00
* Array of thread ids.
2005-11-03 13:34:07 +00:00
*/
pthread_t *threads;
2005-11-28 20:29:47 +00:00
/**
2005-12-06 16:36:42 +00:00
* Logger of the thread pool.
*/
2005-11-21 11:48:04 +00:00
logger_t *pool_logger;
2005-11-28 20:29:47 +00:00
2005-11-21 11:48:04 +00:00
/**
2005-12-06 16:36:42 +00:00
* Logger of the worker threads.
2005-11-21 11:48:04 +00:00
*/
logger_t *worker_logger;
} ;
2005-11-03 13:34:07 +00:00
/**
* Implementation of private_thread_pool_t.process_jobs.
*/
static void process_jobs(private_thread_pool_t *this)
2005-11-03 13:34:07 +00:00
{
job_t *job;
job_type_t job_type;
timeval_t start_time;
timeval_t end_time;
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
2005-12-06 16:36:42 +00:00
this->worker_logger->log(this->worker_logger, CONTROL, "Worker thread running, thread_id: %u", (int)pthread_self());
2005-11-03 13:34:07 +00:00
for (;;) {
2005-11-16 16:50:13 +00:00
2005-11-29 10:25:07 +00:00
job = charon->job_queue->get(charon->job_queue);
2005-11-16 16:50:13 +00:00
job_type = job->get_type(job);
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL2, "Process job of type %s",
mapping_find(job_type_m,job_type));
gettimeofday(&start_time,NULL);
2005-11-16 16:50:13 +00:00
switch (job_type)
{
case INCOMING_PACKET:
{
this->process_incoming_packet_job(this, (incoming_packet_job_t*)job);
job->destroy(job);
break;
}
case INITIATE_IKE_SA:
{
this->process_initiate_ike_sa_job(this, (initiate_ike_sa_job_t*)job);
job->destroy(job);
break;
}
case DELETE_HALF_OPEN_IKE_SA:
{
this->process_delete_half_open_ike_sa_job(this, (delete_half_open_ike_sa_job_t*)job);
job->destroy(job);
break;
}
case DELETE_ESTABLISHED_IKE_SA:
{
this->process_delete_established_ike_sa_job(this, (delete_established_ike_sa_job_t*)job);
job->destroy(job);
break;
}
case RETRANSMIT_REQUEST:
{
this->process_retransmit_request_job(this, (retransmit_request_job_t*)job);
break;
}
default:
{
2005-12-06 16:36:42 +00:00
this->worker_logger->log(this->worker_logger, ERROR, "Job of type %s not supported!",
mapping_find(job_type_m,job_type));
job->destroy(job);
break;
}
}
gettimeofday(&end_time,NULL);
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL | LEVEL2, "Processed job of type %s in %d us",
mapping_find(job_type_m,job_type),
(((end_time.tv_sec - start_time.tv_sec) * 1000000) + (end_time.tv_usec - start_time.tv_usec)));
}
}
/**
* Implementation of private_thread_pool_t.process_incoming_packet_job.
*/
2005-11-28 20:29:47 +00:00
static void process_incoming_packet_job(private_thread_pool_t *this, incoming_packet_job_t *job)
{
packet_t *packet;
message_t *message;
ike_sa_t *ike_sa;
ike_sa_id_t *ike_sa_id;
status_t status;
2005-11-28 20:29:47 +00:00
2005-11-29 08:08:03 +00:00
packet = job->get_packet(job);
2005-11-21 16:41:24 +00:00
message = message_create_from_packet(packet);
2005-11-17 11:21:01 +00:00
status = message->parse_header(message);
if (status != SUCCESS)
{
2005-12-06 16:36:42 +00:00
this->worker_logger->log(this->worker_logger, ERROR, "Message header could not be verified!");
message->destroy(message);
return;
}
2005-11-17 08:48:01 +00:00
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL2, "Message is a %s %s",
mapping_find(exchange_type_m, message->get_exchange_type(message)),
message->get_request(message) ? "request" : "reply");
2005-11-21 11:48:04 +00:00
if ((message->get_major_version(message) != IKE_MAJOR_VERSION) ||
(message->get_minor_version(message) != IKE_MINOR_VERSION))
{
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, ERROR | LEVEL2, "IKE version %d.%d not supported",
message->get_major_version(message),
message->get_minor_version(message));
/*
* This check is not handled in state_t object of IKE_SA to increase speed.
*/
if ((message->get_exchange_type(message) == IKE_SA_INIT) && (message->get_request(message)))
{
message_t *response;
message->get_ike_sa_id(message, &ike_sa_id);
ike_sa_id->switch_initiator(ike_sa_id);
response = message_create_notify_reply(message->get_destination(message),
message->get_source(message),
IKE_SA_INIT,
FALSE,ike_sa_id,INVALID_MAJOR_VERSION);
message->destroy(message);
ike_sa_id->destroy(ike_sa_id);
status = response->generate(response, NULL, NULL, &packet);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Could not generate packet from message");
response->destroy(response);
return;
}
this->worker_logger->log(this->worker_logger, ERROR, "Send notify reply of type INVALID_MAJOR_VERSION");
charon->send_queue->add(charon->send_queue, packet);
response->destroy(response);
return;
}
message->destroy(message);
return;
}
2005-11-17 08:48:01 +00:00
2005-11-29 08:08:03 +00:00
message->get_ike_sa_id(message, &ike_sa_id);
2005-11-21 17:50:56 +00:00
ike_sa_id->switch_initiator(ike_sa_id);
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL3, "Checking out IKE SA %lld:%lld, role %s",
ike_sa_id->get_initiator_spi(ike_sa_id),
ike_sa_id->get_responder_spi(ike_sa_id),
ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
2005-11-21 11:48:04 +00:00
2005-11-29 10:25:07 +00:00
status = charon->ike_sa_manager->checkout(charon->ike_sa_manager,ike_sa_id, &ike_sa);
if ((status != SUCCESS) && (status != CREATED))
{
this->worker_logger->log(this->worker_logger, ERROR, "IKE SA could not be checked out");
ike_sa_id->destroy(ike_sa_id);
message->destroy(message);
/*
* TODO send notify reply of type INVALID_IKE_SPI if SPI could not be found ?
*/
return;
}
if (status == CREATED)
{
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL3, "Create Job to delete half open IKE_SA.");
this->create_delete_half_open_ike_sa_job(this,ike_sa_id,charon->configuration->get_half_open_ike_sa_timeout(charon->configuration));
}
status = ike_sa->process_message(ike_sa, message);
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL3, "%s IKE SA %lld:%lld, role %s",
(status == DELETE_ME) ? "Checkin and delete" : "Checkin",
ike_sa_id->get_initiator_spi(ike_sa_id),
ike_sa_id->get_responder_spi(ike_sa_id),
ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
ike_sa_id->destroy(ike_sa_id);
2005-12-02 07:43:05 +00:00
if (status == DELETE_ME)
{
status = charon->ike_sa_manager->checkin_and_delete(charon->ike_sa_manager, ike_sa);
}
else
{
status = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}
if (status != SUCCESS)
{
2005-12-06 16:36:42 +00:00
this->worker_logger->log(this->worker_logger, ERROR, "Checkin of IKE SA failed!");
}
message->destroy(message);
}
/**
* Implementation of private_thread_pool_t.process_initiate_ike_sa_job.
*/
2005-11-28 20:29:47 +00:00
static void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_job_t *job)
{
/*
* Initiatie an IKE_SA:
* - is defined by a name of a configuration
* - create an empty IKE_SA via manager
* - call initiate_connection on this sa
*/
ike_sa_t *ike_sa;
status_t status;
2005-11-28 20:29:47 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL2, "Creating and checking out IKE SA");
2005-11-29 10:25:07 +00:00
charon->ike_sa_manager->create_and_checkout(charon->ike_sa_manager, &ike_sa);
2005-11-28 20:29:47 +00:00
status = ike_sa->initiate_connection(ike_sa, job->get_connection(job));
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Initiation returned %s, going to delete IKE_SA.",
mapping_find(status_m, status));
2005-11-29 10:25:07 +00:00
charon->ike_sa_manager->checkin_and_delete(charon->ike_sa_manager, ike_sa);
return;
}
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL3, "Create Job to delete half open IKE_SA.");
this->create_delete_half_open_ike_sa_job(this,ike_sa->get_id(ike_sa),charon->configuration->get_half_open_ike_sa_timeout(charon->configuration));
2005-11-28 20:29:47 +00:00
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL2, "Checking in IKE SA");
2005-11-29 10:25:07 +00:00
status = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Could not checkin IKE_SA (%s)",
mapping_find(status_m, status));
2005-11-03 13:34:07 +00:00
}
}
/**
* Implementation of private_thread_pool_t.process_delete_ike_sa_job.
*/
static void process_delete_half_open_ike_sa_job(private_thread_pool_t *this, delete_half_open_ike_sa_job_t *job)
{
ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job);
ike_sa_t *ike_sa;
status_t status;
status = charon->ike_sa_manager->checkout(charon->ike_sa_manager,ike_sa_id, &ike_sa);
if ((status != SUCCESS) && (status != CREATED))
{
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL | LEVEL3, "IKE SA seems to be allready deleted and so doesn't have to be deleted");
return;
}
2005-11-28 20:29:47 +00:00
switch (ike_sa->get_state(ike_sa))
{
case INITIATOR_INIT:
case RESPONDER_INIT:
case IKE_SA_INIT_REQUESTED:
case IKE_SA_INIT_RESPONDED:
case IKE_AUTH_REQUESTED:
{
/* IKE_SA is half open and gets deleted! */
status = charon->ike_sa_manager->checkin_and_delete(charon->ike_sa_manager, ike_sa);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Could not checkin and delete checked out IKE_SA!");
}
break;
}
default:
{
/* IKE_SA is established and so is not getting deleted! */
status = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Could not checkin a checked out IKE_SA!");
}
break;
}
}
}
/**
* Implementation of private_thread_pool_t.process_delete_established_ike_sa_job.
*/
static void process_delete_established_ike_sa_job(private_thread_pool_t *this, delete_established_ike_sa_job_t *job)
{
ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job);
ike_sa_t *ike_sa;
status_t status;
status = charon->ike_sa_manager->checkout(charon->ike_sa_manager,ike_sa_id, &ike_sa);
if ((status != SUCCESS) && (status != CREATED))
{
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL | LEVEL3, "IKE SA seems to be allready deleted and so doesn't have to be deleted");
return;
}
switch (ike_sa->get_state(ike_sa))
{
case INITIATOR_INIT:
case RESPONDER_INIT:
case IKE_SA_INIT_REQUESTED:
case IKE_SA_INIT_RESPONDED:
case IKE_AUTH_REQUESTED:
{
break;
}
default:
{
2005-12-12 14:14:52 +00:00
this->worker_logger->log(this->worker_logger, CONTROL, "Send delete request for IKE_SA.");
ike_sa->send_delete_ike_sa_request(ike_sa);
break;
}
}
2005-12-06 16:48:17 +00:00
this->worker_logger->log(this->worker_logger, CONTROL, "Delete established IKE_SA.");
status = charon->ike_sa_manager->checkin_and_delete(charon->ike_sa_manager, ike_sa);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Could not checkin and delete checked out IKE_SA!");
}
2005-11-03 13:34:07 +00:00
}
/**
* Implementation of private_thread_pool_t.process_retransmit_request_job.
*/
static void process_retransmit_request_job(private_thread_pool_t *this, retransmit_request_job_t *job)
{
ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job);
u_int32_t message_id = job->get_message_id(job);
bool stop_retransmitting = FALSE;
u_int32_t timeout;
ike_sa_t *ike_sa;
status_t status;
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL2, "Checking out IKE SA %lld:%lld, role %s",
ike_sa_id->get_initiator_spi(ike_sa_id),
ike_sa_id->get_responder_spi(ike_sa_id),
ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
status = charon->ike_sa_manager->checkout(charon->ike_sa_manager,ike_sa_id, &ike_sa);
if ((status != SUCCESS) && (status != CREATED))
{
job->destroy(job);
this->worker_logger->log(this->worker_logger, ERROR, "IKE SA could not be checked out. Allready deleted?");
return;
}
status = ike_sa->retransmit_request(ike_sa, message_id);
if (status != SUCCESS)
{
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL | LEVEL3, "Message doesn't have to be retransmitted");
stop_retransmitting = TRUE;
}
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL|LEVEL2, "Checkin IKE SA %lld:%lld, role %s",
ike_sa_id->get_initiator_spi(ike_sa_id),
ike_sa_id->get_responder_spi(ike_sa_id),
ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
status = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "Checkin of IKE SA failed!");
}
if (stop_retransmitting)
{
job->destroy(job);
return;
}
job->increase_retransmit_count(job);
status = charon->configuration->get_retransmit_timeout (charon->configuration,job->get_retransmit_count(job),&timeout);
if (status != SUCCESS)
{
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL | LEVEL2, "Message will not be anymore retransmitted");
job->destroy(job);
/*
* TODO delete IKE_SA ?
*/
return;
}
charon->event_queue->add_relative(charon->event_queue,(job_t *) job,timeout);
}
/**
* Implementation of private_thread_pool_t.create_delete_half_open_ike_sa_job.
*/
static void create_delete_half_open_ike_sa_job(private_thread_pool_t *this,ike_sa_id_t *ike_sa_id, u_int32_t delay)
{
job_t *delete_job;
2005-12-07 07:57:18 +00:00
this->worker_logger->log(this->worker_logger, CONTROL | LEVEL2, "Going to create job to delete half open IKE_SA in %d ms", delay);
delete_job = (job_t *) delete_half_open_ike_sa_job_create(ike_sa_id);
charon->event_queue->add_relative(charon->event_queue,delete_job, delay);
}
2005-11-03 13:34:07 +00:00
/**
* Implementation of thread_pool_t.get_pool_size.
2005-11-03 13:34:07 +00:00
*/
static size_t get_pool_size(private_thread_pool_t *this)
2005-11-03 13:34:07 +00:00
{
return this->pool_size;
2005-11-03 13:34:07 +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 */
for (current = 0; current < this->pool_size; current++) {
2005-11-26 15:44:40 +00:00
this->pool_logger->log(this->pool_logger, CONTROL, "cancelling worker a 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++) {
pthread_join(this->threads[current], NULL);
2005-11-26 15:44:40 +00:00
this->pool_logger->log(this->pool_logger, CONTROL, "worker thread #%d terminated", current+1);
2005-11-03 13:34:07 +00:00
}
/* free mem */
allocator_free(this->threads);
allocator_free(this);
2005-11-03 13:34:07 +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;
private_thread_pool_t *this = allocator_alloc_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;
this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size;
2005-11-03 13:34:07 +00:00
this->process_jobs = process_jobs;
this->process_initiate_ike_sa_job = process_initiate_ike_sa_job;
this->process_delete_half_open_ike_sa_job = process_delete_half_open_ike_sa_job;
this->process_delete_established_ike_sa_job = process_delete_established_ike_sa_job;
this->process_incoming_packet_job = process_incoming_packet_job;
this->process_retransmit_request_job = process_retransmit_request_job;
this->create_delete_half_open_ike_sa_job = create_delete_half_open_ike_sa_job;
2005-11-03 13:34:07 +00:00
this->pool_size = pool_size;
this->threads = allocator_alloc(sizeof(pthread_t) * pool_size);
2005-11-28 20:29:47 +00:00
this->pool_logger = charon->logger_manager->get_logger(charon->logger_manager, THREAD_POOL);
2005-11-28 20:29:47 +00:00
this->worker_logger = charon->logger_manager->get_logger(charon->logger_manager, WORKER);
2005-11-04 13:20:11 +00:00
2005-11-03 13:34:07 +00:00
/* try to create as many threads as possible, up tu pool_size */
for (current = 0; current < pool_size; current++)
{
if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))this->process_jobs, this) == 0)
{
2005-12-06 16:36:42 +00:00
this->pool_logger->log(this->pool_logger, CONTROL, "Created worker thread #%d", current+1);
}
2005-11-26 15:44:40 +00:00
else
{
/* creation failed, is it the first one? */
if (current == 0)
{
2005-12-06 16:36:42 +00:00
this->pool_logger->log(this->pool_logger, ERROR, "Could not create any thread");
allocator_free(this->threads);
allocator_free(this);
2005-11-03 13:34:07 +00:00
return NULL;
}
/* not all threads could be created, but at least one :-/ */
2005-12-06 16:36:42 +00:00
this->pool_logger->log(this->pool_logger, ERROR, "Could only create %d from requested %d threads!", current, pool_size);
2005-11-03 13:34:07 +00:00
this->pool_size = current;
return (thread_pool_t*)this;
}
}
return (thread_pool_t*)this;
}