From ecbe89613c7c0421f8f09267ad057dcbc71b087c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 3 Jun 2014 17:04:10 +0200 Subject: Create threads in detached state --- src/fastd.c | 16 +++++++++++----- src/fastd.h | 3 +++ src/poll.c | 1 - src/resolve.c | 5 +---- src/verify.c | 5 +---- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/fastd.c b/src/fastd.c index 1841c42..b5cc758 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -40,7 +40,6 @@ #include #include -#include #include #include #include @@ -539,6 +538,15 @@ static inline void init(int argc, char *argv[]) { ctx.next_maintenance = fastd_in_seconds(MAINTENANCE_INTERVAL); ctx.unknown_handshakes[0].timeout = ctx.now; + VECTOR_ALLOC(ctx.eth_addrs, 0); + VECTOR_ALLOC(ctx.peers, 0); + VECTOR_ALLOC(ctx.async_pids, 0); + + if (pthread_attr_init(&ctx.detached_thread)) + exit_errno("pthread_attr_init"); + if (pthread_attr_setdetachstate(&ctx.detached_thread, PTHREAD_CREATE_DETACHED)) + exit_errno("pthread_attr_setdetachstate"); + pr_info("fastd " FASTD_VERSION " starting"); fastd_cap_init(); @@ -558,10 +566,6 @@ static inline void init(int argc, char *argv[]) { set_groups(); write_pid(); - VECTOR_ALLOC(ctx.eth_addrs, 0); - VECTOR_ALLOC(ctx.peers, 0); - VECTOR_ALLOC(ctx.async_pids, 0); - fastd_peer_hashtable_init(); #ifdef ENABLE_SYSTEMD @@ -681,6 +685,8 @@ static inline void cleanup(void) { fastd_peer_hashtable_free(); + pthread_attr_destroy(&ctx.detached_thread); + VECTOR_FREE(ctx.async_pids); VECTOR_FREE(ctx.peers); VECTOR_FREE(ctx.eth_addrs); diff --git a/src/fastd.h b/src/fastd.h index f69f2f8..19660db 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -258,6 +259,8 @@ struct fastd_context { int async_rfd; /**< The read side of the pipe used to send data from other thread to the main thread */ int async_wfd; /**< The write side of the pipe used to send data from other thread to the main thread */ + pthread_attr_t detached_thread; /**< pthread_attr_t for creating detached threads */ + int tunfd; /**< The file descriptor of the tunnel interface */ size_t n_socks; /**< The number of sockets in socks */ diff --git a/src/poll.c b/src/poll.c index 3756162..db3e27e 100644 --- a/src/poll.c +++ b/src/poll.c @@ -34,7 +34,6 @@ #include "async.h" #include "peer.h" -#include #include diff --git a/src/resolve.c b/src/resolve.c index b37cef6..1020253 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -34,7 +34,6 @@ #include "async.h" #include -#include /** The argument given to the resolver thread */ @@ -134,7 +133,7 @@ void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote) { arg->constraints = remote->config->address; pthread_t thread; - if ((errno = pthread_create(&thread, NULL, resolve_peer, arg)) != 0) { + if ((errno = pthread_create(&thread, &ctx.detached_thread, resolve_peer, arg)) != 0) { pr_error_errno("unable to create resolver thread"); free(arg->hostname); @@ -142,6 +141,4 @@ void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote) { return; } - - pthread_detach(thread); } diff --git a/src/verify.c b/src/verify.c index 4cf0c67..d875a34 100644 --- a/src/verify.c +++ b/src/verify.c @@ -38,8 +38,6 @@ #include "async.h" #include "shell.h" -#include - /** Calls the on-verify command and returns the result @@ -120,7 +118,7 @@ fastd_tristate_t fastd_verify_peer(fastd_peer_t *peer, fastd_socket_t *sock, con memcpy(arg->ret.protocol_data, data, data_len); pthread_t thread; - if ((errno = pthread_create(&thread, NULL, do_verify_thread, arg)) != 0) { + if ((errno = pthread_create(&thread, &ctx.detached_thread, do_verify_thread, arg)) != 0) { pr_error_errno("unable to create verify thread"); fastd_shell_env_free(env); @@ -129,7 +127,6 @@ fastd_tristate_t fastd_verify_peer(fastd_peer_t *peer, fastd_socket_t *sock, con return fastd_tristate_false; } - pthread_detach(thread); return fastd_tristate_undef; } } -- cgit v1.2.3