tor  0.4.1.0-alpha-dev
compat_pthreads.c
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 
13 #include "orconfig.h"
14 #include "lib/thread/threads.h"
15 #include "lib/wallclock/timeval.h"
16 #include "lib/log/log.h"
17 #include "lib/log/util_bug.h"
18 
19 #include <sys/time.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <time.h>
23 #include <errno.h>
24 #include <string.h>
25 
29 typedef struct tor_pthread_data_t {
30  void (*func)(void *);
31  void *data;
36 static void *
38 {
39  tor_pthread_data_t *data = _data;
40  void (*func)(void*);
41  void *arg;
42  /* mask signals to worker threads to avoid SIGPIPE, etc */
43  sigset_t sigs;
44  /* We're in a subthread; don't handle any signals here. */
45  sigfillset(&sigs);
46  pthread_sigmask(SIG_SETMASK, &sigs, NULL);
47 
48  func = data->func;
49  arg = data->data;
50  tor_free(_data);
51  func(arg);
52  return NULL;
53 }
57 static pthread_attr_t attr_detached;
59 static int threads_initialized = 0;
60 
71 int
72 spawn_func(void (*func)(void *), void *data)
73 {
74  pthread_t thread;
76  if (PREDICT_UNLIKELY(!threads_initialized)) {
78  }
79  d = tor_malloc(sizeof(tor_pthread_data_t));
80  d->data = data;
81  d->func = func;
82  if (pthread_create(&thread, &attr_detached, tor_pthread_helper_fn, d)) {
83  tor_free(d);
84  return -1;
85  }
86 
87  return 0;
88 }
89 
92 void
94 {
95  pthread_exit(NULL);
96 }
97 
99 unsigned long
101 {
102  union {
103  pthread_t thr;
104  unsigned long id;
105  } r;
106  r.thr = pthread_self();
107  return r.id;
108 }
109 
110 /* Conditions. */
111 
113 int
115 {
116  pthread_condattr_t condattr;
117 
118  memset(cond, 0, sizeof(tor_cond_t));
119  /* Default condition attribute. Might be used if clock monotonic is
120  * available else this won't affect anything. */
121  if (pthread_condattr_init(&condattr)) {
122  return -1;
123  }
124 
125 #if defined(HAVE_CLOCK_GETTIME)
126 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && \
127  defined(CLOCK_MONOTONIC)
128  /* Use monotonic time so when we timedwait() on it, any clock adjustment
129  * won't affect the timeout value. */
130  if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC)) {
131  return -1;
132  }
133 #define USE_COND_CLOCK CLOCK_MONOTONIC
134 #else /* !(defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && ...) */
135  /* On OSX Sierra, there is no pthread_condattr_setclock, so we are stuck
136  * with the realtime clock.
137  */
138 #define USE_COND_CLOCK CLOCK_REALTIME
139 #endif /* defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && ... */
140 #endif /* defined(HAVE_CLOCK_GETTIME) */
141  if (pthread_cond_init(&cond->cond, &condattr)) {
142  return -1;
143  }
144  return 0;
145 }
146 
149 void
151 {
152  if (pthread_cond_destroy(&cond->cond)) {
153  // LCOV_EXCL_START
154  log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
155  return;
156  // LCOV_EXCL_STOP
157  }
158 }
166 int
167 tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
168 {
169  int r;
170  if (tv == NULL) {
171  while (1) {
172  r = pthread_cond_wait(&cond->cond, &mutex->mutex);
173  if (r == EINTR) {
174  /* EINTR should be impossible according to POSIX, but POSIX, like the
175  * Pirate's Code, is apparently treated "more like what you'd call
176  * guidelines than actual rules." */
177  continue; // LCOV_EXCL_LINE
178  }
179  return r ? -1 : 0;
180  }
181  } else {
182  struct timeval tvnow, tvsum;
183  struct timespec ts;
184  while (1) {
185 #if defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK)
186  if (clock_gettime(USE_COND_CLOCK, &ts) < 0) {
187  return -1;
188  }
189  tvnow.tv_sec = ts.tv_sec;
190  tvnow.tv_usec = (int)(ts.tv_nsec / 1000);
191  timeradd(tv, &tvnow, &tvsum);
192 #else /* !(defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK)) */
193  if (gettimeofday(&tvnow, NULL) < 0)
194  return -1;
195  timeradd(tv, &tvnow, &tvsum);
196 #endif /* defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK) */
197 
198  ts.tv_sec = tvsum.tv_sec;
199  ts.tv_nsec = tvsum.tv_usec * 1000;
200 
201  r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts);
202  if (r == 0)
203  return 0;
204  else if (r == ETIMEDOUT)
205  return 1;
206  else if (r == EINTR)
207  continue;
208  else
209  return -1;
210  }
211  }
212 }
214 void
216 {
217  pthread_cond_signal(&cond->cond);
218 }
220 void
222 {
223  pthread_cond_broadcast(&cond->cond);
224 }
225 
226 int
228 {
229  int err = pthread_key_create(&threadlocal->key, NULL);
230  return err ? -1 : 0;
231 }
232 
233 void
235 {
236  pthread_key_delete(threadlocal->key);
237  memset(threadlocal, 0, sizeof(tor_threadlocal_t));
238 }
239 
240 void *
242 {
243  return pthread_getspecific(threadlocal->key);
244 }
245 
246 void
247 tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
248 {
249  int err = pthread_setspecific(threadlocal->key, value);
250  tor_assert(err == 0);
251 }
252 
254 void
256 {
257  if (!threads_initialized) {
258  tor_locking_init();
259  const int ret1 = pthread_attr_init(&attr_detached);
260  tor_assert(ret1 == 0);
261 #ifndef PTHREAD_CREATE_DETACHED
262 #define PTHREAD_CREATE_DETACHED 1
263 #endif
264  const int ret2 =
265  pthread_attr_setdetachstate(&attr_detached, PTHREAD_CREATE_DETACHED);
266  tor_assert(ret2 == 0);
268  set_main_thread();
269  }
270 }
#define timeradd(tv1, tv2, tvout)
Definition: timeval.h:26
void spawn_exit(void)
static int threads_initialized
void tor_cond_signal_all(tor_cond_t *cond)
void tor_cond_uninit(tor_cond_t *cond)
Definitions for timing-related constants.
#define LD_GENERAL
Definition: log.h:58
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
#define tor_free(p)
Definition: malloc.h:52
static void * tor_pthread_helper_fn(void *_data)
void tor_threads_init(void)
Header for threads.c.
int tor_threadlocal_init(tor_threadlocal_t *threadlocal)
Declarations for timeval-related macros that some platforms are missing.
tor_assert(buffer)
void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
void set_main_thread(void)
struct tor_pthread_data_t tor_pthread_data_t
int tor_cond_init(tor_cond_t *cond)
void * tor_threadlocal_get(tor_threadlocal_t *threadlocal)
Headers for log.c.
void tor_cond_signal_one(tor_cond_t *cond)
static pthread_attr_t attr_detached
unsigned long tor_get_thread_id(void)
Macros to manage assertions, fatal and non-fatal.
void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal)
int spawn_func(void(*func)(void *), void *data)