From 770afadcdab61d49b501be8c820e7aa2b388301c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 1 May 2016 03:38:35 -0400 Subject: [PATCH] use std::atomic instead of boost::atomic (#678) use std::atomic instead of boost::atomic --- include/libtorrent/config.hpp | 33 --------------------- include/libtorrent/disk_io_thread.hpp | 8 ++--- include/libtorrent/performance_counters.hpp | 10 ++++--- include/libtorrent/storage.hpp | 4 +-- src/assert.cpp | 4 +-- src/performance_counters.cpp | 32 ++++++++++---------- src/storage.cpp | 2 +- test/test_fence.cpp | 2 +- test/test_threads.cpp | 8 ++--- 9 files changed, 36 insertions(+), 67 deletions(-) diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 23e4492a8..dd579f699 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -46,7 +46,6 @@ POSSIBILITY OF SUCH DAMAGE. #endif #endif - #include #include #include @@ -152,7 +151,6 @@ POSSIBILITY OF SUCH DAMAGE. #if defined __AMIGA__ || defined __amigaos__ || defined __AROS__ #define TORRENT_AMIGA #define TORRENT_USE_IPV6 0 -#define TORRENT_USE_BOOST_THREAD 0 #define TORRENT_USE_IOSTREAM 0 // set this to 1 to disable all floating point operations // (disables some float-dependent APIs) @@ -171,7 +169,6 @@ POSSIBILITY OF SUCH DAMAGE. // the locale is always utf-8 #if defined __APPLE__ -# define TORRENT_USE_OSATOMIC 1 # ifndef TORRENT_USE_ICONV # define TORRENT_USE_ICONV 0 # define TORRENT_USE_LOCALE 0 @@ -266,7 +263,6 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_USE_GETADAPTERSADDRESSES 1 #define TORRENT_HAS_SALEN 0 #define TORRENT_USE_GETIPFORWARDTABLE 1 -#define TORRENT_USE_INTERLOCKED_ATOMIC 1 #ifndef TORRENT_USE_UNC_PATHS # define TORRENT_USE_UNC_PATHS 1 #endif @@ -289,7 +285,6 @@ POSSIBILITY OF SUCH DAMAGE. #endif #define TORRENT_USE_RLIMIT 0 #define TORRENT_HAS_FALLOCATE 0 -#define TORRENT_USE_INTERLOCKED_ATOMIC 1 #ifndef TORRENT_USE_UNC_PATHS # define TORRENT_USE_UNC_PATHS 1 #endif @@ -313,14 +308,12 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_HAS_SALEN 0 #define TORRENT_HAS_SEM_RELTIMEDWAIT 1 #define TORRENT_HAVE_MMAP 1 -#define TORRENT_USE_SOLARIS_ATOMIC 1 // ==== BEOS === #elif defined __BEOS__ || defined __HAIKU__ #define TORRENT_BEOS #include // B_PATH_NAME_LENGTH #define TORRENT_HAS_FALLOCATE 0 -#define TORRENT_USE_BEOS_ATOMIC 1 #ifndef TORRENT_USE_ICONV #define TORRENT_USE_ICONV 0 #endif @@ -353,16 +346,6 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_BSD #endif -#if defined __GNUC__ && !(defined TORRENT_USE_OSATOMIC \ - || defined TORRENT_USE_INTERLOCKED_ATOMIC \ - || defined TORRENT_USE_BEOS_ATOMIC \ - || defined TORRENT_USE_SOLARIS_ATOMIC) -// atomic operations in GCC were introduced in 4.1.1 -# if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 1 && __GNUC_PATCHLEVEL__ >= 1) || __GNUC__ > 4 -# define TORRENT_USE_GCC_ATOMIC 1 -# endif -#endif - // on windows, NAME_MAX refers to Unicode characters // on linux it refers to bytes (utf-8 encoded) // TODO: Make this count Unicode characters instead of bytes on windows @@ -448,22 +431,6 @@ int snprintf(char* buf, int len, char const* fmt, ...) #define TORRENT_FORMAT(fmt, ellipsis) #endif -#ifndef TORRENT_USE_INTERLOCKED_ATOMIC -#define TORRENT_USE_INTERLOCKED_ATOMIC 0 -#endif - -#ifndef TORRENT_USE_GCC_ATOMIC -#define TORRENT_USE_GCC_ATOMIC 0 -#endif - -#ifndef TORRENT_USE_OSATOMIC -#define TORRENT_USE_OSATOMIC 0 -#endif - -#ifndef TORRENT_USE_BEOS_ATOMIC -#define TORRENT_USE_BEOS_ATOMIC 0 -#endif - // libiconv presence detection is not implemented yet #ifndef TORRENT_USE_ICONV #define TORRENT_USE_ICONV 1 diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 69cb62ddc..95de78d18 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -59,7 +59,7 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_DISABLE_POOL_ALLOCATOR #include #endif -#include +#include #include "libtorrent/aux_/disable_warnings_pop.hpp" @@ -518,15 +518,15 @@ namespace libtorrent // this is a counter which is atomically incremented // by each thread as it's started up, in order to // assign a unique id to each thread - boost::atomic m_num_threads; + std::atomic m_num_threads; // set to true once we start shutting down - boost::atomic m_abort; + std::atomic m_abort; // this is a counter of how many threads are currently running. // it's used to identify the last thread still running while // shutting down. This last thread is responsible for cleanup - boost::atomic m_num_running_threads; + std::atomic m_num_running_threads; // the actual threads running disk jobs std::vector m_threads; diff --git a/include/libtorrent/performance_counters.hpp b/include/libtorrent/performance_counters.hpp index 586db44a0..65bf005cd 100644 --- a/include/libtorrent/performance_counters.hpp +++ b/include/libtorrent/performance_counters.hpp @@ -38,7 +38,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" #include -#include +#include +#include +#include #include "libtorrent/aux_/disable_warnings_pop.hpp" @@ -451,13 +453,13 @@ namespace libtorrent // TODO: restore these to regular integers. Instead have one copy // of the counters per thread and collect them at convenient // synchronization points -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 - boost::atomic m_stats_counter[num_counters]; +#ifdef ATOMIC_LLONG_LOCK_FREE + std::array, num_counters> m_stats_counter; #else // if the atomic type is't lock-free, use a single lock instead, for // the whole array mutable std::mutex m_mutex; - boost::int64_t m_stats_counter[num_counters]; + std::array m_stats_counter; #endif }; } diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 8fe0976d6..47cac2a02 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include +#include #include "libtorrent/aux_/disable_warnings_pop.hpp" @@ -585,7 +585,7 @@ namespace libtorrent // to this torrent, currently pending, hanging off of // cached_piece_entry objects. This is used to determine // when the fence can be lowered - boost::atomic m_outstanding_jobs; + std::atomic m_outstanding_jobs; // must be held when accessing m_has_fence and // m_blocked_jobs diff --git a/src/assert.cpp b/src/assert.cpp index 5b208fc7e..50b0b79e2 100644 --- a/src/assert.cpp +++ b/src/assert.cpp @@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" #ifdef TORRENT_PRODUCTION_ASSERTS -#include +#include #endif #if (defined TORRENT_DEBUG && TORRENT_USE_ASSERTS) \ @@ -220,7 +220,7 @@ TORRENT_EXPORT void print_backtrace(char* out, int len, int max_depth) char const* libtorrent_assert_log = "asserts.log"; namespace { // the number of asserts we've printed to the log -boost::atomic assert_counter(0); +std::atomic assert_counter(0); } #endif diff --git a/src/performance_counters.cpp b/src/performance_counters.cpp index 97eae3cf5..be12606d5 100644 --- a/src/performance_counters.cpp +++ b/src/performance_counters.cpp @@ -43,10 +43,10 @@ namespace libtorrent { counters::counters() { -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 +#ifdef ATOMIC_LLONG_LOCK_FREE for (int i = 0; i < sizeof(m_stats_counter) / sizeof(m_stats_counter[0]); ++i) - m_stats_counter[i].store(0, boost::memory_order_relaxed); + m_stats_counter[i].store(0, std::memory_order_relaxed); #else memset(m_stats_counter, 0, sizeof(m_stats_counter)); #endif @@ -54,12 +54,12 @@ namespace libtorrent { counters::counters(counters const& c) { -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 +#ifdef ATOMIC_LLONG_LOCK_FREE for (int i = 0; i < sizeof(m_stats_counter) / sizeof(m_stats_counter[0]); ++i) m_stats_counter[i].store( - c.m_stats_counter[i].load(boost::memory_order_relaxed) - , boost::memory_order_relaxed); + c.m_stats_counter[i].load(std::memory_order_relaxed) + , std::memory_order_relaxed); #else std::lock_guard l(c.m_mutex); memcpy(m_stats_counter, c.m_stats_counter, sizeof(m_stats_counter)); @@ -68,12 +68,12 @@ namespace libtorrent { counters& counters::operator=(counters const& c) { -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 +#ifdef ATOMIC_LLONG_LOCK_FREE for (int i = 0; i < sizeof(m_stats_counter) / sizeof(m_stats_counter[0]); ++i) m_stats_counter[i].store( - c.m_stats_counter[i].load(boost::memory_order_relaxed) - , boost::memory_order_relaxed); + c.m_stats_counter[i].load(std::memory_order_relaxed) + , std::memory_order_relaxed); #else std::lock_guard l(m_mutex); std::lock_guard l2(c.m_mutex); @@ -90,8 +90,8 @@ namespace libtorrent { VALGRIND_CHECK_VALUE_IS_DEFINED(m_stats_counter[i]); #endif -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 - return m_stats_counter[i].load(boost::memory_order_relaxed); +#ifdef ATOMIC_LLONG_LOCK_FREE + return m_stats_counter[i].load(std::memory_order_relaxed); #else std::lock_guard l(m_mutex); return m_stats_counter[i]; @@ -109,8 +109,8 @@ namespace libtorrent { TORRENT_ASSERT(c >= 0); TORRENT_ASSERT(c < num_counters); -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 - boost::int64_t pv = m_stats_counter[c].fetch_add(value, boost::memory_order_relaxed); +#ifdef ATOMIC_LLONG_LOCK_FREE + boost::int64_t pv = m_stats_counter[c].fetch_add(value, std::memory_order_relaxed); TORRENT_ASSERT(pv + value >= 0); return pv + value; #else @@ -129,12 +129,12 @@ namespace libtorrent { TORRENT_ASSERT(ratio >= 0); TORRENT_ASSERT(ratio <= 100); -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 - boost::int64_t current = m_stats_counter[c].load(boost::memory_order_relaxed); +#ifdef ATOMIC_LLONG_LOCK_FREE + boost::int64_t current = m_stats_counter[c].load(std::memory_order_relaxed); boost::int64_t new_value = (current * (100-ratio) + value * ratio) / 100; while (!m_stats_counter[c].compare_exchange_weak(current, new_value - , boost::memory_order_relaxed)) + , std::memory_order_relaxed)) { new_value = (current * (100-ratio) + value * ratio) / 100; } @@ -150,7 +150,7 @@ namespace libtorrent { TORRENT_ASSERT(c >= 0); TORRENT_ASSERT(c < num_counters); -#if BOOST_ATOMIC_LLONG_LOCK_FREE == 2 +#ifdef ATOMIC_LLONG_LOCK_FREE m_stats_counter[c].store(value); #else std::lock_guard l(m_mutex); diff --git a/src/storage.cpp b/src/storage.cpp index c76f533da..98e2f3de0 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -179,7 +179,7 @@ namespace libtorrent } #ifdef TORRENT_DISK_STATS - static boost::atomic event_id; + static std::atomic event_id; static std::mutex disk_access_mutex; // this is opened and closed by the disk_io_thread class diff --git a/test/test_fence.cpp b/test/test_fence.cpp index 502512c29..86faecfaa 100644 --- a/test/test_fence.cpp +++ b/test/test_fence.cpp @@ -2,7 +2,7 @@ #include "libtorrent/disk_io_job.hpp" #include "test.hpp" -#include +#include using namespace libtorrent; diff --git a/test/test_threads.cpp b/test/test_threads.cpp index 22fe079f4..896b821ac 100644 --- a/test/test_threads.cpp +++ b/test/test_threads.cpp @@ -33,7 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" #include -#include +#include #include #include "libtorrent/aux_/disable_warnings_pop.hpp" @@ -52,7 +52,7 @@ void fun(std::condition_variable* s, std::mutex* m, int* waiting, int i) fprintf(stderr, "thread %d done\n", i); } -void increment(std::condition_variable* s, std::mutex* m, int* waiting, boost::atomic* c) +void increment(std::condition_variable* s, std::mutex* m, int* waiting, std::atomic* c) { std::unique_lock l(*m); *waiting += 1; @@ -62,7 +62,7 @@ void increment(std::condition_variable* s, std::mutex* m, int* waiting, boost::a ++*c; } -void decrement(std::condition_variable* s, std::mutex* m, int* waiting, boost::atomic* c) +void decrement(std::condition_variable* s, std::mutex* m, int* waiting, std::atomic* c) { std::unique_lock l(*m); *waiting += 1; @@ -99,7 +99,7 @@ TORRENT_TEST(threads) threads.clear(); waiting = 0; - boost::atomic c(0); + std::atomic c(0); for (int i = 0; i < 3; ++i) { threads.emplace_back(&increment, &cond, &m, &waiting, &c);