2013-09-23 03:30:46 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2013-09-23 03:30:46 +02: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-25 02:43:48 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include <stdlib.h>
|
2013-09-23 03:30:46 +02:00
|
|
|
#include "compat.h"
|
2013-09-25 02:43:48 +02:00
|
|
|
#include "compat_threads.h"
|
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
#include "util.h"
|
2013-09-25 02:43:48 +02:00
|
|
|
#include "torlog.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2013-09-26 05:25:02 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2013-09-23 03:30:46 +02:00
|
|
|
|
|
|
|
/** Return a newly allocated, ready-for-use mutex. */
|
|
|
|
tor_mutex_t *
|
|
|
|
tor_mutex_new(void)
|
|
|
|
{
|
|
|
|
tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
|
|
|
|
tor_mutex_init(m);
|
|
|
|
return m;
|
|
|
|
}
|
2013-09-25 17:41:40 +02:00
|
|
|
/** Return a newly allocated, ready-for-use mutex. This one might be
|
2013-09-27 18:32:19 +02:00
|
|
|
* non-recursive, if that's faster. */
|
2013-09-25 17:41:40 +02:00
|
|
|
tor_mutex_t *
|
2013-09-27 18:32:19 +02:00
|
|
|
tor_mutex_new_nonrecursive(void)
|
2013-09-25 17:41:40 +02:00
|
|
|
{
|
|
|
|
tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
|
2013-09-27 18:32:19 +02:00
|
|
|
tor_mutex_init_nonrecursive(m);
|
2013-09-25 17:41:40 +02:00
|
|
|
return m;
|
|
|
|
}
|
2013-09-23 03:30:46 +02:00
|
|
|
/** Release all storage and system resources held by <b>m</b>. */
|
|
|
|
void
|
2017-11-17 18:27:25 +01:00
|
|
|
tor_mutex_free_(tor_mutex_t *m)
|
2013-09-23 03:30:46 +02:00
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
tor_mutex_uninit(m);
|
|
|
|
tor_free(m);
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Allocate and return a new condition variable. */
|
2013-09-23 07:15:30 +02:00
|
|
|
tor_cond_t *
|
|
|
|
tor_cond_new(void)
|
|
|
|
{
|
|
|
|
tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
|
2016-06-08 23:30:22 +02:00
|
|
|
if (BUG(tor_cond_init(cond)<0))
|
|
|
|
tor_free(cond); // LCOV_EXCL_LINE
|
2013-09-23 07:15:30 +02:00
|
|
|
return cond;
|
|
|
|
}
|
2013-09-25 17:05:27 +02:00
|
|
|
|
|
|
|
/** Free all storage held in <b>c</b>. */
|
2013-09-23 07:15:30 +02:00
|
|
|
void
|
2017-11-17 18:27:25 +01:00
|
|
|
tor_cond_free_(tor_cond_t *c)
|
2013-09-23 07:15:30 +02:00
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
tor_cond_uninit(c);
|
|
|
|
tor_free(c);
|
|
|
|
}
|
|
|
|
|
2013-09-23 03:30:46 +02: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-25 02:43:48 +02:00
|
|
|
|
2015-01-21 18:18:11 +01:00
|
|
|
#if defined(HAVE_EVENTFD) || defined(HAVE_PIPE)
|
2017-03-15 16:53:01 +01:00
|
|
|
/* As write(), but retry on EINTR, and return the negative error code on
|
|
|
|
* error. */
|
2015-01-21 18:18:11 +01:00
|
|
|
static int
|
|
|
|
write_ni(int fd, const void *buf, size_t n)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
again:
|
2015-01-22 20:22:39 +01:00
|
|
|
r = (int) write(fd, buf, n);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
else
|
|
|
|
return -errno;
|
|
|
|
}
|
2015-01-21 18:18:11 +01:00
|
|
|
return r;
|
|
|
|
}
|
2017-03-15 16:53:01 +01:00
|
|
|
/* As read(), but retry on EINTR, and return the negative error code on error.
|
|
|
|
*/
|
2015-01-21 18:18:11 +01:00
|
|
|
static int
|
|
|
|
read_ni(int fd, void *buf, size_t n)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
again:
|
2015-01-22 20:22:39 +01:00
|
|
|
r = (int) read(fd, buf, n);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
else
|
|
|
|
return -errno;
|
|
|
|
}
|
2015-01-21 18:18:11 +01:00
|
|
|
return r;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_EVENTFD) || defined(HAVE_PIPE) */
|
2015-01-21 18:18:11 +01:00
|
|
|
|
2017-03-15 16:53:01 +01:00
|
|
|
/** As send(), but retry on EINTR, and return the negative error code on
|
|
|
|
* error. */
|
2015-01-21 18:18:11 +01:00
|
|
|
static int
|
|
|
|
send_ni(int fd, const void *buf, size_t n, int flags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
again:
|
2015-01-22 20:22:39 +01:00
|
|
|
r = (int) send(fd, buf, n, flags);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0) {
|
|
|
|
int error = tor_socket_errno(fd);
|
|
|
|
if (ERRNO_IS_EINTR(error))
|
|
|
|
goto again;
|
|
|
|
else
|
|
|
|
return -error;
|
|
|
|
}
|
2015-01-21 18:18:11 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-03-15 16:53:01 +01:00
|
|
|
/** As recv(), but retry on EINTR, and return the negative error code on
|
|
|
|
* error. */
|
2015-01-21 18:18:11 +01:00
|
|
|
static int
|
|
|
|
recv_ni(int fd, void *buf, size_t n, int flags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
again:
|
2015-08-07 15:12:33 +02:00
|
|
|
r = (int) recv(fd, buf, n, flags);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0) {
|
|
|
|
int error = tor_socket_errno(fd);
|
|
|
|
if (ERRNO_IS_EINTR(error))
|
|
|
|
goto again;
|
|
|
|
else
|
|
|
|
return -error;
|
|
|
|
}
|
2015-01-21 18:18:11 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-09-25 02:43:48 +02:00
|
|
|
#ifdef HAVE_EVENTFD
|
2015-08-07 15:12:33 +02:00
|
|
|
/* Increment the event count on an eventfd <b>fd</b> */
|
2013-09-25 02:43:48 +02:00
|
|
|
static int
|
|
|
|
eventfd_alert(int fd)
|
|
|
|
{
|
|
|
|
uint64_t u = 1;
|
2015-01-21 18:18:11 +01:00
|
|
|
int r = write_ni(fd, (void*)&u, sizeof(u));
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0 && -r != EAGAIN)
|
2013-09-25 02:43:48 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:12:33 +02:00
|
|
|
/* Drain all events from an eventfd <b>fd</b>. */
|
2013-09-25 02:43:48 +02:00
|
|
|
static int
|
|
|
|
eventfd_drain(int fd)
|
|
|
|
{
|
|
|
|
uint64_t u = 0;
|
2015-01-21 18:18:11 +01:00
|
|
|
int r = read_ni(fd, (void*)&u, sizeof(u));
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0 && -r != EAGAIN)
|
|
|
|
return r;
|
2013-09-25 02:43:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_EVENTFD) */
|
2013-09-25 02:43:48 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_PIPE
|
2015-08-07 15:12:33 +02:00
|
|
|
/** Send a byte over a pipe. Return 0 on success or EAGAIN; -1 on error */
|
2013-09-25 02:43:48 +02:00
|
|
|
static int
|
|
|
|
pipe_alert(int fd)
|
|
|
|
{
|
2015-01-22 20:22:39 +01:00
|
|
|
ssize_t r = write_ni(fd, "x", 1);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0 && -r != EAGAIN)
|
2017-03-16 20:34:57 +01:00
|
|
|
return (int)r;
|
2013-09-25 02:43:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:12:33 +02:00
|
|
|
/** Drain all input from a pipe <b>fd</b> and ignore it. Return 0 on
|
|
|
|
* success, -1 on error. */
|
2013-09-25 02:43:48 +02:00
|
|
|
static int
|
|
|
|
pipe_drain(int fd)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
ssize_t r;
|
2015-01-30 20:59:48 +01:00
|
|
|
do {
|
|
|
|
r = read_ni(fd, buf, sizeof(buf));
|
|
|
|
} while (r > 0);
|
2015-01-30 21:18:40 +01:00
|
|
|
if (r < 0 && errno != EAGAIN)
|
2017-03-15 16:53:01 +01:00
|
|
|
return -errno;
|
2015-01-30 20:59:48 +01:00
|
|
|
/* A value of r = 0 means EOF on the fd so successfully drained. */
|
2013-09-25 02:43:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_PIPE) */
|
2013-09-25 02:43:48 +02:00
|
|
|
|
2015-08-07 15:12:33 +02:00
|
|
|
/** Send a byte on socket <b>fd</b>t. Return 0 on success or EAGAIN,
|
|
|
|
* -1 on error. */
|
2013-09-25 02:43:48 +02:00
|
|
|
static int
|
|
|
|
sock_alert(tor_socket_t fd)
|
|
|
|
{
|
2015-01-21 18:18:11 +01:00
|
|
|
ssize_t r = send_ni(fd, "x", 1, 0);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0 && !ERRNO_IS_EAGAIN(-r))
|
2017-03-16 20:34:57 +01:00
|
|
|
return (int)r;
|
2013-09-25 02:43:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:12:33 +02:00
|
|
|
/** Drain all the input from a socket <b>fd</b>, and ignore it. Return 0 on
|
2017-03-15 16:53:01 +01:00
|
|
|
* success, -errno on error. */
|
2013-09-25 02:43:48 +02:00
|
|
|
static int
|
|
|
|
sock_drain(tor_socket_t fd)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
ssize_t r;
|
2015-01-30 20:59:48 +01:00
|
|
|
do {
|
|
|
|
r = recv_ni(fd, buf, sizeof(buf), 0);
|
|
|
|
} while (r > 0);
|
2017-03-15 16:53:01 +01:00
|
|
|
if (r < 0 && !ERRNO_IS_EAGAIN(-r))
|
2017-03-16 20:34:57 +01:00
|
|
|
return (int)r;
|
2015-01-30 20:59:48 +01:00
|
|
|
/* A value of r = 0 means EOF on the fd so successfully drained. */
|
2013-09-25 02:43:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Allocate a new set of alert sockets, and set the appropriate function
|
|
|
|
* pointers, in <b>socks_out</b>. */
|
2013-09-25 02:43:48 +02:00
|
|
|
int
|
2013-09-25 20:31:59 +02:00
|
|
|
alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
|
2013-09-25 02:43:48 +02:00
|
|
|
{
|
2013-09-25 20:31:59 +02:00
|
|
|
tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
|
2013-09-25 02:43:48 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_EVENTFD
|
2013-09-25 17:05:27 +02:00
|
|
|
/* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
|
|
|
|
* associated with a single file descriptor. */
|
2013-09-25 02:43:48 +02:00
|
|
|
#if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOEVENTFD2))
|
|
|
|
socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
|
2013-09-25 02:43:48 +02:00
|
|
|
#endif
|
2013-09-25 20:31:59 +02:00
|
|
|
if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
|
2013-09-25 02:43:48 +02:00
|
|
|
socks[0] = eventfd(0,0);
|
|
|
|
if (socks[0] >= 0) {
|
|
|
|
if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[0]) < 0) {
|
2016-06-08 23:30:22 +02:00
|
|
|
// LCOV_EXCL_START -- if eventfd succeeds, fcntl will.
|
|
|
|
tor_assert_nonfatal_unreached();
|
2013-09-25 02:43:48 +02:00
|
|
|
close(socks[0]);
|
|
|
|
return -1;
|
2016-06-08 23:30:22 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2013-09-25 02:43:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (socks[0] >= 0) {
|
|
|
|
socks_out->read_fd = socks_out->write_fd = socks[0];
|
|
|
|
socks_out->alert_fn = eventfd_alert;
|
|
|
|
socks_out->drain_fn = eventfd_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_EVENTFD) */
|
2013-09-25 02:43:48 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_PIPE2
|
2013-09-25 17:05:27 +02:00
|
|
|
/* Now we're going to try pipes. First type the pipe2() syscall, if we
|
|
|
|
* have it, so we can save some calls... */
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOPIPE2) &&
|
|
|
|
pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
|
2013-09-25 02:43:48 +02:00
|
|
|
socks_out->read_fd = socks[0];
|
|
|
|
socks_out->write_fd = socks[1];
|
|
|
|
socks_out->alert_fn = pipe_alert;
|
|
|
|
socks_out->drain_fn = pipe_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_PIPE2) */
|
2013-09-25 02:43:48 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_PIPE
|
2013-09-25 17:05:27 +02:00
|
|
|
/* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
|
|
|
|
* socketpairs, fwict. */
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOPIPE) &&
|
|
|
|
pipe(socks) == 0) {
|
2013-09-25 02:43:48 +02:00
|
|
|
if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
|
|
|
|
fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[0]) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[1]) < 0) {
|
2016-06-08 23:30:22 +02:00
|
|
|
// LCOV_EXCL_START -- if pipe succeeds, you can fcntl the output
|
|
|
|
tor_assert_nonfatal_unreached();
|
2013-09-25 02:43:48 +02:00
|
|
|
close(socks[0]);
|
|
|
|
close(socks[1]);
|
|
|
|
return -1;
|
2016-06-08 23:30:22 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2013-09-25 02:43:48 +02:00
|
|
|
}
|
|
|
|
socks_out->read_fd = socks[0];
|
|
|
|
socks_out->write_fd = socks[1];
|
|
|
|
socks_out->alert_fn = pipe_alert;
|
|
|
|
socks_out->drain_fn = pipe_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_PIPE) */
|
2013-09-25 02:43:48 +02:00
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/* If nothing else worked, fall back on socketpair(). */
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOSOCKETPAIR) &&
|
|
|
|
tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
|
2013-09-25 17:05:27 +02:00
|
|
|
if (set_socket_nonblocking(socks[0]) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[1])) {
|
2016-06-08 23:30:22 +02:00
|
|
|
// LCOV_EXCL_START -- if socketpair worked, you can make it nonblocking.
|
|
|
|
tor_assert_nonfatal_unreached();
|
2013-09-25 17:05:27 +02:00
|
|
|
tor_close_socket(socks[0]);
|
|
|
|
tor_close_socket(socks[1]);
|
|
|
|
return -1;
|
2016-06-08 23:30:22 +02:00
|
|
|
// LCOV_EXCL_STOP
|
2013-09-25 17:05:27 +02:00
|
|
|
}
|
|
|
|
socks_out->read_fd = socks[0];
|
|
|
|
socks_out->write_fd = socks[1];
|
2013-09-25 02:43:48 +02:00
|
|
|
socks_out->alert_fn = sock_alert;
|
|
|
|
socks_out->drain_fn = sock_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-25 17:05:27 +02:00
|
|
|
|
|
|
|
/** Close the sockets in <b>socks</b>. */
|
|
|
|
void
|
|
|
|
alert_sockets_close(alert_sockets_t *socks)
|
|
|
|
{
|
|
|
|
if (socks->alert_fn == sock_alert) {
|
|
|
|
/* they are sockets. */
|
|
|
|
tor_close_socket(socks->read_fd);
|
|
|
|
tor_close_socket(socks->write_fd);
|
|
|
|
} else {
|
|
|
|
close(socks->read_fd);
|
|
|
|
if (socks->write_fd != socks->read_fd)
|
|
|
|
close(socks->write_fd);
|
|
|
|
}
|
|
|
|
socks->read_fd = socks->write_fd = -1;
|
|
|
|
}
|
2013-09-28 06:52:28 +02:00
|
|
|
|
2017-10-23 18:35:30 +02:00
|
|
|
#ifndef HAVE_STDATOMIC_H
|
2017-04-25 16:29:07 +02: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 16:29:07 +02: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 18:35:30 +02: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 20:07:43 +01:00
|
|
|
#endif /* !defined(HAVE_STDATOMIC_H) */
|
2017-04-25 16:54:34 +02:00
|
|
|
|