4 * @brief Implementation of event_queue_t
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include "event_queue.h"
29 #include <utils/allocator.h>
30 #include <utils/linked_list.h>
34 typedef struct event_t event_t
;
37 * @brief Represents an event as it is stored in the event queue.
39 * A event consists of a event time and an assigned job object.
44 * Time to fire the event.
49 * Every event has its assigned job.
54 * @brief Destroys a event_t object.
56 * @param event_t calling object
58 void (*destroy
) (event_t
*event
);
63 * implements event_t.destroy
65 static void event_destroy(event_t
*event
)
67 allocator_free(event
);
71 * @brief Creates a event for a specific time
73 * @param time absolute time to fire the event
74 * @param job job to add to job-queue at specific time
76 * @returns created event_t object
78 static event_t
*event_create(timeval_t time
, job_t
*job
)
80 event_t
*this = allocator_alloc_thing(event_t
);
82 this->destroy
= event_destroy
;
90 typedef struct private_event_queue_t private_event_queue_t
;
93 * @brief Private Variables and Functions of event_queue_t class.
96 struct private_event_queue_t
{
100 event_queue_t
public;
103 * The events are stored in a linked list of type linked_list_t.
108 * Access to linked_list is locked through this mutex.
110 pthread_mutex_t mutex
;
113 * If the queue is empty or an event has not to be fired
114 * a thread has to wait.
116 * This condvar is used to wake up such a thread.
118 pthread_cond_t condvar
;
122 * Returns the difference of to timeval structs in microseconds
124 * @param end_time end time
125 * @param start_time start time
127 * @warning this function is also defined in the tester class
128 * In later improvements, this function can be added to a general
131 * @return difference in microseconds (end time - start time)
133 static long time_difference(struct timeval
*end_time
, struct timeval
*start_time
)
135 long seconds
, microseconds
;
137 seconds
= (end_time
->tv_sec
- start_time
->tv_sec
);
138 microseconds
= (end_time
->tv_usec
- start_time
->tv_usec
);
139 return ((seconds
* 1000000) + microseconds
);
144 * Implements event_queue_t.get_count
146 static int get_count (private_event_queue_t
*this)
149 pthread_mutex_lock(&(this->mutex
));
150 count
= this->list
->get_count(this->list
);
151 pthread_mutex_unlock(&(this->mutex
));
156 * Implements event_queue_t.get
158 static job_t
*get(private_event_queue_t
*this)
161 timeval_t current_time
;
162 event_t
* next_event
;
166 pthread_mutex_lock(&(this->mutex
));
170 while(this->list
->get_count(this->list
) == 0)
172 /* add mutex unlock handler for cancellation, enable cancellation */
173 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock
, (void*)&(this->mutex
));
174 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
176 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
178 /* reset cancellation, remove mutex-unlock handler (without executing) */
179 pthread_setcancelstate(oldstate
, NULL
);
180 pthread_cleanup_pop(0);
183 this->list
->get_first(this->list
,(void **) &next_event
);
185 gettimeofday(¤t_time
,NULL
);
186 long difference
= time_difference(¤t_time
,&(next_event
->time
));
189 timeout
.tv_sec
= next_event
->time
.tv_sec
;
190 timeout
.tv_nsec
= next_event
->time
.tv_usec
* 1000;
192 pthread_cond_timedwait( &(this->condvar
), &(this->mutex
),&timeout
);
196 /* event available */
197 this->list
->remove_first(this->list
,(void **) &next_event
);
199 job
= next_event
->job
;
201 next_event
->destroy(next_event
);
206 pthread_cond_signal( &(this->condvar
));
208 pthread_mutex_unlock(&(this->mutex
));
214 * Implements function add_absolute of event_queue_t.
215 * See #event_queue_s.add_absolute for description.
217 static void add_absolute(private_event_queue_t
*this, job_t
*job
, timeval_t time
)
219 event_t
*event
= event_create(time
,job
);
220 event_t
*current_event
;
223 pthread_mutex_lock(&(this->mutex
));
225 /* while just used to break out */
228 if (this->list
->get_count(this->list
) == 0)
230 this->list
->insert_first(this->list
,event
);
234 /* check last entry */
235 this->list
->get_last(this->list
,(void **) ¤t_event
);
237 if (time_difference(&(event
->time
), &(current_event
->time
)) >= 0)
239 /* my event has to be fired after the last event in list */
240 this->list
->insert_last(this->list
,event
);
244 /* check first entry */
245 this->list
->get_first(this->list
,(void **) ¤t_event
);
247 if (time_difference(&(event
->time
), &(current_event
->time
)) < 0)
249 /* my event has to be fired before the first event in list */
250 this->list
->insert_first(this->list
,event
);
254 iterator_t
* iterator
;
256 iterator
= this->list
->create_iterator(this->list
,TRUE
);
258 iterator
->has_next(iterator
);
259 /* first element has not to be checked (already done) */
261 while(iterator
->has_next(iterator
))
263 status
= iterator
->current(iterator
,(void **) ¤t_event
);
265 if (time_difference(&(event
->time
), &(current_event
->time
)) <= 0)
267 /* my event has to be fired before the current event in list */
268 iterator
->insert_before(iterator
,event
);
272 iterator
->destroy(iterator
);
276 pthread_cond_signal( &(this->condvar
));
277 pthread_mutex_unlock(&(this->mutex
));
281 * Implements event_queue_t.add_relative.
283 static void add_relative(event_queue_t
*this, job_t
*job
, u_int32_t ms
)
285 timeval_t current_time
;
287 int micros
= ms
* 1000;
289 gettimeofday(¤t_time
, NULL
);
291 time
.tv_usec
= ((current_time
.tv_usec
+ micros
) % 1000000);
292 time
.tv_sec
= current_time
.tv_sec
+ ((current_time
.tv_usec
+ micros
)/ 1000000);
294 this->add_absolute(this, job
, time
);
299 * Implements event_queue_t.destroy.
301 static void event_queue_destroy(private_event_queue_t
*this)
303 while (this->list
->get_count(this->list
) > 0)
307 if (this->list
->remove_first(this->list
,(void *) &event
) != SUCCESS
)
309 this->list
->destroy(this->list
);
312 event
->job
->destroy_all(event
->job
);
313 event
->destroy(event
);
315 this->list
->destroy(this->list
);
317 pthread_mutex_destroy(&(this->mutex
));
319 pthread_cond_destroy(&(this->condvar
));
321 allocator_free(this);
325 * Documented in header
327 event_queue_t
*event_queue_create()
329 private_event_queue_t
*this = allocator_alloc_thing(private_event_queue_t
);
331 this->public.get_count
= (int (*) (event_queue_t
*event_queue
)) get_count
;
332 this->public.get
= (job_t
*(*) (event_queue_t
*event_queue
)) get
;
333 this->public.add_absolute
= (void (*) (event_queue_t
*event_queue
, job_t
*job
, timeval_t time
)) add_absolute
;
334 this->public.add_relative
= (void (*) (event_queue_t
*event_queue
, job_t
*job
, u_int32_t ms
)) add_relative
;
335 this->public.destroy
= (void (*) (event_queue_t
*event_queue
)) event_queue_destroy
;
337 this->list
= linked_list_create();;
338 pthread_mutex_init(&(this->mutex
), NULL
);
339 pthread_cond_init(&(this->condvar
), NULL
);
341 return (&this->public);