diff options
author | Nick Mathewson <nickm@torproject.org> | 2017-10-23 12:35:30 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-11-21 14:06:48 -0500 |
commit | 25f882a9cfe39ccb2942fe55f41cdcbfd1ba288e (patch) | |
tree | 340150a7a7641addea2dd9fd52724ebd7be71296 /src/common/compat_threads.c | |
parent | 880d596f4dbb65885ebbc8b1b88e852529779324 (diff) | |
download | tor-25f882a9cfe39ccb2942fe55f41cdcbfd1ba288e.tar.gz tor-25f882a9cfe39ccb2942fe55f41cdcbfd1ba288e.zip |
Use stdatomic.h for atomic_counter_t where available.
Closes ticket 23953.
Diffstat (limited to 'src/common/compat_threads.c')
-rw-r--r-- | src/common/compat_threads.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c index 208d3138d9..e8a97f1254 100644 --- a/src/common/compat_threads.c +++ b/src/common/compat_threads.c @@ -352,12 +352,7 @@ alert_sockets_close(alert_sockets_t *socks) socks->read_fd = socks->write_fd = -1; } -/* - * XXXX We might be smart to move to compiler intrinsics or real atomic - * XXXX operations at some point. But not yet. - * - */ - +#ifndef HAVE_STDATOMIC_H /** Initialize a new atomic counter with the value 0 */ void atomic_counter_init(atomic_counter_t *counter) @@ -397,4 +392,16 @@ atomic_counter_get(atomic_counter_t *counter) 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 |