]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Merge branch 'bug26779_033' into bug26779_035
authorNick Mathewson <nickm@torproject.org>
Wed, 8 Aug 2018 19:50:29 +0000 (15:50 -0400)
committerNick Mathewson <nickm@torproject.org>
Wed, 8 Aug 2018 19:50:29 +0000 (15:50 -0400)
1  2 
configure.ac
src/lib/thread/compat_threads.c
src/lib/thread/threads.h

diff --cc configure.ac
Simple merge
index 24129946b2b532584c8faec4e4d15361f1ff0ade,0000000000000000000000000000000000000000..7f1970af45fbf3cfd62988e5549639c25c89df75
mode 100644,000000..100644
--- /dev/null
@@@ -1,111 -1,0 +1,111 @@@
- #ifndef HAVE_STDATOMIC_H
 +/* 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();
 +}
 +
- #endif /* !defined(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_WORKING_STDATOMIC) */
index 89d2a9d93e5d3d705a15ec47872e500e5dc7346d,8bf8225689fcbb458b0c56803fe8db2abe8075e3..4d5191124c20c1d7c4e3470a304230933bc8e4c9
  #define TOR_COMPAT_THREADS_H
  
  #include "orconfig.h"
 -#include "torint.h"
 -#include "testsupport.h"
 -
 -#if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
 -#include <pthread.h>
 -#endif
 +#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