2013-09-22 21:30:46 -04:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2018-06-20 08:13:28 -04:00
|
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
2013-09-22 21:30:46 -04:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2016-02-27 18:19:57 +01:00
|
|
|
/**
|
|
|
|
* \file compat_threads.c
|
|
|
|
*
|
|
|
|
* \brief Cross-platform threading and inter-thread communication logic.
|
|
|
|
* (Platform-specific parts are written in the other compat_*threads
|
|
|
|
* modules.)
|
|
|
|
*/
|
|
|
|
|
2013-09-24 20:43:48 -04:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include <stdlib.h>
|
2018-06-20 09:35:05 -04:00
|
|
|
#include "common/compat.h"
|
|
|
|
#include "common/compat_threads.h"
|
2013-09-24 20:43:48 -04:00
|
|
|
|
2018-06-20 09:35:05 -04:00
|
|
|
#include "common/util.h"
|
2018-06-22 10:32:10 -04:00
|
|
|
#include "lib/log/torlog.h"
|
2013-09-24 20:43:48 -04:00
|
|
|
|
2013-09-25 11:05:27 -04:00
|
|
|
/** Allocate and return a new condition variable. */
|
2013-09-23 01:15:30 -04:00
|
|
|
tor_cond_t *
|
|
|
|
tor_cond_new(void)
|
|
|
|
{
|
|
|
|
tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
|
2016-06-08 17:30:22 -04:00
|
|
|
if (BUG(tor_cond_init(cond)<0))
|
|
|
|
tor_free(cond); // LCOV_EXCL_LINE
|
2013-09-23 01:15:30 -04:00
|
|
|
return cond;
|
|
|
|
}
|
2013-09-25 11:05:27 -04:00
|
|
|
|
|
|
|
/** Free all storage held in <b>c</b>. */
|
2013-09-23 01:15:30 -04:00
|
|
|
void
|
2017-11-17 12:27:25 -05:00
|
|
|
tor_cond_free_(tor_cond_t *c)
|
2013-09-23 01:15:30 -04:00
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
tor_cond_uninit(c);
|
|
|
|
tor_free(c);
|
|
|
|
}
|
|
|
|
|
2013-09-22 21:30:46 -04:00
|
|
|
/** 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();
|
|
|
|
}
|
2013-09-24 20:43:48 -04:00
|
|
|
|
2017-10-23 12:35:30 -04:00
|
|
|
#ifndef HAVE_STDATOMIC_H
|
2017-04-25 10:29:07 -04:00
|
|
|
/** Initialize a new atomic counter with the value 0 */
|
|
|
|
void
|
|
|
|
atomic_counter_init(atomic_counter_t *counter)
|
|
|
|
{
|
2017-04-26 08:48:24 +02:00
|
|
|
memset(counter, 0, sizeof(*counter));
|
2017-04-25 10:29:07 -04:00
|
|
|
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;
|
|
|
|
}
|
2017-10-23 12:35:30 -04:00
|
|
|
/** 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;
|
|
|
|
}
|
2017-11-21 14:07:43 -05:00
|
|
|
#endif /* !defined(HAVE_STDATOMIC_H) */
|