diff options
Diffstat (limited to 'Source/charon/queues')
-rw-r--r-- | Source/charon/queues/event_queue.c | 381 | ||||
-rw-r--r-- | Source/charon/queues/event_queue.h | 114 | ||||
-rw-r--r-- | Source/charon/queues/job_queue.c | 162 | ||||
-rw-r--r-- | Source/charon/queues/job_queue.h | 92 | ||||
-rw-r--r-- | Source/charon/queues/send_queue.c | 167 | ||||
-rw-r--r-- | Source/charon/queues/send_queue.h | 94 |
6 files changed, 1010 insertions, 0 deletions
diff --git a/Source/charon/queues/event_queue.c b/Source/charon/queues/event_queue.c new file mode 100644 index 000000000..7b0843a6c --- /dev/null +++ b/Source/charon/queues/event_queue.c @@ -0,0 +1,381 @@ +/** + * @file event_queue.c + * + * @brief Event-Queue based on class 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 <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 <pthread.h> +#include <stdlib.h> + +#include "event_queue.h" + +#include "../allocator.h" +#include "../types.h" +#include "../utils/linked_list.h" + + + +/** + * @brief Represents an event as it is stored in the event queue. + * + * A event consists of a event time and an assigned job object. + * + */ +typedef struct event_s event_t; + +struct event_s{ + /** + * Time to fire the event. + */ + timeval_t time; + + /** + * Every event has its assigned job. + */ + job_t * job; + + /** + * @brief Destroys a event_t object. + * + * @param event_t calling object + * @returns always SUCCESS + */ + status_t (*destroy) (event_t *event); +}; + + +/** + * @brief implements function destroy of event_t + */ +static status_t event_destroy(event_t *event) +{ + allocator_free(event); + return SUCCESS; +} + +/** + * @brief Creates a event for a specific time + * + * @param time absolute time to fire the event + * @param job job to add to job-queue at specific time + * + * @returns + * - created event_t object + * - NULL if memory allocation failed + */ +static event_t *event_create(timeval_t time, job_t *job) +{ + event_t *this = allocator_alloc_thing(event_t); + if (this == NULL) + { + return this; + } + + this->destroy = event_destroy; + this->time = time; + this->job = job; + + return this; +} + + +/** + * @brief Private Variables and Functions of event_queue_t class. + * + */ +typedef struct private_event_queue_s private_event_queue_t; + + +struct private_event_queue_s { + /** + * Public part. + */ + event_queue_t public; + + /** + * The events are stored in a linked list of type linked_list_t. + */ + linked_list_t *list; + + /** + * Access to linked_list is locked through this mutex. + */ + pthread_mutex_t mutex; + + /** + * If the queue is empty or an event has not to be fired + * a thread has to wait. + * + * This condvar is used to wake up such a thread. + */ + pthread_cond_t condvar; +}; + +/** + * Returns the difference of to timeval structs in microseconds + * + * @param end_time end time + * @param start_time start time + * + * @warning this function is also defined in the tester class + * In later improvements, this function can be added to a general + * class type! + * + * @return difference in microseconds (end time - start time) + */ +static long time_difference(struct timeval *end_time, struct timeval *start_time) +{ + long seconds, microseconds; + + seconds = (end_time->tv_sec - start_time->tv_sec); + microseconds = (end_time->tv_usec - start_time->tv_usec); + return ((seconds * 1000000) + microseconds); +} + + +/** + * Implements function get_count of event_queue_t. + * See #event_queue_s.get_count for description. + */ +static int get_count (private_event_queue_t *this) +{ + int count; + pthread_mutex_lock(&(this->mutex)); + count = this->list->get_count(this->list); + pthread_mutex_unlock(&(this->mutex)); + return count; +} + +/** + * Implements function get of event_queue_t. + * See #event_queue_s.get for description. + */ +static status_t get(private_event_queue_t *this, job_t **job) +{ + timespec_t timeout; + timeval_t current_time; + event_t * next_event; + int oldstate; + + pthread_mutex_lock(&(this->mutex)); + + while (1) + { + while(this->list->get_count(this->list) == 0) + { + /* add mutex unlock handler for cancellation, enable cancellation */ + pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex)); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + + pthread_cond_wait( &(this->condvar), &(this->mutex)); + + /* reset cancellation, remove mutex-unlock handler (without executing) */ + pthread_setcancelstate(oldstate, NULL); + pthread_cleanup_pop(0); + } + + this->list->get_first(this->list,(void **) &next_event); + + gettimeofday(¤t_time,NULL); + long difference = time_difference(¤t_time,&(next_event->time)); + if (difference <= 0) + { + timeout.tv_sec = next_event->time.tv_sec; + timeout.tv_nsec = next_event->time.tv_usec * 1000; + + pthread_cond_timedwait( &(this->condvar), &(this->mutex),&timeout); + } + else + { + /* event available */ + this->list->remove_first(this->list,(void **) &next_event); + + *job = next_event->job; + + next_event->destroy(next_event); + break; + } + + } + pthread_cond_signal( &(this->condvar)); + + pthread_mutex_unlock(&(this->mutex)); + + return SUCCESS; +} + +/** + * Implements function add_absolute of event_queue_t. + * See #event_queue_s.add_absolute for description. + */ +static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t time) +{ + event_t *event = event_create(time,job); + event_t *current_event; + status_t status; + + if (event == NULL) + { + return FAILED; + } + pthread_mutex_lock(&(this->mutex)); + + /* while just used to break out */ + while(1) + { + if (this->list->get_count(this->list) == 0) + { + status = this->list->insert_first(this->list,event); + break; + } + + /* check last entry */ + this->list->get_last(this->list,(void **) ¤t_event); + + if (time_difference(&(event->time), &(current_event->time)) >= 0) + { + /* my event has to be fired after the last event in list */ + status = this->list->insert_last(this->list,event); + break; + } + + /* check first entry */ + this->list->get_first(this->list,(void **) ¤t_event); + + if (time_difference(&(event->time), &(current_event->time)) < 0) + { + /* my event has to be fired before the first event in list */ + status = this->list->insert_first(this->list,event); + break; + } + + linked_list_iterator_t * iterator; + + status = this->list->create_iterator(this->list,&iterator,TRUE); + if (status != SUCCESS) + { + break; + } + + + iterator->has_next(iterator); + /* first element has not to be checked (already done) */ + + while(iterator->has_next(iterator)) + { + status = iterator->current(iterator,(void **) ¤t_event); + + if (time_difference(&(event->time), &(current_event->time)) <= 0) + { + /* my event has to be fired before the current event in list */ + status = this->list->insert_before(this->list,iterator,event); + break; + } + } + iterator->destroy(iterator); + break; + } + + pthread_cond_signal( &(this->condvar)); + pthread_mutex_unlock(&(this->mutex)); + + if (status != SUCCESS) + { + event->destroy(event); + } + return status; +} + +/** + * Implements function add_relative of event_queue_t. + * See #event_queue_s.add_relative for description. + */ +static status_t add_relative(event_queue_t *this, job_t *job, u_int32_t ms) +{ + timeval_t current_time; + timeval_t time; + int micros = ms * 1000; + + gettimeofday(¤t_time, NULL); + + time.tv_usec = ((current_time.tv_usec + micros) % 1000000); + time.tv_sec = current_time.tv_sec + ((current_time.tv_usec + micros)/ 1000000); + + return this->add_absolute(this, job, time); +} + + +/** + * Implements function destroy of event_queue_t. + * See #event_queue_s.destroy for description. + */ +static status_t event_queue_destroy(private_event_queue_t *this) +{ + while (this->list->get_count(this->list) > 0) + { + event_t *event; + + if (this->list->remove_first(this->list,(void *) &event) != SUCCESS) + { + this->list->destroy(this->list); + break; + } + event->job->destroy(event->job); + event->destroy(event); + } + this->list->destroy(this->list); + + pthread_mutex_destroy(&(this->mutex)); + + pthread_cond_destroy(&(this->condvar)); + + allocator_free(this); + return SUCCESS; +} + +/* + * Documented in header + */ +event_queue_t *event_queue_create() +{ + linked_list_t *linked_list = linked_list_create(); + if (linked_list == NULL) + { + return NULL; + } + + private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t); + if (this == NULL) + { + linked_list->destroy(linked_list); + return NULL; + } + + this->public.get_count = (int (*) (event_queue_t *event_queue)) get_count; + this->public.get = (status_t (*) (event_queue_t *event_queue, job_t **job)) get; + this->public.add_absolute = (status_t (*) (event_queue_t *event_queue, job_t *job, timeval_t time)) add_absolute; + this->public.add_relative = (status_t (*) (event_queue_t *event_queue, job_t *job, u_int32_t ms)) add_relative; + this->public.destroy = (status_t (*) (event_queue_t *event_queue)) event_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/queues/event_queue.h b/Source/charon/queues/event_queue.h new file mode 100644 index 000000000..52e4c8153 --- /dev/null +++ b/Source/charon/queues/event_queue.h @@ -0,0 +1,114 @@ +/** + * @file event_queue.h + * + * @brief Event-Queue based on class 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 <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. + */ + +#ifndef EVENT_QUEUE_H_ +#define EVENT_QUEUE_H_ + +#include <sys/time.h> + +#include "../types.h" +#include "../job.h" + +/** + * @brief Event-Queue used to store timed events. + * + * Although the event-queue is based on a linked_list_t + * all access functions are thread-save implemented. + */ +typedef struct event_queue_s event_queue_t; + +struct event_queue_s { + + /** + * @brief Returns number of events in queue. + * + * @param event_queue calling object + * @return number of events in queue + */ + int (*get_count) (event_queue_t *event_queue); + + /** + * @brief Get the next job from the event-queue. + * + * If no event is pending, this function blocks until a job can be returned. + * + * @param event_queue calling object + * @param[out] job pointer to a job pointer where to job is returned to + * @return - SUCCESS if succeeded + * - FAILED otherwisesa + */ + status_t (*get) (event_queue_t *event_queue, job_t **job); + + /** + * @brief Adds a event to the queue, using a relative time. + * + * This function is non blocking and adds a job_t at a specific time to the list. + * The specific job object has to get destroyed by the thread which + * removes the job. + * + * @param event_queue calling object + * @param[in] job job to add to the queue (job is not copied) + * @param[in] time relative time, when the event has to get fired + * @returns + * - SUCCESS if succeeded + * - FAILED otherwise + */ + status_t (*add_relative) (event_queue_t *event_queue, job_t *job, u_int32_t ms); + + /** + * @brief Adds a event to the queue, using an absolute time. + * + * This function is non blocking and adds a job_t at a specific time to the list. + * The specific job object has to get destroyed by the thread which + * removes the job. + * + * @param event_queue calling object + * @param[in] job job to add to the queue (job is not copied) + * @param[in] absolute time time, when the event has to get fired + * @returns + * - SUCCESS if succeeded + * - FAILED otherwise + */ + status_t (*add_absolute) (event_queue_t *event_queue, job_t *job, timeval_t time); + + /** + * @brief Destroys a event_queue object. + * + * @warning The caller of this function has to make sure + * that no thread is going to add or get an event from the event_queue + * after calling this function. + * + * @param event_queue calling object + * @returns always SUCCESS + */ + status_t (*destroy) (event_queue_t *event_queue); +}; + +/** + * @brief Creates an empty event_queue + * + * @returns + * - Empty event_queue_t object + * - NULL if memory allocation failed + */ +event_queue_t *event_queue_create(); +#endif /*EVENT_QUEUE_H_*/ diff --git a/Source/charon/queues/job_queue.c b/Source/charon/queues/job_queue.c new file mode 100644 index 000000000..46423a961 --- /dev/null +++ b/Source/charon/queues/job_queue.c @@ -0,0 +1,162 @@ +/** + * @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 <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 "job_queue.h" + +#include "../allocator.h" +#include "../utils/linked_list.h" + +/** + * @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 + */ +static int get_count(private_job_queue_t *this) +{ + int count; + pthread_mutex_lock(&(this->mutex)); + count = this->list->get_count(this->list); + pthread_mutex_unlock(&(this->mutex)); + return count; +} + +/** + * @brief implements function get of job_queue_t + */ +static status_t get(private_job_queue_t *this, job_t **job) +{ + int oldstate; + pthread_mutex_lock(&(this->mutex)); + /* go to wait while no jobs available */ + while(this->list->get_count(this->list) == 0) + { + /* add mutex unlock handler for cancellation, enable cancellation */ + pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex)); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + + pthread_cond_wait( &(this->condvar), &(this->mutex)); + + /* reset cancellation, remove mutex-unlock handler (without executing) */ + pthread_setcancelstate(oldstate, NULL); + pthread_cleanup_pop(0); + } + this->list->remove_first(this->list,(void **) job); + pthread_mutex_unlock(&(this->mutex)); + return SUCCESS; +} + +/** + * @brief implements function add of job_queue_t + */ +static status_t add(private_job_queue_t *this, job_t *job) +{ + 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 + * + */ +static status_t job_queue_destroy (private_job_queue_t *this) +{ + while (this->list->get_count(this->list) > 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)); + + allocator_free(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 = allocator_alloc_thing(private_job_queue_t); + if (this == NULL) + { + linked_list->destroy(linked_list); + return NULL; + } + + this->public.get_count = (int(*)(job_queue_t*))get_count; + this->public.get = (status_t(*)(job_queue_t*, job_t**))get; + this->public.add = (status_t(*)(job_queue_t*, job_t*))add; + this->public.destroy = (status_t(*)(job_queue_t*))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/queues/job_queue.h b/Source/charon/queues/job_queue.h new file mode 100644 index 000000000..c7ceacae3 --- /dev/null +++ b/Source/charon/queues/job_queue.h @@ -0,0 +1,92 @@ +/** + * @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 <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. + */ + +#ifndef JOB_QUEUE_H_ +#define JOB_QUEUE_H_ + +#include "../types.h" +#include "../job.h" + +/** + * @brief Job-Queue + * + * Although the job-queue is based on a linked_list_t + * all access functions are thread-save implemented + */ +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 + * @returns number of items in queue + */ + int (*get_count) (job_queue_t *job_queue); + + /** + * @brief get the next job from the queue + * + * If the queue is empty, this function blocks until a job can be returned. + * + * After using, the returned job has to get destroyed by the caller. + * + * @param job_queue_t calling object + * @param[out] 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 and adds a job_t to the list. + * The specific job object has to get destroyed by the thread which + * removes the job. + * + * @param job_queue_t calling object + * @param[in] 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 The caller of this function has to make sure + * that no thread is going to add or get a job from the job_queue + * after calling this function. + * + * @param job_queue_t calling object + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*destroy) (job_queue_t *job_queue); +}; + +/** + * @brief Creates an empty job_queue + * + * @return job_queue_t empty job_queue + */ +job_queue_t *job_queue_create(); +#endif /*JOB_QUEUE_H_*/ diff --git a/Source/charon/queues/send_queue.c b/Source/charon/queues/send_queue.c new file mode 100644 index 000000000..1b416f731 --- /dev/null +++ b/Source/charon/queues/send_queue.c @@ -0,0 +1,167 @@ +/** + * @file send_queue.c + * + * @brief Send-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 <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 <pthread.h> + +#include "send_queue.h" + +#include "../allocator.h" +#include "../utils/linked_list.h" + + /** + * @brief Private Variables and Functions of send_queue class + * + */ +typedef struct private_send_queue_s private_send_queue_t; + + +struct private_send_queue_s { + /** + * Public part of the send_queue_t object + */ + send_queue_t public; + + /** + * The packets 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 send_queue_t + */ +static int get_count(private_send_queue_t *this) +{ + int count; + pthread_mutex_lock(&(this->mutex)); + count = this->list->get_count(this->list); + pthread_mutex_unlock(&(this->mutex)); + return count; +} + + /** + * @brief implements function get of send_queue_t + */ +static status_t get(private_send_queue_t *this, packet_t **packet) +{ + int oldstate; + pthread_mutex_lock(&(this->mutex)); + /* go to wait while no packets available */ + + while(this->list->get_count(this->list) == 0) + { + /* add mutex unlock handler for cancellation, enable cancellation */ + pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex)); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + pthread_cond_wait( &(this->condvar), &(this->mutex)); + + /* reset cancellation, remove mutex-unlock handler (without executing) */ + pthread_setcancelstate(oldstate, NULL); + pthread_cleanup_pop(0); + } + this->list->remove_first(this->list,(void **) packet); + pthread_mutex_unlock(&(this->mutex)); + return SUCCESS; +} + + /** + * @brief implements function add of send_queue_t + */ +static status_t add(private_send_queue_t *this, packet_t *packet) +{ + pthread_mutex_lock(&(this->mutex)); + this->list->insert_last(this->list,packet); + pthread_cond_signal( &(this->condvar)); + pthread_mutex_unlock(&(this->mutex)); + return SUCCESS; +} + + /** + * @brief implements function destroy of send_queue_t + * + */ +static status_t destroy (private_send_queue_t *this) +{ + + /* destroy all packets in list before destroying list */ + while (this->list->get_count(this->list) > 0) + { + packet_t *packet; + if (this->list->remove_first(this->list,(void *) &packet) != SUCCESS) + { + this->list->destroy(this->list); + break; + } + packet->destroy(packet); + } + this->list->destroy(this->list); + + pthread_mutex_destroy(&(this->mutex)); + + pthread_cond_destroy(&(this->condvar)); + + allocator_free(this); + return SUCCESS; +} + + /* + * + * Documented in header + */ +send_queue_t *send_queue_create() +{ + linked_list_t *linked_list = linked_list_create(); + if (linked_list == NULL) + { + return NULL; + } + + private_send_queue_t *this = allocator_alloc_thing(private_send_queue_t); + if (this == NULL) + { + linked_list->destroy(linked_list); + return NULL; + } + + this->public.get_count = (int(*)(send_queue_t*)) get_count; + this->public.get = (status_t(*)(send_queue_t*, packet_t**)) get; + this->public.add = (status_t(*)(send_queue_t*, packet_t*)) add; + this->public.destroy = (status_t(*)(send_queue_t*)) 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/queues/send_queue.h b/Source/charon/queues/send_queue.h new file mode 100644 index 000000000..072d2f966 --- /dev/null +++ b/Source/charon/queues/send_queue.h @@ -0,0 +1,94 @@ +/** + * @file send_queue.h + * + * @brief Send-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 <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. + */ + +#ifndef SEND_QUEUE_H_ +#define SEND_QUEUE_H_ + +#include "../types.h" +#include "../packet.h" + +/** + * @brief Send-Queue + * + * Although the send-queue is based on a linked_list_t + * all access functions are thread-save implemented + */ +typedef struct send_queue_s send_queue_t; + +struct send_queue_s { + + /** + * @brief returns number of packets in queue + * + * @param send_queue_t calling object + * @param[out] count integer pointer to store the count in + * @returns number of items in queue + */ + int (*get_count) (send_queue_t *send_queue); + + /** + * @brief get the next packet from the queue + * + * If the queue is empty, this function blocks until a packet can be returned. + * + * After using, the returned packet has to get destroyed by the caller. + * + * @param send_queue_t calling object + * @param[out] packet pointer to a packet_t pointer where to packet is returned to + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*get) (send_queue_t *send_queue, packet_t **packet); + + /** + * @brief adds a packet to the queue + * + * This function is non blocking and adds a packet_t to the list. + * The specific packet object has to get destroyed by the thread which + * removes the packet. + * + * @param send_queue_t calling object + * @param[in] packet packet_t to add to the queue (packet is not copied) + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*add) (send_queue_t *send_queue, packet_t *packet); + + /** + * @brief destroys a send_queue object + * + * @warning The caller of this function has to make sure + * that no thread is going to add or get a packet from the send_queue + * after calling this function. + * + * @param send_queue_t calling object + * @returns SUCCESS if succeeded, FAILED otherwise + */ + status_t (*destroy) (send_queue_t *send_queue); +}; + +/** + * @brief Creates an empty send_queue_t + * + * @return send_queue_t empty send_queue_t + */ +send_queue_t *send_queue_create(); + +#endif /*SEND_QUEUE_H_*/ |