Moved data structures to new collections subfolder
[strongswan.git] / src / libstrongswan / threading / thread.c
1 /*
2 * Copyright (C) 2009-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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 #define _GNU_SOURCE
17 #include <pthread.h>
18 #include <signal.h>
19 #include <semaphore.h>
20
21 #ifdef HAVE_GETTID
22 #include <sys/types.h>
23 #include <unistd.h>
24 #endif
25
26 #ifdef HAVE_SYS_GETTID
27 #include <sys/syscall.h>
28 static inline pid_t gettid()
29 {
30 return syscall(SYS_gettid);
31 }
32 #endif
33
34 #include <library.h>
35 #include <debug.h>
36
37 #include <threading/thread_value.h>
38 #include <threading/mutex.h>
39 #include <collections/linked_list.h>
40
41 #include "thread.h"
42
43 typedef struct private_thread_t private_thread_t;
44
45 struct private_thread_t {
46 /**
47 * Public interface.
48 */
49 thread_t public;
50
51 /**
52 * Human-readable ID of this thread.
53 */
54 u_int id;
55
56 /**
57 * ID of the underlying thread.
58 */
59 pthread_t thread_id;
60
61 /**
62 * Main function of this thread (NULL for the main thread).
63 */
64 thread_main_t main;
65
66 /**
67 * Argument for the main function.
68 */
69 void *arg;
70
71 /**
72 * Stack of cleanup handlers.
73 */
74 linked_list_t *cleanup_handlers;
75
76 /**
77 * Mutex to make modifying thread properties safe.
78 */
79 mutex_t *mutex;
80
81 /**
82 * Semaphore used to sync the creation/start of the thread.
83 */
84 sem_t created;
85
86 /**
87 * TRUE if this thread has been detached or joined, i.e. can be cleaned
88 * up after terminating.
89 */
90 bool detached_or_joined;
91
92 /**
93 * TRUE if the threads has terminated (cancelled, via thread_exit or
94 * returned from the main function)
95 */
96 bool terminated;
97
98 };
99
100 typedef struct {
101 /**
102 * Cleanup callback function.
103 */
104 thread_cleanup_t cleanup;
105
106 /**
107 * Argument provided to the cleanup function.
108 */
109 void *arg;
110
111 } cleanup_handler_t;
112
113
114 /**
115 * Next thread ID.
116 */
117 static u_int next_id;
118
119 /**
120 * Mutex to safely access the next thread ID.
121 */
122 static mutex_t *id_mutex;
123
124 /**
125 * Store the thread object in a thread-specific value.
126 */
127 static thread_value_t *current_thread;
128
129
130 #ifndef HAVE_PTHREAD_CANCEL
131 /* if pthread_cancel is not available, we emulate it using a signal */
132 #define SIG_CANCEL (SIGRTMIN+7)
133
134 /* the signal handler for SIG_CANCEL uses pthread_exit to terminate the
135 * "cancelled" thread */
136 static void cancel_signal_handler(int sig)
137 {
138 pthread_exit(NULL);
139 }
140 #endif
141
142
143 /**
144 * Destroy an internal thread object.
145 *
146 * @note The mutex of this thread object has to be locked, it gets unlocked
147 * automatically.
148 */
149 static void thread_destroy(private_thread_t *this)
150 {
151 if (!this->terminated || !this->detached_or_joined)
152 {
153 this->mutex->unlock(this->mutex);
154 return;
155 }
156 this->cleanup_handlers->destroy(this->cleanup_handlers);
157 this->mutex->unlock(this->mutex);
158 this->mutex->destroy(this->mutex);
159 sem_destroy(&this->created);
160 free(this);
161 }
162
163 METHOD(thread_t, cancel, void,
164 private_thread_t *this)
165 {
166 this->mutex->lock(this->mutex);
167 if (pthread_equal(this->thread_id, pthread_self()))
168 {
169 this->mutex->unlock(this->mutex);
170 DBG1(DBG_LIB, "!!! CANNOT CANCEL CURRENT THREAD !!!");
171 return;
172 }
173 #ifdef HAVE_PTHREAD_CANCEL
174 pthread_cancel(this->thread_id);
175 #else
176 pthread_kill(this->thread_id, SIG_CANCEL);
177 #endif /* HAVE_PTHREAD_CANCEL */
178 this->mutex->unlock(this->mutex);
179 }
180
181 METHOD(thread_t, kill_, void,
182 private_thread_t *this, int sig)
183 {
184 this->mutex->lock(this->mutex);
185 if (pthread_equal(this->thread_id, pthread_self()))
186 {
187 /* it might actually be possible to send a signal to pthread_self (there
188 * is an example in raise(3) describing that), the problem is though,
189 * that the thread only returns here after the signal handler has
190 * returned, so depending on the signal, the lock might not get
191 * unlocked. */
192 this->mutex->unlock(this->mutex);
193 DBG1(DBG_LIB, "!!! CANNOT SEND SIGNAL TO CURRENT THREAD !!!");
194 return;
195 }
196 pthread_kill(this->thread_id, sig);
197 this->mutex->unlock(this->mutex);
198 }
199
200 METHOD(thread_t, detach, void,
201 private_thread_t *this)
202 {
203 this->mutex->lock(this->mutex);
204 pthread_detach(this->thread_id);
205 this->detached_or_joined = TRUE;
206 thread_destroy(this);
207 }
208
209 METHOD(thread_t, join, void*,
210 private_thread_t *this)
211 {
212 pthread_t thread_id;
213 void *val;
214
215 this->mutex->lock(this->mutex);
216 if (pthread_equal(this->thread_id, pthread_self()))
217 {
218 this->mutex->unlock(this->mutex);
219 DBG1(DBG_LIB, "!!! CANNOT JOIN CURRENT THREAD !!!");
220 return NULL;
221 }
222 if (this->detached_or_joined)
223 {
224 this->mutex->unlock(this->mutex);
225 DBG1(DBG_LIB, "!!! CANNOT JOIN DETACHED THREAD !!!");
226 return NULL;
227 }
228 thread_id = this->thread_id;
229 this->detached_or_joined = TRUE;
230 if (this->terminated)
231 {
232 /* thread has terminated before the call to join */
233 thread_destroy(this);
234 }
235 else
236 {
237 /* thread_destroy is called when the thread terminates normally */
238 this->mutex->unlock(this->mutex);
239 }
240 pthread_join(thread_id, &val);
241
242 return val;
243 }
244
245 /**
246 * Create an internal thread object.
247 */
248 static private_thread_t *thread_create_internal()
249 {
250 private_thread_t *this;
251
252 INIT(this,
253 .public = {
254 .cancel = _cancel,
255 .kill = _kill_,
256 .detach = _detach,
257 .join = _join,
258 },
259 .cleanup_handlers = linked_list_create(),
260 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
261 );
262 sem_init(&this->created, FALSE, 0);
263
264 return this;
265 }
266
267 /**
268 * Main cleanup function for threads.
269 */
270 static void thread_cleanup(private_thread_t *this)
271 {
272 cleanup_handler_t *handler;
273 this->mutex->lock(this->mutex);
274 while (this->cleanup_handlers->remove_last(this->cleanup_handlers,
275 (void**)&handler) == SUCCESS)
276 {
277 handler->cleanup(handler->arg);
278 free(handler);
279 }
280 this->terminated = TRUE;
281 thread_destroy(this);
282 }
283
284 /**
285 * Main function wrapper for threads.
286 */
287 static void *thread_main(private_thread_t *this)
288 {
289 void *res;
290
291 sem_wait(&this->created);
292 current_thread->set(current_thread, this);
293 pthread_cleanup_push((thread_cleanup_t)thread_cleanup, this);
294
295 /* TODO: this is not 100% portable as pthread_t is an opaque type (i.e.
296 * could be of any size, or even a struct) */
297 #ifdef HAVE_GETTID
298 DBG2(DBG_LIB, "created thread %.2d [%u]",
299 this->id, gettid());
300 #else
301 DBG2(DBG_LIB, "created thread %.2d [%lx]",
302 this->id, (u_long)this->thread_id);
303 #endif
304
305 res = this->main(this->arg);
306 pthread_cleanup_pop(TRUE);
307
308 return res;
309 }
310
311 /**
312 * Described in header.
313 */
314 thread_t *thread_create(thread_main_t main, void *arg)
315 {
316 private_thread_t *this = thread_create_internal();
317
318 this->main = main;
319 this->arg = arg;
320 if (pthread_create(&this->thread_id, NULL, (void*)thread_main, this) != 0)
321 {
322 DBG1(DBG_LIB, "failed to create thread!");
323 this->mutex->lock(this->mutex);
324 thread_destroy(this);
325 return NULL;
326 }
327 id_mutex->lock(id_mutex);
328 this->id = next_id++;
329 id_mutex->unlock(id_mutex);
330 sem_post(&this->created);
331
332 return &this->public;
333 }
334
335 /**
336 * Described in header.
337 */
338 thread_t *thread_current()
339 {
340 return current_thread->get(current_thread);
341 }
342
343 /**
344 * Described in header.
345 */
346 u_int thread_current_id()
347 {
348 private_thread_t *this = (private_thread_t*)thread_current();
349
350 return this ? this->id : 0;
351 }
352
353 /**
354 * Described in header.
355 */
356 void thread_cleanup_push(thread_cleanup_t cleanup, void *arg)
357 {
358 private_thread_t *this = (private_thread_t*)thread_current();
359 cleanup_handler_t *handler;
360
361 INIT(handler,
362 .cleanup = cleanup,
363 .arg = arg,
364 );
365
366 this->mutex->lock(this->mutex);
367 this->cleanup_handlers->insert_last(this->cleanup_handlers, handler);
368 this->mutex->unlock(this->mutex);
369 }
370
371 /**
372 * Described in header.
373 */
374 void thread_cleanup_pop(bool execute)
375 {
376 private_thread_t *this = (private_thread_t*)thread_current();
377 cleanup_handler_t *handler;
378
379 this->mutex->lock(this->mutex);
380 if (this->cleanup_handlers->remove_last(this->cleanup_handlers,
381 (void**)&handler) != SUCCESS)
382 {
383 this->mutex->unlock(this->mutex);
384 DBG1(DBG_LIB, "!!! THREAD CLEANUP ERROR !!!");
385 return;
386 }
387 this->mutex->unlock(this->mutex);
388
389 if (execute)
390 {
391 handler->cleanup(handler->arg);
392 }
393 free(handler);
394 }
395
396 /**
397 * Described in header.
398 */
399 bool thread_cancelability(bool enable)
400 {
401 #ifdef HAVE_PTHREAD_CANCEL
402 int old;
403
404 pthread_setcancelstate(enable ? PTHREAD_CANCEL_ENABLE
405 : PTHREAD_CANCEL_DISABLE, &old);
406
407 return old == PTHREAD_CANCEL_ENABLE;
408 #else
409 sigset_t new, old;
410
411 sigemptyset(&new);
412 sigaddset(&new, SIG_CANCEL);
413 pthread_sigmask(enable ? SIG_UNBLOCK : SIG_BLOCK, &new, &old);
414
415 return sigismember(&old, SIG_CANCEL) == 0;
416 #endif /* HAVE_PTHREAD_CANCEL */
417 }
418
419 /**
420 * Described in header.
421 */
422 void thread_cancellation_point()
423 {
424 bool old = thread_cancelability(TRUE);
425
426 #ifdef HAVE_PTHREAD_CANCEL
427 pthread_testcancel();
428 #endif /* HAVE_PTHREAD_CANCEL */
429 thread_cancelability(old);
430 }
431
432 /**
433 * Described in header.
434 */
435 void thread_exit(void *val)
436 {
437 pthread_exit(val);
438 }
439
440 /**
441 * A dummy thread value that reserved pthread_key_t value "0". A buggy PKCS#11
442 * library mangles this key, without owning it, so we allocate it for them.
443 */
444 static thread_value_t *dummy1;
445
446 /**
447 * Described in header.
448 */
449 void threads_init()
450 {
451 private_thread_t *main_thread = thread_create_internal();
452
453 dummy1 = thread_value_create(NULL);
454
455 next_id = 1;
456 main_thread->id = 0;
457 main_thread->thread_id = pthread_self();
458 current_thread = thread_value_create(NULL);
459 current_thread->set(current_thread, (void*)main_thread);
460 id_mutex = mutex_create(MUTEX_TYPE_DEFAULT);
461
462 #ifndef HAVE_PTHREAD_CANCEL
463 { /* install a signal handler for our custom SIG_CANCEL */
464 struct sigaction action = {
465 .sa_handler = cancel_signal_handler
466 };
467 sigaction(SIG_CANCEL, &action, NULL);
468 }
469 #endif /* HAVE_PTHREAD_CANCEL */
470 }
471
472 /**
473 * Described in header.
474 */
475 void threads_deinit()
476 {
477 private_thread_t *main_thread = (private_thread_t*)thread_current();
478
479 dummy1->destroy(dummy1);
480
481 main_thread->mutex->lock(main_thread->mutex);
482 thread_destroy(main_thread);
483 current_thread->destroy(current_thread);
484 id_mutex->destroy(id_mutex);
485 }