Using the thread wrapper in charon, libstrongswan and their plugins.

This commit is contained in:
Tobias Brunner
2009-12-23 17:03:41 +01:00
parent c48eea9203
commit 4a5a5dd290
36 changed files with 211 additions and 249 deletions
+26 -51
View File
@@ -15,10 +15,12 @@
#include "bus.h" #include "bus.h"
#include <pthread.h>
#include <stdint.h> #include <stdint.h>
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <threading/thread_value.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
ENUM(debug_names, DBG_DMN, DBG_LIB, ENUM(debug_names, DBG_DMN, DBG_LIB,
@@ -68,15 +70,10 @@ struct private_bus_t {
*/ */
mutex_t *mutex; mutex_t *mutex;
/**
* Thread local storage for a unique, simple thread ID
*/
pthread_key_t thread_id;
/** /**
* Thread local storage the threads IKE_SA * Thread local storage the threads IKE_SA
*/ */
pthread_key_t thread_sa; thread_value_t *thread_sa;
}; };
typedef struct entry_t entry_t; typedef struct entry_t entry_t;
@@ -131,28 +128,6 @@ static void entry_destroy(entry_t *entry)
free(entry); free(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. * Implementation of bus_t.add_listener.
*/ */
@@ -189,7 +164,7 @@ static void remove_listener(private_bus_t *this, listener_t *listener)
typedef struct cleanup_data_t cleanup_data_t; 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 { struct cleanup_data_t {
/** bus instance */ /** 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) 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) static void listen_(private_bus_t *this, listener_t *listener, job_t *job)
{ {
int old; bool old;
cleanup_data_t data; cleanup_data_t data;
data.this = this; 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->mutex->lock(this->mutex);
this->listeners->insert_last(this->listeners, data.entry); this->listeners->insert_last(this->listeners, data.entry);
charon->processor->queue_job(charon->processor, job); charon->processor->queue_job(charon->processor, job);
pthread_cleanup_push((void*)this->mutex->unlock, this->mutex); thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
pthread_cleanup_push((void*)listener_cleanup, &data); thread_cleanup_push((thread_cleanup_t)listener_cleanup, &data);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old); old = thread_cancelability(TRUE);
while (data.entry->blocker) while (data.entry->blocker)
{ {
data.entry->condvar->wait(data.entry->condvar, this->mutex); data.entry->condvar->wait(data.entry->condvar, this->mutex);
} }
pthread_setcancelstate(old, NULL); thread_cancelability(old);
pthread_cleanup_pop(FALSE); thread_cleanup_pop(FALSE);
/* unlock mutex */ /* unlock mutex */
pthread_cleanup_pop(TRUE); thread_cleanup_pop(TRUE);
entry_destroy(data.entry); 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) 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) 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; log_data_t data;
data.ike_sa = pthread_getspecific(this->thread_sa); data.ike_sa = this->thread_sa->get(this->thread_sa);
data.thread = get_thread_number(this); data.thread = thread_current_id();
data.group = group; data.group = group;
data.level = level; data.level = level;
data.format = format; data.format = format;
@@ -369,7 +344,7 @@ static void alert(private_bus_t *this, alert_t alert, ...)
va_list args; va_list args;
bool keep; bool keep;
ike_sa = pthread_getspecific(this->thread_sa); ike_sa = this->thread_sa->get(this->thread_sa);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); 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; entry_t *entry;
bool keep; bool keep;
ike_sa = pthread_getspecific(this->thread_sa); ike_sa = this->thread_sa->get(this->thread_sa);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); 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; entry_t *entry;
bool keep; bool keep;
ike_sa = pthread_getspecific(this->thread_sa); ike_sa = this->thread_sa->get(this->thread_sa);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); 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; entry_t *entry;
bool keep; bool keep;
ike_sa = pthread_getspecific(this->thread_sa); ike_sa = this->thread_sa->get(this->thread_sa);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); 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; entry_t *entry;
bool keep; bool keep;
ike_sa = pthread_getspecific(this->thread_sa); ike_sa = this->thread_sa->get(this->thread_sa);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); 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; entry_t *entry;
bool keep; bool keep;
ike_sa = pthread_getspecific(this->thread_sa); ike_sa = this->thread_sa->get(this->thread_sa);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); enumerator = this->listeners->create_enumerator(this->listeners);
@@ -703,7 +678,7 @@ static bool authorize(private_bus_t *this, bool final)
entry_t *entry; entry_t *entry;
bool keep, success = TRUE; 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); this->mutex->lock(this->mutex);
enumerator = this->listeners->create_enumerator(this->listeners); 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) static void destroy(private_bus_t *this)
{ {
this->thread_sa->destroy(this->thread_sa);
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
this->listeners->destroy_function(this->listeners, (void*)entry_destroy); this->listeners->destroy_function(this->listeners, (void*)entry_destroy);
free(this); free(this);
@@ -770,8 +746,7 @@ bus_t *bus_create()
this->listeners = linked_list_create(); this->listeners = linked_list_create();
this->mutex = mutex_create(MUTEX_TYPE_RECURSIVE); this->mutex = mutex_create(MUTEX_TYPE_RECURSIVE);
pthread_key_create(&this->thread_id, NULL); this->thread_sa = thread_value_create(NULL);
pthread_key_create(&this->thread_sa, NULL);
return &this->public; return &this->public;
} }
-1
View File
@@ -16,7 +16,6 @@
#include "backend_manager.h" #include "backend_manager.h"
#include <sys/types.h> #include <sys/types.h>
#include <pthread.h>
#include <daemon.h> #include <daemon.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
+8 -9
View File
@@ -13,11 +13,10 @@
* for more details. * for more details.
*/ */
#include <pthread.h>
#include "credential_manager.h" #include "credential_manager.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread_value.h>
#include <threading/rwlock.h> #include <threading/rwlock.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
#include <credentials/sets/cert_cache.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 * 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 * 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->public.destroy = (void*)sets_enumerator_destroy;
enumerator->global = this->sets->create_enumerator(this->sets); enumerator->global = this->sets->create_enumerator(this->sets);
enumerator->local = NULL; enumerator->local = NULL;
local = pthread_getspecific(this->local_sets); local = this->local_sets->get(this->local_sets);
if (local) if (local)
{ {
enumerator->local = local->create_enumerator(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; linked_list_t *sets;
sets = pthread_getspecific(this->local_sets); sets = this->local_sets->get(this->local_sets);
if (!sets) if (!sets)
{ /* first invocation */ { /* first invocation */
sets = linked_list_create(); sets = linked_list_create();
pthread_setspecific(this->local_sets, sets); this->local_sets->set(this->local_sets, sets);
} }
sets->insert_last(sets, set); sets->insert_last(sets, set);
} }
@@ -395,7 +394,7 @@ static void remove_local_set(private_credential_manager_t *this,
{ {
linked_list_t *sets; linked_list_t *sets;
sets = pthread_getspecific(this->local_sets); sets = this->local_sets->get(this->local_sets);
sets->remove(sets, set, NULL); 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->cache_queue->destroy(this->cache_queue);
this->sets->remove(this->sets, this->cache, NULL); this->sets->remove(this->sets, this->cache, NULL);
this->sets->destroy(this->sets); this->sets->destroy(this->sets);
pthread_key_delete(this->local_sets); this->local_sets->destroy(this->local_sets);
this->cache->destroy(this->cache); this->cache->destroy(this->cache);
this->lock->destroy(this->lock); this->lock->destroy(this->lock);
free(this); free(this);
@@ -1660,7 +1659,7 @@ credential_manager_t *credential_manager_create()
this->public.destroy = (void(*)(credential_manager_t*))destroy; this->public.destroy = (void(*)(credential_manager_t*))destroy;
this->sets = linked_list_create(); 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 = cert_cache_create();
this->cache_queue = linked_list_create(); this->cache_queue = linked_list_create();
this->sets->insert_first(this->sets, this->cache); this->sets->insert_first(this->sets, this->cache);
+12 -8
View File
@@ -23,7 +23,6 @@
#define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */ #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
#include <signal.h> #include <signal.h>
#undef _POSIX_PTHREAD_SEMANTICS #undef _POSIX_PTHREAD_SEMANTICS
#include <pthread.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@@ -41,6 +40,7 @@
#include <library.h> #include <library.h>
#include <utils/backtrace.h> #include <utils/backtrace.h>
#include <threading/thread.h>
#include <selectors/traffic_selector.h> #include <selectors/traffic_selector.h>
#include <config/proposal.h> #include <config/proposal.h>
@@ -64,6 +64,11 @@ struct private_daemon_t {
*/ */
sigset_t signal_set; sigset_t signal_set;
/**
* Reference to main thread.
*/
thread_t *main_thread;
#ifdef CAPABILITIES #ifdef CAPABILITIES
/** /**
* capabilities to keep * capabilities to keep
@@ -226,7 +231,7 @@ static void kill_daemon(private_daemon_t *this, char *reason)
{ {
fprintf(stderr, "killing daemon: %s\n", 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 */ /* initialization failed, terminate daemon */
unlink(PID_FILE); unlink(PID_FILE);
@@ -235,9 +240,9 @@ static void kill_daemon(private_daemon_t *this, char *reason)
else else
{ {
DBG1(DBG_DMN, "sending SIGTERM to ourself"); 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 */ /* 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; 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 = backtrace_create(2);
backtrace->log(backtrace, stderr); backtrace->log(backtrace, stderr);
backtrace->destroy(backtrace); backtrace->destroy(backtrace);
@@ -575,7 +580,7 @@ private_daemon_t *daemon_create(void)
this->public.uid = 0; this->public.uid = 0;
this->public.gid = 0; this->public.gid = 0;
this->public.main_thread_id = pthread_self(); this->main_thread = thread_current();
#ifdef CAPABILITIES #ifdef CAPABILITIES
this->caps = cap_init(); this->caps = cap_init();
keep_cap(this, CAP_NET_ADMIN); keep_cap(this, CAP_NET_ADMIN);
@@ -586,7 +591,6 @@ private_daemon_t *daemon_create(void)
#endif /* CAPABILITIES */ #endif /* CAPABILITIES */
/* add handler for SEGV and ILL, /* add handler for SEGV and ILL,
* add handler for USR1 (cancellation).
* INT, TERM and HUP are handled by sigwait() in run() */ * INT, TERM and HUP are handled by sigwait() in run() */
action.sa_handler = segv_handler; action.sa_handler = segv_handler;
action.sa_flags = 0; action.sa_flags = 0;
@@ -600,7 +604,7 @@ private_daemon_t *daemon_create(void)
action.sa_handler = SIG_IGN; action.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &action, NULL); sigaction(SIGPIPE, &action, NULL);
pthread_sigmask(SIG_SETMASK, &action.sa_mask, 0); pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
return this; return this;
} }
-5
View File
@@ -301,11 +301,6 @@ struct daemon_t {
*/ */
gid_t gid; gid_t gid;
/**
* The thread_id of main-thread.
*/
pthread_t main_thread_id;
/** /**
* Do not drop a given capability after initialization. * Do not drop a given capability after initialization.
* *
-2
View File
@@ -15,8 +15,6 @@
#include "kernel_interface.h" #include "kernel_interface.h"
#include <pthread.h>
#include <daemon.h> #include <daemon.h>
typedef struct private_kernel_interface_t private_kernel_interface_t; typedef struct private_kernel_interface_t private_kernel_interface_t;
-6
View File
@@ -17,7 +17,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#include "receiver.h" #include "receiver.h"
@@ -56,11 +55,6 @@ struct private_receiver_t {
*/ */
callback_job_t *job; callback_job_t *job;
/**
* Assigned thread.
*/
pthread_t assigned_thread;
/** /**
* current secret to use for cookie calculation * current secret to use for cookie calculation
*/ */
+7 -6
View File
@@ -15,13 +15,14 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include "sender.h" #include "sender.h"
#include <daemon.h> #include <daemon.h>
#include <network/socket.h> #include <network/socket.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#include <threading/thread.h>
#include <threading/condvar.h>
#include <threading/mutex.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) static job_requeue_t send_packets(private_sender_t * this)
{ {
packet_t *packet; packet_t *packet;
int oldstate; bool oldstate;
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
while (this->list->get_count(this->list) == 0) while (this->list->get_count(this->list) == 0)
{ {
/* add cleanup handler, wait for packet, remove cleanup handler */ /* add cleanup handler, wait for packet, remove cleanup handler */
pthread_cleanup_push((void(*)(void*))this->mutex->unlock, this->mutex); thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); oldstate = thread_cancelability(TRUE);
this->got->wait(this->got, this->mutex); this->got->wait(this->got, this->mutex);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
pthread_cleanup_pop(0); thread_cleanup_pop(FALSE);
} }
this->list->remove_first(this->list, (void**)&packet); this->list->remove_first(this->list, (void**)&packet);
this->sent->signal(this->sent); this->sent->signal(this->sent);
+6 -6
View File
@@ -18,7 +18,6 @@
/* for struct in6_pktinfo */ /* for struct in6_pktinfo */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <pthread.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <string.h> #include <string.h>
@@ -38,6 +37,7 @@
#include "socket.h" #include "socket.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
/* constants for packet handling */ /* constants for packet handling */
#define IP_LEN sizeof(struct iphdr) #define IP_LEN sizeof(struct iphdr)
@@ -127,8 +127,8 @@ static status_t receiver(private_socket_t *this, packet_t **packet)
packet_t *pkt; packet_t *pkt;
struct udphdr *udp; struct udphdr *udp;
host_t *source = NULL, *dest = NULL; host_t *source = NULL, *dest = NULL;
int bytes_read = 0; int bytes_read = 0, data_offset;
int data_offset, oldstate; bool oldstate;
fd_set rfds; fd_set rfds;
FD_ZERO(&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"); 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) if (select(max(this->recv4, this->recv6) + 1, &rfds, NULL, NULL, NULL) <= 0)
{ {
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
return FAILED; return FAILED;
} }
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (this->recv4 && FD_ISSET(this->recv4, &rfds)) if (this->recv4 && FD_ISSET(this->recv4, &rfds))
{ {
+7 -6
View File
@@ -23,7 +23,6 @@
#define __EXTENSIONS__ #define __EXTENSIONS__
#endif #endif
#include <pthread.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <string.h> #include <string.h>
@@ -45,6 +44,7 @@
#include "socket.h" #include "socket.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
/* length of non-esp marker */ /* length of non-esp marker */
#define MARKER_LEN sizeof(u_int32_t) #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; chunk_t data;
packet_t *pkt; packet_t *pkt;
host_t *source = NULL, *dest = NULL; host_t *source = NULL, *dest = NULL;
int bytes_read = 0; int bytes_read = 0, data_offset;
int data_offset, oldstate; bool oldstate;
fd_set rfds; fd_set rfds;
int max_fd = 0, selected = 0; int max_fd = 0, selected = 0;
u_int16_t port = 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)); max_fd = max(max(this->ipv4, this->ipv4_natt), max(this->ipv6, this->ipv6_natt));
DBG2(DBG_NET, "waiting for data on sockets"); 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) if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
{ {
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
return FAILED; return FAILED;
} }
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (FD_ISSET(this->ipv4, &rfds)) if (FD_ISSET(this->ipv4, &rfds))
{ {
@@ -21,6 +21,7 @@
#include <daemon.h> #include <daemon.h>
#include <utils/host.h> #include <utils/host.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
/** /**
@@ -21,7 +21,6 @@
#include <linux/udp.h> #include <linux/udp.h>
#include <net/if.h> #include <net/if.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@@ -30,6 +29,7 @@
#include "kernel_klips_ipsec.h" #include "kernel_klips_ipsec.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#include <processing/jobs/acquire_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]; unsigned char buf[PFKEY_BUFFER_SIZE];
struct sadb_msg *msg = (struct sadb_msg*)buf; 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); len = recv(this->socket_events, buf, sizeof(buf), 0);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (len < 0) if (len < 0)
{ {
@@ -26,8 +26,8 @@
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <linux/xfrm.h> #include <linux/xfrm.h>
#include <linux/udp.h> #include <linux/udp.h>
#include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
@@ -36,6 +36,7 @@
#include "kernel_netlink_shared.h" #include "kernel_netlink_shared.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <utils/hashtable.h> #include <utils/hashtable.h>
#include <processing/jobs/callback_job.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 nlmsghdr *hdr = (struct nlmsghdr*)response;
struct sockaddr_nl addr; struct sockaddr_nl addr;
socklen_t addr_len = sizeof(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, len = recvfrom(this->socket_xfrm_events, response, sizeof(response), 0,
(struct sockaddr*)&addr, &addr_len); (struct sockaddr*)&addr, &addr_len);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (len < 0) if (len < 0)
{ {
@@ -17,7 +17,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <net/if.h> #include <net/if.h>
@@ -26,6 +25,8 @@
#include "kernel_netlink_shared.h" #include "kernel_netlink_shared.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
#include <processing/jobs/callback_job.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 nlmsghdr *hdr = (struct nlmsghdr*)response;
struct sockaddr_nl addr; struct sockaddr_nl addr;
socklen_t addr_len = sizeof(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, len = recvfrom(this->socket_events, response, sizeof(response), 0,
(struct sockaddr*)&addr, &addr_len); (struct sockaddr*)&addr, &addr_len);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (len < 0) if (len < 0)
{ {
@@ -49,13 +49,14 @@
#endif /*HAVE_NATT*/ #endif /*HAVE_NATT*/
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include "kernel_pfkey_ipsec.h" #include "kernel_pfkey_ipsec.h"
#include <daemon.h> #include <daemon.h>
#include <utils/host.h> #include <utils/host.h>
#include <threading/thread.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#include <processing/jobs/acquire_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]; unsigned char buf[PFKEY_BUFFER_SIZE];
struct sadb_msg *msg = (struct sadb_msg*)buf; 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); len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (len < 0) if (len < 0)
{ {
@@ -19,13 +19,13 @@
#include <ifaddrs.h> #include <ifaddrs.h>
#include <net/route.h> #include <net/route.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#include <errno.h> #include <errno.h>
#include "kernel_pfroute_net.h" #include "kernel_pfroute_net.h"
#include <daemon.h> #include <daemon.h>
#include <utils/host.h> #include <utils/host.h>
#include <threading/thread.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
#include <processing/jobs/callback_job.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]; unsigned char buf[PFROUTE_BUFFER_SIZE];
struct rt_msghdr *msg = (struct rt_msghdr*)buf; 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); len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (len < 0) if (len < 0)
{ {
@@ -68,7 +68,7 @@ static bool ike_state_change(private_load_tester_listener_t *this,
if (this->shutdown_on == ++this->established) if (this->shutdown_on == ++this->established)
{ {
DBG1(DBG_CFG, "load-test complete, raising SIGTERM"); DBG1(DBG_CFG, "load-test complete, raising SIGTERM");
pthread_kill(charon->main_thread_id, SIGTERM); kill(0, SIGTERM);
} }
} }
} }
@@ -24,6 +24,7 @@
#include <daemon.h> #include <daemon.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
typedef struct private_load_tester_plugin_t private_load_tester_plugin_t; typedef struct private_load_tester_plugin_t private_load_tester_plugin_t;
+12 -10
View File
@@ -23,13 +23,13 @@
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include <signal.h> #include <signal.h>
#include <libxml/xmlreader.h> #include <libxml/xmlreader.h>
#include <libxml/xmlwriter.h> #include <libxml/xmlwriter.h>
#include <library.h> #include <library.h>
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <processing/jobs/callback_job.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"); xmlTextWriterStartElement(writer, "item");
xmlTextWriterWriteFormatAttribute(writer, "level", "%d", level); xmlTextWriterWriteFormatAttribute(writer, "level", "%d", level);
xmlTextWriterWriteFormatAttribute(writer, "source", "%N", debug_names, group); xmlTextWriterWriteFormatAttribute(writer, "source", "%N", debug_names, group);
xmlTextWriterWriteFormatAttribute(writer, "thread", "%u", pthread_self()); xmlTextWriterWriteFormatAttribute(writer, "thread", "%u", thread_current_id());
xmlTextWriterWriteVFormatString(writer, format, args); xmlTextWriterWriteVFormatString(writer, format, args);
xmlTextWriterEndElement(writer); xmlTextWriterEndElement(writer);
/* </item> */ /* </item> */
@@ -622,17 +622,18 @@ static void closefdp(int *fd)
*/ */
static job_requeue_t process(int *fdp) static job_requeue_t process(int *fdp)
{ {
int oldstate, fd = *fdp; int fd = *fdp;
bool oldstate;
char buffer[4096]; char buffer[4096];
size_t len; size_t len;
xmlTextReaderPtr reader; xmlTextReaderPtr reader;
char *id = NULL, *type = NULL; char *id = NULL, *type = NULL;
pthread_cleanup_push((void*)closefdp, (void*)&fd); thread_cleanup_push((thread_cleanup_t)closefdp, (void*)&fd);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); oldstate = thread_cancelability(TRUE);
len = read(fd, buffer, sizeof(buffer)); len = read(fd, buffer, sizeof(buffer));
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
pthread_cleanup_pop(0); thread_cleanup_pop(FALSE);
if (len <= 0) if (len <= 0)
{ {
close(fd); close(fd);
@@ -682,13 +683,14 @@ static job_requeue_t process(int *fdp)
static job_requeue_t dispatch(private_smp_t *this) static job_requeue_t dispatch(private_smp_t *this)
{ {
struct sockaddr_un strokeaddr; struct sockaddr_un strokeaddr;
int oldstate, fd, *fdp, strokeaddrlen = sizeof(strokeaddr); int fd, *fdp, strokeaddrlen = sizeof(strokeaddr);
callback_job_t *job; callback_job_t *job;
bool oldstate;
/* wait for connections, but allow thread to terminate */ /* 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); fd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (fd < 0) if (fd < 0)
{ {
+4 -5
View File
@@ -23,11 +23,10 @@
#include <sys/fcntl.h> #include <sys/fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#include <daemon.h> #include <daemon.h>
#include <threading/mutex.h> /* for Mac OS X compatible accept */ #include <threading/thread.h>
#include "stroke_config.h" #include "stroke_config.h"
#include "stroke_control.h" #include "stroke_control.h"
@@ -547,13 +546,13 @@ static job_requeue_t receive(private_stroke_socket_t *this)
struct sockaddr_un strokeaddr; struct sockaddr_un strokeaddr;
int strokeaddrlen = sizeof(strokeaddr); int strokeaddrlen = sizeof(strokeaddr);
int strokefd; int strokefd;
int oldstate; bool oldstate;
callback_job_t *job; callback_job_t *job;
stroke_job_context_t *ctx; stroke_job_context_t *ctx;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); oldstate = thread_cancelability(TRUE);
strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen); strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (strokefd < 0) if (strokefd < 0)
{ {
+5 -4
View File
@@ -21,11 +21,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include "uci_control.h" #include "uci_control.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#define FIFO_FILE "/var/run/charon.fifo" #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) static job_requeue_t receive(private_uci_control_t *this)
{ {
char message[128]; char message[128];
int oldstate, len; int len;
bool oldstate;
FILE *in; FILE *in;
memset(message, 0, sizeof(message)); memset(message, 0, sizeof(message));
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); oldstate = thread_cancelability(TRUE);
in = fopen(FIFO_FILE, "r"); in = fopen(FIFO_FILE, "r");
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
if (in) if (in)
{ {
len = fread(message, 1, sizeof(message) - 1, in); len = fread(message, 1, sizeof(message) - 1, in);
+19 -12
View File
@@ -15,13 +15,14 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "processor.h" #include "processor.h"
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <utils/linked_list.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) static void restart(private_processor_t *this)
{ {
pthread_t thread; thread_t *thread;
/* respawn thread if required */ /* respawn thread if required */
this->mutex->lock(this->mutex);
if (this->desired_threads == 0 || 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->total_threads--;
this->thread_terminated->broadcast(this->thread_terminated); 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) 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); this->mutex->lock(this->mutex);
while (this->desired_threads >= this->total_threads) 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->list->remove_first(this->list, (void**)&job);
this->mutex->unlock(this->mutex); this->mutex->unlock(this->mutex);
/* terminated threads are restarted, so we have a constant pool */ /* 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); job->execute(job);
pthread_cleanup_pop(0); thread_cleanup_pop(FALSE);
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
} }
this->total_threads--; this->total_threads--;
@@ -185,14 +190,16 @@ static void set_threads(private_processor_t *this, u_int count)
if (count > this->total_threads) if (count > this->total_threads)
{ /* increase thread count */ { /* increase thread count */
int i; int i;
pthread_t current; thread_t *current;
this->desired_threads = count; this->desired_threads = count;
DBG1(DBG_JOB, "spawning %d worker threads", count - this->total_threads); DBG1(DBG_JOB, "spawning %d worker threads", count - this->total_threads);
for (i = this->total_threads; i < count; i++) for (i = this->total_threads; i < count; i++)
{ {
if (pthread_create(&current, NULL, (void*)process_jobs, this) == 0) current = thread_create((thread_main_t)process_jobs, this);
if (current)
{ {
current->detach(current);
this->total_threads++; this->total_threads++;
} }
} }
+7 -7
View File
@@ -16,13 +16,14 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include "scheduler.h" #include "scheduler.h"
#include <daemon.h> #include <daemon.h>
#include <processing/processor.h> #include <processing/processor.h>
#include <processing/jobs/callback_job.h> #include <processing/jobs/callback_job.h>
#include <threading/thread.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
/* the initial size of the heap */ /* the initial size of the heap */
@@ -185,8 +186,7 @@ static job_requeue_t schedule(private_scheduler_t * this)
{ {
timeval_t now; timeval_t now;
event_t *event; event_t *event;
int oldstate; bool timed = FALSE, oldstate;
bool timed = FALSE;
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
@@ -215,8 +215,8 @@ static job_requeue_t schedule(private_scheduler_t * this)
} }
timed = TRUE; timed = TRUE;
} }
pthread_cleanup_push((void*)this->mutex->unlock, this->mutex); thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); oldstate = thread_cancelability(TRUE);
if (timed) if (timed)
{ {
@@ -227,8 +227,8 @@ static job_requeue_t schedule(private_scheduler_t * this)
DBG2(DBG_JOB, "no events, waiting"); DBG2(DBG_JOB, "no events, waiting");
this->condvar->wait(this->condvar, this->mutex); this->condvar->wait(this->condvar, this->mutex);
} }
pthread_setcancelstate(oldstate, NULL); thread_cancelability(oldstate);
pthread_cleanup_pop(TRUE); thread_cleanup_pop(TRUE);
return JOB_REQUEUE_DIRECT; return JOB_REQUEUE_DIRECT;
} }
+1
View File
@@ -22,6 +22,7 @@
#include <daemon.h> #include <daemon.h>
#include <sa/ike_sa_id.h> #include <sa/ike_sa_id.h>
#include <bus/bus.h> #include <bus/bus.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <threading/rwlock.h> #include <threading/rwlock.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
+5 -4
View File
@@ -29,12 +29,12 @@
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h> #include <sys/time.h>
#include <pthread.h>
#include "cowfs.h" #include "cowfs.h"
#include <library.h> #include <library.h>
#include <debug.h> #include <debug.h>
#include <threading/thread.h>
/** define _XOPEN_SOURCE 500 fails when using libstrongswan, define popen */ /** define _XOPEN_SOURCE 500 fails when using libstrongswan, define popen */
extern ssize_t pread(int fd, void *buf, size_t count, off_t offset); 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 */ /** optional COW overlay */
int over_fd; int over_fd;
/** thread processing FUSE */ /** 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_exit(this->fuse);
fuse_unmount(this->mount, this->chan); fuse_unmount(this->mount, this->chan);
pthread_join(this->thread, NULL); this->thread->join(this->thread);
fuse_destroy(this->fuse); fuse_destroy(this->fuse);
free(this->mount); free(this->mount);
free(this->master); free(this->master);
@@ -862,7 +862,8 @@ cowfs_t *cowfs_create(char *master, char *host, char *mount)
this->host = strdup(host); this->host = strdup(host);
this->over = NULL; 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"); DBG1("creating thread to handle FUSE failed");
fuse_unmount(mount, this->chan); fuse_unmount(mount, this->chan);
+13 -10
View File
@@ -19,11 +19,12 @@
#include "session.h" #include "session.h"
#include <fcgiapp.h> #include <fcgiapp.h>
#include <pthread.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <debug.h> #include <debug.h>
#include <threading/thread.h>
#include <threading/condvar.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
#include <utils/hashtable.h> #include <utils/hashtable.h>
@@ -51,7 +52,7 @@ struct private_dispatcher_t {
/** /**
* thread list * thread list
*/ */
pthread_t *threads; thread_t **threads;
/** /**
* number of threads in "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) static void dispatch(private_dispatcher_t *this)
{ {
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); thread_cancelability(FALSE);
while (TRUE) while (TRUE)
{ {
@@ -295,9 +296,9 @@ static void dispatch(private_dispatcher_t *this)
time_t now; time_t now;
char *sid; char *sid;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); thread_cancelability(TRUE);
request = request_create(this->fd, this->debug); request = request_create(this->fd, this->debug);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); thread_cancelability(FALSE);
if (request == NULL) if (request == NULL)
{ {
@@ -354,11 +355,12 @@ static void dispatch(private_dispatcher_t *this)
static void run(private_dispatcher_t *this, int threads) static void run(private_dispatcher_t *this, int threads)
{ {
this->thread_count = threads; this->thread_count = threads;
this->threads = malloc(sizeof(pthread_t) * threads); this->threads = malloc(sizeof(thread_t*) * threads);
while (threads) while (threads)
{ {
if (pthread_create(&this->threads[threads - 1], this->threads[threads - 1] = thread_create((thread_main_t)dispatch,
NULL, (void*)dispatch, this) == 0) this);
if (this->threads[threads - 1])
{ {
threads--; threads--;
} }
@@ -393,8 +395,9 @@ static void destroy(private_dispatcher_t *this)
FCGX_ShutdownPending(); FCGX_ShutdownPending();
while (this->thread_count--) while (this->thread_count--)
{ {
pthread_cancel(this->threads[this->thread_count]); thread_t *thread = this->threads[this->thread_count];
pthread_join(this->threads[this->thread_count], NULL); thread->cancel(thread);
thread->join(thread);
} }
enumerator = this->sessions->create_enumerator(this->sessions); enumerator = this->sessions->create_enumerator(this->sessions);
while (enumerator->enumerate(enumerator, &sid, &entry)) while (enumerator->enumerate(enumerator, &sid, &entry))
+17 -15
View File
@@ -20,10 +20,13 @@
#include <library.h> #include <library.h>
#include <debug.h> #include <debug.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <pthread.h> #include <pthread.h>
#include <string.h>
#include <ClearSilver/ClearSilver.h> #include <ClearSilver/ClearSilver.h>
#include <threading/thread.h>
#include <threading/thread_value.h>
typedef struct private_request_t private_request_t; 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 * ClearSilver cgiwrap is not threadsave, so we use a private
* context for each thread. * context for each thread.
*/ */
static pthread_key_t this_key; static thread_value_t *thread_this;
/** /**
* control variable for pthread_once * 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) 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); 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) 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); FCGX_VFPrintF(this->req.out, format, args);
return 0; 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) 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); 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) static char *getenv_cb(void *null, const char *key)
{ {
char *value; 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); value = FCGX_GetParam(key, this->req.envp);
return value ? strdup(value) : NULL; return value ? strdup(value) : NULL;
@@ -137,7 +139,7 @@ static int iterenv_cb(void *null, int num, char **key, char **value)
{ {
*key = NULL; *key = NULL;
*value = 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) if (num < this->req_env_len)
{ {
char *eq; 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) 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, cgi_cookie_set (this->cgi, name, value,
FCGX_GetParam("SCRIPT_NAME", this->req.envp), FCGX_GetParam("SCRIPT_NAME", this->req.envp),
NULL, NULL, 0, 0); NULL, NULL, 0, 0);
@@ -280,7 +282,7 @@ static void render(private_request_t *this, char *template)
{ {
NEOERR* err; NEOERR* err;
pthread_setspecific(this_key, this); thread_this->set(thread_this, this);
err = cgi_display(this->cgi, template); err = cgi_display(this->cgi, template);
if (err) if (err)
{ {
@@ -345,7 +347,7 @@ static void destroy(private_request_t *this)
{ {
if (ref_put(&this->ref)) if (ref_put(&this->ref))
{ {
pthread_setspecific(this_key, this); thread_this->set(thread_this, this);
cgi_destroy(&this->cgi); cgi_destroy(&this->cgi);
FCGX_Finish_r(&this->req); FCGX_Finish_r(&this->req);
free(this); free(this);
@@ -360,7 +362,7 @@ static void init(void)
{ {
cgiwrap_init_emu(NULL, read_cb, writef_cb, write_cb, cgiwrap_init_emu(NULL, read_cb, writef_cb, write_cb,
getenv_cb, putenv_cb, iterenv_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); private_request_t *this = malloc_thing(private_request_t);
bool failed = FALSE; bool failed = FALSE;
pthread_cleanup_push(free, this); thread_cleanup_push(free, this);
if (FCGX_InitRequest(&this->req, fd, 0) != 0 || if (FCGX_InitRequest(&this->req, fd, 0) != 0 ||
FCGX_Accept_r(&this->req) != 0) FCGX_Accept_r(&this->req) != 0)
{ {
failed = TRUE; failed = TRUE;
} }
pthread_cleanup_pop(failed); thread_cleanup_pop(failed);
if (failed) if (failed)
{ {
return NULL; return NULL;
@@ -404,7 +406,7 @@ request_t *request_create(int fd, bool debug)
this->public.destroy = (void(*)(request_t*))destroy; this->public.destroy = (void(*)(request_t*))destroy;
pthread_once(&once, init); pthread_once(&once, init);
pthread_setspecific(this_key, this); thread_this->set(thread_this, this);
this->ref = 1; this->ref = 1;
this->closed = FALSE; this->closed = FALSE;
+2
View File
@@ -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 \ database/database.h database/database_factory.h database/database_factory.c \
fetcher/fetcher.h fetcher/fetcher_manager.h fetcher/fetcher_manager.c \ fetcher/fetcher.h fetcher/fetcher_manager.h fetcher/fetcher_manager.c \
selectors/traffic_selector.c selectors/traffic_selector.h \ 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/mutex.h threading/mutex.c threading/condvar.h \
threading/rwlock.h threading/rwlock.c \ threading/rwlock.h threading/rwlock.c \
threading/lock_profiler.h \ threading/lock_profiler.h \
-1
View File
@@ -18,7 +18,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <pthread.h>
#include <utils.h> #include <utils.h>
#include <debug.h> #include <debug.h>
@@ -14,12 +14,12 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <pthread.h>
#include "credential_factory.h" #include "credential_factory.h"
#include <debug.h> #include <debug.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
#include <threading/thread_value.h>
#include <threading/rwlock.h> #include <threading/rwlock.h>
#include <credentials/certificates/x509.h> #include <credentials/certificates/x509.h>
@@ -50,7 +50,7 @@ struct private_credential_factory_t {
/** /**
* Thread specific recursiveness counter * Thread specific recursiveness counter
*/ */
pthread_key_t recursive; thread_value_t *recursive;
/** /**
* lock access to builders * lock access to builders
@@ -121,8 +121,8 @@ static void* create(private_credential_factory_t *this, credential_type_t type,
int failures = 0; int failures = 0;
uintptr_t level; uintptr_t level;
level = (uintptr_t)pthread_getspecific(this->recursive); level = (uintptr_t)this->recursive->get(this->recursive);
pthread_setspecific(this->recursive, (void*)level + 1); this->recursive->set(this->recursive, (void*)level + 1);
this->lock->read_lock(this->lock); this->lock->read_lock(this->lock);
enumerator = this->constructors->create_enumerator(this->constructors); 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", DBG1("building %N - %N failed, tried %d builders",
credential_type_names, type, names, subtype, failures); credential_type_names, type, names, subtype, failures);
} }
pthread_setspecific(this->recursive, (void*)level); this->recursive->set(this->recursive, (void*)level);
return construct; 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) static void destroy(private_credential_factory_t *this)
{ {
this->constructors->destroy_function(this->constructors, free); this->constructors->destroy_function(this->constructors, free);
pthread_key_delete(this->recursive); this->recursive->destroy(this->recursive);
this->lock->destroy(this->lock); this->lock->destroy(this->lock);
free(this); free(this);
} }
@@ -182,7 +182,7 @@ credential_factory_t *credential_factory_create()
this->public.destroy = (void(*)(credential_factory_t*))destroy; this->public.destroy = (void(*)(credential_factory_t*))destroy;
this->constructors = linked_list_create(); this->constructors = linked_list_create();
pthread_key_create(&this->recursive, NULL); this->recursive = thread_value_create(NULL);
this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT); this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
return &this->public; return &this->public;
+6
View File
@@ -21,6 +21,7 @@
#include <utils.h> #include <utils.h>
#include <chunk.h> #include <chunk.h>
#include <debug.h> #include <debug.h>
#include <threading/thread.h>
#include <utils/identification.h> #include <utils/identification.h>
#include <utils/host.h> #include <utils/host.h>
#ifdef LEAK_DETECTIVE #ifdef LEAK_DETECTIVE
@@ -81,6 +82,9 @@ void library_deinit()
this->detective->destroy(this->detective); this->detective->destroy(this->detective);
} }
#endif /* LEAK_DETECTIVE */ #endif /* LEAK_DETECTIVE */
threads_deinit();
free(this); free(this);
lib = NULL; lib = NULL;
} }
@@ -94,6 +98,8 @@ bool library_init(char *settings)
private_library_t *this = malloc_thing(private_library_t); private_library_t *this = malloc_thing(private_library_t);
lib = &this->public; lib = &this->public;
threads_init();
lib->leak_detective = FALSE; lib->leak_detective = FALSE;
#ifdef LEAK_DETECTIVE #ifdef LEAK_DETECTIVE
@@ -15,12 +15,12 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <string.h> #include <string.h>
#include <pthread.h>
#include <mysql.h> #include <mysql.h>
#include "mysql_database.h" #include "mysql_database.h"
#include <debug.h> #include <debug.h>
#include <threading/thread_value.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include <utils/linked_list.h> #include <utils/linked_list.h>
@@ -104,19 +104,20 @@ static void conn_release(conn_t *conn)
{ {
conn->in_use = FALSE; conn->in_use = FALSE;
} }
/** /**
* thread specific initialization flag * thread specific initialization flag
*/ */
pthread_key_t initialized; thread_value_t *initialized;
/** /**
* Initialize a thread for mysql usage * Initialize a thread for mysql usage
*/ */
static void thread_initialize() 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(); mysql_thread_init();
} }
} }
@@ -130,11 +131,7 @@ bool mysql_database_init()
{ {
return FALSE; return FALSE;
} }
if (pthread_key_create(&initialized, (void*)mysql_thread_end)) initialized = thread_value_create((thread_cleanup_t)mysql_thread_end);
{
mysql_library_end();
return FALSE;
}
return TRUE; return TRUE;
} }
@@ -143,7 +140,7 @@ bool mysql_database_init()
*/ */
void mysql_database_deinit() void mysql_database_deinit()
{ {
pthread_key_delete(initialized); initialized->destroy(initialized);
mysql_thread_end(); mysql_thread_end();
/* mysql_library_end(); would be the clean way, however, it hangs... */ /* mysql_library_end(); would be the clean way, however, it hangs... */
} }
@@ -18,11 +18,11 @@
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/engine.h> #include <openssl/engine.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <pthread.h>
#include "openssl_plugin.h" #include "openssl_plugin.h"
#include <library.h> #include <library.h>
#include <threading/thread.h>
#include <threading/mutex.h> #include <threading/mutex.h>
#include "openssl_util.h" #include "openssl_util.h"
#include "openssl_crypter.h" #include "openssl_crypter.h"
@@ -120,7 +120,7 @@ static void destroy_function(struct CRYPTO_dynlock_value *lock,
*/ */
static unsigned long id_function(void) static unsigned long id_function(void)
{ {
return (unsigned long)pthread_self(); return (unsigned long)thread_current_id();
} }
/** /**
+6 -12
View File
@@ -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 * Management of thread-specific Vstr_conf objects
*/ */
#include <pthread.h> #include <threading/thread_value.h>
static pthread_key_t vstr_conf_key; static thread_value_t *vstr_conf;
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 Vstr_conf *create_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() static inline Vstr_conf *get_vstr_conf()
{ {
Vstr_conf *conf; Vstr_conf *conf;
pthread_once(&vstr_conf_key_once, init_vstr_conf_key); conf = (Vstr_conf*)vstr_conf->get(vstr_conf);
conf = (Vstr_conf*)pthread_getspecific(vstr_conf_key);
if (!conf) if (!conf)
{ {
conf = create_vstr_conf(); conf = create_vstr_conf();
pthread_setspecific(vstr_conf_key, conf); vstr_conf->set(vstr_conf, conf);
} }
return conf; return conf;
} }
@@ -412,7 +405,7 @@ static void destroy(private_printf_hook_t *this)
#ifdef USE_VSTR #ifdef USE_VSTR
/* freeing the Vstr_conf of the main thread */ /* freeing the Vstr_conf of the main thread */
pthread_key_delete(vstr_conf_key); vstr_conf->destroy(vstr_conf);
vstr_free_conf(conf); vstr_free_conf(conf);
vstr_exit(); vstr_exit();
#endif #endif
@@ -438,6 +431,7 @@ printf_hook_t *printf_hook_create()
free(this); free(this);
return NULL; return NULL;
} }
vstr_conf = thread_value_create((thread_cleanup_t)vstr_free_conf);
#endif #endif
return &this->public; return &this->public;
+1
View File
@@ -23,6 +23,7 @@
#include <library.h> #include <library.h>
#include <debug.h> #include <debug.h>
#include "condvar.h"
#include "mutex.h" #include "mutex.h"
#include "lock_profiler.h" #include "lock_profiler.h"
-29
View File
@@ -25,35 +25,6 @@
typedef struct mutex_t mutex_t; typedef struct mutex_t mutex_t;
typedef enum mutex_type_t mutex_type_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. * Type of mutex.
*/ */