- implemented event_queue
[strongswan.git] / Source / charon / event_queue.c
1 /**
2 * @file event_queue.c
3 *
4 * @brief Event-Queue based on 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 <freeswan.h>
24 #include <pluto/constants.h>
25 #include <pluto/defs.h>
26 #include <pthread.h>
27 #include <stdlib.h>
28
29
30 #include "types.h"
31 #include "event_queue.h"
32
33
34
35 /**
36 * @brief represents an event as it is stored in the event queue
37 *
38 * A event consists of a event time and an assigned job object
39 *
40 */
41 typedef struct event_s event_t;
42
43 struct event_s{
44 /**
45 * Time to fire the event
46 */
47 timeval_t time;
48
49 /**
50 * Every event has its assigned job
51 */
52 job_t * job;
53
54 /**
55 * @brief Destroys a event_t object
56 *
57 * @param event_t calling object
58 * @returns SUCCESS if succeeded, FAILED otherwise
59 */
60 status_t (*destroy) (event_t *event);
61 };
62
63
64 /**
65 * @brief implements function destroy of event_t
66 */
67 static status_t event_destroy(event_t *event)
68 {
69 if (event == NULL)
70 {
71 return FAILED;
72 }
73 pfree(event);
74 return SUCCESS;
75 }
76
77 /**
78 * @brief Creates a event for a specific time
79 *
80 * @param time to fire the event
81 * @param job job to add to job-queue at specific time
82 *
83 * @return event_t event object
84 */
85 static event_t *event_create(timeval_t time, job_t *job)
86 {
87 event_t *this = alloc_thing(event_t, "event_t");
88
89 this->destroy = event_destroy;
90
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 class
100 *
101 */
102 typedef struct private_event_queue_s private_event_queue_t;
103
104
105 struct private_event_queue_s {
106 event_queue_t public;
107
108 /**
109 * The events are stored in a linked list
110 */
111 linked_list_t *list;
112
113 /**
114 * access to linked_list is locked through this mutex
115 */
116 pthread_mutex_t mutex;
117
118 /**
119 * If the queue is empty or an event has not to be fired
120 * a thread has to wait
121 * This condvar is used to wake up such a thread
122 */
123 pthread_cond_t condvar;
124 };
125
126 /**
127 * Returns the difference of to timeval structs in microseconds
128 *
129 * @param end_time end time
130 * @param start_time start time
131 *
132 * @warning this function is also defined in the tester class
133 * In later improvements, this function can be added to a general
134 * class type!
135 *
136 * @return difference in microseconds
137 */
138 static long time_difference(struct timeval *end_time, struct timeval *start_time)
139 {
140 long seconds, microseconds;
141
142 seconds = (end_time->tv_sec - start_time->tv_sec);
143 microseconds = (end_time->tv_usec - start_time->tv_usec);
144 return ((seconds * 1000000) + microseconds);
145 }
146
147
148 /**
149 * @brief implements function get_count of event_queue_t
150 */
151 static status_t get_count (private_event_queue_t *this, int *count)
152 {
153 pthread_mutex_lock(&(this->mutex));
154 status_t status = this->list->get_count(this->list,count);
155 pthread_mutex_unlock(&(this->mutex));
156 return status;
157 }
158
159 /**
160 * @brief implements function get of event_queue_t
161 */
162 static status_t get(private_event_queue_t *this, job_t **job)
163 {
164 timespec_t timeout;
165 timeval_t current_time;
166 event_t * next_event;
167 int count;
168
169 pthread_mutex_lock(&(this->mutex));
170
171 while (1)
172 {
173 this->list->get_count(this->list,&count);
174 while(count == 0)
175 {
176 pthread_cond_wait( &(this->condvar), &(this->mutex));
177 this->list->get_count(this->list,&count);
178 }
179
180 this->list->get_first(this->list,(void **) &next_event);
181 gettimeofday(&current_time,NULL);
182 long difference = time_difference(&current_time,&(next_event->time));
183 if (difference <= 0)
184 {
185 timeout.tv_sec = next_event->time.tv_sec;
186 timeout.tv_nsec = next_event->time.tv_usec * 1000;
187
188 pthread_cond_timedwait( &(this->condvar), &(this->mutex),&timeout);
189 }
190 else
191 {
192 /* event available */
193 this->list->remove_first(this->list,(void **) &next_event);
194
195 *job = next_event->job;
196
197 next_event->destroy(next_event);
198 break;
199 }
200
201 }
202 pthread_cond_signal( &(this->condvar));
203
204 pthread_mutex_unlock(&(this->mutex));
205
206 return SUCCESS;
207 }
208
209 /**
210 * @brief implements function add of event_queue_t
211 */
212 static status_t add(private_event_queue_t *this, job_t *job, timeval_t time)
213 {
214 event_t *event = event_create(time,job);
215 linked_list_element_t * current_list_element;
216 event_t *current_event;
217 status_t status;
218 bool has_next;
219 int count;
220
221 if (event == NULL)
222 {
223 return FAILED;
224 }
225 pthread_mutex_lock(&(this->mutex));
226
227 /* while just used to break out */
228 while(1)
229 {
230 this->list->get_count(this->list,&count);
231 if (count == 0)
232 {
233 status = this->list->insert_first(this->list,event);
234 break;
235 }
236
237 /* check last entry */
238 this->list->get_last(this->list,(void **) &current_event);
239
240 if (time_difference(&(event->time), &(current_event->time)) >= 0)
241 {
242 /* my event has to be fired after the last event in list */
243 status = this->list->insert_last(this->list,event);
244 break;
245 }
246
247 /* check first entry */
248 this->list->get_first(this->list,(void **) &current_event);
249
250 if (time_difference(&(event->time), &(current_event->time)) < 0)
251 {
252 /* my event has to be fired before the first event in list */
253 status = this->list->insert_first(this->list,event);
254 break;
255 }
256
257 linked_list_iterator_t * iterator;
258
259 status = this->list->create_iterator(this->list,&iterator,TRUE);
260 if (status != SUCCESS)
261 {
262 break;
263 }
264
265
266 status = iterator->has_next(iterator,&has_next);
267 /* first element has not to be checked (already done) */
268 status = iterator->has_next(iterator,&has_next);
269 if (status != SUCCESS)
270 {
271 break;
272 }
273
274 while(has_next)
275 {
276 status = iterator->current(iterator,&current_list_element);
277 if (status != SUCCESS)
278 {
279 break;
280 }
281 current_event = (event_t *) current_list_element->value;
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 = this->list->insert_before(this->list,current_list_element,event);
287
288 break;
289 }
290
291 iterator->has_next(iterator,&has_next);
292 if (status != SUCCESS)
293 {
294 break;
295 }
296 }
297 break;
298 }
299
300 pthread_cond_signal( &(this->condvar));
301 pthread_mutex_unlock(&(this->mutex));
302
303 if (status != SUCCESS)
304 {
305 event->destroy(event);
306 }
307 return status;
308 }
309
310
311 /**
312 * @brief implements function destroy of event_queue_t
313 */
314 static status_t event_queue_destroy(private_event_queue_t *this)
315 {
316 int count;
317 this->list->get_count(this->list,&count);
318 while (count > 0)
319 {
320 event_t *event;
321
322 if (this->list->remove_first(this->list,(void *) &event) != SUCCESS)
323 {
324 this->list->destroy(this->list);
325 break;
326 }
327 event->job->destroy(event->job);
328 event->destroy(event);
329 this->list->get_count(this->list,&count);
330 }
331 this->list->destroy(this->list);
332
333 pthread_mutex_destroy(&(this->mutex));
334
335 pthread_cond_destroy(&(this->condvar));
336
337 pfree(this);
338 return SUCCESS;
339 }
340
341 /*
342 *
343 * Documented in header
344 */
345 event_queue_t *event_queue_create()
346 {
347 linked_list_t *linked_list = linked_list_create();
348 if (linked_list == NULL)
349 {
350 return NULL;
351 }
352
353 private_event_queue_t *this = alloc_thing(private_event_queue_t, "private_event_queue_t");
354 if (this == NULL)
355 {
356 linked_list->destroy(linked_list);
357 return NULL;
358 }
359
360 this->public.get_count = (status_t (*) (event_queue_t *event_queue, int *count)) get_count;
361 this->public.get = (status_t (*) (event_queue_t *event_queue, job_t **job)) get;
362 this->public.add = (status_t (*) (event_queue_t *event_queue, job_t *job, timeval_t time)) add;
363 this->public.destroy = (status_t (*) (event_queue_t *event_queue)) event_queue_destroy;
364
365 this->list = linked_list;
366 pthread_mutex_init(&(this->mutex), NULL);
367 pthread_cond_init(&(this->condvar), NULL);
368
369 return (&this->public);
370 }