From adca27eb76663402473a69fc3944919495f99fd6 Mon Sep 17 00:00:00 2001 From: Jan Hutter Date: Thu, 3 Nov 2005 13:32:40 +0000 Subject: [PATCH] - job queue implemented but not tested --- Source/charon/job_queue.c | 180 ++++++++++++++++++++++++++++++++++++ Source/charon/job_queue.h | 138 +++++++++++++++++++++++++++ Source/charon/linked_list.c | 16 +++- Source/charon/tester.c | 21 ++++- 4 files changed, 349 insertions(+), 6 deletions(-) create mode 100644 Source/charon/job_queue.c create mode 100644 Source/charon/job_queue.h diff --git a/Source/charon/job_queue.c b/Source/charon/job_queue.c new file mode 100644 index 000000000..d5fbdb085 --- /dev/null +++ b/Source/charon/job_queue.c @@ -0,0 +1,180 @@ +/** + * @file job_queue.c + * + * @brief Job-Queue based on linked_list_t + * + */ + +/* + * 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 . + * + * 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 +#include +#include +#include +#include + +#include "job_queue.h" + +/** + * @brief implements function destroy of job_t + */ +static status_t job_destroy(job_t *job) +{ + pfree(job); + return SUCCESS; +} + +/* + * Creates a job (documented in header-file) + */ +job_t *job_create(job_type_t type, void *assigned_data) +{ + job_t *this = alloc_thing(job_t, "job_t"); + + this->destroy = job_destroy; + + this->type = type; + this->assigned_data = assigned_data; + + return this; +} + +/** + * @brief Private Variables and Functions of job_queue class + * + */ +typedef struct private_job_queue_s private_job_queue_t; + + +struct private_job_queue_s { + job_queue_t public; + + /** + * The jobs are stored in a linked list + */ + linked_list_t *list; + /** + * access to linked_list is locked through this mutex + */ + pthread_mutex_t mutex; + + /** + * If the queue is empty a thread has to wait + * This condvar is used to wake up such a thread + */ + pthread_cond_t condvar; +}; + + +/** + * @brief implements function get_count of job_queue_t + */ +status_t get_count(job_queue_t *job_queue, int *count) +{ + private_job_queue_t *this = (private_job_queue_t *) job_queue; + pthread_mutex_lock(&(this->mutex)); + *count = this->list->count; + pthread_mutex_unlock(&(this->mutex)); + return SUCCESS; +} + +/** + * @brief implements function get of job_queue_t + */ +status_t get(job_queue_t *job_queue, job_t **job) +{ + private_job_queue_t *this = (private_job_queue_t *) job_queue; + pthread_mutex_lock(&(this->mutex)); + while(this->list->count == 0) + { + pthread_cond_wait( &(this->condvar), &(this->mutex)); + } + this->list->remove_first(this->list,(void **) job); + pthread_mutex_unlock(&(this->mutex)); + return SUCCESS; +} + +/** + * @brief implements function add of job_queue_t + */ +status_t add(job_queue_t *job_queue, job_t *job) +{ + private_job_queue_t *this = (private_job_queue_t *) job_queue; + pthread_mutex_lock(&(this->mutex)); + this->list->insert_last(this->list,job); + pthread_cond_signal( &(this->condvar)); + pthread_mutex_unlock(&(this->mutex)); + return SUCCESS; +} + +/** + * @brief implements function destroy of job_queue_t + * + */ +status_t job_queue_destroy (job_queue_t *job_queue) +{ + private_job_queue_t *this = (private_job_queue_t *) job_queue; + + while (this->list->count > 0) + { + job_t *job; + if (this->list->remove_first(this->list,(void *) &job) != SUCCESS) + { + this->list->destroy(this->list); + break; + } + job->destroy(job); + } + this->list->destroy(this->list); + + pthread_mutex_destroy(&(this->mutex)); + + pthread_cond_destroy(&(this->condvar)); + + pfree(this); + return SUCCESS; +} + +/* + * + * Documented in header + */ +job_queue_t *job_queue_create() +{ + linked_list_t *linked_list = linked_list_create(); + if (linked_list == NULL) + { + return NULL; + } + + private_job_queue_t *this = alloc_thing(private_job_queue_t, "private_job_queue_t"); + if (this == NULL) + { + linked_list->destroy(linked_list); + return NULL; + } + + this->public.get_count = get_count; + this->public.get = get; + this->public.add = add; + this->public.destroy = job_queue_destroy; + + this->list = linked_list; + pthread_mutex_init(&(this->mutex), NULL); + pthread_cond_init(&(this->condvar), NULL); + + return (&this->public); +} diff --git a/Source/charon/job_queue.h b/Source/charon/job_queue.h new file mode 100644 index 000000000..3b6349836 --- /dev/null +++ b/Source/charon/job_queue.h @@ -0,0 +1,138 @@ +/** + * @file job_queue.h + * + * @brief Job-Queue based on linked_list_t + * + */ + +/* + * 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 . + * + * 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. + */ + +#ifndef JOB_QUEUE_H_ +#define JOB_QUEUE_H_ + +#include "linked_list.h" + +/** + * Type of Jobs + */ +typedef enum job_type_e job_type_t; + +enum job_type_e{ + /** + * Job is to process an incoming IKEv2-Message + */ + INCOMING_PACKET, + /** + * Job is to retransmit an IKEv2-Message + */ + RETRANSMIT_REQUEST, + /** + * Job is to establish an ike sa as initiator + */ + ESTABLISH_IKE_SA +}; + + +/** + * @brief Job like it is represented in the job queue + */ +typedef struct job_s job_t; + + +struct job_s{ + job_type_t type; + /** + * Every job has its assigned_data + */ + void * assigned_data; + + /** + * @brief Destroys a job_t object + * + * @param job_t calling object + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*destroy) (job_t *job); +}; + +/** + * @brief Creates a job of specific type + * + * @param type type of the job + * @param assigned_data value to assign to the job + * + * @return job_t job object + */ +job_t *job_create(job_type_t type, void *assigned_data); + +/** + * @brief Job-Queue + */ +typedef struct job_queue_s job_queue_t; + +struct job_queue_s { + + /** + * @brief Returns number of jobs in queue + * + * @param job_queue_t calling object + * @param count integer pointer to store the job count in + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*get_count) (job_queue_t *job_queue, int *count); + + /** + * @brief Get the next job from the queue + * + * If the queue is empty, this function blocks until job can be returned. + * + * After using, the returned job has to get destroyed. + * + * @param job_queue_t calling object + * @param job pointer to a job pointer where to job is returned to + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*get) (job_queue_t *job_queue, job_t **job); + + /** + * @brief Adds a job to the queue + * + * This function is non blocking + * + * @param job_queue_t calling object + * @param job job to add to the queue (job is not copied) + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*add) (job_queue_t *job_queue, job_t *job); + + /** + * @brief Destroys a job_queue object + * + * @warning Has only to be called if no other thread is accessing the queue + * + * @param job_queue_t calling object + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*destroy) (job_queue_t *job_queue); +}; + +/** + * @brief Creates a job_queue + * * + * @return job_queue_t empty job_queue + */ +job_queue_t *job_queue_create(); +#endif /*JOB_QUEUE_H_*/ diff --git a/Source/charon/linked_list.c b/Source/charon/linked_list.c index 50aa4bd07..0a84545ca 100644 --- a/Source/charon/linked_list.c +++ b/Source/charon/linked_list.c @@ -31,18 +31,21 @@ /** * @brief implements function destroy of linked_list_item_t */ -static status_t destroy_linked_list_element(linked_list_element_t *linked_list_element) +static status_t linked_list_element_destroy(linked_list_element_t *linked_list_element) { linked_list_element_t * this = linked_list_element; pfree(this); return SUCCESS; } +/* + * Creates a linked list (documented in header-file) + */ linked_list_element_t *linked_list_element_create(void *value) { linked_list_element_t *this = alloc_thing(linked_list_element_t, "linked_list_element_t"); - this->destroy = destroy_linked_list_element; + this->destroy = linked_list_element_destroy; this->previous=NULL; this->next=NULL; @@ -232,7 +235,7 @@ static status_t get_last(linked_list_t *linked_list, void **item) /** * @brief implements function destroy of linked_list_t */ -static status_t destroy_linked_list(linked_list_t *linked_list) +static status_t linked_list_destroy(linked_list_t *linked_list) { linked_list_t *this = linked_list; @@ -250,7 +253,10 @@ static status_t destroy_linked_list(linked_list_t *linked_list) return SUCCESS; } - +/* + * + * Documented in header + */ linked_list_t *linked_list_create() { linked_list_t *this = alloc_thing(linked_list_t, "linked_list_t"); @@ -261,7 +267,7 @@ linked_list_t *linked_list_create() this->insert_last = insert_last; this->remove_first = remove_first; this->remove_last = remove_last; - this->destroy = destroy_linked_list; + this->destroy = linked_list_destroy; this->count = 0; this->first = NULL; diff --git a/Source/charon/tester.c b/Source/charon/tester.c index fb92a4d35..4a5ab0a8b 100644 --- a/Source/charon/tester.c +++ b/Source/charon/tester.c @@ -74,34 +74,53 @@ static void test_linked_list(private_tester_t *this) void *test_value = NULL; linked_list_t *linked_list = linked_list_create(); + this->assert_true(this,(linked_list->count == 0), "count check"); + linked_list->insert_first(linked_list,"one"); + this->assert_true(this,(linked_list->count == 1), "count check"); + linked_list->insert_first(linked_list,"two"); + this->assert_true(this,(linked_list->count == 2), "count check"); + linked_list->insert_first(linked_list,"three"); + this->assert_true(this,(linked_list->count == 3), "count check"); + linked_list->insert_first(linked_list,"four"); + this->assert_true(this,(linked_list->count == 4), "count check"); + linked_list->insert_first(linked_list,"five"); + this->assert_true(this,(linked_list->count == 5), "count check"); this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check"); this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "get_first value check"); + this->assert_true(this,(linked_list->count == 5), "count check"); this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check"); - this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check"); + this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check"); + this->assert_true(this,(linked_list->count == 5), "count check"); this->assert_true(this,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check"); this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "remove_first value check"); + this->assert_true(this,(linked_list->count == 4), "count check"); this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check"); this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check"); + this->assert_true(this,(linked_list->count == 4), "count check"); this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check"); this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check"); + this->assert_true(this,(linked_list->count == 4), "count check"); this->assert_true(this,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check"); this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "remove_last value check"); + this->assert_true(this,(linked_list->count == 3), "count check"); this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check"); this->assert_true(this,(strcmp((char *) test_value,"two") == 0), "get_last value check"); + this->assert_true(this,(linked_list->count == 3), "count check"); this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check"); this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check"); + this->assert_true(this,(linked_list->count == 3), "count check"); this->assert_true(this,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check"); }