Merge branch 'bug26913_033' into maint-0.3.3
[tor.git] / src / common / compat_threads.h
blob8bf8225689fcbb458b0c56803fe8db2abe8075e3
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #ifndef TOR_COMPAT_THREADS_H
7 #define TOR_COMPAT_THREADS_H
9 #include "orconfig.h"
10 #include "torint.h"
11 #include "testsupport.h"
13 #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
14 #include <pthread.h>
15 #endif
17 #if defined(HAVE_STDATOMIC_H) && defined(STDATOMIC_WORKS)
18 #define HAVE_WORKING_STDATOMIC
19 #endif
21 #ifdef HAVE_WORKING_STDATOMIC
22 #include <stdatomic.h>
23 #endif
25 #if defined(_WIN32)
26 #define USE_WIN32_THREADS
27 #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
28 #define USE_PTHREADS
29 #else
30 #error "No threading system was found"
31 #endif /* defined(_WIN32) || ... */
33 int spawn_func(void (*func)(void *), void *data);
34 void spawn_exit(void) ATTR_NORETURN;
36 /* Because we use threads instead of processes on most platforms (Windows,
37 * Linux, etc), we need locking for them. On platforms with poor thread
38 * support or broken gethostbyname_r, these functions are no-ops. */
40 /** A generic lock structure for multithreaded builds. */
41 typedef struct tor_mutex_t {
42 #if defined(USE_WIN32_THREADS)
43 /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
44 CRITICAL_SECTION mutex;
45 #elif defined(USE_PTHREADS)
46 /** Pthreads-only: with pthreads, we implement locks with
47 * pthread_mutex_t. */
48 pthread_mutex_t mutex;
49 #else
50 /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
51 int _unused;
52 #endif /* defined(USE_WIN32_THREADS) || ... */
53 } tor_mutex_t;
55 tor_mutex_t *tor_mutex_new(void);
56 tor_mutex_t *tor_mutex_new_nonrecursive(void);
57 void tor_mutex_init(tor_mutex_t *m);
58 void tor_mutex_init_nonrecursive(tor_mutex_t *m);
59 void tor_mutex_acquire(tor_mutex_t *m);
60 void tor_mutex_release(tor_mutex_t *m);
61 void tor_mutex_free_(tor_mutex_t *m);
62 #define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
63 void tor_mutex_uninit(tor_mutex_t *m);
64 unsigned long tor_get_thread_id(void);
65 void tor_threads_init(void);
67 /** Conditions need nonrecursive mutexes with pthreads. */
68 #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
70 void set_main_thread(void);
71 int in_main_thread(void);
73 typedef struct tor_cond_t {
74 #ifdef USE_PTHREADS
75 pthread_cond_t cond;
76 #elif defined(USE_WIN32_THREADS)
77 HANDLE event;
79 CRITICAL_SECTION lock;
80 int n_waiting;
81 int n_to_wake;
82 int generation;
83 #else
84 #error no known condition implementation.
85 #endif /* defined(USE_PTHREADS) || ... */
86 } tor_cond_t;
88 tor_cond_t *tor_cond_new(void);
89 void tor_cond_free_(tor_cond_t *cond);
90 #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
91 int tor_cond_init(tor_cond_t *cond);
92 void tor_cond_uninit(tor_cond_t *cond);
93 int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
94 const struct timeval *tv);
95 void tor_cond_signal_one(tor_cond_t *cond);
96 void tor_cond_signal_all(tor_cond_t *cond);
98 /** Helper type used to manage waking up the main thread while it's in
99 * the libevent main loop. Used by the work queue code. */
100 typedef struct alert_sockets_s {
101 /* XXXX This structure needs a better name. */
102 /** Socket that the main thread should listen for EV_READ events on.
103 * Note that this socket may be a regular fd on a non-Windows platform.
105 tor_socket_t read_fd;
106 /** Socket to use when alerting the main thread. */
107 tor_socket_t write_fd;
108 /** Function to alert the main thread */
109 int (*alert_fn)(tor_socket_t write_fd);
110 /** Function to make the main thread no longer alerted. */
111 int (*drain_fn)(tor_socket_t read_fd);
112 } alert_sockets_t;
114 /* Flags to disable one or more alert_sockets backends. */
115 #define ASOCKS_NOEVENTFD2 (1u<<0)
116 #define ASOCKS_NOEVENTFD (1u<<1)
117 #define ASOCKS_NOPIPE2 (1u<<2)
118 #define ASOCKS_NOPIPE (1u<<3)
119 #define ASOCKS_NOSOCKETPAIR (1u<<4)
121 int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
122 void alert_sockets_close(alert_sockets_t *socks);
124 typedef struct tor_threadlocal_s {
125 #ifdef _WIN32
126 DWORD index;
127 #else
128 pthread_key_t key;
129 #endif
130 } tor_threadlocal_t;
132 /** Initialize a thread-local variable.
134 * After you call this function on a tor_threadlocal_t, you can call
135 * tor_threadlocal_set to change the current value of this variable for the
136 * current thread, and tor_threadlocal_get to retrieve the current value for
137 * the current thread. Each thread has its own value.
139 int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
141 * Release all resource associated with a thread-local variable.
143 void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
145 * Return the current value of a thread-local variable for this thread.
147 * It's undefined behavior to use this function if the threadlocal hasn't
148 * been initialized, or has been destroyed.
150 void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
152 * Change the current value of a thread-local variable for this thread to
153 * <b>value</b>.
155 * It's undefined behavior to use this function if the threadlocal hasn't
156 * been initialized, or has been destroyed.
158 void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
161 * Atomic counter type; holds a size_t value.
163 #ifdef HAVE_WORKING_STDATOMIC
164 typedef struct atomic_counter_t {
165 atomic_size_t val;
166 } atomic_counter_t;
167 #define ATOMIC_LINKAGE static
168 #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
169 typedef struct atomic_counter_t {
170 tor_mutex_t mutex;
171 size_t val;
172 } atomic_counter_t;
173 #define ATOMIC_LINKAGE
174 #endif /* defined(HAVE_WORKING_STDATOMIC) */
176 ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
177 ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
178 ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
179 ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
180 ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
181 ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
182 size_t newval);
183 #undef ATOMIC_LINKAGE
185 #ifdef HAVE_WORKING_STDATOMIC
186 /** Initialize a new atomic counter with the value 0 */
187 static inline void
188 atomic_counter_init(atomic_counter_t *counter)
190 atomic_init(&counter->val, 0);
192 /** Clean up all resources held by an atomic counter. */
193 static inline void
194 atomic_counter_destroy(atomic_counter_t *counter)
196 (void)counter;
198 /** Add a value to an atomic counter. */
199 static inline void
200 atomic_counter_add(atomic_counter_t *counter, size_t add)
202 (void) atomic_fetch_add(&counter->val, add);
204 /** Subtract a value from an atomic counter. */
205 static inline void
206 atomic_counter_sub(atomic_counter_t *counter, size_t sub)
208 (void) atomic_fetch_sub(&counter->val, sub);
210 /** Return the current value of an atomic counter */
211 static inline size_t
212 atomic_counter_get(atomic_counter_t *counter)
214 return atomic_load(&counter->val);
216 /** Replace the value of an atomic counter; return the old one. */
217 static inline size_t
218 atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
220 return atomic_exchange(&counter->val, newval);
223 #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
224 #endif /* defined(HAVE_WORKING_STDATOMIC) */
226 #endif /* !defined(TOR_COMPAT_THREADS_H) */