2013-09-23 03:30:46 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2018-06-20 14:13:28 +02:00
|
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
2013-09-23 03:30:46 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2018-07-10 18:22:01 +02:00
|
|
|
/**
|
|
|
|
* \file threads.h
|
|
|
|
* \brief Header for threads.c
|
|
|
|
**/
|
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
#ifndef TOR_COMPAT_THREADS_H
|
|
|
|
#define TOR_COMPAT_THREADS_H
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
2018-06-21 18:20:52 +02:00
|
|
|
#include "lib/cc/torint.h"
|
2018-06-21 19:12:23 +02:00
|
|
|
#include "lib/testsupport/testsupport.h"
|
2018-06-22 16:30:45 +02:00
|
|
|
#include "lib/lock/compat_mutex.h"
|
2013-09-23 03:30:46 +02:00
|
|
|
|
2018-08-08 21:41:40 +02:00
|
|
|
#if defined(HAVE_STDATOMIC_H) && defined(STDATOMIC_WORKS)
|
|
|
|
#define HAVE_WORKING_STDATOMIC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WORKING_STDATOMIC
|
2017-10-23 18:35:30 +02:00
|
|
|
#include <stdatomic.h>
|
|
|
|
#endif
|
|
|
|
|
2018-06-28 15:14:42 +02:00
|
|
|
struct timeval;
|
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
int spawn_func(void (*func)(void *), void *data);
|
|
|
|
void spawn_exit(void) ATTR_NORETURN;
|
|
|
|
|
|
|
|
unsigned long tor_get_thread_id(void);
|
|
|
|
void tor_threads_init(void);
|
|
|
|
|
2013-09-27 18:32:19 +02:00
|
|
|
/** Conditions need nonrecursive mutexes with pthreads. */
|
|
|
|
#define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
|
2013-09-25 17:41:40 +02:00
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
void set_main_thread(void);
|
|
|
|
int in_main_thread(void);
|
|
|
|
|
2013-09-23 04:08:41 +02:00
|
|
|
typedef struct tor_cond_t {
|
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_cond_t cond;
|
|
|
|
#elif defined(USE_WIN32_THREADS)
|
|
|
|
HANDLE event;
|
|
|
|
|
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
int n_waiting;
|
|
|
|
int n_to_wake;
|
|
|
|
int generation;
|
|
|
|
#else
|
|
|
|
#error no known condition implementation.
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(USE_PTHREADS) || ... */
|
2013-09-23 04:08:41 +02:00
|
|
|
} tor_cond_t;
|
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
tor_cond_t *tor_cond_new(void);
|
2017-11-17 18:27:25 +01:00
|
|
|
void tor_cond_free_(tor_cond_t *cond);
|
2017-12-07 16:44:04 +01:00
|
|
|
#define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
|
2013-09-23 07:15:30 +02:00
|
|
|
int tor_cond_init(tor_cond_t *cond);
|
|
|
|
void tor_cond_uninit(tor_cond_t *cond);
|
2013-09-23 04:08:41 +02:00
|
|
|
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
|
|
|
|
const struct timeval *tv);
|
2013-09-23 03:30:46 +02:00
|
|
|
void tor_cond_signal_one(tor_cond_t *cond);
|
|
|
|
void tor_cond_signal_all(tor_cond_t *cond);
|
|
|
|
|
2015-08-12 16:49:19 +02:00
|
|
|
typedef struct tor_threadlocal_s {
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD index;
|
|
|
|
#else
|
|
|
|
pthread_key_t key;
|
|
|
|
#endif
|
|
|
|
} tor_threadlocal_t;
|
|
|
|
|
2015-08-17 19:36:56 +02:00
|
|
|
/** Initialize a thread-local variable.
|
|
|
|
*
|
|
|
|
* After you call this function on a tor_threadlocal_t, you can call
|
|
|
|
* tor_threadlocal_set to change the current value of this variable for the
|
|
|
|
* current thread, and tor_threadlocal_get to retrieve the current value for
|
|
|
|
* the current thread. Each thread has its own value.
|
|
|
|
**/
|
2015-08-12 16:49:19 +02:00
|
|
|
int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
|
2015-08-17 19:36:56 +02:00
|
|
|
/**
|
|
|
|
* Release all resource associated with a thread-local variable.
|
|
|
|
*/
|
2015-08-12 16:49:19 +02:00
|
|
|
void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
|
2015-08-17 19:36:56 +02:00
|
|
|
/**
|
|
|
|
* Return the current value of a thread-local variable for this thread.
|
2015-08-18 14:36:58 +02:00
|
|
|
*
|
|
|
|
* It's undefined behavior to use this function if the threadlocal hasn't
|
|
|
|
* been initialized, or has been destroyed.
|
2015-08-17 19:36:56 +02:00
|
|
|
*/
|
2015-08-12 16:49:19 +02:00
|
|
|
void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
|
2015-08-17 19:36:56 +02:00
|
|
|
/**
|
|
|
|
* Change the current value of a thread-local variable for this thread to
|
|
|
|
* <b>value</b>.
|
2015-08-18 14:36:58 +02:00
|
|
|
*
|
|
|
|
* It's undefined behavior to use this function if the threadlocal hasn't
|
|
|
|
* been initialized, or has been destroyed.
|
2015-08-17 19:36:56 +02:00
|
|
|
*/
|
2015-08-12 16:49:19 +02:00
|
|
|
void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
|
|
|
|
|
2017-04-25 16:29:07 +02:00
|
|
|
/**
|
|
|
|
* Atomic counter type; holds a size_t value.
|
|
|
|
*/
|
2018-08-08 21:41:40 +02:00
|
|
|
#ifdef HAVE_WORKING_STDATOMIC
|
2017-10-23 18:35:30 +02:00
|
|
|
typedef struct atomic_counter_t {
|
|
|
|
atomic_size_t val;
|
|
|
|
} atomic_counter_t;
|
|
|
|
#define ATOMIC_LINKAGE static
|
2018-08-08 21:41:40 +02:00
|
|
|
#else /* !(defined(HAVE_WORKING_STDATOMIC)) */
|
2017-04-25 16:29:07 +02:00
|
|
|
typedef struct atomic_counter_t {
|
|
|
|
tor_mutex_t mutex;
|
|
|
|
size_t val;
|
|
|
|
} atomic_counter_t;
|
2017-10-23 18:35:30 +02:00
|
|
|
#define ATOMIC_LINKAGE
|
2018-08-08 21:41:40 +02:00
|
|
|
#endif /* defined(HAVE_WORKING_STDATOMIC) */
|
2017-10-23 18:35:30 +02:00
|
|
|
|
|
|
|
ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
|
|
|
|
ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
|
|
|
|
ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
|
|
|
|
ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
|
|
|
|
ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
|
|
|
|
ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
|
|
|
|
size_t newval);
|
|
|
|
#undef ATOMIC_LINKAGE
|
|
|
|
|
2018-08-08 21:41:40 +02:00
|
|
|
#ifdef HAVE_WORKING_STDATOMIC
|
2017-10-23 18:35:30 +02:00
|
|
|
/** Initialize a new atomic counter with the value 0 */
|
|
|
|
static inline void
|
|
|
|
atomic_counter_init(atomic_counter_t *counter)
|
|
|
|
{
|
|
|
|
atomic_init(&counter->val, 0);
|
|
|
|
}
|
|
|
|
/** Clean up all resources held by an atomic counter. */
|
|
|
|
static inline void
|
|
|
|
atomic_counter_destroy(atomic_counter_t *counter)
|
|
|
|
{
|
|
|
|
(void)counter;
|
|
|
|
}
|
|
|
|
/** Add a value to an atomic counter. */
|
|
|
|
static inline void
|
|
|
|
atomic_counter_add(atomic_counter_t *counter, size_t add)
|
|
|
|
{
|
|
|
|
(void) atomic_fetch_add(&counter->val, add);
|
|
|
|
}
|
|
|
|
/** Subtract a value from an atomic counter. */
|
|
|
|
static inline void
|
|
|
|
atomic_counter_sub(atomic_counter_t *counter, size_t sub)
|
|
|
|
{
|
|
|
|
(void) atomic_fetch_sub(&counter->val, sub);
|
|
|
|
}
|
|
|
|
/** Return the current value of an atomic counter */
|
|
|
|
static inline size_t
|
|
|
|
atomic_counter_get(atomic_counter_t *counter)
|
|
|
|
{
|
|
|
|
return atomic_load(&counter->val);
|
|
|
|
}
|
|
|
|
/** Replace the value of an atomic counter; return the old one. */
|
|
|
|
static inline size_t
|
|
|
|
atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
|
|
|
|
{
|
|
|
|
return atomic_exchange(&counter->val, newval);
|
|
|
|
}
|
2017-04-25 16:29:07 +02:00
|
|
|
|
2018-08-08 21:41:40 +02:00
|
|
|
#else /* !(defined(HAVE_WORKING_STDATOMIC)) */
|
|
|
|
#endif /* defined(HAVE_WORKING_STDATOMIC) */
|
2017-04-25 16:29:07 +02:00
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(TOR_COMPAT_THREADS_H) */
|