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