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"
35 * @brief Represents an event as it is stored in the event queue.
37 * A event consists of a event time and an assigned job object.
40 typedef struct event_s event_t
;
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
;
99 * @brief Private Variables and Functions of event_queue_t class.
102 typedef struct private_event_queue_s private_event_queue_t
;
105 struct private_event_queue_s
{
109 event_queue_t
public;
112 * The events are stored in a linked list of type linked_list_t.
117 * Access to linked_list is locked through this mutex.
119 pthread_mutex_t mutex
;
122 * If the queue is empty or an event has not to be fired
123 * a thread has to wait.
125 * This condvar is used to wake up such a thread.
127 pthread_cond_t condvar
;
131 * Returns the difference of to timeval structs in microseconds
133 * @param end_time end time
134 * @param start_time start time
136 * @warning this function is also defined in the tester class
137 * In later improvements, this function can be added to a general
140 * @return difference in microseconds (end time - start time)
142 static long time_difference(struct timeval
*end_time
, struct timeval
*start_time
)
144 long seconds
, microseconds
;
146 seconds
= (end_time
->tv_sec
- start_time
->tv_sec
);
147 microseconds
= (end_time
->tv_usec
- start_time
->tv_usec
);
148 return ((seconds
* 1000000) + microseconds
);
153 * Implements function get_count of event_queue_t.
154 * See #event_queue_s.get_count for description.
156 static int get_count (private_event_queue_t
*this)
159 pthread_mutex_lock(&(this->mutex
));
160 count
= this->list
->get_count(this->list
);
161 pthread_mutex_unlock(&(this->mutex
));
166 * Implements function get of event_queue_t.
167 * See #event_queue_s.get for description.
169 static status_t
get(private_event_queue_t
*this, job_t
**job
)
172 timeval_t current_time
;
173 event_t
* next_event
;
176 pthread_mutex_lock(&(this->mutex
));
180 while(this->list
->get_count(this->list
) == 0)
182 /* add mutex unlock handler for cancellation, enable cancellation */
183 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock
, (void*)&(this->mutex
));
184 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
186 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
188 /* reset cancellation, remove mutex-unlock handler (without executing) */
189 pthread_setcancelstate(oldstate
, NULL
);
190 pthread_cleanup_pop(0);
193 this->list
->get_first(this->list
,(void **) &next_event
);
195 gettimeofday(¤t_time
,NULL
);
196 long difference
= time_difference(¤t_time
,&(next_event
->time
));
199 timeout
.tv_sec
= next_event
->time
.tv_sec
;
200 timeout
.tv_nsec
= next_event
->time
.tv_usec
* 1000;
202 pthread_cond_timedwait( &(this->condvar
), &(this->mutex
),&timeout
);
206 /* event available */
207 this->list
->remove_first(this->list
,(void **) &next_event
);
209 *job
= next_event
->job
;
211 next_event
->destroy(next_event
);
216 pthread_cond_signal( &(this->condvar
));
218 pthread_mutex_unlock(&(this->mutex
));
224 * Implements function add_absolute of event_queue_t.
225 * See #event_queue_s.add_absolute for description.
227 static status_t
add_absolute(private_event_queue_t
*this, job_t
*job
, timeval_t time
)
229 event_t
*event
= event_create(time
,job
);
230 event_t
*current_event
;
237 pthread_mutex_lock(&(this->mutex
));
239 /* while just used to break out */
242 if (this->list
->get_count(this->list
) == 0)
244 status
= this->list
->insert_first(this->list
,event
);
248 /* check last entry */
249 this->list
->get_last(this->list
,(void **) ¤t_event
);
251 if (time_difference(&(event
->time
), &(current_event
->time
)) >= 0)
253 /* my event has to be fired after the last event in list */
254 status
= this->list
->insert_last(this->list
,event
);
258 /* check first entry */
259 this->list
->get_first(this->list
,(void **) ¤t_event
);
261 if (time_difference(&(event
->time
), &(current_event
->time
)) < 0)
263 /* my event has to be fired before the first event in list */
264 status
= this->list
->insert_first(this->list
,event
);
268 linked_list_iterator_t
* iterator
;
270 status
= this->list
->create_iterator(this->list
,&iterator
,TRUE
);
271 if (status
!= SUCCESS
)
277 iterator
->has_next(iterator
);
278 /* first element has not to be checked (already done) */
280 while(iterator
->has_next(iterator
))
282 status
= iterator
->current(iterator
,(void **) ¤t_event
);
284 if (time_difference(&(event
->time
), &(current_event
->time
)) <= 0)
286 /* my event has to be fired before the current event in list */
287 status
= this->list
->insert_before(this->list
,iterator
,event
);
291 iterator
->destroy(iterator
);
295 pthread_cond_signal( &(this->condvar
));
296 pthread_mutex_unlock(&(this->mutex
));
298 if (status
!= SUCCESS
)
300 event
->destroy(event
);
306 * Implements function add_relative of event_queue_t.
307 * See #event_queue_s.add_relative for description.
309 static status_t
add_relative(event_queue_t
*this, job_t
*job
, u_int32_t ms
)
311 timeval_t current_time
;
313 int micros
= ms
* 1000;
315 gettimeofday(¤t_time
, NULL
);
317 time
.tv_usec
= ((current_time
.tv_usec
+ micros
) % 1000000);
318 time
.tv_sec
= current_time
.tv_sec
+ ((current_time
.tv_usec
+ micros
)/ 1000000);
320 return this->add_absolute(this, job
, time
);
325 * Implements function destroy of event_queue_t.
326 * See #event_queue_s.destroy for description.
328 static status_t
event_queue_destroy(private_event_queue_t
*this)
330 while (this->list
->get_count(this->list
) > 0)
334 if (this->list
->remove_first(this->list
,(void *) &event
) != SUCCESS
)
336 this->list
->destroy(this->list
);
339 event
->job
->destroy(event
->job
);
340 event
->destroy(event
);
342 this->list
->destroy(this->list
);
344 pthread_mutex_destroy(&(this->mutex
));
346 pthread_cond_destroy(&(this->condvar
));
348 allocator_free(this);
353 * Documented in header
355 event_queue_t
*event_queue_create()
357 linked_list_t
*linked_list
= linked_list_create();
358 if (linked_list
== NULL
)
363 private_event_queue_t
*this = allocator_alloc_thing(private_event_queue_t
);
366 linked_list
->destroy(linked_list
);
370 this->public.get_count
= (int (*) (event_queue_t
*event_queue
)) get_count
;
371 this->public.get
= (status_t (*) (event_queue_t
*event_queue
, job_t
**job
)) get
;
372 this->public.add_absolute
= (status_t (*) (event_queue_t
*event_queue
, job_t
*job
, timeval_t time
)) add_absolute
;
373 this->public.add_relative
= (status_t (*) (event_queue_t
*event_queue
, job_t
*job
, u_int32_t ms
)) add_relative
;
374 this->public.destroy
= (status_t (*) (event_queue_t
*event_queue
)) event_queue_destroy
;
376 this->list
= linked_list
;
377 pthread_mutex_init(&(this->mutex
), NULL
);
378 pthread_cond_init(&(this->condvar
), NULL
);
380 return (&this->public);