prop250: Initialize the SR subsystem and us it!
[tor.git] / src / common / compat_threads.h
blob171a9f93ffccbcc1a8c0ac31613992715fc0f214
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, 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(_WIN32)
18 #define USE_WIN32_THREADS
19 #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
20 #define USE_PTHREADS
21 #else
22 #error "No threading system was found"
23 #endif
25 int spawn_func(void (*func)(void *), void *data);
26 void spawn_exit(void) ATTR_NORETURN;
28 /* Because we use threads instead of processes on most platforms (Windows,
29 * Linux, etc), we need locking for them. On platforms with poor thread
30 * support or broken gethostbyname_r, these functions are no-ops. */
32 /** A generic lock structure for multithreaded builds. */
33 typedef struct tor_mutex_t {
34 #if defined(USE_WIN32_THREADS)
35 /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
36 CRITICAL_SECTION mutex;
37 #elif defined(USE_PTHREADS)
38 /** Pthreads-only: with pthreads, we implement locks with
39 * pthread_mutex_t. */
40 pthread_mutex_t mutex;
41 #else
42 /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
43 int _unused;
44 #endif
45 } tor_mutex_t;
47 tor_mutex_t *tor_mutex_new(void);
48 tor_mutex_t *tor_mutex_new_nonrecursive(void);
49 void tor_mutex_init(tor_mutex_t *m);
50 void tor_mutex_init_nonrecursive(tor_mutex_t *m);
51 void tor_mutex_acquire(tor_mutex_t *m);
52 void tor_mutex_release(tor_mutex_t *m);
53 void tor_mutex_free(tor_mutex_t *m);
54 void tor_mutex_uninit(tor_mutex_t *m);
55 unsigned long tor_get_thread_id(void);
56 void tor_threads_init(void);
58 /** Conditions need nonrecursive mutexes with pthreads. */
59 #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
61 void set_main_thread(void);
62 int in_main_thread(void);
64 typedef struct tor_cond_t {
65 #ifdef USE_PTHREADS
66 pthread_cond_t cond;
67 #elif defined(USE_WIN32_THREADS)
68 HANDLE event;
70 CRITICAL_SECTION lock;
71 int n_waiting;
72 int n_to_wake;
73 int generation;
74 #else
75 #error no known condition implementation.
76 #endif
77 } tor_cond_t;
79 tor_cond_t *tor_cond_new(void);
80 void tor_cond_free(tor_cond_t *cond);
81 int tor_cond_init(tor_cond_t *cond);
82 void tor_cond_uninit(tor_cond_t *cond);
83 int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
84 const struct timeval *tv);
85 void tor_cond_signal_one(tor_cond_t *cond);
86 void tor_cond_signal_all(tor_cond_t *cond);
88 /** Helper type used to manage waking up the main thread while it's in
89 * the libevent main loop. Used by the work queue code. */
90 typedef struct alert_sockets_s {
91 /* XXXX This structure needs a better name. */
92 /** Socket that the main thread should listen for EV_READ events on.
93 * Note that this socket may be a regular fd on a non-Windows platform.
95 tor_socket_t read_fd;
96 /** Socket to use when alerting the main thread. */
97 tor_socket_t write_fd;
98 /** Function to alert the main thread */
99 int (*alert_fn)(tor_socket_t write_fd);
100 /** Function to make the main thread no longer alerted. */
101 int (*drain_fn)(tor_socket_t read_fd);
102 } alert_sockets_t;
104 /* Flags to disable one or more alert_sockets backends. */
105 #define ASOCKS_NOEVENTFD2 (1u<<0)
106 #define ASOCKS_NOEVENTFD (1u<<1)
107 #define ASOCKS_NOPIPE2 (1u<<2)
108 #define ASOCKS_NOPIPE (1u<<3)
109 #define ASOCKS_NOSOCKETPAIR (1u<<4)
111 int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
112 void alert_sockets_close(alert_sockets_t *socks);
114 typedef struct tor_threadlocal_s {
115 #ifdef _WIN32
116 DWORD index;
117 #else
118 pthread_key_t key;
119 #endif
120 } tor_threadlocal_t;
122 /** Initialize a thread-local variable.
124 * After you call this function on a tor_threadlocal_t, you can call
125 * tor_threadlocal_set to change the current value of this variable for the
126 * current thread, and tor_threadlocal_get to retrieve the current value for
127 * the current thread. Each thread has its own value.
129 int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
131 * Release all resource associated with a thread-local variable.
133 void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
135 * Return the current value of a thread-local variable for this thread.
137 * It's undefined behavior to use this function if the threadlocal hasn't
138 * been initialized, or has been destroyed.
140 void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
142 * Change the current value of a thread-local variable for this thread to
143 * <b>value</b>.
145 * It's undefined behavior to use this function if the threadlocal hasn't
146 * been initialized, or has been destroyed.
148 void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
150 #endif