diff options
Diffstat (limited to 'Source/charon/event_queue.c')
-rw-r--r-- | Source/charon/event_queue.c | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/Source/charon/event_queue.c b/Source/charon/event_queue.c index 610d9822e..d086bf44f 100644 --- a/Source/charon/event_queue.c +++ b/Source/charon/event_queue.c @@ -1,8 +1,8 @@ /** * @file event_queue.c - * + * * @brief Event-Queue based on linked_list_t - * + * */ /* @@ -25,8 +25,8 @@ #include <pluto/defs.h> #include <pthread.h> #include <stdlib.h> - - + + #include "types.h" #include "event_queue.h" #include "linked_list.h" @@ -35,9 +35,9 @@ /** * @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; @@ -54,14 +54,14 @@ struct event_s{ /** * @brief Destroys a event_t object - * + * * @param event_t calling object * @returns SUCCESS if succeeded, FAILED otherwise */ status_t (*destroy) (event_t *event); }; - + /** * @brief implements function destroy of event_t */ @@ -71,7 +71,7 @@ static status_t event_destroy(event_t *event) { return FAILED; } - pfree(event); + allocator_free(event); return SUCCESS; } @@ -80,66 +80,66 @@ static status_t event_destroy(event_t *event) * * @param time to fire the event * @param job job to add to job-queue at specific time - * + * * @return event_t event object */ static event_t *event_create(timeval_t time, job_t *job) { - event_t *this = alloc_thing(event_t, "event_t"); + event_t *this = allocator_alloc_thing(event_t, "event_t"); this->destroy = event_destroy; this->time = time; this->job = job; - + return this; } /** * @brief Private Variables and Functions of event_queue class - * + * */ typedef struct private_event_queue_s private_event_queue_t; - + struct private_event_queue_s { event_queue_t public; - + /** * The events 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 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; + 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 */ 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); @@ -167,9 +167,9 @@ static status_t get(private_event_queue_t *this, job_t **job) event_t * next_event; int count; int oldstate; - + pthread_mutex_lock(&(this->mutex)); - + while (1) { this->list->get_count(this->list,&count); @@ -178,18 +178,18 @@ static status_t get(private_event_queue_t *this, job_t **job) /* 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_count(this->list,&count); } - + 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) @@ -203,18 +203,18 @@ static status_t get(private_event_queue_t *this, job_t **job) { /* 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; } @@ -227,7 +227,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t event_t *current_event; status_t status; int count; - + if (event == NULL) { return FAILED; @@ -243,7 +243,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t status = this->list->insert_first(this->list,event); break; } - + /* check last entry */ this->list->get_last(this->list,(void **) ¤t_event); @@ -253,7 +253,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t status = this->list->insert_last(this->list,event); break; } - + /* check first entry */ this->list->get_first(this->list,(void **) ¤t_event); @@ -263,27 +263,27 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t 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) */ - + /* 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); + status = this->list->insert_before(this->list,iterator,event); break; } } @@ -298,7 +298,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t { event->destroy(event); } - return status; + return status; } /** @@ -309,12 +309,12 @@ 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); } @@ -323,13 +323,13 @@ static status_t add_relative(event_queue_t *this, job_t *job, u_int32_t ms) * @brief implements function destroy of event_queue_t */ static status_t event_queue_destroy(private_event_queue_t *this) -{ +{ int count; this->list->get_count(this->list,&count); while (count > 0) { - event_t *event; - + event_t *event; + if (this->list->remove_first(this->list,(void *) &event) != SUCCESS) { this->list->destroy(this->list); @@ -340,17 +340,17 @@ static status_t event_queue_destroy(private_event_queue_t *this) this->list->get_count(this->list,&count); } this->list->destroy(this->list); - + pthread_mutex_destroy(&(this->mutex)); - + pthread_cond_destroy(&(this->condvar)); - - pfree(this); + + allocator_free(this); return SUCCESS; } /* - * + * * Documented in header */ event_queue_t *event_queue_create() @@ -360,23 +360,23 @@ event_queue_t *event_queue_create() { return NULL; } - - private_event_queue_t *this = alloc_thing(private_event_queue_t, "private_event_queue_t"); + + private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t, "private_event_queue_t"); if (this == NULL) { linked_list->destroy(linked_list); return NULL; } - + this->public.get_count = (status_t (*) (event_queue_t *event_queue, int *count)) 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); } |