watcher: if the processor has no threads, execute the job with watcher thread
[strongswan.git] / src / libstrongswan / processing / watcher.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "watcher.h"
17
18 #include <library.h>
19 #include <threading/thread.h>
20 #include <threading/mutex.h>
21 #include <threading/condvar.h>
22 #include <collections/linked_list.h>
23 #include <processing/jobs/callback_job.h>
24
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/select.h>
28
29 typedef struct private_watcher_t private_watcher_t;
30
31 /**
32 * Private data of an watcher_t object.
33 */
34 struct private_watcher_t {
35
36 /**
37 * Public watcher_t interface.
38 */
39 watcher_t public;
40
41 /**
42 * List of registered FDs, as entry_t
43 */
44 linked_list_t *fds;
45
46 /**
47 * Lock to access FD list
48 */
49 mutex_t *mutex;
50
51 /**
52 * Condvar to signal completion of callback
53 */
54 condvar_t *condvar;
55
56 /**
57 * Notification pipe to signal watcher thread
58 */
59 int notify[2];
60 };
61
62 /**
63 * Entry for a registered file descriptor
64 */
65 typedef struct {
66 /** file descriptor */
67 int fd;
68 /** events to watch */
69 watcher_event_t events;
70 /** registered callback function */
71 watcher_cb_t cb;
72 /** user data to pass to callback */
73 void *data;
74 /** callback currently active? */
75 bool active;
76 } entry_t;
77
78 /**
79 * Data we pass on for an async notification
80 */
81 typedef struct {
82 /** file descriptor */
83 int fd;
84 /** event type */
85 watcher_event_t event;
86 /** registered callback function */
87 watcher_cb_t cb;
88 /** user data to pass to callback */
89 void *data;
90 /** keep registered? */
91 bool keep;
92 /** reference to watcher */
93 private_watcher_t *this;
94 } notify_data_t;
95
96 /**
97 * Notify watcher thread about changes
98 */
99 static void update(private_watcher_t *this)
100 {
101 char buf[1] = { 'u' };
102
103 if (this->notify[1] != -1)
104 {
105 ignore_result(write(this->notify[1], buf, sizeof(buf)));
106 }
107 }
108
109 /**
110 * Cleanup function if callback gets cancelled
111 */
112 static void unregister(notify_data_t *data)
113 {
114 /* if a thread processing a callback gets cancelled, we mark the entry
115 * as cancelled, like the callback would return FALSE. This is required
116 * to not queue this watcher again if all threads have been gone. */
117 data->keep = FALSE;
118 }
119
120 /**
121 * Execute callback of registered FD, asynchronous
122 */
123 static job_requeue_t notify_async(notify_data_t *data)
124 {
125 thread_cleanup_push((void*)unregister, data);
126 data->keep = data->cb(data->data, data->fd, data->event);
127 thread_cleanup_pop(FALSE);
128 return JOB_REQUEUE_NONE;
129 }
130
131 /**
132 * Clean up notification data, reactivate FD
133 */
134 static void notify_end(notify_data_t *data)
135 {
136 private_watcher_t *this = data->this;
137 enumerator_t *enumerator;
138 entry_t *entry;
139
140 /* reactivate the disabled entry */
141 this->mutex->lock(this->mutex);
142 enumerator = this->fds->create_enumerator(this->fds);
143 while (enumerator->enumerate(enumerator, &entry))
144 {
145 if (entry->fd == data->fd)
146 {
147 if (!data->keep)
148 {
149 entry->events &= ~data->event;
150 if (!entry->events)
151 {
152 this->fds->remove_at(this->fds, enumerator);
153 free(entry);
154 break;
155 }
156 }
157 entry->active = TRUE;
158 break;
159 }
160 }
161 enumerator->destroy(enumerator);
162
163 update(this);
164 this->condvar->broadcast(this->condvar);
165 this->mutex->unlock(this->mutex);
166
167 free(data);
168 }
169
170 /**
171 * Execute the callback for a registered FD
172 */
173 static job_t* notify(private_watcher_t *this, entry_t *entry,
174 watcher_event_t event)
175 {
176 notify_data_t *data;
177
178 /* get a copy of entry for async job, but with specific event */
179 INIT(data,
180 .fd = entry->fd,
181 .event = event,
182 .cb = entry->cb,
183 .data = entry->data,
184 .keep = TRUE,
185 .this = this,
186 );
187
188 /* deactivate entry, so we can select() other FDs even if the async
189 * processing did not handle the event yet */
190 entry->active = FALSE;
191
192 return (job_t*)callback_job_create_with_prio((void*)notify_async, data,
193 (void*)notify_end, (callback_job_cancel_t)return_false,
194 JOB_PRIO_CRITICAL);
195 }
196
197 /**
198 * Thread cancellation function for watcher thread
199 */
200 static void activate_all(private_watcher_t *this)
201 {
202 enumerator_t *enumerator;
203 entry_t *entry;
204
205 /* When the watcher thread gets cancelled, we have to reactivate any entry
206 * and signal threads in remove() to go on. */
207
208 this->mutex->lock(this->mutex);
209 enumerator = this->fds->create_enumerator(this->fds);
210 while (enumerator->enumerate(enumerator, &entry))
211 {
212 entry->active = TRUE;
213 }
214 enumerator->destroy(enumerator);
215 this->condvar->broadcast(this->condvar);
216 this->mutex->unlock(this->mutex);
217 }
218
219 /**
220 * Dispatching function
221 */
222 static job_requeue_t watch(private_watcher_t *this)
223 {
224 enumerator_t *enumerator;
225 entry_t *entry;
226 fd_set rd, wr, ex;
227 int maxfd = 0, res;
228
229 FD_ZERO(&rd);
230 FD_ZERO(&wr);
231 FD_ZERO(&ex);
232
233 this->mutex->lock(this->mutex);
234 if (this->fds->get_count(this->fds) == 0)
235 {
236 this->mutex->unlock(this->mutex);
237 return JOB_REQUEUE_NONE;
238 }
239
240 if (this->notify[0] != -1)
241 {
242 FD_SET(this->notify[0], &rd);
243 maxfd = this->notify[0];
244 }
245
246 enumerator = this->fds->create_enumerator(this->fds);
247 while (enumerator->enumerate(enumerator, &entry))
248 {
249 if (entry->active)
250 {
251 if (entry->events & WATCHER_READ)
252 {
253 FD_SET(entry->fd, &rd);
254 }
255 if (entry->events & WATCHER_WRITE)
256 {
257 FD_SET(entry->fd, &wr);
258 }
259 if (entry->events & WATCHER_EXCEPT)
260 {
261 FD_SET(entry->fd, &ex);
262 }
263 maxfd = max(maxfd, entry->fd);
264 }
265 }
266 enumerator->destroy(enumerator);
267 this->mutex->unlock(this->mutex);
268
269 while (TRUE)
270 {
271 char buf[1];
272 bool old;
273 job_t *job = NULL;
274
275 thread_cleanup_push((void*)activate_all, this);
276 old = thread_cancelability(TRUE);
277 res = select(maxfd + 1, &rd, &wr, &ex, NULL);
278 thread_cancelability(old);
279 thread_cleanup_pop(FALSE);
280 if (res > 0)
281 {
282 if (this->notify[0] != -1 && FD_ISSET(this->notify[0], &rd))
283 {
284 ignore_result(read(this->notify[0], buf, sizeof(buf)));
285 return JOB_REQUEUE_DIRECT;
286 }
287
288 this->mutex->lock(this->mutex);
289 enumerator = this->fds->create_enumerator(this->fds);
290 while (enumerator->enumerate(enumerator, &entry))
291 {
292 if (FD_ISSET(entry->fd, &rd))
293 {
294 job = notify(this, entry, WATCHER_READ);
295 break;
296 }
297 if (FD_ISSET(entry->fd, &wr))
298 {
299 job = notify(this, entry, WATCHER_WRITE);
300 break;
301 }
302 if (FD_ISSET(entry->fd, &ex))
303 {
304 job = notify(this, entry, WATCHER_EXCEPT);
305 break;
306 }
307 }
308 enumerator->destroy(enumerator);
309 this->mutex->unlock(this->mutex);
310
311 if (job)
312 {
313 if (lib->processor->get_threads(lib->processor))
314 {
315 lib->processor->queue_job(lib->processor, job);
316 }
317 else
318 {
319 job->execute(job);
320 job->destroy(job);
321 }
322 /* we temporarily disable a notified FD, rebuild FDSET */
323 return JOB_REQUEUE_DIRECT;
324 }
325 }
326 }
327 }
328
329 METHOD(watcher_t, add, void,
330 private_watcher_t *this, int fd, watcher_event_t events,
331 watcher_cb_t cb, void *data)
332 {
333 entry_t *entry;
334
335 INIT(entry,
336 .fd = fd,
337 .events = events,
338 .cb = cb,
339 .data = data,
340 .active = TRUE,
341 );
342
343 this->mutex->lock(this->mutex);
344 this->fds->insert_last(this->fds, entry);
345 if (this->fds->get_count(this->fds) == 1)
346 {
347 lib->processor->queue_job(lib->processor,
348 (job_t*)callback_job_create_with_prio((void*)watch, this,
349 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
350 }
351 else
352 {
353 update(this);
354 }
355 this->mutex->unlock(this->mutex);
356 }
357
358 METHOD(watcher_t, remove_, void,
359 private_watcher_t *this, int fd)
360 {
361 enumerator_t *enumerator;
362 entry_t *entry;
363
364 this->mutex->lock(this->mutex);
365 while (TRUE)
366 {
367 bool is_in_callback = FALSE;
368
369 enumerator = this->fds->create_enumerator(this->fds);
370 while (enumerator->enumerate(enumerator, &entry))
371 {
372 if (entry->fd == fd)
373 {
374 if (entry->active)
375 {
376 this->fds->remove_at(this->fds, enumerator);
377 free(entry);
378 }
379 else
380 {
381 is_in_callback = TRUE;
382 break;
383 }
384 }
385 }
386 enumerator->destroy(enumerator);
387 if (!is_in_callback)
388 {
389 break;
390 }
391 this->condvar->wait(this->condvar, this->mutex);
392 }
393
394 update(this);
395 this->mutex->unlock(this->mutex);
396 }
397
398 METHOD(watcher_t, destroy, void,
399 private_watcher_t *this)
400 {
401 this->mutex->destroy(this->mutex);
402 this->condvar->destroy(this->condvar);
403 this->fds->destroy(this->fds);
404 if (this->notify[0] != -1)
405 {
406 close(this->notify[0]);
407 }
408 if (this->notify[1] != -1)
409 {
410 close(this->notify[1]);
411 }
412 free(this);
413 }
414
415 /**
416 * See header
417 */
418 watcher_t *watcher_create()
419 {
420 private_watcher_t *this;
421
422 INIT(this,
423 .public = {
424 .add = _add,
425 .remove = _remove_,
426 .destroy = _destroy,
427 },
428 .fds = linked_list_create(),
429 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
430 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
431 .notify[0] = -1,
432 .notify[1] = -1,
433 );
434
435 if (pipe(this->notify) != 0)
436 {
437 DBG1(DBG_LIB, "creating watcher notify pipe failed: %s",
438 strerror(errno));
439 }
440 return &this->public;
441 }