46 pthread_sigmask(SIG_SETMASK, &sigs, NULL);
106 r.thr = pthread_self();
116 pthread_condattr_t condattr;
121 if (pthread_condattr_init(&condattr)) {
125 #if defined(HAVE_CLOCK_GETTIME)
126 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && \
127 defined(CLOCK_MONOTONIC)
130 if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC)) {
133 #define USE_COND_CLOCK CLOCK_MONOTONIC
138 #define USE_COND_CLOCK CLOCK_REALTIME
141 if (pthread_cond_init(&cond->cond, &condattr)) {
152 if (pthread_cond_destroy(&cond->cond)) {
154 log_warn(
LD_GENERAL,
"Error freeing condition: %s", strerror(errno));
172 r = pthread_cond_wait(&cond->cond, &mutex->mutex);
185 #if defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK)
186 if (clock_gettime(USE_COND_CLOCK, &ts) < 0) {
189 tvnow.tv_sec = ts.tv_sec;
190 tvnow.tv_usec = (int)(ts.tv_nsec / 1000);
193 if (gettimeofday(&tvnow, NULL) < 0)
198 ts.tv_sec = tvsum.tv_sec;
199 ts.tv_nsec = tvsum.tv_usec * 1000;
201 r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts);
204 else if (r == ETIMEDOUT)
217 pthread_cond_signal(&cond->cond);
223 pthread_cond_broadcast(&cond->cond);
229 int err = pthread_key_create(&threadlocal->key, NULL);
236 pthread_key_delete(threadlocal->key);
243 return pthread_getspecific(threadlocal->key);
249 int err = pthread_setspecific(threadlocal->key, value);
261 #ifndef PTHREAD_CREATE_DETACHED
262 #define PTHREAD_CREATE_DETACHED 1
265 pthread_attr_setdetachstate(&
attr_detached, PTHREAD_CREATE_DETACHED);
void tor_locking_init(void)
static void * tor_pthread_helper_fn(void *_data)
void tor_cond_signal_all(tor_cond_t *cond)
static pthread_attr_t attr_detached
void * tor_threadlocal_get(tor_threadlocal_t *threadlocal)
int tor_cond_init(tor_cond_t *cond)
int spawn_func(void(*func)(void *), void *data)
void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal)
void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
void tor_threads_init(void)
void tor_cond_uninit(tor_cond_t *cond)
void tor_cond_signal_one(tor_cond_t *cond)
static int threads_initialized
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
int tor_threadlocal_init(tor_threadlocal_t *threadlocal)
unsigned long tor_get_thread_id(void)
void set_main_thread(void)
Definitions for timing-related constants.
Declarations for timeval-related macros that some platforms are missing.
#define timeradd(tv1, tv2, tvout)
Macros to manage assertions, fatal and non-fatal.