use std::atomic instead of boost::atomic (#678)

use std::atomic instead of boost::atomic
This commit is contained in:
Arvid Norberg 2016-05-01 03:38:35 -04:00
parent a3de04dffc
commit 770afadcda
9 changed files with 36 additions and 67 deletions

View File

@ -46,7 +46,6 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#endif
#include <boost/config.hpp>
#include <boost/asio/detail/config.hpp>
#include <boost/version.hpp>
@ -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 <storage/StorageDefs.h> // 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

View File

@ -59,7 +59,7 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
#include <boost/pool/pool.hpp>
#endif
#include <boost/atomic.hpp>
#include <atomic>
#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<int> m_num_threads;
std::atomic<int> m_num_threads;
// set to true once we start shutting down
boost::atomic<bool> m_abort;
std::atomic<bool> 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<int> m_num_running_threads;
std::atomic<int> m_num_running_threads;
// the actual threads running disk jobs
std::vector<std::thread> m_threads;

View File

@ -38,7 +38,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/cstdint.hpp>
#include <boost/atomic.hpp>
#include <atomic>
#include <mutex>
#include <array>
#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<boost::int64_t> m_stats_counter[num_counters];
#ifdef ATOMIC_LLONG_LOCK_FREE
std::array<std::atomic<boost::int64_t>, 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<boost::int64_t, num_counters> m_stats_counter;
#endif
};
}

View File

@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/unordered_set.hpp>
#include <boost/atomic.hpp>
#include <atomic>
#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<int> m_outstanding_jobs;
std::atomic<int> m_outstanding_jobs;
// must be held when accessing m_has_fence and
// m_blocked_jobs

View File

@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_push.hpp"
#ifdef TORRENT_PRODUCTION_ASSERTS
#include <boost/atomic.hpp>
#include <atomic>
#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<int> assert_counter(0);
std::atomic<int> assert_counter(0);
}
#endif

View File

@ -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<std::mutex> 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<std::mutex> l(m_mutex);
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> l(m_mutex);

View File

@ -179,7 +179,7 @@ namespace libtorrent
}
#ifdef TORRENT_DISK_STATS
static boost::atomic<int> event_id;
static std::atomic<int> event_id;
static std::mutex disk_access_mutex;
// this is opened and closed by the disk_io_thread class

View File

@ -2,7 +2,7 @@
#include "libtorrent/disk_io_job.hpp"
#include "test.hpp"
#include <boost/atomic.hpp>
#include <atomic>
using namespace libtorrent;

View File

@ -33,7 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/bind.hpp>
#include <boost/atomic.hpp>
#include <atomic>
#include <list>
#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<int>* c)
void increment(std::condition_variable* s, std::mutex* m, int* waiting, std::atomic<int>* c)
{
std::unique_lock<std::mutex> 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<int>* c)
void decrement(std::condition_variable* s, std::mutex* m, int* waiting, std::atomic<int>* c)
{
std::unique_lock<std::mutex> l(*m);
*waiting += 1;
@ -99,7 +99,7 @@ TORRENT_TEST(threads)
threads.clear();
waiting = 0;
boost::atomic<int> c(0);
std::atomic<int> c(0);
for (int i = 0; i < 3; ++i)
{
threads.emplace_back(&increment, &cond, &m, &waiting, &c);