- return value cleanup
[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 * @brief 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 pthread_cond_timedwait( &(this->condvar), &(this->mutex),&timeout);
193 }
194 else
195 {
196 /* event available */
197 this->list->remove_first(this->list,(void **) &next_event);
198
199 job = next_event->job;
200
201 next_event->destroy(next_event);
202 break;
203 }
204
205 }
206 pthread_cond_signal( &(this->condvar));
207
208 pthread_mutex_unlock(&(this->mutex));
209
210 return job;
211 }
212
213 /**
214 * Implements function add_absolute of event_queue_t.
215 * See #event_queue_s.add_absolute for description.
216 */
217 static void add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
218 {
219 event_t *event = event_create(time,job);
220 event_t *current_event;
221 status_t status;
222
223 pthread_mutex_lock(&(this->mutex));
224
225 /* while just used to break out */
226 while(1)
227 {
228 if (this->list->get_count(this->list) == 0)
229 {
230 this->list->insert_first(this->list,event);
231 break;
232 }
233
234 /* check last entry */
235 this->list->get_last(this->list,(void **) &current_event);
236
237 if (time_difference(&(event->time), &(current_event->time)) >= 0)
238 {
239 /* my event has to be fired after the last event in list */
240 this->list->insert_last(this->list,event);
241 break;
242 }
243
244 /* check first entry */
245 this->list->get_first(this->list,(void **) &current_event);
246
247 if (time_difference(&(event->time), &(current_event->time)) < 0)
248 {
249 /* my event has to be fired before the first event in list */
250 this->list->insert_first(this->list,event);
251 break;
252 }
253
254 iterator_t * iterator;
255
256 this->list->create_iterator(this->list,&iterator,TRUE);
257
258 iterator->has_next(iterator);
259 /* first element has not to be checked (already done) */
260
261 while(iterator->has_next(iterator))
262 {
263 status = iterator->current(iterator,(void **) &current_event);
264
265 if (time_difference(&(event->time), &(current_event->time)) <= 0)
266 {
267 /* my event has to be fired before the current event in list */
268 iterator->insert_before(iterator,event);
269 break;
270 }
271 }
272 iterator->destroy(iterator);
273 break;
274 }
275
276 pthread_cond_signal( &(this->condvar));
277 pthread_mutex_unlock(&(this->mutex));
278 }
279
280 /**
281 * Implements event_queue_t.add_relative.
282 */
283 static void add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
284 {
285 timeval_t current_time;
286 timeval_t time;
287 int micros = ms * 1000;
288
289 gettimeofday(&current_time, NULL);
290
291 time.tv_usec = ((current_time.tv_usec + micros) % 1000000);
292 time.tv_sec = current_time.tv_sec + ((current_time.tv_usec + micros)/ 1000000);
293
294 this->add_absolute(this, job, time);
295 }
296
297
298 /**
299 * Implements event_queue_t.destroy.
300 */
301 static void event_queue_destroy(private_event_queue_t *this)
302 {
303 while (this->list->get_count(this->list) > 0)
304 {
305 event_t *event;
306
307 if (this->list->remove_first(this->list,(void *) &event) != SUCCESS)
308 {
309 this->list->destroy(this->list);
310 break;
311 }
312 event->job->destroy_all(event->job);
313 event->destroy(event);
314 }
315 this->list->destroy(this->list);
316
317 pthread_mutex_destroy(&(this->mutex));
318
319 pthread_cond_destroy(&(this->condvar));
320
321 allocator_free(this);
322 }
323
324 /*
325 * Documented in header
326 */
327 event_queue_t *event_queue_create()
328 {
329 private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t);
330
331 this->public.get_count = (int (*) (event_queue_t *event_queue)) get_count;
332 this->public.get = (job_t *(*) (event_queue_t *event_queue)) get;
333 this->public.add_absolute = (void (*) (event_queue_t *event_queue, job_t *job, timeval_t time)) add_absolute;
334 this->public.add_relative = (void (*) (event_queue_t *event_queue, job_t *job, u_int32_t ms)) add_relative;
335 this->public.destroy = (void (*) (event_queue_t *event_queue)) event_queue_destroy;
336
337 this->list = linked_list_create();;
338 pthread_mutex_init(&(this->mutex), NULL);
339 pthread_cond_init(&(this->condvar), NULL);
340
341 return (&this->public);
342 }