#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,
*/
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;
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.
*/
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 */
};
/**
- * 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 listen_(private_bus_t *this, listener_t *listener, job_t *job)
{
- int old;
+ bool old;
cleanup_data_t data;
data.this = this;
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);
}
*/
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);
}
/**
*/
static ike_sa_t* get_sa(private_bus_t *this)
{
- return pthread_getspecific(this->thread_sa);
+ return this->thread_sa->get(this->thread_sa);
}
/**
{
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;
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);
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);
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);
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);
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);
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);
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);
*/
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);
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;
}
#include "backend_manager.h"
#include <sys/types.h>
-#include <pthread.h>
#include <daemon.h>
#include <utils/linked_list.h>
* 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>
/**
* 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
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);
{
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);
}
{
linked_list_t *sets;
- sets = pthread_getspecific(this->local_sets);
+ sets = this->local_sets->get(this->local_sets);
sets->remove(sets, set, NULL);
}
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);
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);
#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>
#include <library.h>
#include <utils/backtrace.h>
+#include <threading/thread.h>
#include <selectors/traffic_selector.h>
#include <config/proposal.h>
*/
sigset_t signal_set;
+ /**
+ * Reference to main thread.
+ */
+ thread_t *main_thread;
+
#ifdef CAPABILITIES
/**
* capabilities to keep
{
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);
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);
}
}
{
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);
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);
#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;
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;
}
*/
gid_t gid;
- /**
- * The thread_id of main-thread.
- */
- pthread_t main_thread_id;
-
/**
* Do not drop a given capability after initialization.
*
#include "kernel_interface.h"
-#include <pthread.h>
-
#include <daemon.h>
typedef struct private_kernel_interface_t private_kernel_interface_t;
#include <stdlib.h>
#include <unistd.h>
-#include <pthread.h>
#include "receiver.h"
*/
callback_job_t *job;
- /**
- * Assigned thread.
- */
- pthread_t assigned_thread;
-
/**
* current secret to use for cookie calculation
*/
*/
#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>
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);
/* for struct in6_pktinfo */
#define _GNU_SOURCE
-#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include "socket.h"
#include <daemon.h>
+#include <threading/thread.h>
/* constants for packet handling */
#define IP_LEN sizeof(struct iphdr)
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);
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))
{
#define __EXTENSIONS__
#endif
-#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include "socket.h"
#include <daemon.h>
+#include <threading/thread.h>
/* length of non-esp marker */
#define MARKER_LEN sizeof(u_int32_t)
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;
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))
{
#include <daemon.h>
#include <utils/host.h>
#include <utils/linked_list.h>
+#include <threading/condvar.h>
#include <threading/mutex.h>
/**
#include <linux/udp.h>
#include <net/if.h>
#include <unistd.h>
-#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#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>
{
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)
{
#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>
#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>
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)
{
#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>
#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>
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)
{
#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>
{
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)
{
#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>
{
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)
{
if (this->shutdown_on == ++this->established)
{
DBG1(DBG_CFG, "load-test complete, raising SIGTERM");
- pthread_kill(charon->main_thread_id, SIGTERM);
+ kill(0, SIGTERM);
}
}
}
#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;
#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>
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> */
*/
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);
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)
{
#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"
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)
{
#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"
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);
*/
#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>
*/
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);
}
/**
*/
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)
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--;
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++;
}
}
*/
#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 */
{
timeval_t now;
event_t *event;
- int oldstate;
- bool timed = FALSE;
+ bool timed = FALSE, oldstate;
this->mutex->lock(this->mutex);
}
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)
{
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;
}
#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>
#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);
/** optional COW overlay */
int over_fd;
/** thread processing FUSE */
- pthread_t thread;
+ thread_t *thread;
};
/**
{
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);
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);
#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>
/**
* thread list
*/
- pthread_t *threads;
+ thread_t **threads;
/**
* number of threads in "threads"
*/
static void dispatch(private_dispatcher_t *this)
{
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+ thread_cancelability(FALSE);
while (TRUE)
{
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)
{
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--;
}
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))
#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;
/**
};
/**
- * 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
*/
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);
}
*/
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;
*/
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);
}
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;
{
*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;
*/
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);
{
NEOERR* err;
- pthread_setspecific(this_key, this);
+ thread_this->set(thread_this, this);
err = cgi_display(this->cgi, template);
if (err)
{
{
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);
{
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);
}
/*
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;
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;
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 \
#include <stdio.h>
#include <string.h>
#include <time.h>
-#include <pthread.h>
#include <utils.h>
#include <debug.h>
*/
#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>
/**
* Thread specific recursiveness counter
*/
- pthread_key_t recursive;
+ thread_value_t *recursive;
/**
* lock access to builders
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);
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;
}
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);
}
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;
#include <utils.h>
#include <chunk.h>
#include <debug.h>
+#include <threading/thread.h>
#include <utils/identification.h>
#include <utils/host.h>
#ifdef LEAK_DETECTIVE
this->detective->destroy(this->detective);
}
#endif /* LEAK_DETECTIVE */
+
+ threads_deinit();
+
free(this);
lib = NULL;
}
private_library_t *this = malloc_thing(private_library_t);
lib = &this->public;
+ threads_init();
+
lib->leak_detective = FALSE;
#ifdef LEAK_DETECTIVE
#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>
{
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();
}
}
{
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;
}
*/
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... */
}
#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"
*/
static unsigned long id_function(void)
{
- return (unsigned long)pthread_self();
+ return (unsigned long)thread_current_id();
}
/**
/**
* 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()
{
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;
}
#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
free(this);
return NULL;
}
+ vstr_conf = thread_value_create((thread_cleanup_t)vstr_free_conf);
#endif
return &this->public;
#include <library.h>
#include <debug.h>
+#include "condvar.h"
#include "mutex.h"
#include "lock_profiler.h"
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.
*/