changed enum and structs names to _t
[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 <types.h>
29 #include <utils/allocator.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 * @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 typedef struct private_event_queue_t private_event_queue_t;
99
100 /**
101 * @brief Private Variables and Functions of event_queue_t class.
102 *
103 */
104 struct private_event_queue_t {
105 /**
106 * Public part.
107 */
108 event_queue_t public;
109
110 /**
111 * The events are stored in a linked list of type linked_list_t.
112 */
113 linked_list_t *list;
114
115 /**
116 * Access to linked_list is locked through this mutex.
117 */
118 pthread_mutex_t mutex;
119
120 /**
121 * If the queue is empty or an event has not to be fired
122 * a thread has to wait.
123 *
124 * This condvar is used to wake up such a thread.
125 */
126 pthread_cond_t condvar;
127 };
128
129 /**
130 * Returns the difference of to timeval structs in microseconds
131 *
132 * @param end_time end time
133 * @param start_time start time
134 *
135 * @warning this function is also defined in the tester class
136 * In later improvements, this function can be added to a general
137 * class type!
138 *
139 * @return difference in microseconds (end time - start time)
140 */
141 static long time_difference(struct timeval *end_time, struct timeval *start_time)
142 {
143 long seconds, microseconds;
144
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);
148 }
149
150
151 /**
152 * Implements function get_count of event_queue_t.
153 * See #event_queue_s.get_count for description.
154 */
155 static int get_count (private_event_queue_t *this)
156 {
157 int count;
158 pthread_mutex_lock(&(this->mutex));
159 count = this->list->get_count(this->list);
160 pthread_mutex_unlock(&(this->mutex));
161 return count;
162 }
163
164 /**
165 * Implements function get of event_queue_t.
166 * See #event_queue_s.get for description.
167 */
168 static status_t get(private_event_queue_t *this, job_t **job)
169 {
170 timespec_t timeout;
171 timeval_t current_time;
172 event_t * next_event;
173 int oldstate;
174
175 pthread_mutex_lock(&(this->mutex));
176
177 while (1)
178 {
179 while(this->list->get_count(this->list) == 0)
180 {
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);
184
185 pthread_cond_wait( &(this->condvar), &(this->mutex));
186
187 /* reset cancellation, remove mutex-unlock handler (without executing) */
188 pthread_setcancelstate(oldstate, NULL);
189 pthread_cleanup_pop(0);
190 }
191
192 this->list->get_first(this->list,(void **) &next_event);
193
194 gettimeofday(&current_time,NULL);
195 long difference = time_difference(&current_time,&(next_event->time));
196 if (difference <= 0)
197 {
198 timeout.tv_sec = next_event->time.tv_sec;
199 timeout.tv_nsec = next_event->time.tv_usec * 1000;
200
201 pthread_cond_timedwait( &(this->condvar), &(this->mutex),&timeout);
202 }
203 else
204 {
205 /* event available */
206 this->list->remove_first(this->list,(void **) &next_event);
207
208 *job = next_event->job;
209
210 next_event->destroy(next_event);
211 break;
212 }
213
214 }
215 pthread_cond_signal( &(this->condvar));
216
217 pthread_mutex_unlock(&(this->mutex));
218
219 return SUCCESS;
220 }
221
222 /**
223 * Implements function add_absolute of event_queue_t.
224 * See #event_queue_s.add_absolute for description.
225 */
226 static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
227 {
228 event_t *event = event_create(time,job);
229 event_t *current_event;
230 status_t status;
231
232 if (event == NULL)
233 {
234 return FAILED;
235 }
236 pthread_mutex_lock(&(this->mutex));
237
238 /* while just used to break out */
239 while(1)
240 {
241 if (this->list->get_count(this->list) == 0)
242 {
243 status = this->list->insert_first(this->list,event);
244 break;
245 }
246
247 /* check last entry */
248 this->list->get_last(this->list,(void **) &current_event);
249
250 if (time_difference(&(event->time), &(current_event->time)) >= 0)
251 {
252 /* my event has to be fired after the last event in list */
253 status = this->list->insert_last(this->list,event);
254 break;
255 }
256
257 /* check first entry */
258 this->list->get_first(this->list,(void **) &current_event);
259
260 if (time_difference(&(event->time), &(current_event->time)) < 0)
261 {
262 /* my event has to be fired before the first event in list */
263 status = this->list->insert_first(this->list,event);
264 break;
265 }
266
267 linked_list_iterator_t * iterator;
268
269 status = this->list->create_iterator(this->list,&iterator,TRUE);
270 if (status != SUCCESS)
271 {
272 break;
273 }
274
275
276 iterator->has_next(iterator);
277 /* first element has not to be checked (already done) */
278
279 while(iterator->has_next(iterator))
280 {
281 status = iterator->current(iterator,(void **) &current_event);
282
283 if (time_difference(&(event->time), &(current_event->time)) <= 0)
284 {
285 /* my event has to be fired before the current event in list */
286 status = iterator->insert_before(iterator,event);
287 break;
288 }
289 }
290 iterator->destroy(iterator);
291 break;
292 }
293
294 pthread_cond_signal( &(this->condvar));
295 pthread_mutex_unlock(&(this->mutex));
296
297 if (status != SUCCESS)
298 {
299 event->destroy(event);
300 }
301 return status;
302 }
303
304 /**
305 * Implements function add_relative of event_queue_t.
306 * See #event_queue_s.add_relative for description.
307 */
308 static status_t add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
309 {
310 timeval_t current_time;
311 timeval_t time;
312 int micros = ms * 1000;
313
314 gettimeofday(&current_time, NULL);
315
316 time.tv_usec = ((current_time.tv_usec + micros) % 1000000);
317 time.tv_sec = current_time.tv_sec + ((current_time.tv_usec + micros)/ 1000000);
318
319 return this->add_absolute(this, job, time);
320 }
321
322
323 /**
324 * Implements function destroy of event_queue_t.
325 * See #event_queue_s.destroy for description.
326 */
327 static status_t event_queue_destroy(private_event_queue_t *this)
328 {
329 while (this->list->get_count(this->list) > 0)
330 {
331 event_t *event;
332
333 if (this->list->remove_first(this->list,(void *) &event) != SUCCESS)
334 {
335 this->list->destroy(this->list);
336 break;
337 }
338 event->job->destroy_all(event->job);
339 event->destroy(event);
340 }
341 this->list->destroy(this->list);
342
343 pthread_mutex_destroy(&(this->mutex));
344
345 pthread_cond_destroy(&(this->condvar));
346
347 allocator_free(this);
348 return SUCCESS;
349 }
350
351 /*
352 * Documented in header
353 */
354 event_queue_t *event_queue_create()
355 {
356 linked_list_t *linked_list = linked_list_create();
357 if (linked_list == NULL)
358 {
359 return NULL;
360 }
361
362 private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t);
363 if (this == NULL)
364 {
365 linked_list->destroy(linked_list);
366 return NULL;
367 }
368
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;
374
375 this->list = linked_list;
376 pthread_mutex_init(&(this->mutex), NULL);
377 pthread_cond_init(&(this->condvar), NULL);
378
379 return (&this->public);
380 }