#include "orconfig.h"
#include "lib/thread/threads.h"
#include "lib/wallclock/timeval.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <string.h>
typedef struct tor_pthread_data_t {
void (*func)(void *);
void *data;
} tor_pthread_data_t;
static void *
tor_pthread_helper_fn(void *_data)
{
tor_pthread_data_t *data = _data;
void (*func)(void*);
void *arg;
sigset_t sigs;
sigfillset(&sigs);
pthread_sigmask(SIG_SETMASK, &sigs, NULL);
func = data->func;
arg = data->data;
tor_free(_data);
func(arg);
return NULL;
}
static pthread_attr_t attr_detached;
static int threads_initialized = 0;
int
spawn_func(void (*func)(void *), void *data)
{
pthread_t thread;
tor_pthread_data_t *d;
if (PREDICT_UNLIKELY(!threads_initialized)) {
tor_threads_init();
}
d = tor_malloc(sizeof(tor_pthread_data_t));
d->data = data;
d->func = func;
if (pthread_create(&thread, &attr_detached, tor_pthread_helper_fn, d)) {
tor_free(d);
return -1;
}
return 0;
}
void
spawn_exit(void)
{
pthread_exit(NULL);
}
unsigned long
tor_get_thread_id(void)
{
union {
pthread_t thr;
unsigned long id;
} r;
r.thr = pthread_self();
return r.id;
}
int
tor_cond_init(tor_cond_t *cond)
{
pthread_condattr_t condattr;
memset(cond, 0, sizeof(tor_cond_t));
if (pthread_condattr_init(&condattr)) {
return -1;
}
#if defined(HAVE_CLOCK_GETTIME)
#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && \
defined(CLOCK_MONOTONIC)
if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC)) {
return -1;
}
#define USE_COND_CLOCK CLOCK_MONOTONIC
#else
#define USE_COND_CLOCK CLOCK_REALTIME
#endif
#endif
if (pthread_cond_init(&cond->cond, &condattr)) {
return -1;
}
return 0;
}
void
tor_cond_uninit(tor_cond_t *cond)
{
if (pthread_cond_destroy(&cond->cond)) {
log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
return;
}
}
int
tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
{
int r;
if (tv == NULL) {
while (1) {
r = pthread_cond_wait(&cond->cond, &mutex->mutex);
if (r == EINTR) {
continue; }
return r ? -1 : 0;
}
} else {
struct timeval tvnow, tvsum;
struct timespec ts;
while (1) {
#if defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK)
if (clock_gettime(USE_COND_CLOCK, &ts) < 0) {
return -1;
}
tvnow.tv_sec = ts.tv_sec;
tvnow.tv_usec = (int)(ts.tv_nsec / 1000);
timeradd(tv, &tvnow, &tvsum);
#else
if (gettimeofday(&tvnow, NULL) < 0)
return -1;
timeradd(tv, &tvnow, &tvsum);
#endif
ts.tv_sec = tvsum.tv_sec;
ts.tv_nsec = tvsum.tv_usec * 1000;
r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts);
if (r == 0)
return 0;
else if (r == ETIMEDOUT)
return 1;
else if (r == EINTR)
continue;
else
return -1;
}
}
}
void
tor_cond_signal_one(tor_cond_t *cond)
{
pthread_cond_signal(&cond->cond);
}
void
tor_cond_signal_all(tor_cond_t *cond)
{
pthread_cond_broadcast(&cond->cond);
}
int
tor_threadlocal_init(tor_threadlocal_t *threadlocal)
{
int err = pthread_key_create(&threadlocal->key, NULL);
return err ? -1 : 0;
}
void
tor_threadlocal_destroy(tor_threadlocal_t *threadlocal)
{
pthread_key_delete(threadlocal->key);
memset(threadlocal, 0, sizeof(tor_threadlocal_t));
}
void *
tor_threadlocal_get(tor_threadlocal_t *threadlocal)
{
return pthread_getspecific(threadlocal->key);
}
void
tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
{
int err = pthread_setspecific(threadlocal->key, value);
tor_assert(err == 0);
}
void
tor_threads_init(void)
{
if (!threads_initialized) {
tor_locking_init();
const int ret1 = pthread_attr_init(&attr_detached);
tor_assert(ret1 == 0);
#ifndef PTHREAD_CREATE_DETACHED
#define PTHREAD_CREATE_DETACHED 1
#endif
const int ret2 =
pthread_attr_setdetachstate(&attr_detached, PTHREAD_CREATE_DETACHED);
tor_assert(ret2 == 0);
threads_initialized = 1;
}
set_main_thread();
}