4 * @brief Event-Queue based on class linked_list_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
57 * @returns always SUCCESS
59 status_t (*destroy
) (event_t
*event
);
64 * @brief implements function destroy of event_t
66 static status_t
event_destroy(event_t
*event
)
68 allocator_free(event
);
73 * @brief Creates a event for a specific time
75 * @param time absolute time to fire the event
76 * @param job job to add to job-queue at specific time
79 * - created event_t object
80 * - NULL if memory allocation failed
82 static event_t
*event_create(timeval_t time
, job_t
*job
)
84 event_t
*this = allocator_alloc_thing(event_t
);
90 this->destroy
= event_destroy
;
98 typedef struct private_event_queue_t private_event_queue_t
;
101 * @brief Private Variables and Functions of event_queue_t class.
104 struct private_event_queue_t
{
108 event_queue_t
public;
111 * The events are stored in a linked list of type linked_list_t.
116 * Access to linked_list is locked through this mutex.
118 pthread_mutex_t mutex
;
121 * If the queue is empty or an event has not to be fired
122 * a thread has to wait.
124 * This condvar is used to wake up such a thread.
126 pthread_cond_t condvar
;
130 * Returns the difference of to timeval structs in microseconds
132 * @param end_time end time
133 * @param start_time start time
135 * @warning this function is also defined in the tester class
136 * In later improvements, this function can be added to a general
139 * @return difference in microseconds (end time - start time)
141 static long time_difference(struct timeval
*end_time
, struct timeval
*start_time
)
143 long seconds
, microseconds
;
145 seconds
= (end_time
->tv_sec
- start_time
->tv_sec
);
146 microseconds
= (end_time
->tv_usec
- start_time
->tv_usec
);
147 return ((seconds
* 1000000) + microseconds
);
152 * Implements function get_count of event_queue_t.
153 * See #event_queue_s.get_count for description.
155 static int get_count (private_event_queue_t
*this)
158 pthread_mutex_lock(&(this->mutex
));
159 count
= this->list
->get_count(this->list
);
160 pthread_mutex_unlock(&(this->mutex
));
165 * Implements function get of event_queue_t.
166 * See #event_queue_s.get for description.
168 static status_t
get(private_event_queue_t
*this, job_t
**job
)
171 timeval_t current_time
;
172 event_t
* next_event
;
175 pthread_mutex_lock(&(this->mutex
));
179 while(this->list
->get_count(this->list
) == 0)
181 /* add mutex unlock handler for cancellation, enable cancellation */
182 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock
, (void*)&(this->mutex
));
183 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
185 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
187 /* reset cancellation, remove mutex-unlock handler (without executing) */
188 pthread_setcancelstate(oldstate
, NULL
);
189 pthread_cleanup_pop(0);
192 this->list
->get_first(this->list
,(void **) &next_event
);
194 gettimeofday(¤t_time
,NULL
);
195 long difference
= time_difference(¤t_time
,&(next_event
->time
));
198 timeout
.tv_sec
= next_event
->time
.tv_sec
;
199 timeout
.tv_nsec
= next_event
->time
.tv_usec
* 1000;
201 pthread_cond_timedwait( &(this->condvar
), &(this->mutex
),&timeout
);
205 /* event available */
206 this->list
->remove_first(this->list
,(void **) &next_event
);
208 *job
= next_event
->job
;
210 next_event
->destroy(next_event
);
215 pthread_cond_signal( &(this->condvar
));
217 pthread_mutex_unlock(&(this->mutex
));
223 * Implements function add_absolute of event_queue_t.
224 * See #event_queue_s.add_absolute for description.
226 static status_t
add_absolute(private_event_queue_t
*this, job_t
*job
, timeval_t time
)
228 event_t
*event
= event_create(time
,job
);
229 event_t
*current_event
;
236 pthread_mutex_lock(&(this->mutex
));
238 /* while just used to break out */
241 if (this->list
->get_count(this->list
) == 0)
243 status
= this->list
->insert_first(this->list
,event
);
247 /* check last entry */
248 this->list
->get_last(this->list
,(void **) ¤t_event
);
250 if (time_difference(&(event
->time
), &(current_event
->time
)) >= 0)
252 /* my event has to be fired after the last event in list */
253 status
= this->list
->insert_last(this->list
,event
);
257 /* check first entry */
258 this->list
->get_first(this->list
,(void **) ¤t_event
);
260 if (time_difference(&(event
->time
), &(current_event
->time
)) < 0)
262 /* my event has to be fired before the first event in list */
263 status
= this->list
->insert_first(this->list
,event
);
267 linked_list_iterator_t
* iterator
;
269 status
= this->list
->create_iterator(this->list
,&iterator
,TRUE
);
270 if (status
!= SUCCESS
)
276 iterator
->has_next(iterator
);
277 /* first element has not to be checked (already done) */
279 while(iterator
->has_next(iterator
))
281 status
= iterator
->current(iterator
,(void **) ¤t_event
);
283 if (time_difference(&(event
->time
), &(current_event
->time
)) <= 0)
285 /* my event has to be fired before the current event in list */
286 status
= iterator
->insert_before(iterator
,event
);
290 iterator
->destroy(iterator
);
294 pthread_cond_signal( &(this->condvar
));
295 pthread_mutex_unlock(&(this->mutex
));
297 if (status
!= SUCCESS
)
299 event
->destroy(event
);
305 * Implements function add_relative of event_queue_t.
306 * See #event_queue_s.add_relative for description.
308 static status_t
add_relative(event_queue_t
*this, job_t
*job
, u_int32_t ms
)
310 timeval_t current_time
;
312 int micros
= ms
* 1000;
314 gettimeofday(¤t_time
, NULL
);
316 time
.tv_usec
= ((current_time
.tv_usec
+ micros
) % 1000000);
317 time
.tv_sec
= current_time
.tv_sec
+ ((current_time
.tv_usec
+ micros
)/ 1000000);
319 return this->add_absolute(this, job
, time
);
324 * Implements function destroy of event_queue_t.
325 * See #event_queue_s.destroy for description.
327 static status_t
event_queue_destroy(private_event_queue_t
*this)
329 while (this->list
->get_count(this->list
) > 0)
333 if (this->list
->remove_first(this->list
,(void *) &event
) != SUCCESS
)
335 this->list
->destroy(this->list
);
338 event
->job
->destroy_all(event
->job
);
339 event
->destroy(event
);
341 this->list
->destroy(this->list
);
343 pthread_mutex_destroy(&(this->mutex
));
345 pthread_cond_destroy(&(this->condvar
));
347 allocator_free(this);
352 * Documented in header
354 event_queue_t
*event_queue_create()
356 linked_list_t
*linked_list
= linked_list_create();
357 if (linked_list
== NULL
)
362 private_event_queue_t
*this = allocator_alloc_thing(private_event_queue_t
);
365 linked_list
->destroy(linked_list
);
369 this->public.get_count
= (int (*) (event_queue_t
*event_queue
)) get_count
;
370 this->public.get
= (status_t (*) (event_queue_t
*event_queue
, job_t
**job
)) get
;
371 this->public.add_absolute
= (status_t (*) (event_queue_t
*event_queue
, job_t
*job
, timeval_t time
)) add_absolute
;
372 this->public.add_relative
= (status_t (*) (event_queue_t
*event_queue
, job_t
*job
, u_int32_t ms
)) add_relative
;
373 this->public.destroy
= (status_t (*) (event_queue_t
*event_queue
)) event_queue_destroy
;
375 this->list
= linked_list
;
376 pthread_mutex_init(&(this->mutex
), NULL
);
377 pthread_cond_init(&(this->condvar
), NULL
);
379 return (&this->public);