4 * @brief Implementation of event_queue_t
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include "event_queue.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
)
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 = malloc_thing(event_t
);
82 this->destroy
= event_destroy
;
89 typedef struct private_event_queue_t private_event_queue_t
;
92 * Private Variables and Functions of event_queue_t class.
95 struct private_event_queue_t
{
102 * The events are stored in a linked list of type linked_list_t.
107 * Access to linked_list is locked through this mutex.
109 pthread_mutex_t mutex
;
112 * If the queue is empty or an event has not to be fired
113 * a thread has to wait.
115 * This condvar is used to wake up such a thread.
117 pthread_cond_t condvar
;
121 * Returns the difference of to timeval structs in milliseconds
123 static long time_difference(struct timeval
*end_time
, struct timeval
*start_time
)
128 s
= (end_time
->tv_sec
- start_time
->tv_sec
);
129 us
= (end_time
->tv_usec
- start_time
->tv_usec
);
130 return ((s
* 1000) + us
/1000);
134 * Implements event_queue_t.get_count
136 static int get_count(private_event_queue_t
*this)
139 pthread_mutex_lock(&(this->mutex
));
140 count
= this->list
->get_count(this->list
);
141 pthread_mutex_unlock(&(this->mutex
));
146 * Implements event_queue_t.get
148 static job_t
*get(private_event_queue_t
*this)
151 timeval_t current_time
;
152 event_t
* next_event
;
156 pthread_mutex_lock(&(this->mutex
));
160 while(this->list
->get_count(this->list
) == 0)
162 /* add mutex unlock handler for cancellation, enable cancellation */
163 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock
, (void*)&(this->mutex
));
164 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
166 pthread_cond_wait( &(this->condvar
), &(this->mutex
));
168 /* reset cancellation, remove mutex-unlock handler (without executing) */
169 pthread_setcancelstate(oldstate
, NULL
);
170 pthread_cleanup_pop(0);
173 this->list
->get_first(this->list
,(void **) &next_event
);
175 gettimeofday(¤t_time
, NULL
);
176 long difference
= time_difference(¤t_time
,&(next_event
->time
));
179 timeout
.tv_sec
= next_event
->time
.tv_sec
;
180 timeout
.tv_nsec
= next_event
->time
.tv_usec
* 1000;
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_timedwait(&(this->condvar
), &(this->mutex
), &timeout
);
188 /* reset cancellation, remove mutex-unlock handler (without executing) */
189 pthread_setcancelstate(oldstate
, NULL
);
190 pthread_cleanup_pop(0);
194 /* event available */
195 this->list
->remove_first(this->list
,(void **) &next_event
);
197 job
= next_event
->job
;
199 next_event
->destroy(next_event
);
203 pthread_cond_signal( &(this->condvar
));
204 pthread_mutex_unlock(&(this->mutex
));
210 * Implements function add_absolute of event_queue_t.
211 * See #event_queue_s.add_absolute for description.
213 static void add_absolute(private_event_queue_t
*this, job_t
*job
, timeval_t time
)
215 event_t
*event
= event_create(time
,job
);
216 event_t
*current_event
;
219 pthread_mutex_lock(&(this->mutex
));
221 /* while just used to break out */
224 if (this->list
->get_count(this->list
) == 0)
226 this->list
->insert_first(this->list
,event
);
230 /* check last entry */
231 this->list
->get_last(this->list
,(void **) ¤t_event
);
233 if (time_difference(&(event
->time
), &(current_event
->time
)) >= 0)
235 /* my event has to be fired after the last event in list */
236 this->list
->insert_last(this->list
,event
);
240 /* check first entry */
241 this->list
->get_first(this->list
,(void **) ¤t_event
);
243 if (time_difference(&(event
->time
), &(current_event
->time
)) < 0)
245 /* my event has to be fired before the first event in list */
246 this->list
->insert_first(this->list
,event
);
250 iterator_t
* iterator
;
252 iterator
= this->list
->create_iterator(this->list
,TRUE
);
254 iterator
->has_next(iterator
);
255 /* first element has not to be checked (already done) */
257 while(iterator
->has_next(iterator
))
259 status
= iterator
->current(iterator
,(void **) ¤t_event
);
261 if (time_difference(&(event
->time
), &(current_event
->time
)) <= 0)
263 /* my event has to be fired before the current event in list */
264 iterator
->insert_before(iterator
,event
);
268 iterator
->destroy(iterator
);
272 pthread_cond_signal( &(this->condvar
));
273 pthread_mutex_unlock(&(this->mutex
));
277 * Implements event_queue_t.add_relative.
279 static void add_relative(event_queue_t
*this, job_t
*job
, u_int32_t ms
)
281 timeval_t current_time
;
284 time_t s
= ms
/ 1000;
285 suseconds_t us
= (ms
- s
* 1000) * 1000;
287 gettimeofday(¤t_time
, NULL
);
289 time
.tv_usec
= (current_time
.tv_usec
+ us
) % 1000000;
290 time
.tv_sec
= current_time
.tv_sec
+ (current_time
.tv_usec
+ us
)/1000000 + s
;
292 this->add_absolute(this, job
, time
);
297 * Implements event_queue_t.destroy.
299 static void event_queue_destroy(private_event_queue_t
*this)
301 while (this->list
->get_count(this->list
) > 0)
305 if (this->list
->remove_first(this->list
,(void *) &event
) != SUCCESS
)
307 this->list
->destroy(this->list
);
310 event
->job
->destroy(event
->job
);
311 event
->destroy(event
);
313 this->list
->destroy(this->list
);
315 pthread_mutex_destroy(&(this->mutex
));
317 pthread_cond_destroy(&(this->condvar
));
323 * Documented in header
325 event_queue_t
*event_queue_create()
327 private_event_queue_t
*this = malloc_thing(private_event_queue_t
);
329 this->public.get_count
= (int (*) (event_queue_t
*event_queue
)) get_count
;
330 this->public.get
= (job_t
*(*) (event_queue_t
*event_queue
)) get
;
331 this->public.add_absolute
= (void (*) (event_queue_t
*event_queue
, job_t
*job
, timeval_t time
)) add_absolute
;
332 this->public.add_relative
= (void (*) (event_queue_t
*event_queue
, job_t
*job
, u_int32_t ms
)) add_relative
;
333 this->public.destroy
= (void (*) (event_queue_t
*event_queue
)) event_queue_destroy
;
335 this->list
= linked_list_create();
336 pthread_mutex_init(&(this->mutex
), NULL
);
337 pthread_cond_init(&(this->condvar
), NULL
);
339 return (&this->public);