diff options
36 files changed, 211 insertions, 249 deletions
diff --git a/src/charon/bus/bus.c b/src/charon/bus/bus.c index d77797e92..524a77682 100644 --- a/src/charon/bus/bus.c +++ b/src/charon/bus/bus.c @@ -15,10 +15,12 @@ #include "bus.h" -#include <pthread.h> #include <stdint.h> #include <daemon.h> +#include <threading/thread.h> +#include <threading/thread_value.h> +#include <threading/condvar.h> #include <threading/mutex.h> ENUM(debug_names, DBG_DMN, DBG_LIB, @@ -69,14 +71,9 @@ struct private_bus_t { mutex_t *mutex; /** - * Thread local storage for a unique, simple thread ID - */ - pthread_key_t thread_id; - - /** * Thread local storage the threads IKE_SA */ - pthread_key_t thread_sa; + thread_value_t *thread_sa; }; typedef struct entry_t entry_t; @@ -132,28 +129,6 @@ static void entry_destroy(entry_t *entry) } /** - * Get a unique thread number for a calling thread. Since - * pthread_self returns large and ugly numbers, use this function - * for logging; these numbers are incremental starting at 1 - */ -static u_int get_thread_number(private_bus_t *this) -{ - static uintptr_t current_num = 0; - uintptr_t stored_num; - - stored_num = (uintptr_t)pthread_getspecific(this->thread_id); - if (stored_num == 0) - { /* first call of current thread */ - pthread_setspecific(this->thread_id, (void*)++current_num); - return current_num; - } - else - { - return stored_num; - } -} - -/** * Implementation of bus_t.add_listener. */ static void add_listener(private_bus_t *this, listener_t *listener) @@ -189,7 +164,7 @@ static void remove_listener(private_bus_t *this, listener_t *listener) typedef struct cleanup_data_t cleanup_data_t; /** - * data to remove a listener using pthread_cleanup handler + * data to remove a listener using thread_cleanup_t handler */ struct cleanup_data_t { /** bus instance */ @@ -199,7 +174,7 @@ struct cleanup_data_t { }; /** - * pthread_cleanup handler to remove a listener + * thread_cleanup_t handler to remove a listener */ static void listener_cleanup(cleanup_data_t *data) { @@ -212,7 +187,7 @@ static void listener_cleanup(cleanup_data_t *data) */ static void listen_(private_bus_t *this, listener_t *listener, job_t *job) { - int old; + bool old; cleanup_data_t data; data.this = this; @@ -221,17 +196,17 @@ static void listen_(private_bus_t *this, listener_t *listener, job_t *job) this->mutex->lock(this->mutex); this->listeners->insert_last(this->listeners, data.entry); charon->processor->queue_job(charon->processor, job); - pthread_cleanup_push((void*)this->mutex->unlock, this->mutex); - pthread_cleanup_push((void*)listener_cleanup, &data); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old); + thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex); + thread_cleanup_push((thread_cleanup_t)listener_cleanup, &data); + old = thread_cancelability(TRUE); while (data.entry->blocker) { data.entry->condvar->wait(data.entry->condvar, this->mutex); } - pthread_setcancelstate(old, NULL); - pthread_cleanup_pop(FALSE); + thread_cancelability(old); + thread_cleanup_pop(FALSE); /* unlock mutex */ - pthread_cleanup_pop(TRUE); + thread_cleanup_pop(TRUE); entry_destroy(data.entry); } @@ -240,7 +215,7 @@ static void listen_(private_bus_t *this, listener_t *listener, job_t *job) */ static void set_sa(private_bus_t *this, ike_sa_t *ike_sa) { - pthread_setspecific(this->thread_sa, ike_sa); + this->thread_sa->set(this->thread_sa, ike_sa); } /** @@ -248,7 +223,7 @@ static void set_sa(private_bus_t *this, ike_sa_t *ike_sa) */ static ike_sa_t* get_sa(private_bus_t *this) { - return pthread_getspecific(this->thread_sa); + return this->thread_sa->get(this->thread_sa); } /** @@ -311,8 +286,8 @@ static void vlog(private_bus_t *this, debug_t group, level_t level, { log_data_t data; - data.ike_sa = pthread_getspecific(this->thread_sa); - data.thread = get_thread_number(this); + data.ike_sa = this->thread_sa->get(this->thread_sa); + data.thread = thread_current_id(); data.group = group; data.level = level; data.format = format; @@ -369,7 +344,7 @@ static void alert(private_bus_t *this, alert_t alert, ...) va_list args; bool keep; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -434,7 +409,7 @@ static void child_state_change(private_bus_t *this, child_sa_t *child_sa, entry_t *entry; bool keep; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -467,7 +442,7 @@ static void message(private_bus_t *this, message_t *message, bool incoming) entry_t *entry; bool keep; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -533,7 +508,7 @@ static void child_keys(private_bus_t *this, child_sa_t *child_sa, entry_t *entry; bool keep; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -566,7 +541,7 @@ static void child_updown(private_bus_t *this, child_sa_t *child_sa, bool up) entry_t *entry; bool keep; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -599,7 +574,7 @@ static void child_rekey(private_bus_t *this, child_sa_t *old, child_sa_t *new) entry_t *entry; bool keep; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -703,7 +678,7 @@ static bool authorize(private_bus_t *this, bool final) entry_t *entry; bool keep, success = TRUE; - ike_sa = pthread_getspecific(this->thread_sa); + ike_sa = this->thread_sa->get(this->thread_sa); this->mutex->lock(this->mutex); enumerator = this->listeners->create_enumerator(this->listeners); @@ -736,6 +711,7 @@ static bool authorize(private_bus_t *this, bool final) */ static void destroy(private_bus_t *this) { + this->thread_sa->destroy(this->thread_sa); this->mutex->destroy(this->mutex); this->listeners->destroy_function(this->listeners, (void*)entry_destroy); free(this); @@ -770,8 +746,7 @@ bus_t *bus_create() this->listeners = linked_list_create(); this->mutex = mutex_create(MUTEX_TYPE_RECURSIVE); - pthread_key_create(&this->thread_id, NULL); - pthread_key_create(&this->thread_sa, NULL); + this->thread_sa = thread_value_create(NULL); return &this->public; } diff --git a/src/charon/config/backend_manager.c b/src/charon/config/backend_manager.c index 938927cd4..90ef58563 100644 --- a/src/charon/config/backend_manager.c +++ b/src/charon/config/backend_manager.c @@ -16,7 +16,6 @@ #include "backend_manager.h" #include <sys/types.h> -#include <pthread.h> #include <daemon.h> #include <utils/linked_list.h> diff --git a/src/charon/credentials/credential_manager.c b/src/charon/credentials/credential_manager.c index 1e851779c..29aefd71c 100644 --- a/src/charon/credentials/credential_manager.c +++ b/src/charon/credentials/credential_manager.c @@ -13,11 +13,10 @@ * for more details. */ -#include <pthread.h> - #include "credential_manager.h" #include <daemon.h> +#include <threading/thread_value.h> #include <threading/rwlock.h> #include <utils/linked_list.h> #include <credentials/sets/cert_cache.h> @@ -48,7 +47,7 @@ struct private_credential_manager_t { /** * thread local set of credentials, linked_list_t with credential_set_t's */ - pthread_key_t local_sets; + thread_value_t *local_sets; /** * trust relationship and certificate cache @@ -152,7 +151,7 @@ static enumerator_t *create_sets_enumerator(private_credential_manager_t *this) enumerator->public.destroy = (void*)sets_enumerator_destroy; enumerator->global = this->sets->create_enumerator(this->sets); enumerator->local = NULL; - local = pthread_getspecific(this->local_sets); + local = this->local_sets->get(this->local_sets); if (local) { enumerator->local = local->create_enumerator(local); @@ -378,11 +377,11 @@ static void add_local_set(private_credential_manager_t *this, { linked_list_t *sets; - sets = pthread_getspecific(this->local_sets); + sets = this->local_sets->get(this->local_sets); if (!sets) { /* first invocation */ sets = linked_list_create(); - pthread_setspecific(this->local_sets, sets); + this->local_sets->set(this->local_sets, sets); } sets->insert_last(sets, set); } @@ -395,7 +394,7 @@ static void remove_local_set(private_credential_manager_t *this, { linked_list_t *sets; - sets = pthread_getspecific(this->local_sets); + sets = this->local_sets->get(this->local_sets); sets->remove(sets, set, NULL); } @@ -1633,7 +1632,7 @@ static void destroy(private_credential_manager_t *this) this->cache_queue->destroy(this->cache_queue); this->sets->remove(this->sets, this->cache, NULL); this->sets->destroy(this->sets); - pthread_key_delete(this->local_sets); + this->local_sets->destroy(this->local_sets); this->cache->destroy(this->cache); this->lock->destroy(this->lock); free(this); @@ -1660,7 +1659,7 @@ credential_manager_t *credential_manager_create() this->public.destroy = (void(*)(credential_manager_t*))destroy; this->sets = linked_list_create(); - pthread_key_create(&this->local_sets, (void*)this->sets->destroy); + this->local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy); this->cache = cert_cache_create(); this->cache_queue = linked_list_create(); this->sets->insert_first(this->sets, this->cache); diff --git a/src/charon/daemon.c b/src/charon/daemon.c index d895cf499..9d6f4f1d3 100644 --- a/src/charon/daemon.c +++ b/src/charon/daemon.c @@ -23,7 +23,6 @@ #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */ #include <signal.h> #undef _POSIX_PTHREAD_SEMANTICS -#include <pthread.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> @@ -41,6 +40,7 @@ #include <library.h> #include <utils/backtrace.h> +#include <threading/thread.h> #include <selectors/traffic_selector.h> #include <config/proposal.h> @@ -64,6 +64,11 @@ struct private_daemon_t { */ sigset_t signal_set; + /** + * Reference to main thread. + */ + thread_t *main_thread; + #ifdef CAPABILITIES /** * capabilities to keep @@ -226,7 +231,7 @@ static void kill_daemon(private_daemon_t *this, char *reason) { fprintf(stderr, "killing daemon: %s\n", reason); } - if (this->public.main_thread_id == pthread_self()) + if (this->main_thread == thread_current()) { /* initialization failed, terminate daemon */ unlink(PID_FILE); @@ -235,9 +240,9 @@ static void kill_daemon(private_daemon_t *this, char *reason) else { DBG1(DBG_DMN, "sending SIGTERM to ourself"); - pthread_kill(this->public.main_thread_id, SIGTERM); + this->main_thread->kill(this->main_thread, SIGTERM); /* thread must die, since he produced a ciritcal failure and can't continue */ - pthread_exit(NULL); + thread_exit(NULL); } } @@ -530,7 +535,7 @@ static void segv_handler(int signal) { backtrace_t *backtrace; - DBG1(DBG_DMN, "thread %u received %d", pthread_self(), signal); + DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal); backtrace = backtrace_create(2); backtrace->log(backtrace, stderr); backtrace->destroy(backtrace); @@ -575,7 +580,7 @@ private_daemon_t *daemon_create(void) this->public.uid = 0; this->public.gid = 0; - this->public.main_thread_id = pthread_self(); + this->main_thread = thread_current(); #ifdef CAPABILITIES this->caps = cap_init(); keep_cap(this, CAP_NET_ADMIN); @@ -586,7 +591,6 @@ private_daemon_t *daemon_create(void) #endif /* CAPABILITIES */ /* add handler for SEGV and ILL, - * add handler for USR1 (cancellation). * INT, TERM and HUP are handled by sigwait() in run() */ action.sa_handler = segv_handler; action.sa_flags = 0; @@ -600,7 +604,7 @@ private_daemon_t *daemon_create(void) action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &action, NULL); - pthread_sigmask(SIG_SETMASK, &action.sa_mask, 0); + pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL); return this; } diff --git a/src/charon/daemon.h b/src/charon/daemon.h index 435232834..cb5946d5d 100644 --- a/src/charon/daemon.h +++ b/src/charon/daemon.h @@ -302,11 +302,6 @@ struct daemon_t { gid_t gid; /** - * The thread_id of main-thread. - */ - pthread_t main_thread_id; - - /** * Do not drop a given capability after initialization. * * Some plugins might need additional capabilites. They tell the daemon diff --git a/src/charon/kernel/kernel_interface.c b/src/charon/kernel/kernel_interface.c index 17118f6c6..99bf94e9b 100644 --- a/src/charon/kernel/kernel_interface.c +++ b/src/charon/kernel/kernel_interface.c @@ -15,8 +15,6 @@ #include "kernel_interface.h" -#include <pthread.h> - #include <daemon.h> typedef struct private_kernel_interface_t private_kernel_interface_t; diff --git a/src/charon/network/receiver.c b/src/charon/network/receiver.c index 1a33251b6..6cd99439b 100644 --- a/src/charon/network/receiver.c +++ b/src/charon/network/receiver.c @@ -17,7 +17,6 @@ #include <stdlib.h> #include <unistd.h> -#include <pthread.h> #include "receiver.h" @@ -57,11 +56,6 @@ struct private_receiver_t { callback_job_t *job; /** - * Assigned thread. - */ - pthread_t assigned_thread; - - /** * current secret to use for cookie calculation */ char secret[SECRET_LENGTH]; diff --git a/src/charon/network/sender.c b/src/charon/network/sender.c index c644f1d41..3be5861dd 100644 --- a/src/charon/network/sender.c +++ b/src/charon/network/sender.c @@ -15,13 +15,14 @@ */ #include <stdlib.h> -#include <pthread.h> #include "sender.h" #include <daemon.h> #include <network/socket.h> #include <processing/jobs/callback_job.h> +#include <threading/thread.h> +#include <threading/condvar.h> #include <threading/mutex.h> @@ -85,19 +86,19 @@ static void send_(private_sender_t *this, packet_t *packet) static job_requeue_t send_packets(private_sender_t * this) { packet_t *packet; - int oldstate; + bool oldstate; this->mutex->lock(this->mutex); while (this->list->get_count(this->list) == 0) { /* add cleanup handler, wait for packet, remove cleanup handler */ - pthread_cleanup_push((void(*)(void*))this->mutex->unlock, this->mutex); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex); + oldstate = thread_cancelability(TRUE); this->got->wait(this->got, this->mutex); - pthread_setcancelstate(oldstate, NULL); - pthread_cleanup_pop(0); + thread_cancelability(oldstate); + thread_cleanup_pop(FALSE); } this->list->remove_first(this->list, (void**)&packet); this->sent->signal(this->sent); diff --git a/src/charon/network/socket-raw.c b/src/charon/network/socket-raw.c index 18b31d637..6cc0463b2 100644 --- a/src/charon/network/socket-raw.c +++ b/src/charon/network/socket-raw.c @@ -18,7 +18,6 @@ /* for struct in6_pktinfo */ #define _GNU_SOURCE -#include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> @@ -38,6 +37,7 @@ #include "socket.h" #include <daemon.h> +#include <threading/thread.h> /* constants for packet handling */ #define IP_LEN sizeof(struct iphdr) @@ -127,8 +127,8 @@ static status_t receiver(private_socket_t *this, packet_t **packet) packet_t *pkt; struct udphdr *udp; host_t *source = NULL, *dest = NULL; - int bytes_read = 0; - int data_offset, oldstate; + int bytes_read = 0, data_offset; + bool oldstate; fd_set rfds; FD_ZERO(&rfds); @@ -144,13 +144,13 @@ static status_t receiver(private_socket_t *this, packet_t **packet) DBG2(DBG_NET, "waiting for data on raw sockets"); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); if (select(max(this->recv4, this->recv6) + 1, &rfds, NULL, NULL, NULL) <= 0) { - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); return FAILED; } - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (this->recv4 && FD_ISSET(this->recv4, &rfds)) { diff --git a/src/charon/network/socket.c b/src/charon/network/socket.c index ab276aedc..8d2b3badb 100644 --- a/src/charon/network/socket.c +++ b/src/charon/network/socket.c @@ -23,7 +23,6 @@ #define __EXTENSIONS__ #endif -#include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> @@ -45,6 +44,7 @@ #include "socket.h" #include <daemon.h> +#include <threading/thread.h> /* length of non-esp marker */ #define MARKER_LEN sizeof(u_int32_t) @@ -117,8 +117,9 @@ static status_t receiver(private_socket_t *this, packet_t **packet) chunk_t data; packet_t *pkt; host_t *source = NULL, *dest = NULL; - int bytes_read = 0; - int data_offset, oldstate; + int bytes_read = 0, data_offset; + bool oldstate; + fd_set rfds; int max_fd = 0, selected = 0; u_int16_t port = 0; @@ -144,13 +145,13 @@ static status_t receiver(private_socket_t *this, packet_t **packet) max_fd = max(max(this->ipv4, this->ipv4_natt), max(this->ipv6, this->ipv6_natt)); DBG2(DBG_NET, "waiting for data on sockets"); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0) { - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); return FAILED; } - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (FD_ISSET(this->ipv4, &rfds)) { diff --git a/src/charon/plugins/eap_radius/radius_client.c b/src/charon/plugins/eap_radius/radius_client.c index e6d68c920..a28f80571 100644 --- a/src/charon/plugins/eap_radius/radius_client.c +++ b/src/charon/plugins/eap_radius/radius_client.c @@ -21,6 +21,7 @@ #include <daemon.h> #include <utils/host.h> #include <utils/linked_list.h> +#include <threading/condvar.h> #include <threading/mutex.h> /** diff --git a/src/charon/plugins/kernel_klips/kernel_klips_ipsec.c b/src/charon/plugins/kernel_klips/kernel_klips_ipsec.c index 29a77de8b..c4adc2413 100644 --- a/src/charon/plugins/kernel_klips/kernel_klips_ipsec.c +++ b/src/charon/plugins/kernel_klips/kernel_klips_ipsec.c @@ -21,7 +21,6 @@ #include <linux/udp.h> #include <net/if.h> #include <unistd.h> -#include <pthread.h> #include <stdio.h> #include <string.h> #include <time.h> @@ -30,6 +29,7 @@ #include "kernel_klips_ipsec.h" #include <daemon.h> +#include <threading/thread.h> #include <threading/mutex.h> #include <processing/jobs/callback_job.h> #include <processing/jobs/acquire_job.h> @@ -1375,11 +1375,12 @@ static job_requeue_t receive_events(private_kernel_klips_ipsec_t *this) { unsigned char buf[PFKEY_BUFFER_SIZE]; struct sadb_msg *msg = (struct sadb_msg*)buf; - int len, oldstate; + int len; + bool oldstate; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); len = recv(this->socket_events, buf, sizeof(buf), 0); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (len < 0) { diff --git a/src/charon/plugins/kernel_netlink/kernel_netlink_ipsec.c b/src/charon/plugins/kernel_netlink/kernel_netlink_ipsec.c index c7f005a32..7fd5c560f 100644 --- a/src/charon/plugins/kernel_netlink/kernel_netlink_ipsec.c +++ b/src/charon/plugins/kernel_netlink/kernel_netlink_ipsec.c @@ -26,8 +26,8 @@ #include <linux/rtnetlink.h> #include <linux/xfrm.h> #include <linux/udp.h> -#include <pthread.h> #include <unistd.h> +#include <time.h> #include <errno.h> #include <string.h> #include <fcntl.h> @@ -36,6 +36,7 @@ #include "kernel_netlink_shared.h" #include <daemon.h> +#include <threading/thread.h> #include <threading/mutex.h> #include <utils/hashtable.h> #include <processing/jobs/callback_job.h> @@ -748,12 +749,13 @@ static job_requeue_t receive_events(private_kernel_netlink_ipsec_t *this) struct nlmsghdr *hdr = (struct nlmsghdr*)response; struct sockaddr_nl addr; socklen_t addr_len = sizeof(addr); - int len, oldstate; + int len; + bool oldstate; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); len = recvfrom(this->socket_xfrm_events, response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_len); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (len < 0) { diff --git a/src/charon/plugins/kernel_netlink/kernel_netlink_net.c b/src/charon/plugins/kernel_netlink/kernel_netlink_net.c index 4447dc096..4a9fdf69a 100644 --- a/src/charon/plugins/kernel_netlink/kernel_netlink_net.c +++ b/src/charon/plugins/kernel_netlink/kernel_netlink_net.c @@ -17,7 +17,6 @@ #include <sys/socket.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> -#include <pthread.h> #include <unistd.h> #include <errno.h> #include <net/if.h> @@ -26,6 +25,8 @@ #include "kernel_netlink_shared.h" #include <daemon.h> +#include <threading/thread.h> +#include <threading/condvar.h> #include <threading/mutex.h> #include <utils/linked_list.h> #include <processing/jobs/callback_job.h> @@ -481,12 +482,13 @@ static job_requeue_t receive_events(private_kernel_netlink_net_t *this) struct nlmsghdr *hdr = (struct nlmsghdr*)response; struct sockaddr_nl addr; socklen_t addr_len = sizeof(addr); - int len, oldstate; + int len; + bool oldstate; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); len = recvfrom(this->socket_events, response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_len); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (len < 0) { diff --git a/src/charon/plugins/kernel_pfkey/kernel_pfkey_ipsec.c b/src/charon/plugins/kernel_pfkey/kernel_pfkey_ipsec.c index 8dc03b80b..9c50746ac 100644 --- a/src/charon/plugins/kernel_pfkey/kernel_pfkey_ipsec.c +++ b/src/charon/plugins/kernel_pfkey/kernel_pfkey_ipsec.c @@ -49,13 +49,14 @@ #endif /*HAVE_NATT*/ #include <unistd.h> -#include <pthread.h> +#include <time.h> #include <errno.h> #include "kernel_pfkey_ipsec.h" #include <daemon.h> #include <utils/host.h> +#include <threading/thread.h> #include <threading/mutex.h> #include <processing/jobs/callback_job.h> #include <processing/jobs/acquire_job.h> @@ -1083,11 +1084,12 @@ static job_requeue_t receive_events(private_kernel_pfkey_ipsec_t *this) { unsigned char buf[PFKEY_BUFFER_SIZE]; struct sadb_msg *msg = (struct sadb_msg*)buf; - int len, oldstate; + int len; + bool oldstate; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (len < 0) { diff --git a/src/charon/plugins/kernel_pfroute/kernel_pfroute_net.c b/src/charon/plugins/kernel_pfroute/kernel_pfroute_net.c index 9aee53928..9f1baf5b5 100644 --- a/src/charon/plugins/kernel_pfroute/kernel_pfroute_net.c +++ b/src/charon/plugins/kernel_pfroute/kernel_pfroute_net.c @@ -19,13 +19,13 @@ #include <ifaddrs.h> #include <net/route.h> #include <unistd.h> -#include <pthread.h> #include <errno.h> #include "kernel_pfroute_net.h" #include <daemon.h> #include <utils/host.h> +#include <threading/thread.h> #include <threading/mutex.h> #include <utils/linked_list.h> #include <processing/jobs/callback_job.h> @@ -326,11 +326,12 @@ static job_requeue_t receive_events(private_kernel_pfroute_net_t *this) { unsigned char buf[PFROUTE_BUFFER_SIZE]; struct rt_msghdr *msg = (struct rt_msghdr*)buf; - int len, oldstate; + int len; + bool oldstate; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (len < 0) { diff --git a/src/charon/plugins/load_tester/load_tester_listener.c b/src/charon/plugins/load_tester/load_tester_listener.c index 540d4fa4f..96b0cf1ec 100644 --- a/src/charon/plugins/load_tester/load_tester_listener.c +++ b/src/charon/plugins/load_tester/load_tester_listener.c @@ -68,7 +68,7 @@ static bool ike_state_change(private_load_tester_listener_t *this, if (this->shutdown_on == ++this->established) { DBG1(DBG_CFG, "load-test complete, raising SIGTERM"); - pthread_kill(charon->main_thread_id, SIGTERM); + kill(0, SIGTERM); } } } diff --git a/src/charon/plugins/load_tester/load_tester_plugin.c b/src/charon/plugins/load_tester/load_tester_plugin.c index 9c6cc9974..e25f97423 100644 --- a/src/charon/plugins/load_tester/load_tester_plugin.c +++ b/src/charon/plugins/load_tester/load_tester_plugin.c @@ -24,6 +24,7 @@ #include <daemon.h> #include <processing/jobs/callback_job.h> +#include <threading/condvar.h> #include <threading/mutex.h> typedef struct private_load_tester_plugin_t private_load_tester_plugin_t; diff --git a/src/charon/plugins/smp/smp.c b/src/charon/plugins/smp/smp.c index 66e4fa6d9..21e682a0a 100644 --- a/src/charon/plugins/smp/smp.c +++ b/src/charon/plugins/smp/smp.c @@ -23,13 +23,13 @@ #include <sys/un.h> #include <unistd.h> #include <errno.h> -#include <pthread.h> #include <signal.h> #include <libxml/xmlreader.h> #include <libxml/xmlwriter.h> #include <library.h> #include <daemon.h> +#include <threading/thread.h> #include <processing/jobs/callback_job.h> @@ -361,7 +361,7 @@ static bool xml_callback(xmlTextWriterPtr writer, debug_t group, level_t level, xmlTextWriterStartElement(writer, "item"); xmlTextWriterWriteFormatAttribute(writer, "level", "%d", level); xmlTextWriterWriteFormatAttribute(writer, "source", "%N", debug_names, group); - xmlTextWriterWriteFormatAttribute(writer, "thread", "%u", pthread_self()); + xmlTextWriterWriteFormatAttribute(writer, "thread", "%u", thread_current_id()); xmlTextWriterWriteVFormatString(writer, format, args); xmlTextWriterEndElement(writer); /* </item> */ @@ -622,17 +622,18 @@ static void closefdp(int *fd) */ static job_requeue_t process(int *fdp) { - int oldstate, fd = *fdp; + int fd = *fdp; + bool oldstate; char buffer[4096]; size_t len; xmlTextReaderPtr reader; char *id = NULL, *type = NULL; - pthread_cleanup_push((void*)closefdp, (void*)&fd); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + thread_cleanup_push((thread_cleanup_t)closefdp, (void*)&fd); + oldstate = thread_cancelability(TRUE); len = read(fd, buffer, sizeof(buffer)); - pthread_setcancelstate(oldstate, NULL); - pthread_cleanup_pop(0); + thread_cancelability(oldstate); + thread_cleanup_pop(FALSE); if (len <= 0) { close(fd); @@ -682,13 +683,14 @@ static job_requeue_t process(int *fdp) static job_requeue_t dispatch(private_smp_t *this) { struct sockaddr_un strokeaddr; - int oldstate, fd, *fdp, strokeaddrlen = sizeof(strokeaddr); + int fd, *fdp, strokeaddrlen = sizeof(strokeaddr); callback_job_t *job; + bool oldstate; /* wait for connections, but allow thread to terminate */ - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); fd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (fd < 0) { diff --git a/src/charon/plugins/stroke/stroke_socket.c b/src/charon/plugins/stroke/stroke_socket.c index 6a599992e..820e097f1 100644 --- a/src/charon/plugins/stroke/stroke_socket.c +++ b/src/charon/plugins/stroke/stroke_socket.c @@ -23,11 +23,10 @@ #include <sys/fcntl.h> #include <unistd.h> #include <errno.h> -#include <pthread.h> #include <processing/jobs/callback_job.h> #include <daemon.h> -#include <threading/mutex.h> /* for Mac OS X compatible accept */ +#include <threading/thread.h> #include "stroke_config.h" #include "stroke_control.h" @@ -547,13 +546,13 @@ static job_requeue_t receive(private_stroke_socket_t *this) struct sockaddr_un strokeaddr; int strokeaddrlen = sizeof(strokeaddr); int strokefd; - int oldstate; + bool oldstate; callback_job_t *job; stroke_job_context_t *ctx; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (strokefd < 0) { diff --git a/src/charon/plugins/uci/uci_control.c b/src/charon/plugins/uci/uci_control.c index 9bfc4ecee..3c4928be4 100644 --- a/src/charon/plugins/uci/uci_control.c +++ b/src/charon/plugins/uci/uci_control.c @@ -21,11 +21,11 @@ #include <sys/types.h> #include <sys/stat.h> #include <errno.h> -#include <pthread.h> #include "uci_control.h" #include <daemon.h> +#include <threading/thread.h> #include <processing/jobs/callback_job.h> #define FIFO_FILE "/var/run/charon.fifo" @@ -237,13 +237,14 @@ static void process(private_uci_control_t *this, char *message) static job_requeue_t receive(private_uci_control_t *this) { char message[128]; - int oldstate, len; + int len; + bool oldstate; FILE *in; memset(message, 0, sizeof(message)); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + oldstate = thread_cancelability(TRUE); in = fopen(FIFO_FILE, "r"); - pthread_setcancelstate(oldstate, NULL); + thread_cancelability(oldstate); if (in) { len = fread(message, 1, sizeof(message) - 1, in); diff --git a/src/charon/processing/processor.c b/src/charon/processing/processor.c index 620c01c9c..58203afa3 100644 --- a/src/charon/processing/processor.c +++ b/src/charon/processing/processor.c @@ -15,13 +15,14 @@ */ #include <stdlib.h> -#include <pthread.h> #include <string.h> #include <errno.h> #include "processor.h" #include <daemon.h> +#include <threading/thread.h> +#include <threading/condvar.h> #include <threading/mutex.h> #include <utils/linked_list.h> @@ -80,17 +81,21 @@ static void process_jobs(private_processor_t *this); */ static void restart(private_processor_t *this) { - pthread_t thread; + thread_t *thread; /* respawn thread if required */ + this->mutex->lock(this->mutex); if (this->desired_threads == 0 || - pthread_create(&thread, NULL, (void*)process_jobs, this) != 0) + (thread = thread_create((thread_main_t)process_jobs, this)) == NULL) { - this->mutex->lock(this->mutex); this->total_threads--; this->thread_terminated->broadcast(this->thread_terminated); - this->mutex->unlock(this->mutex); } + else + { + thread->detach(thread); + } + this->mutex->unlock(this->mutex); } /** @@ -98,11 +103,11 @@ static void restart(private_processor_t *this) */ static void process_jobs(private_processor_t *this) { - int oldstate; + bool oldstate; - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); + oldstate = thread_cancelability(FALSE); - DBG2(DBG_JOB, "started worker thread, thread_ID: %06u", (int)pthread_self()); + DBG2(DBG_JOB, "started worker thread, thread_ID: %u", thread_current_id()); this->mutex->lock(this->mutex); while (this->desired_threads >= this->total_threads) @@ -119,9 +124,9 @@ static void process_jobs(private_processor_t *this) this->list->remove_first(this->list, (void**)&job); this->mutex->unlock(this->mutex); /* terminated threads are restarted, so we have a constant pool */ - pthread_cleanup_push((void*)restart, this); + thread_cleanup_push((thread_cleanup_t)restart, this); job->execute(job); - pthread_cleanup_pop(0); + thread_cleanup_pop(FALSE); this->mutex->lock(this->mutex); } this->total_threads--; @@ -185,14 +190,16 @@ static void set_threads(private_processor_t *this, u_int count) if (count > this->total_threads) { /* increase thread count */ int i; - pthread_t current; + thread_t *current; this->desired_threads = count; DBG1(DBG_JOB, "spawning %d worker threads", count - this->total_threads); for (i = this->total_threads; i < count; i++) { - if (pthread_create(¤t, NULL, (void*)process_jobs, this) == 0) + current = thread_create((thread_main_t)process_jobs, this); + if (current) { + current->detach(current); this->total_threads++; } } diff --git a/src/charon/processing/scheduler.c b/src/charon/processing/scheduler.c index 69c54900c..345af502a 100644 --- a/src/charon/processing/scheduler.c +++ b/src/charon/processing/scheduler.c @@ -16,13 +16,14 @@ */ #include <stdlib.h> -#include <pthread.h> #include "scheduler.h" #include <daemon.h> #include <processing/processor.h> #include <processing/jobs/callback_job.h> +#include <threading/thread.h> +#include <threading/condvar.h> #include <threading/mutex.h> /* the initial size of the heap */ @@ -185,8 +186,7 @@ static job_requeue_t schedule(private_scheduler_t * this) { timeval_t now; event_t *event; - int oldstate; - bool timed = FALSE; + bool timed = FALSE, oldstate; this->mutex->lock(this->mutex); @@ -215,8 +215,8 @@ static job_requeue_t schedule(private_scheduler_t * this) } timed = TRUE; } - pthread_cleanup_push((void*)this->mutex->unlock, this->mutex); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); + thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex); + oldstate = thread_cancelability(TRUE); if (timed) { @@ -227,8 +227,8 @@ static job_requeue_t schedule(private_scheduler_t * this) DBG2(DBG_JOB, "no events, waiting"); this->condvar->wait(this->condvar, this->mutex); } - pthread_setcancelstate(oldstate, NULL); - pthread_cleanup_pop(TRUE); + thread_cancelability(oldstate); + thread_cleanup_pop(TRUE); return JOB_REQUEUE_DIRECT; } diff --git a/src/charon/sa/ike_sa_manager.c b/src/charon/sa/ike_sa_manager.c index 6eb9d14bb..3ef0f3bb0 100644 --- a/src/charon/sa/ike_sa_manager.c +++ b/src/charon/sa/ike_sa_manager.c @@ -22,6 +22,7 @@ #include <daemon.h> #include <sa/ike_sa_id.h> #include <bus/bus.h> +#include <threading/condvar.h> #include <threading/mutex.h> #include <threading/rwlock.h> #include <utils/linked_list.h> diff --git a/src/dumm/cowfs.c b/src/dumm/cowfs.c index 0e83b77b1..f7b6b0cf3 100644 --- a/src/dumm/cowfs.c +++ b/src/dumm/cowfs.c @@ -29,12 +29,12 @@ #include <dirent.h> #include <errno.h> #include <sys/time.h> -#include <pthread.h> #include "cowfs.h" #include <library.h> #include <debug.h> +#include <threading/thread.h> /** define _XOPEN_SOURCE 500 fails when using libstrongswan, define popen */ extern ssize_t pread(int fd, void *buf, size_t count, off_t offset); @@ -64,7 +64,7 @@ struct private_cowfs_t { /** optional COW overlay */ int over_fd; /** thread processing FUSE */ - pthread_t thread; + thread_t *thread; }; /** @@ -792,7 +792,7 @@ static void destroy(private_cowfs_t *this) { fuse_exit(this->fuse); fuse_unmount(this->mount, this->chan); - pthread_join(this->thread, NULL); + this->thread->join(this->thread); fuse_destroy(this->fuse); free(this->mount); free(this->master); @@ -862,7 +862,8 @@ cowfs_t *cowfs_create(char *master, char *host, char *mount) this->host = strdup(host); this->over = NULL; - if (pthread_create(&this->thread, NULL, (void*)fuse_loop, this->fuse) != 0) + this->thread = thread_create((thread_main_t)fuse_loop, this->fuse); + if (!this->thread) { DBG1("creating thread to handle FUSE failed"); fuse_unmount(mount, this->chan); diff --git a/src/libfast/dispatcher.c b/src/libfast/dispatcher.c index 05c11224b..7690230d3 100644 --- a/src/libfast/dispatcher.c +++ b/src/libfast/dispatcher.c @@ -19,11 +19,12 @@ #include "session.h" #include <fcgiapp.h> -#include <pthread.h> #include <signal.h> #include <unistd.h> #include <debug.h> +#include <threading/thread.h> +#include <threading/condvar.h> #include <threading/mutex.h> #include <utils/linked_list.h> #include <utils/hashtable.h> @@ -51,7 +52,7 @@ struct private_dispatcher_t { /** * thread list */ - pthread_t *threads; + thread_t **threads; /** * number of threads in "threads" @@ -286,7 +287,7 @@ static void cleanup_sessions(private_dispatcher_t *this, time_t now) */ static void dispatch(private_dispatcher_t *this) { - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + thread_cancelability(FALSE); while (TRUE) { @@ -295,9 +296,9 @@ static void dispatch(private_dispatcher_t *this) time_t now; char *sid; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + thread_cancelability(TRUE); request = request_create(this->fd, this->debug); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + thread_cancelability(FALSE); if (request == NULL) { @@ -354,11 +355,12 @@ static void dispatch(private_dispatcher_t *this) static void run(private_dispatcher_t *this, int threads) { this->thread_count = threads; - this->threads = malloc(sizeof(pthread_t) * threads); + this->threads = malloc(sizeof(thread_t*) * threads); while (threads) { - if (pthread_create(&this->threads[threads - 1], - NULL, (void*)dispatch, this) == 0) + this->threads[threads - 1] = thread_create((thread_main_t)dispatch, + this); + if (this->threads[threads - 1]) { threads--; } @@ -393,8 +395,9 @@ static void destroy(private_dispatcher_t *this) FCGX_ShutdownPending(); while (this->thread_count--) { - pthread_cancel(this->threads[this->thread_count]); - pthread_join(this->threads[this->thread_count], NULL); + thread_t *thread = this->threads[this->thread_count]; + thread->cancel(thread); + thread->join(thread); } enumerator = this->sessions->create_enumerator(this->sessions); while (enumerator->enumerate(enumerator, &sid, &entry)) diff --git a/src/libfast/request.c b/src/libfast/request.c index 95575940e..3f4894c45 100644 --- a/src/libfast/request.c +++ b/src/libfast/request.c @@ -20,10 +20,13 @@ #include <library.h> #include <debug.h> #include <stdlib.h> -#include <string.h> #include <pthread.h> +#include <string.h> #include <ClearSilver/ClearSilver.h> +#include <threading/thread.h> +#include <threading/thread_value.h> + typedef struct private_request_t private_request_t; /** @@ -68,11 +71,10 @@ struct private_request_t { }; /** - * key to a the threads "this" request, used for ClearSilver cgiwrap callbacks. * ClearSilver cgiwrap is not threadsave, so we use a private * context for each thread. */ -static pthread_key_t this_key; +static thread_value_t *thread_this; /** * control variable for pthread_once @@ -84,7 +86,7 @@ pthread_once_t once = PTHREAD_ONCE_INIT; */ static int read_cb(void *null, char *buf, int size) { - private_request_t *this = (private_request_t*)pthread_getspecific(this_key); + private_request_t *this = (private_request_t*)thread_this->get(thread_this); return FCGX_GetStr(buf, size, this->req.in); } @@ -94,7 +96,7 @@ static int read_cb(void *null, char *buf, int size) */ static int writef_cb(void *null, const char *format, va_list args) { - private_request_t *this = (private_request_t*)pthread_getspecific(this_key); + private_request_t *this = (private_request_t*)thread_this->get(thread_this); FCGX_VFPrintF(this->req.out, format, args); return 0; @@ -104,7 +106,7 @@ static int writef_cb(void *null, const char *format, va_list args) */ static int write_cb(void *null, const char *buf, int size) { - private_request_t *this = (private_request_t*)pthread_getspecific(this_key); + private_request_t *this = (private_request_t*)thread_this->get(thread_this); return FCGX_PutStr(buf, size, this->req.out); } @@ -115,7 +117,7 @@ static int write_cb(void *null, const char *buf, int size) static char *getenv_cb(void *null, const char *key) { char *value; - private_request_t *this = (private_request_t*)pthread_getspecific(this_key); + private_request_t *this = (private_request_t*)thread_this->get(thread_this); value = FCGX_GetParam(key, this->req.envp); return value ? strdup(value) : NULL; @@ -137,7 +139,7 @@ static int iterenv_cb(void *null, int num, char **key, char **value) { *key = NULL; *value = NULL; - private_request_t *this = (private_request_t*)pthread_getspecific(this_key); + private_request_t *this = (private_request_t*)thread_this->get(thread_this); if (num < this->req_env_len) { char *eq; @@ -206,7 +208,7 @@ static char* get_query_data(private_request_t *this, char *name) */ static void add_cookie(private_request_t *this, char *name, char *value) { - pthread_setspecific(this_key, this); + thread_this->set(thread_this, this); cgi_cookie_set (this->cgi, name, value, FCGX_GetParam("SCRIPT_NAME", this->req.envp), NULL, NULL, 0, 0); @@ -280,7 +282,7 @@ static void render(private_request_t *this, char *template) { NEOERR* err; - pthread_setspecific(this_key, this); + thread_this->set(thread_this, this); err = cgi_display(this->cgi, template); if (err) { @@ -345,7 +347,7 @@ static void destroy(private_request_t *this) { if (ref_put(&this->ref)) { - pthread_setspecific(this_key, this); + thread_this->set(thread_this, this); cgi_destroy(&this->cgi); FCGX_Finish_r(&this->req); free(this); @@ -360,7 +362,7 @@ static void init(void) { cgiwrap_init_emu(NULL, read_cb, writef_cb, write_cb, getenv_cb, putenv_cb, iterenv_cb); - pthread_key_create(&this_key, NULL); + thread_this = thread_value_create(NULL); } /* @@ -372,13 +374,13 @@ request_t *request_create(int fd, bool debug) private_request_t *this = malloc_thing(private_request_t); bool failed = FALSE; - pthread_cleanup_push(free, this); + thread_cleanup_push(free, this); if (FCGX_InitRequest(&this->req, fd, 0) != 0 || FCGX_Accept_r(&this->req) != 0) { failed = TRUE; } - pthread_cleanup_pop(failed); + thread_cleanup_pop(failed); if (failed) { return NULL; @@ -404,7 +406,7 @@ request_t *request_create(int fd, bool debug) this->public.destroy = (void(*)(request_t*))destroy; pthread_once(&once, init); - pthread_setspecific(this_key, this); + thread_this->set(thread_this, this); this->ref = 1; this->closed = FALSE; diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index fd297a0b7..7ee15052c 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -43,6 +43,8 @@ credentials/ietf_attributes/ietf_attributes.c credentials/ietf_attributes/ietf_a database/database.h database/database_factory.h database/database_factory.c \ fetcher/fetcher.h fetcher/fetcher_manager.h fetcher/fetcher_manager.c \ selectors/traffic_selector.c selectors/traffic_selector.h \ +threading/thread.h threading/thread.c \ +threading/thread_value.h threading/thread_value.c \ threading/mutex.h threading/mutex.c threading/condvar.h \ threading/rwlock.h threading/rwlock.c \ threading/lock_profiler.h \ diff --git a/src/libstrongswan/asn1/asn1.c b/src/libstrongswan/asn1/asn1.c index 0b6859690..fffc18863 100644 --- a/src/libstrongswan/asn1/asn1.c +++ b/src/libstrongswan/asn1/asn1.c @@ -18,7 +18,6 @@ #include <stdio.h> #include <string.h> #include <time.h> -#include <pthread.h> #include <utils.h> #include <debug.h> diff --git a/src/libstrongswan/credentials/credential_factory.c b/src/libstrongswan/credentials/credential_factory.c index ff2393f69..5139ad504 100644 --- a/src/libstrongswan/credentials/credential_factory.c +++ b/src/libstrongswan/credentials/credential_factory.c @@ -14,12 +14,12 @@ */ #include <stdint.h> -#include <pthread.h> #include "credential_factory.h" #include <debug.h> #include <utils/linked_list.h> +#include <threading/thread_value.h> #include <threading/rwlock.h> #include <credentials/certificates/x509.h> @@ -50,7 +50,7 @@ struct private_credential_factory_t { /** * Thread specific recursiveness counter */ - pthread_key_t recursive; + thread_value_t *recursive; /** * lock access to builders @@ -121,8 +121,8 @@ static void* create(private_credential_factory_t *this, credential_type_t type, int failures = 0; uintptr_t level; - level = (uintptr_t)pthread_getspecific(this->recursive); - pthread_setspecific(this->recursive, (void*)level + 1); + level = (uintptr_t)this->recursive->get(this->recursive); + this->recursive->set(this->recursive, (void*)level + 1); this->lock->read_lock(this->lock); enumerator = this->constructors->create_enumerator(this->constructors); @@ -154,7 +154,7 @@ static void* create(private_credential_factory_t *this, credential_type_t type, DBG1("building %N - %N failed, tried %d builders", credential_type_names, type, names, subtype, failures); } - pthread_setspecific(this->recursive, (void*)level); + this->recursive->set(this->recursive, (void*)level); return construct; } @@ -164,7 +164,7 @@ static void* create(private_credential_factory_t *this, credential_type_t type, static void destroy(private_credential_factory_t *this) { this->constructors->destroy_function(this->constructors, free); - pthread_key_delete(this->recursive); + this->recursive->destroy(this->recursive); this->lock->destroy(this->lock); free(this); } @@ -182,7 +182,7 @@ credential_factory_t *credential_factory_create() this->public.destroy = (void(*)(credential_factory_t*))destroy; this->constructors = linked_list_create(); - pthread_key_create(&this->recursive, NULL); + this->recursive = thread_value_create(NULL); this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT); return &this->public; diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c index 5aeb5c038..4e8bb87d0 100644 --- a/src/libstrongswan/library.c +++ b/src/libstrongswan/library.c @@ -21,6 +21,7 @@ #include <utils.h> #include <chunk.h> #include <debug.h> +#include <threading/thread.h> #include <utils/identification.h> #include <utils/host.h> #ifdef LEAK_DETECTIVE @@ -81,6 +82,9 @@ void library_deinit() this->detective->destroy(this->detective); } #endif /* LEAK_DETECTIVE */ + + threads_deinit(); + free(this); lib = NULL; } @@ -94,6 +98,8 @@ bool library_init(char *settings) private_library_t *this = malloc_thing(private_library_t); lib = &this->public; + threads_init(); + lib->leak_detective = FALSE; #ifdef LEAK_DETECTIVE diff --git a/src/libstrongswan/plugins/mysql/mysql_database.c b/src/libstrongswan/plugins/mysql/mysql_database.c index 6c2a25b50..2338428f2 100644 --- a/src/libstrongswan/plugins/mysql/mysql_database.c +++ b/src/libstrongswan/plugins/mysql/mysql_database.c @@ -15,12 +15,12 @@ #define _GNU_SOURCE #include <string.h> -#include <pthread.h> #include <mysql.h> #include "mysql_database.h" #include <debug.h> +#include <threading/thread_value.h> #include <threading/mutex.h> #include <utils/linked_list.h> @@ -104,19 +104,20 @@ static void conn_release(conn_t *conn) { conn->in_use = FALSE; } + /** * thread specific initialization flag */ -pthread_key_t initialized; +thread_value_t *initialized; /** * Initialize a thread for mysql usage */ static void thread_initialize() { - if (pthread_getspecific(initialized) == NULL) + if (initialized->get(initialized) == NULL) { - pthread_setspecific(initialized, (void*)TRUE); + initialized->set(initialized, (void*)TRUE); mysql_thread_init(); } } @@ -130,11 +131,7 @@ bool mysql_database_init() { return FALSE; } - if (pthread_key_create(&initialized, (void*)mysql_thread_end)) - { - mysql_library_end(); - return FALSE; - } + initialized = thread_value_create((thread_cleanup_t)mysql_thread_end); return TRUE; } @@ -143,7 +140,7 @@ bool mysql_database_init() */ void mysql_database_deinit() { - pthread_key_delete(initialized); + initialized->destroy(initialized); mysql_thread_end(); /* mysql_library_end(); would be the clean way, however, it hangs... */ } diff --git a/src/libstrongswan/plugins/openssl/openssl_plugin.c b/src/libstrongswan/plugins/openssl/openssl_plugin.c index 46446fc25..548a76bb4 100644 --- a/src/libstrongswan/plugins/openssl/openssl_plugin.c +++ b/src/libstrongswan/plugins/openssl/openssl_plugin.c @@ -18,11 +18,11 @@ #include <openssl/evp.h> #include <openssl/engine.h> #include <openssl/crypto.h> -#include <pthread.h> #include "openssl_plugin.h" #include <library.h> +#include <threading/thread.h> #include <threading/mutex.h> #include "openssl_util.h" #include "openssl_crypter.h" @@ -120,7 +120,7 @@ static void destroy_function(struct CRYPTO_dynlock_value *lock, */ static unsigned long id_function(void) { - return (unsigned long)pthread_self(); + return (unsigned long)thread_current_id(); } /** diff --git a/src/libstrongswan/printf_hook.c b/src/libstrongswan/printf_hook.c index df3c7c810..0b516c99e 100644 --- a/src/libstrongswan/printf_hook.c +++ b/src/libstrongswan/printf_hook.c @@ -191,15 +191,9 @@ static void vstr_fmt_add_handler(Vstr_conf *conf, printf_hook_handler_t *handler /** * Management of thread-specific Vstr_conf objects */ -#include <pthread.h> +#include <threading/thread_value.h> -static pthread_key_t vstr_conf_key; -static pthread_once_t vstr_conf_key_once = PTHREAD_ONCE_INIT; - -static void init_vstr_conf_key(void) -{ - pthread_key_create(&vstr_conf_key, (void*)vstr_free_conf); -} +static thread_value_t *vstr_conf; static Vstr_conf *create_vstr_conf() { @@ -223,12 +217,11 @@ static Vstr_conf *create_vstr_conf() static inline Vstr_conf *get_vstr_conf() { Vstr_conf *conf; - pthread_once(&vstr_conf_key_once, init_vstr_conf_key); - conf = (Vstr_conf*)pthread_getspecific(vstr_conf_key); + conf = (Vstr_conf*)vstr_conf->get(vstr_conf); if (!conf) { conf = create_vstr_conf(); - pthread_setspecific(vstr_conf_key, conf); + vstr_conf->set(vstr_conf, conf); } return conf; } @@ -412,7 +405,7 @@ static void destroy(private_printf_hook_t *this) #ifdef USE_VSTR /* freeing the Vstr_conf of the main thread */ - pthread_key_delete(vstr_conf_key); + vstr_conf->destroy(vstr_conf); vstr_free_conf(conf); vstr_exit(); #endif @@ -438,6 +431,7 @@ printf_hook_t *printf_hook_create() free(this); return NULL; } + vstr_conf = thread_value_create((thread_cleanup_t)vstr_free_conf); #endif return &this->public; diff --git a/src/libstrongswan/threading/mutex.c b/src/libstrongswan/threading/mutex.c index 0bfe06959..9c05ff044 100644 --- a/src/libstrongswan/threading/mutex.c +++ b/src/libstrongswan/threading/mutex.c @@ -23,6 +23,7 @@ #include <library.h> #include <debug.h> +#include "condvar.h" #include "mutex.h" #include "lock_profiler.h" diff --git a/src/libstrongswan/threading/mutex.h b/src/libstrongswan/threading/mutex.h index ca242f680..ac36b6a25 100644 --- a/src/libstrongswan/threading/mutex.h +++ b/src/libstrongswan/threading/mutex.h @@ -25,35 +25,6 @@ typedef struct mutex_t mutex_t; typedef enum mutex_type_t mutex_type_t; -#include "condvar.h" - -#ifdef __APPLE__ -/* on Mac OS X 10.5 several system calls we use are no cancellation points. - * fortunately, select isn't one of them, so we wrap some of the others with - * calls to select(2). - */ -#include <sys/socket.h> -#include <sys/select.h> - -#define WRAP_WITH_SELECT(func, socket, ...)\ - fd_set rfds; FD_ZERO(&rfds); FD_SET(socket, &rfds);\ - if (select(socket + 1, &rfds, NULL, NULL, NULL) <= 0) { return -1; }\ - return func(socket, __VA_ARGS__) - -static inline int cancellable_accept(int socket, struct sockaddr *address, - socklen_t *address_len) -{ - WRAP_WITH_SELECT(accept, socket, address, address_len); -} -#define accept cancellable_accept -static inline int cancellable_recvfrom(int socket, void *buffer, size_t length, - int flags, struct sockaddr *address, socklen_t *address_len) -{ - WRAP_WITH_SELECT(recvfrom, socket, buffer, length, flags, address, address_len); -} -#define recvfrom cancellable_recvfrom -#endif /* __APPLE__ */ - /** * Type of mutex. */ |