9a3419cb28f5d71ab075ab46dffb6c5b3acb8425
[strongswan.git] / src / charon / queues / event_queue.c
1 /**
2 * @file event_queue.c
3 *
4 * @brief Implementation of event_queue_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
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>.
17 *
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
21 * for more details.
22 */
23
24 #include <pthread.h>
25 #include <stdlib.h>
26
27 #include "event_queue.h"
28
29 #include <types.h>
30 #include <utils/linked_list.h>
31
32
33
34 typedef struct event_t event_t;
35
36 /**
37 * @brief Represents an event as it is stored in the event queue.
38 *
39 * A event consists of a event time and an assigned job object.
40 *
41 */
42 struct event_t{
43 /**
44 * Time to fire the event.
45 */
46 timeval_t time;
47
48 /**
49 * Every event has its assigned job.
50 */
51 job_t * job;
52
53 /**
54 * @brief Destroys a event_t object.
55 *
56 * @param event_t calling object
57 */
58 void (*destroy) (event_t *event);
59 };
60
61
62 /**
63 * implements event_t.destroy
64 */
65 static void event_destroy(event_t *event)
66 {
67 free(event);
68 }
69
70 /**
71 * @brief Creates a event for a specific time
72 *
73 * @param time absolute time to fire the event
74 * @param job job to add to job-queue at specific time
75 *
76 * @returns created event_t object
77 */
78 static event_t *event_create(timeval_t time, job_t *job)
79 {
80 event_t *this = malloc_thing(event_t);
81
82 this->destroy = event_destroy;
83 this->time = time;
84 this->job = job;
85
86 return this;
87 }
88
89 typedef struct private_event_queue_t private_event_queue_t;
90
91 /**
92 * Private Variables and Functions of event_queue_t class.
93 *
94 */
95 struct private_event_queue_t {
96 /**
97 * Public part.
98 */
99 event_queue_t public;
100
101 /**
102 * The events are stored in a linked list of type linked_list_t.
103 */
104 linked_list_t *list;
105
106 /**
107 * Access to linked_list is locked through this mutex.
108 */
109 pthread_mutex_t mutex;
110
111 /**
112 * If the queue is empty or an event has not to be fired
113 * a thread has to wait.
114 *
115 * This condvar is used to wake up such a thread.
116 */
117 pthread_cond_t condvar;
118 };
119
120 /**
121 * Returns the difference of to timeval structs in milliseconds
122 */
123 static long time_difference(struct timeval *end_time, struct timeval *start_time)
124 {
125 time_t s;
126 suseconds_t us;
127
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);
131 }
132
133 /**
134 * Implements event_queue_t.get_count
135 */
136 static int get_count(private_event_queue_t *this)
137 {
138 int count;
139 pthread_mutex_lock(&(this->mutex));
140 count = this->list->get_count(this->list);
141 pthread_mutex_unlock(&(this->mutex));
142 return count;
143 }
144
145 /**
146 * Implements event_queue_t.get
147 */
148 static job_t *get(private_event_queue_t *this)
149 {
150 timespec_t timeout;
151 timeval_t current_time;
152 event_t * next_event;
153 job_t *job;
154 int oldstate;
155
156 pthread_mutex_lock(&(this->mutex));
157
158 while (1)
159 {
160 while(this->list->get_count(this->list) == 0)
161 {
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);
165
166 pthread_cond_wait( &(this->condvar), &(this->mutex));
167
168 /* reset cancellation, remove mutex-unlock handler (without executing) */
169 pthread_setcancelstate(oldstate, NULL);
170 pthread_cleanup_pop(0);
171 }
172
173 this->list->get_first(this->list,(void **) &next_event);
174
175 gettimeofday(&current_time, NULL);
176 long difference = time_difference(&current_time,&(next_event->time));
177 if (difference <= 0)
178 {
179 timeout.tv_sec = next_event->time.tv_sec;
180 timeout.tv_nsec = next_event->time.tv_usec * 1000;
181
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);
185
186 pthread_cond_timedwait(&(this->condvar), &(this->mutex), &timeout);
187
188 /* reset cancellation, remove mutex-unlock handler (without executing) */
189 pthread_setcancelstate(oldstate, NULL);
190 pthread_cleanup_pop(0);
191 }
192 else
193 {
194 /* event available */
195 this->list->remove_first(this->list,(void **) &next_event);
196
197 job = next_event->job;
198
199 next_event->destroy(next_event);
200 break;
201 }
202 }
203 pthread_cond_signal( &(this->condvar));
204 pthread_mutex_unlock(&(this->mutex));
205
206 return job;
207 }
208
209 /**
210 * Implements function add_absolute of event_queue_t.
211 * See #event_queue_s.add_absolute for description.
212 */
213 static void add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
214 {
215 event_t *event = event_create(time,job);
216 event_t *current_event;
217 status_t status;
218
219 pthread_mutex_lock(&(this->mutex));
220
221 /* while just used to break out */
222 while(1)
223 {
224 if (this->list->get_count(this->list) == 0)
225 {
226 this->list->insert_first(this->list,event);
227 break;
228 }
229
230 /* check last entry */
231 this->list->get_last(this->list,(void **) &current_event);
232
233 if (time_difference(&(event->time), &(current_event->time)) >= 0)
234 {
235 /* my event has to be fired after the last event in list */
236 this->list->insert_last(this->list,event);
237 break;
238 }
239
240 /* check first entry */
241 this->list->get_first(this->list,(void **) &current_event);
242
243 if (time_difference(&(event->time), &(current_event->time)) < 0)
244 {
245 /* my event has to be fired before the first event in list */
246 this->list->insert_first(this->list,event);
247 break;
248 }
249
250 iterator_t * iterator;
251
252 iterator = this->list->create_iterator(this->list,TRUE);
253
254 iterator->has_next(iterator);
255 /* first element has not to be checked (already done) */
256
257 while(iterator->has_next(iterator))
258 {
259 status = iterator->current(iterator,(void **) &current_event);
260
261 if (time_difference(&(event->time), &(current_event->time)) <= 0)
262 {
263 /* my event has to be fired before the current event in list */
264 iterator->insert_before(iterator,event);
265 break;
266 }
267 }
268 iterator->destroy(iterator);
269 break;
270 }
271
272 pthread_cond_signal( &(this->condvar));
273 pthread_mutex_unlock(&(this->mutex));
274 }
275
276 /**
277 * Implements event_queue_t.add_relative.
278 */
279 static void add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
280 {
281 timeval_t current_time;
282 timeval_t time;
283
284 time_t s = ms / 1000;
285 suseconds_t us = (ms - s * 1000) * 1000;
286
287 gettimeofday(&current_time, NULL);
288
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;
291
292 this->add_absolute(this, job, time);
293 }
294
295
296 /**
297 * Implements event_queue_t.destroy.
298 */
299 static void event_queue_destroy(private_event_queue_t *this)
300 {
301 while (this->list->get_count(this->list) > 0)
302 {
303 event_t *event;
304
305 if (this->list->remove_first(this->list,(void *) &event) != SUCCESS)
306 {
307 this->list->destroy(this->list);
308 break;
309 }
310 event->job->destroy(event->job);
311 event->destroy(event);
312 }
313 this->list->destroy(this->list);
314
315 pthread_mutex_destroy(&(this->mutex));
316
317 pthread_cond_destroy(&(this->condvar));
318
319 free(this);
320 }
321
322 /*
323 * Documented in header
324 */
325 event_queue_t *event_queue_create()
326 {
327 private_event_queue_t *this = malloc_thing(private_event_queue_t);
328
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;
334
335 this->list = linked_list_create();
336 pthread_mutex_init(&(this->mutex), NULL);
337 pthread_cond_init(&(this->condvar), NULL);
338
339 return (&this->public);
340 }