- moved queues into subfolder queues
[strongswan.git] / Source / charon / queues / event_queue.c
1 /**
2 * @file event_queue.c
3 *
4 * @brief Event-Queue based on class linked_list_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include <pthread.h>
24 #include <stdlib.h>
25
26 #include "event_queue.h"
27
28 #include "../allocator.h"
29 #include "../types.h"
30 #include "../utils/linked_list.h"
31
32
33
34 /**
35 * @brief Represents an event as it is stored in the event queue.
36 *
37 * A event consists of a event time and an assigned job object.
38 *
39 */
40 typedef struct event_s event_t;
41
42 struct event_s{
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 * @returns always SUCCESS
58 */
59 status_t (*destroy) (event_t *event);
60 };
61
62
63 /**
64 * @brief implements function destroy of event_t
65 */
66 static status_t event_destroy(event_t *event)
67 {
68 allocator_free(event);
69 return SUCCESS;
70 }
71
72 /**
73 * @brief Creates a event for a specific time
74 *
75 * @param time absolute time to fire the event
76 * @param job job to add to job-queue at specific time
77 *
78 * @returns
79 * - created event_t object
80 * - NULL if memory allocation failed
81 */
82 static event_t *event_create(timeval_t time, job_t *job)
83 {
84 event_t *this = allocator_alloc_thing(event_t);
85 if (this == NULL)
86 {
87 return this;
88 }
89
90 this->destroy = event_destroy;
91 this->time = time;
92 this->job = job;
93
94 return this;
95 }
96
97
98 /**
99 * @brief Private Variables and Functions of event_queue_t class.
100 *
101 */
102 typedef struct private_event_queue_s private_event_queue_t;
103
104
105 struct private_event_queue_s {
106 /**
107 * Public part.
108 */
109 event_queue_t public;
110
111 /**
112 * The events are stored in a linked list of type linked_list_t.
113 */
114 linked_list_t *list;
115
116 /**
117 * Access to linked_list is locked through this mutex.
118 */
119 pthread_mutex_t mutex;
120
121 /**
122 * If the queue is empty or an event has not to be fired
123 * a thread has to wait.
124 *
125 * This condvar is used to wake up such a thread.
126 */
127 pthread_cond_t condvar;
128 };
129
130 /**
131 * Returns the difference of to timeval structs in microseconds
132 *
133 * @param end_time end time
134 * @param start_time start time
135 *
136 * @warning this function is also defined in the tester class
137 * In later improvements, this function can be added to a general
138 * class type!
139 *
140 * @return difference in microseconds (end time - start time)
141 */
142 static long time_difference(struct timeval *end_time, struct timeval *start_time)
143 {
144 long seconds, microseconds;
145
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);
149 }
150
151
152 /**
153 * Implements function get_count of event_queue_t.
154 * See #event_queue_s.get_count for description.
155 */
156 static int get_count (private_event_queue_t *this)
157 {
158 int count;
159 pthread_mutex_lock(&(this->mutex));
160 count = this->list->get_count(this->list);
161 pthread_mutex_unlock(&(this->mutex));
162 return count;
163 }
164
165 /**
166 * Implements function get of event_queue_t.
167 * See #event_queue_s.get for description.
168 */
169 static status_t get(private_event_queue_t *this, job_t **job)
170 {
171 timespec_t timeout;
172 timeval_t current_time;
173 event_t * next_event;
174 int oldstate;
175
176 pthread_mutex_lock(&(this->mutex));
177
178 while (1)
179 {
180 while(this->list->get_count(this->list) == 0)
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_wait( &(this->condvar), &(this->mutex));
187
188 /* reset cancellation, remove mutex-unlock handler (without executing) */
189 pthread_setcancelstate(oldstate, NULL);
190 pthread_cleanup_pop(0);
191 }
192
193 this->list->get_first(this->list,(void **) &next_event);
194
195 gettimeofday(&current_time,NULL);
196 long difference = time_difference(&current_time,&(next_event->time));
197 if (difference <= 0)
198 {
199 timeout.tv_sec = next_event->time.tv_sec;
200 timeout.tv_nsec = next_event->time.tv_usec * 1000;
201
202 pthread_cond_timedwait( &(this->condvar), &(this->mutex),&timeout);
203 }
204 else
205 {
206 /* event available */
207 this->list->remove_first(this->list,(void **) &next_event);
208
209 *job = next_event->job;
210
211 next_event->destroy(next_event);
212 break;
213 }
214
215 }
216 pthread_cond_signal( &(this->condvar));
217
218 pthread_mutex_unlock(&(this->mutex));
219
220 return SUCCESS;
221 }
222
223 /**
224 * Implements function add_absolute of event_queue_t.
225 * See #event_queue_s.add_absolute for description.
226 */
227 static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
228 {
229 event_t *event = event_create(time,job);
230 event_t *current_event;
231 status_t status;
232
233 if (event == NULL)
234 {
235 return FAILED;
236 }
237 pthread_mutex_lock(&(this->mutex));
238
239 /* while just used to break out */
240 while(1)
241 {
242 if (this->list->get_count(this->list) == 0)
243 {
244 status = this->list->insert_first(this->list,event);
245 break;
246 }
247
248 /* check last entry */
249 this->list->get_last(this->list,(void **) &current_event);
250
251 if (time_difference(&(event->time), &(current_event->time)) >= 0)
252 {
253 /* my event has to be fired after the last event in list */
254 status = this->list->insert_last(this->list,event);
255 break;
256 }
257
258 /* check first entry */
259 this->list->get_first(this->list,(void **) &current_event);
260
261 if (time_difference(&(event->time), &(current_event->time)) < 0)
262 {
263 /* my event has to be fired before the first event in list */
264 status = this->list->insert_first(this->list,event);
265 break;
266 }
267
268 linked_list_iterator_t * iterator;
269
270 status = this->list->create_iterator(this->list,&iterator,TRUE);
271 if (status != SUCCESS)
272 {
273 break;
274 }
275
276
277 iterator->has_next(iterator);
278 /* first element has not to be checked (already done) */
279
280 while(iterator->has_next(iterator))
281 {
282 status = iterator->current(iterator,(void **) &current_event);
283
284 if (time_difference(&(event->time), &(current_event->time)) <= 0)
285 {
286 /* my event has to be fired before the current event in list */
287 status = this->list->insert_before(this->list,iterator,event);
288 break;
289 }
290 }
291 iterator->destroy(iterator);
292 break;
293 }
294
295 pthread_cond_signal( &(this->condvar));
296 pthread_mutex_unlock(&(this->mutex));
297
298 if (status != SUCCESS)
299 {
300 event->destroy(event);
301 }
302 return status;
303 }
304
305 /**
306 * Implements function add_relative of event_queue_t.
307 * See #event_queue_s.add_relative for description.
308 */
309 static status_t add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
310 {
311 timeval_t current_time;
312 timeval_t time;
313 int micros = ms * 1000;
314
315 gettimeofday(&current_time, NULL);
316
317 time.tv_usec = ((current_time.tv_usec + micros) % 1000000);
318 time.tv_sec = current_time.tv_sec + ((current_time.tv_usec + micros)/ 1000000);
319
320 return this->add_absolute(this, job, time);
321 }
322
323
324 /**
325 * Implements function destroy of event_queue_t.
326 * See #event_queue_s.destroy for description.
327 */
328 static status_t event_queue_destroy(private_event_queue_t *this)
329 {
330 while (this->list->get_count(this->list) > 0)
331 {
332 event_t *event;
333
334 if (this->list->remove_first(this->list,(void *) &event) != SUCCESS)
335 {
336 this->list->destroy(this->list);
337 break;
338 }
339 event->job->destroy(event->job);
340 event->destroy(event);
341 }
342 this->list->destroy(this->list);
343
344 pthread_mutex_destroy(&(this->mutex));
345
346 pthread_cond_destroy(&(this->condvar));
347
348 allocator_free(this);
349 return SUCCESS;
350 }
351
352 /*
353 * Documented in header
354 */
355 event_queue_t *event_queue_create()
356 {
357 linked_list_t *linked_list = linked_list_create();
358 if (linked_list == NULL)
359 {
360 return NULL;
361 }
362
363 private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t);
364 if (this == NULL)
365 {
366 linked_list->destroy(linked_list);
367 return NULL;
368 }
369
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;
375
376 this->list = linked_list;
377 pthread_mutex_init(&(this->mutex), NULL);
378 pthread_cond_init(&(this->condvar), NULL);
379
380 return (&this->public);
381 }