aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/queues
diff options
context:
space:
mode:
Diffstat (limited to 'src/charon/queues')
-rw-r--r--src/charon/queues/event_queue.c75
-rw-r--r--src/charon/queues/job_queue.c18
-rw-r--r--src/charon/queues/send_queue.c47
3 files changed, 39 insertions, 101 deletions
diff --git a/src/charon/queues/event_queue.c b/src/charon/queues/event_queue.c
index 9a3419cb2..10f139e7a 100644
--- a/src/charon/queues/event_queue.c
+++ b/src/charon/queues/event_queue.c
@@ -34,12 +34,9 @@
typedef struct event_t event_t;
/**
- * @brief Represents an event as it is stored in the event queue.
- *
- * A event consists of a event time and an assigned job object.
- *
+ * Event containing a job and a schedule time
*/
-struct event_t{
+struct event_t {
/**
* Time to fire the event.
*/
@@ -49,48 +46,12 @@ struct event_t{
* Every event has its assigned job.
*/
job_t * job;
-
- /**
- * @brief Destroys a event_t object.
- *
- * @param event_t calling object
- */
- void (*destroy) (event_t *event);
};
-
-/**
- * implements event_t.destroy
- */
-static void event_destroy(event_t *event)
-{
- free(event);
-}
-
-/**
- * @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
- */
-static event_t *event_create(timeval_t time, job_t *job)
-{
- event_t *this = malloc_thing(event_t);
-
- this->destroy = event_destroy;
- this->time = time;
- this->job = job;
-
- return this;
-}
-
typedef struct private_event_queue_t private_event_queue_t;
/**
* Private Variables and Functions of event_queue_t class.
- *
*/
struct private_event_queue_t {
/**
@@ -155,7 +116,7 @@ static job_t *get(private_event_queue_t *this)
pthread_mutex_lock(&(this->mutex));
- while (1)
+ while (TRUE)
{
while(this->list->get_count(this->list) == 0)
{
@@ -170,7 +131,7 @@ static job_t *get(private_event_queue_t *this)
pthread_cleanup_pop(0);
}
- this->list->get_first(this->list,(void **) &next_event);
+ this->list->get_first(this->list, (void **)&next_event);
gettimeofday(&current_time, NULL);
long difference = time_difference(&current_time,&(next_event->time));
@@ -192,11 +153,9 @@ static job_t *get(private_event_queue_t *this)
else
{
/* event available */
- this->list->remove_first(this->list,(void **) &next_event);
-
+ this->list->remove_first(this->list, (void **)&next_event);
job = next_event->job;
-
- next_event->destroy(next_event);
+ free(next_event);
break;
}
}
@@ -212,9 +171,14 @@ static job_t *get(private_event_queue_t *this)
*/
static void add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
{
- event_t *event = event_create(time,job);
+ event_t *event;
event_t *current_event;
status_t status;
+
+ /* create event */
+ event = malloc_thing(event_t);
+ event->time = time;
+ event->job = job;
pthread_mutex_lock(&(this->mutex));
@@ -298,24 +262,15 @@ static void add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
*/
static void event_queue_destroy(private_event_queue_t *this)
{
- while (this->list->get_count(this->list) > 0)
+ event_t *event;
+ while (this->list->remove_last(this->list, (void**)&event) == SUCCESS)
{
- 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);
+ free(event);
}
this->list->destroy(this->list);
-
pthread_mutex_destroy(&(this->mutex));
-
pthread_cond_destroy(&(this->condvar));
-
free(this);
}
diff --git a/src/charon/queues/job_queue.c b/src/charon/queues/job_queue.c
index c1df42136..d33cd5ae4 100644
--- a/src/charon/queues/job_queue.c
+++ b/src/charon/queues/job_queue.c
@@ -86,14 +86,14 @@ static job_t *get(private_job_queue_t *this)
/* 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);
+ this->list->remove_first(this->list, (void **)&job);
pthread_mutex_unlock(&(this->mutex));
return job;
}
@@ -114,22 +114,14 @@ static void add(private_job_queue_t *this, job_t *job)
*/
static void job_queue_destroy (private_job_queue_t *this)
{
- while (this->list->get_count(this->list) > 0)
+ job_t *job;
+ while (this->list->remove_last(this->list, (void**)&job) == SUCCESS)
{
- 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));
-
free(this);
}
diff --git a/src/charon/queues/send_queue.c b/src/charon/queues/send_queue.c
index 1112beffb..e92f63043 100644
--- a/src/charon/queues/send_queue.c
+++ b/src/charon/queues/send_queue.c
@@ -69,9 +69,9 @@ struct private_send_queue_t {
static int get_count(private_send_queue_t *this)
{
int count;
- pthread_mutex_lock(&(this->mutex));
+ pthread_mutex_lock(&this->mutex);
count = this->list->get_count(this->list);
- pthread_mutex_unlock(&(this->mutex));
+ pthread_mutex_unlock(&this->mutex);
return count;
}
@@ -82,22 +82,23 @@ static packet_t *get(private_send_queue_t *this)
{
int oldstate;
packet_t *packet;
- pthread_mutex_lock(&(this->mutex));
- /* go to wait while no packets available */
+ 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_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&this->mutex);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
- pthread_cond_wait( &(this->condvar), &(this->mutex));
-
+ 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));
+ this->list->remove_first(this->list, (void**)&packet);
+ pthread_mutex_unlock(&this->mutex);
return packet;
}
@@ -114,10 +115,10 @@ static void add(private_send_queue_t *this, packet_t *packet)
src->get_address(src), src->get_port(src),
dst->get_address(dst), dst->get_port(dst));
- pthread_mutex_lock(&(this->mutex));
- this->list->insert_last(this->list,packet);
- pthread_cond_signal( &(this->condvar));
- pthread_mutex_unlock(&(this->mutex));
+ pthread_mutex_lock(&this->mutex);
+ this->list->insert_last(this->list, packet);
+ pthread_cond_signal(&this->condvar);
+ pthread_mutex_unlock(&this->mutex);
}
/**
@@ -125,24 +126,14 @@ static void add(private_send_queue_t *this, packet_t *packet)
*/
static void 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;
+ while (this->list->remove_last(this->list, (void**)&packet) == SUCCESS)
{
- 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));
-
free(this);
}
@@ -160,9 +151,9 @@ send_queue_t *send_queue_create(void)
this->public.destroy = (void(*)(send_queue_t*)) destroy;
this->list = linked_list_create();
- pthread_mutex_init(&(this->mutex), NULL);
- pthread_cond_init(&(this->condvar), NULL);
+ pthread_mutex_init(&this->mutex, NULL);
+ pthread_cond_init(&this->condvar, NULL);
this->logger = logger_manager->get_logger(logger_manager, SOCKET);
-
+
return (&this->public);
}