[tor-commits] [tor/master] Merge branch 'bug26779_033' into bug26779_035

nickm at torproject.org nickm at torproject.org
Wed Aug 15 06:16:35 UTC 2018


commit 26f1167e7151791a04a2c8584360f37602320779
Merge: 778715052 b66386865
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Aug 8 15:50:29 2018 -0400

    Merge branch 'bug26779_033' into bug26779_035

 changes/bug26779                |  4 ++++
 configure.ac                    | 20 ++++++++++++++++++++
 src/lib/thread/compat_threads.c |  4 ++--
 src/lib/thread/threads.h        | 18 +++++++++++-------
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --cc src/lib/thread/compat_threads.c
index 24129946b,000000000..7f1970af4
mode 100644,000000..100644
--- a/src/lib/thread/compat_threads.c
+++ b/src/lib/thread/compat_threads.c
@@@ -1,111 -1,0 +1,111 @@@
 +/* Copyright (c) 2003-2004, Roger Dingledine
 + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 + * Copyright (c) 2007-2018, The Tor Project, Inc. */
 +/* See LICENSE for licensing information */
 +
 +/**
 + * \file compat_threads.c
 + *
 + * \brief Cross-platform threading and inter-thread communication logic.
 + *  (Platform-specific parts are written in the other compat_*threads
 + *  modules.)
 + */
 +
 +#include "orconfig.h"
 +#include <stdlib.h>
 +#include "lib/thread/threads.h"
 +
 +#include "lib/log/log.h"
 +#include "lib/log/util_bug.h"
 +
 +#include <string.h>
 +
 +/** Allocate and return a new condition variable. */
 +tor_cond_t *
 +tor_cond_new(void)
 +{
 +  tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
 +  if (BUG(tor_cond_init(cond)<0))
 +    tor_free(cond); // LCOV_EXCL_LINE
 +  return cond;
 +}
 +
 +/** Free all storage held in <b>c</b>. */
 +void
 +tor_cond_free_(tor_cond_t *c)
 +{
 +  if (!c)
 +    return;
 +  tor_cond_uninit(c);
 +  tor_free(c);
 +}
 +
 +/** Identity of the "main" thread */
 +static unsigned long main_thread_id = -1;
 +
 +/** Start considering the current thread to be the 'main thread'.  This has
 + * no effect on anything besides in_main_thread(). */
 +void
 +set_main_thread(void)
 +{
 +  main_thread_id = tor_get_thread_id();
 +}
 +/** Return true iff called from the main thread. */
 +int
 +in_main_thread(void)
 +{
 +  return main_thread_id == tor_get_thread_id();
 +}
 +
- #ifndef HAVE_STDATOMIC_H
++#ifndef HAVE_WORKING_STDATOMIC
 +/** Initialize a new atomic counter with the value 0 */
 +void
 +atomic_counter_init(atomic_counter_t *counter)
 +{
 +  memset(counter, 0, sizeof(*counter));
 +  tor_mutex_init_nonrecursive(&counter->mutex);
 +}
 +/** Clean up all resources held by an atomic counter. */
 +void
 +atomic_counter_destroy(atomic_counter_t *counter)
 +{
 +  tor_mutex_uninit(&counter->mutex);
 +  memset(counter, 0, sizeof(*counter));
 +}
 +/** Add a value to an atomic counter. */
 +void
 +atomic_counter_add(atomic_counter_t *counter, size_t add)
 +{
 +  tor_mutex_acquire(&counter->mutex);
 +  counter->val += add;
 +  tor_mutex_release(&counter->mutex);
 +}
 +/** Subtract a value from an atomic counter. */
 +void
 +atomic_counter_sub(atomic_counter_t *counter, size_t sub)
 +{
 +  // this relies on unsigned overflow, but that's fine.
 +  atomic_counter_add(counter, -sub);
 +}
 +/** Return the current value of an atomic counter */
 +size_t
 +atomic_counter_get(atomic_counter_t *counter)
 +{
 +  size_t val;
 +  tor_mutex_acquire(&counter->mutex);
 +  val = counter->val;
 +  tor_mutex_release(&counter->mutex);
 +  return val;
 +}
 +/** Replace the value of an atomic counter; return the old one. */
 +size_t
 +atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
 +{
 +  size_t oldval;
 +  tor_mutex_acquire(&counter->mutex);
 +  oldval = counter->val;
 +  counter->val = newval;
 +  tor_mutex_release(&counter->mutex);
 +  return oldval;
 +}
- #endif /* !defined(HAVE_STDATOMIC_H) */
++#endif /* !defined(HAVE_WORKING_STDATOMIC) */
diff --cc src/lib/thread/threads.h
index 89d2a9d93,000000000..4d5191124
mode 100644,000000..100644
--- a/src/lib/thread/threads.h
+++ b/src/lib/thread/threads.h
@@@ -1,164 -1,0 +1,168 @@@
 +/* Copyright (c) 2003-2004, Roger Dingledine
 + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 + * Copyright (c) 2007-2018, The Tor Project, Inc. */
 +/* See LICENSE for licensing information */
 +
 +/**
 + * \file threads.h
 + * \brief Header for threads.c
 + **/
 +
 +#ifndef TOR_COMPAT_THREADS_H
 +#define TOR_COMPAT_THREADS_H
 +
 +#include "orconfig.h"
 +#include "lib/cc/torint.h"
 +#include "lib/testsupport/testsupport.h"
 +#include "lib/lock/compat_mutex.h"
 +
- #ifdef HAVE_STDATOMIC_H
++#if defined(HAVE_STDATOMIC_H) && defined(STDATOMIC_WORKS)
++#define HAVE_WORKING_STDATOMIC
++#endif
++
++#ifdef HAVE_WORKING_STDATOMIC
 +#include <stdatomic.h>
 +#endif
 +
 +struct timeval;
 +
 +int spawn_func(void (*func)(void *), void *data);
 +void spawn_exit(void) ATTR_NORETURN;
 +
 +unsigned long tor_get_thread_id(void);
 +void tor_threads_init(void);
 +
 +/** Conditions need nonrecursive mutexes with pthreads. */
 +#define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
 +
 +void set_main_thread(void);
 +int in_main_thread(void);
 +
 +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.
 +#endif /* defined(USE_PTHREADS) || ... */
 +} tor_cond_t;
 +
 +tor_cond_t *tor_cond_new(void);
 +void tor_cond_free_(tor_cond_t *cond);
 +#define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
 +int tor_cond_init(tor_cond_t *cond);
 +void tor_cond_uninit(tor_cond_t *cond);
 +int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
 +                  const struct timeval *tv);
 +void tor_cond_signal_one(tor_cond_t *cond);
 +void tor_cond_signal_all(tor_cond_t *cond);
 +
 +typedef struct tor_threadlocal_s {
 +#ifdef _WIN32
 +  DWORD index;
 +#else
 +  pthread_key_t key;
 +#endif
 +} tor_threadlocal_t;
 +
 +/** 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.
 + **/
 +int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
 +/**
 + * Release all resource associated with a thread-local variable.
 + */
 +void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
 +/**
 + * Return the current value of a thread-local variable for this thread.
 + *
 + * It's undefined behavior to use this function if the threadlocal hasn't
 + * been initialized, or has been destroyed.
 + */
 +void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
 +/**
 + * Change the current value of a thread-local variable for this thread to
 + * <b>value</b>.
 + *
 + * It's undefined behavior to use this function if the threadlocal hasn't
 + * been initialized, or has been destroyed.
 + */
 +void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
 +
 +/**
 + * Atomic counter type; holds a size_t value.
 + */
- #ifdef HAVE_STDATOMIC_H
++#ifdef HAVE_WORKING_STDATOMIC
 +typedef struct atomic_counter_t {
 +  atomic_size_t val;
 +} atomic_counter_t;
 +#define ATOMIC_LINKAGE static
- #else /* !(defined(HAVE_STDATOMIC_H)) */
++#else /* !(defined(HAVE_WORKING_STDATOMIC)) */
 +typedef struct atomic_counter_t {
 +  tor_mutex_t mutex;
 +  size_t val;
 +} atomic_counter_t;
 +#define ATOMIC_LINKAGE
- #endif /* defined(HAVE_STDATOMIC_H) */
++#endif /* defined(HAVE_WORKING_STDATOMIC) */
 +
 +ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
 +ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
 +ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
 +ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
 +ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
 +ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
 +                                              size_t newval);
 +#undef ATOMIC_LINKAGE
 +
- #ifdef HAVE_STDATOMIC_H
++#ifdef HAVE_WORKING_STDATOMIC
 +/** Initialize a new atomic counter with the value 0 */
 +static inline void
 +atomic_counter_init(atomic_counter_t *counter)
 +{
 +  atomic_init(&counter->val, 0);
 +}
 +/** Clean up all resources held by an atomic counter. */
 +static inline void
 +atomic_counter_destroy(atomic_counter_t *counter)
 +{
 +  (void)counter;
 +}
 +/** Add a value to an atomic counter. */
 +static inline void
 +atomic_counter_add(atomic_counter_t *counter, size_t add)
 +{
 +  (void) atomic_fetch_add(&counter->val, add);
 +}
 +/** Subtract a value from an atomic counter. */
 +static inline void
 +atomic_counter_sub(atomic_counter_t *counter, size_t sub)
 +{
 +  (void) atomic_fetch_sub(&counter->val, sub);
 +}
 +/** Return the current value of an atomic counter */
 +static inline size_t
 +atomic_counter_get(atomic_counter_t *counter)
 +{
 +  return atomic_load(&counter->val);
 +}
 +/** Replace the value of an atomic counter; return the old one. */
 +static inline size_t
 +atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
 +{
 +  return atomic_exchange(&counter->val, newval);
 +}
 +
- #else /* !(defined(HAVE_STDATOMIC_H)) */
- #endif /* defined(HAVE_STDATOMIC_H) */
++#else /* !(defined(HAVE_WORKING_STDATOMIC)) */
++#endif /* defined(HAVE_WORKING_STDATOMIC) */
 +
 +#endif /* !defined(TOR_COMPAT_THREADS_H) */





More information about the tor-commits mailing list