- updated comments
- revised get_count function
This commit is contained in:
@@ -35,7 +35,7 @@ void test_thread_pool(tester_t *tester)
|
||||
size_t pool_size;
|
||||
|
||||
thread_pool_t *pool = thread_pool_create(desired_pool_size);
|
||||
pool->get_pool_size(pool, &pool_size);
|
||||
pool_size = pool->get_pool_size(pool);
|
||||
tester->assert_true(tester, (desired_pool_size == pool_size), "thread creation");
|
||||
tester->assert_true(tester, (pool->destroy(pool) == SUCCESS), "threadpool destruction");
|
||||
}
|
||||
|
||||
+50
-16
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file thread_pool.c
|
||||
*
|
||||
* @brief Thread-pool with some threads processing the job_queue
|
||||
* @brief Thread pool with some threads processing the job_queue.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -32,13 +32,21 @@
|
||||
#include "globals.h"
|
||||
|
||||
/**
|
||||
* structure with private members for thread_pool
|
||||
* @brief structure with private members for thread_pool_t
|
||||
*/
|
||||
typedef struct {
|
||||
typedef struct private_thread_pool_s private_thread_pool_t;
|
||||
|
||||
struct private_thread_pool_s {
|
||||
/**
|
||||
* inclusion of public members
|
||||
*/
|
||||
thread_pool_t public;
|
||||
/**
|
||||
* @brief Processing function of a worker thread
|
||||
*
|
||||
* @param this private_thread_pool_t-Object
|
||||
*/
|
||||
void (*function) (private_thread_pool_t *this);
|
||||
/**
|
||||
* number of running threads
|
||||
*/
|
||||
@@ -51,18 +59,25 @@ typedef struct {
|
||||
* logger of the threadpool
|
||||
*/
|
||||
logger_t *logger;
|
||||
} private_thread_pool_t;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* implements private_thread_pool_t.function
|
||||
*/
|
||||
static void job_processing(private_thread_pool_t *this)
|
||||
{
|
||||
/* cancellation disabled by default */
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
|
||||
this->logger->log(this->logger, CONTROL_MORE, "thread %u started working", pthread_self());
|
||||
|
||||
for (;;) {
|
||||
job_t *job;
|
||||
|
||||
global_job_queue->get(global_job_queue, &job);
|
||||
this->logger->log(this->logger, CONTROL_MORE, "thread %u got a job", pthread_self());
|
||||
|
||||
/* process them here */
|
||||
|
||||
@@ -72,12 +87,11 @@ static void job_processing(private_thread_pool_t *this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of thread_pool_t.get_pool_size
|
||||
* implementation of thread_pool_t.get_pool_size
|
||||
*/
|
||||
static status_t get_pool_size(private_thread_pool_t *this, size_t *size)
|
||||
static size_t get_pool_size(private_thread_pool_t *this)
|
||||
{
|
||||
*size = this->pool_size;
|
||||
return SUCCESS;
|
||||
return this->pool_size;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,8 +119,10 @@ static status_t destroy(private_thread_pool_t *this)
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of default constructor for thread_pool_t
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* see header
|
||||
*/
|
||||
thread_pool_t *thread_pool_create(size_t pool_size)
|
||||
{
|
||||
@@ -116,21 +132,40 @@ thread_pool_t *thread_pool_create(size_t pool_size)
|
||||
|
||||
/* fill in public fields */
|
||||
this->public.destroy = (status_t(*)(thread_pool_t*))destroy;
|
||||
this->public.get_pool_size = (status_t(*)(thread_pool_t*, size_t*))get_pool_size;
|
||||
this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size;
|
||||
|
||||
this->function = job_processing;
|
||||
this->pool_size = pool_size;
|
||||
|
||||
this->threads = allocator_alloc(sizeof(pthread_t) * pool_size);
|
||||
this->logger = logger_create("thread_pool", 0);
|
||||
if (this->threads == NULL)
|
||||
{
|
||||
allocator_free(this);
|
||||
return NULL;
|
||||
}
|
||||
this->logger = logger_create("thread_pool", ALL);
|
||||
if (this->threads == NULL)
|
||||
{
|
||||
allocator_free(this);
|
||||
allocator_free(this->threads);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 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*))job_processing, this))
|
||||
for (current = 0; current < pool_size; current++)
|
||||
{
|
||||
if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))this->function, this) == 0)
|
||||
{
|
||||
/* did we get any? */
|
||||
this->logger->log(this->logger, CONTROL, "thread %u created", this->threads[current]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* creation failed, is it the first one? */
|
||||
if (current == 0)
|
||||
{
|
||||
this->logger->log(this->logger, CONTROL, "could not create any thread: %s\n", strerror(errno));
|
||||
allocator_free(this->threads);
|
||||
allocator_free(this->logger);
|
||||
allocator_free(this);
|
||||
return NULL;
|
||||
}
|
||||
@@ -141,6 +176,5 @@ thread_pool_t *thread_pool_create(size_t pool_size)
|
||||
return (thread_pool_t*)this;
|
||||
}
|
||||
}
|
||||
|
||||
return (thread_pool_t*)this;
|
||||
}
|
||||
|
||||
+12
-11
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file thread_pool.h
|
||||
*
|
||||
* @brief Thread-pool with some threads processing the job_queue
|
||||
* @brief Thread pool with some threads processing the job_queue
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* @brief A thread_pool contains a pool of threads processing the job queue
|
||||
* @brief A thread_pool contains a pool of threads processing the job queue.
|
||||
*
|
||||
* Current implementation uses as many threads as specified in constructor.
|
||||
* A more improved version would dynamically increase thread count if necessary...
|
||||
@@ -37,20 +37,20 @@ typedef struct thread_pool_s thread_pool_t;
|
||||
|
||||
struct thread_pool_s {
|
||||
/**
|
||||
* @brief return currently active threads
|
||||
* @brief return currently instanciated threads
|
||||
*
|
||||
* @param thread_pool thread_pool_t object
|
||||
* @param size [out] actual size of pool
|
||||
* @return SUCCESS Thread flagged for termination
|
||||
* @param thread_pool thread_pool_t object
|
||||
* @return size of thread pool
|
||||
*/
|
||||
status_t (*get_pool_size) (thread_pool_t *thread_pool, size_t *pool_size);
|
||||
size_t (*get_pool_size) (thread_pool_t *thread_pool);
|
||||
/**
|
||||
* @brief destroy pool
|
||||
*
|
||||
* sends cancellation request to all threads and AWAITS their termination.
|
||||
*
|
||||
* @param thread_pool thread_pool_t object
|
||||
* @return SUCCESS
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*destroy) (thread_pool_t *thread_pool);
|
||||
};
|
||||
@@ -58,9 +58,10 @@ struct thread_pool_s {
|
||||
/**
|
||||
* @brief Create the thread pool using using pool_size of threads
|
||||
*
|
||||
* @param pool_size desired pool size
|
||||
* @return NULL when no thread could be created
|
||||
* thread_pool when one ore more threads could be created
|
||||
* @param pool_size desired pool size
|
||||
* @return
|
||||
* - NULL if no threads could be created
|
||||
* - thread_pool if one ore more threads could be instanciated
|
||||
*/
|
||||
thread_pool_t *thread_pool_create(size_t pool_size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user