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 <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,
@@ -68,15 +70,10 @@ 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;
@@ -131,28 +128,6 @@ static void entry_destroy(entry_t *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.
*/
@@ -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;
}
-1
View File
@@ -16,7 +16,6 @@
#include "backend_manager.h"
#include <sys/types.h>
#include <pthread.h>
#include <daemon.h>
#include <utils/linked_list.h>
+8 -9
View File
@@ -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);
+12 -8
View File
@@ -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;
}
-5
View File
@@ -301,11 +301,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.
*
-2
View File
@@ -15,8 +15,6 @@
#include "kernel_interface.h"
#include <pthread.h>
#include <daemon.h>
typedef struct private_kernel_interface_t private_kernel_interface_t;
-6
View File
@@ -17,7 +17,6 @@
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "receiver.h"
@@ -56,11 +55,6 @@ struct private_receiver_t {
*/
callback_job_t *job;
/**
* Assigned thread.
*/
pthread_t assigned_thread;
/**
* current secret to use for cookie calculation
*/
+7 -6
View File
@@ -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);
+6 -6
View File
@@ -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))
{
+7 -6
View File
@@ -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))
{
@@ -21,6 +21,7 @@
#include <daemon.h>
#include <utils/host.h>
#include <utils/linked_list.h>
#include <threading/condvar.h>
#include <threading/mutex.h>
/**
@@ -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)
{
@@ -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)
{
@@ -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)
{
@@ -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)
{
@@ -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)
{
@@ -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);
}
}
}
@@ -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;
+12 -10
View File
@@ -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)
{
+4 -5
View File
@@ -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)
{
+5 -4
View File
@@ -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);
+19 -12
View File
@@ -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(&current, NULL, (void*)process_jobs, this) == 0)
current = thread_create((thread_main_t)process_jobs, this);
if (current)
{
current->detach(current);
this->total_threads++;
}
}
+7 -7
View File
@@ -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;
}
+1
View File
@@ -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>