2013-09-23 03:30:46 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2013-09-23 03:30:46 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#ifndef TOR_COMPAT_THREADS_H
|
|
|
|
#define TOR_COMPAT_THREADS_H
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include "torint.h"
|
|
|
|
#include "testsupport.h"
|
|
|
|
|
|
|
|
#if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#define USE_WIN32_THREADS
|
|
|
|
#elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
|
|
|
|
#define USE_PTHREADS
|
|
|
|
#else
|
|
|
|
#error "No threading system was found"
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) || ... */
|
2013-09-23 03:30:46 +02:00
|
|
|
|
|
|
|
int spawn_func(void (*func)(void *), void *data);
|
|
|
|
void spawn_exit(void) ATTR_NORETURN;
|
|
|
|
|
|
|
|
/* Because we use threads instead of processes on most platforms (Windows,
|
|
|
|
* Linux, etc), we need locking for them. On platforms with poor thread
|
|
|
|
* support or broken gethostbyname_r, these functions are no-ops. */
|
|
|
|
|
|
|
|
/** A generic lock structure for multithreaded builds. */
|
|
|
|
typedef struct tor_mutex_t {
|
|
|
|
#if defined(USE_WIN32_THREADS)
|
|
|
|
/** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
|
|
|
|
CRITICAL_SECTION mutex;
|
|
|
|
#elif defined(USE_PTHREADS)
|
|
|
|
/** Pthreads-only: with pthreads, we implement locks with
|
|
|
|
* pthread_mutex_t. */
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
#else
|
|
|
|
/** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
|
|
|
|
int _unused;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(USE_WIN32_THREADS) || ... */
|
2013-09-23 03:30:46 +02:00
|
|
|
} tor_mutex_t;
|
|
|
|
|
|
|
|
tor_mutex_t *tor_mutex_new(void);
|
2013-09-27 18:32:19 +02:00
|
|
|
tor_mutex_t *tor_mutex_new_nonrecursive(void);
|
2013-09-23 03:30:46 +02:00
|
|
|
void tor_mutex_init(tor_mutex_t *m);
|
2013-09-27 18:32:19 +02:00
|
|
|
void tor_mutex_init_nonrecursive(tor_mutex_t *m);
|
2013-09-23 03:30:46 +02:00
|
|
|
void tor_mutex_acquire(tor_mutex_t *m);
|
|
|
|
void tor_mutex_release(tor_mutex_t *m);
|
|
|
|
void tor_mutex_free(tor_mutex_t *m);
|
|
|
|
void tor_mutex_uninit(tor_mutex_t *m);
|
|
|
|
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);
|
|
|
|
void tor_cond_free(tor_cond_t *cond);
|
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);
|
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Helper type used to manage waking up the main thread while it's in
|
|
|
|
* the libevent main loop. Used by the work queue code. */
|
2013-09-25 02:43:48 +02:00
|
|
|
typedef struct alert_sockets_s {
|
2013-09-25 17:05:27 +02:00
|
|
|
/* XXXX This structure needs a better name. */
|
|
|
|
/** Socket that the main thread should listen for EV_READ events on.
|
|
|
|
* Note that this socket may be a regular fd on a non-Windows platform.
|
|
|
|
*/
|
2013-09-25 02:43:48 +02:00
|
|
|
tor_socket_t read_fd;
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Socket to use when alerting the main thread. */
|
2013-09-25 02:43:48 +02:00
|
|
|
tor_socket_t write_fd;
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Function to alert the main thread */
|
2013-09-25 02:43:48 +02:00
|
|
|
int (*alert_fn)(tor_socket_t write_fd);
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Function to make the main thread no longer alerted. */
|
2013-09-25 02:43:48 +02:00
|
|
|
int (*drain_fn)(tor_socket_t read_fd);
|
|
|
|
} alert_sockets_t;
|
|
|
|
|
2013-09-25 20:31:59 +02:00
|
|
|
/* Flags to disable one or more alert_sockets backends. */
|
|
|
|
#define ASOCKS_NOEVENTFD2 (1u<<0)
|
|
|
|
#define ASOCKS_NOEVENTFD (1u<<1)
|
|
|
|
#define ASOCKS_NOPIPE2 (1u<<2)
|
|
|
|
#define ASOCKS_NOPIPE (1u<<3)
|
|
|
|
#define ASOCKS_NOSOCKETPAIR (1u<<4)
|
|
|
|
|
|
|
|
int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
|
2013-09-25 17:05:27 +02:00
|
|
|
void alert_sockets_close(alert_sockets_t *socks);
|
2013-09-25 02:43:48 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
typedef struct atomic_counter_t {
|
|
|
|
tor_mutex_t mutex;
|
|
|
|
size_t val;
|
|
|
|
} atomic_counter_t;
|
|
|
|
|
|
|
|
void atomic_counter_init(atomic_counter_t *counter);
|
|
|
|
void atomic_counter_destroy(atomic_counter_t *counter);
|
|
|
|
void atomic_counter_add(atomic_counter_t *counter, size_t add);
|
|
|
|
void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
|
|
|
|
size_t atomic_counter_get(atomic_counter_t *counter);
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(TOR_COMPAT_THREADS_H) */
|
2013-09-28 06:52:28 +02:00
|
|
|
|