tor  0.4.2.0-alpha-dev
threads.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #ifndef TOR_COMPAT_THREADS_H
12 #define TOR_COMPAT_THREADS_H
13 
14 #include "orconfig.h"
15 #include "lib/cc/torint.h"
17 #include "lib/lock/compat_mutex.h"
18 
19 #if defined(HAVE_STDATOMIC_H) && defined(STDATOMIC_WORKS)
20 #define HAVE_WORKING_STDATOMIC
21 #endif
22 
23 #ifdef HAVE_WORKING_STDATOMIC
24 #include <stdatomic.h>
25 #endif
26 
27 struct timeval;
28 
29 int spawn_func(void (*func)(void *), void *data);
30 void spawn_exit(void) ATTR_NORETURN;
31 
32 unsigned long tor_get_thread_id(void);
33 void tor_threads_init(void);
34 
36 #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
37 
38 void set_main_thread(void);
39 int in_main_thread(void);
40 
41 typedef struct tor_cond_t {
42 #ifdef USE_PTHREADS
43  pthread_cond_t cond;
44 #elif defined(USE_WIN32_THREADS)
45  HANDLE event;
46 
47  CRITICAL_SECTION lock;
48  int n_waiting;
49  int n_to_wake;
50  int generation;
51 #else
52 #error no known condition implementation.
53 #endif /* defined(USE_PTHREADS) || ... */
54 } tor_cond_t;
55 
57 void tor_cond_free_(tor_cond_t *cond);
58 #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
59 int tor_cond_init(tor_cond_t *cond);
60 void tor_cond_uninit(tor_cond_t *cond);
61 int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
62  const struct timeval *tv);
65 
66 typedef struct tor_threadlocal_s {
67 #ifdef _WIN32
68  DWORD index;
69 #else
70  pthread_key_t key;
71 #endif
73 
81 int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
92 void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
100 void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
101 
105 #ifdef HAVE_WORKING_STDATOMIC
106 typedef struct atomic_counter_t {
107  atomic_size_t val;
109 #define ATOMIC_LINKAGE static
110 #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
111 typedef struct atomic_counter_t {
112  tor_mutex_t mutex;
113  size_t val;
115 #define ATOMIC_LINKAGE
116 #endif /* defined(HAVE_WORKING_STDATOMIC) */
117 
118 ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
119 ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
120 ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
121 ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
122 ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
123 ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
124  size_t newval);
125 #undef ATOMIC_LINKAGE
126 
127 #ifdef HAVE_WORKING_STDATOMIC
128 
129 static inline void
131 {
132  atomic_init(&counter->val, 0);
133 }
135 static inline void
137 {
138  (void)counter;
139 }
141 static inline void
142 atomic_counter_add(atomic_counter_t *counter, size_t add)
143 {
144  (void) atomic_fetch_add(&counter->val, add);
145 }
147 static inline void
148 atomic_counter_sub(atomic_counter_t *counter, size_t sub)
149 {
150  (void) atomic_fetch_sub(&counter->val, sub);
151 }
153 static inline size_t
155 {
156  return atomic_load(&counter->val);
157 }
159 static inline size_t
160 atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
161 {
162  return atomic_exchange(&counter->val, newval);
163 }
164 
165 #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
166 #endif /* defined(HAVE_WORKING_STDATOMIC) */
167 
168 #endif /* !defined(TOR_COMPAT_THREADS_H) */
void tor_cond_signal_one(tor_cond_t *cond)
ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal)
void tor_cond_signal_all(tor_cond_t *cond)
void tor_threads_init(void)
void tor_cond_uninit(tor_cond_t *cond)
void tor_cond_free_(tor_cond_t *cond)
ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter)
int tor_threadlocal_init(tor_threadlocal_t *threadlocal)
Integer definitions used throughout Tor.
void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
void set_main_thread(void)
ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter)
int tor_cond_init(tor_cond_t *cond)
int in_main_thread(void)
struct atomic_counter_t atomic_counter_t
unsigned long tor_get_thread_id(void)
void * tor_threadlocal_get(tor_threadlocal_t *threadlocal)
int spawn_func(void(*func)(void *), void *data)
Header for compat_mutex.c.
ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add)
void spawn_exit(void) ATTR_NORETURN
Macros to implement mocking and selective exposure for the test code.
tor_cond_t * tor_cond_new(void)
ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub)
ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter)