stabilize build system: depends, installer, boost/bdb fixes, cross targets groundwork
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2013.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_CONFIG_H
|
||||
#define BOOST_FIBERS_DETAIL_CONFIG_H
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/predef.h>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#ifdef BOOST_FIBERS_DECL
|
||||
# undef BOOST_FIBERS_DECL
|
||||
#endif
|
||||
|
||||
#if (defined(BOOST_ALL_DYN_LINK) || defined(BOOST_FIBERS_DYN_LINK) ) && ! defined(BOOST_FIBERS_STATIC_LINK)
|
||||
# if defined(BOOST_FIBERS_SOURCE)
|
||||
# define BOOST_FIBERS_DECL BOOST_SYMBOL_EXPORT
|
||||
# define BOOST_FIBERS_BUILD_DLL
|
||||
# else
|
||||
# define BOOST_FIBERS_DECL BOOST_SYMBOL_IMPORT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if ! defined(BOOST_FIBERS_DECL)
|
||||
# define BOOST_FIBERS_DECL
|
||||
#endif
|
||||
|
||||
#if ! defined(BOOST_FIBERS_SOURCE) && ! defined(BOOST_ALL_NO_LIB) && ! defined(BOOST_FIBERS_NO_LIB)
|
||||
# define BOOST_LIB_NAME boost_fiber
|
||||
# if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_FIBERS_DYN_LINK)
|
||||
# define BOOST_DYN_LINK
|
||||
# endif
|
||||
# include <boost/config/auto_link.hpp>
|
||||
#endif
|
||||
|
||||
#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
|
||||
# define BOOST_FIBERS_HAS_FUTEX
|
||||
#endif
|
||||
|
||||
#if (!defined(BOOST_FIBERS_HAS_FUTEX) && \
|
||||
(defined(BOOST_FIBERS_SPINLOCK_TTAS_FUTEX) || defined(BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX)))
|
||||
# error "futex not supported on this platform"
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_FIBERS_SPIN_MAX_COLLISIONS)
|
||||
# define BOOST_FIBERS_SPIN_MAX_COLLISIONS 16
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_FIBERS_SPIN_MAX_TESTS)
|
||||
# define BOOST_FIBERS_SPIN_MAX_TESTS 500
|
||||
#endif
|
||||
|
||||
// modern architectures have cachelines with 64byte length
|
||||
// ARM Cortex-A15 32/64byte, Cortex-A9 16/32/64bytes
|
||||
// MIPS 74K: 32byte, 4KEc: 16byte
|
||||
// ist shoudl be safe to use 64byte for all
|
||||
static constexpr std::size_t cache_alignment{ 64 };
|
||||
static constexpr std::size_t cacheline_length{ 64 };
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_CONFIG_H
|
||||
@@ -0,0 +1,118 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2015.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_SPINLOCK_QUEUE_H
|
||||
#define BOOST_FIBERS_DETAIL_SPINLOCK_QUEUE_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/fiber/context.hpp>
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/spinlock.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class context_spinlock_queue {
|
||||
private:
|
||||
typedef context * slot_type;
|
||||
|
||||
alignas(cache_alignment) mutable spinlock splk_{};
|
||||
std::size_t pidx_{ 0 };
|
||||
std::size_t cidx_{ 0 };
|
||||
std::size_t capacity_;
|
||||
slot_type * slots_;
|
||||
|
||||
void resize_() {
|
||||
slot_type * old_slots = slots_;
|
||||
slots_ = new slot_type[2*capacity_];
|
||||
std::size_t offset = capacity_ - cidx_;
|
||||
std::memcpy( slots_, old_slots + cidx_, offset * sizeof( slot_type) );
|
||||
if ( 0 < cidx_) {
|
||||
std::memcpy( slots_ + offset, old_slots, pidx_ * sizeof( slot_type) );
|
||||
}
|
||||
cidx_ = 0;
|
||||
pidx_ = capacity_ - 1;
|
||||
capacity_ *= 2;
|
||||
delete [] old_slots;
|
||||
}
|
||||
|
||||
bool is_full_() const noexcept {
|
||||
return cidx_ == ((pidx_ + 1) % capacity_);
|
||||
}
|
||||
|
||||
bool is_empty_() const noexcept {
|
||||
return cidx_ == pidx_;
|
||||
}
|
||||
|
||||
public:
|
||||
context_spinlock_queue( std::size_t capacity = 4096) :
|
||||
capacity_{ capacity } {
|
||||
slots_ = new slot_type[capacity_];
|
||||
}
|
||||
|
||||
~context_spinlock_queue() {
|
||||
delete [] slots_;
|
||||
}
|
||||
|
||||
context_spinlock_queue( context_spinlock_queue const&) = delete;
|
||||
context_spinlock_queue & operator=( context_spinlock_queue const&) = delete;
|
||||
|
||||
bool empty() const noexcept {
|
||||
spinlock_lock lk{ splk_ };
|
||||
return is_empty_();
|
||||
}
|
||||
|
||||
void push( context * c) {
|
||||
spinlock_lock lk{ splk_ };
|
||||
if ( is_full_() ) {
|
||||
resize_();
|
||||
}
|
||||
slots_[pidx_] = c;
|
||||
pidx_ = (pidx_ + 1) % capacity_;
|
||||
}
|
||||
|
||||
context * pop() {
|
||||
spinlock_lock lk{ splk_ };
|
||||
context * c = nullptr;
|
||||
if ( ! is_empty_() ) {
|
||||
c = slots_[cidx_];
|
||||
cidx_ = (cidx_ + 1) % capacity_;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
context * steal() {
|
||||
spinlock_lock lk{ splk_ };
|
||||
context * c = nullptr;
|
||||
if ( ! is_empty_() ) {
|
||||
c = slots_[cidx_];
|
||||
if ( c->is_context( type::pinned_context) ) {
|
||||
return nullptr;
|
||||
}
|
||||
cidx_ = (cidx_ + 1) % capacity_;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_SPINLOCK_QUEUE_H
|
||||
@@ -0,0 +1,199 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2013.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_CONTEXT_SPMC_QUEUE_H
|
||||
#define BOOST_FIBERS_DETAIL_CONTEXT_SPMC_QUEUE_H
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/context.hpp>
|
||||
|
||||
// David Chase and Yossi Lev. Dynamic circular work-stealing deque.
|
||||
// In SPAA ’05: Proceedings of the seventeenth annual ACM symposium
|
||||
// on Parallelism in algorithms and architectures, pages 21–28,
|
||||
// New York, NY, USA, 2005. ACM.
|
||||
//
|
||||
// Nhat Minh Lê, Antoniu Pop, Albert Cohen, and Francesco Zappa Nardelli. 2013.
|
||||
// Correct and efficient work-stealing for weak memory models.
|
||||
// In Proceedings of the 18th ACM SIGPLAN symposium on Principles and practice
|
||||
// of parallel programming (PPoPP '13). ACM, New York, NY, USA, 69-80.
|
||||
|
||||
#if BOOST_COMP_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-private-field"
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class context_spmc_queue {
|
||||
private:
|
||||
class array {
|
||||
private:
|
||||
typedef std::atomic< context * > atomic_type;
|
||||
typedef std::aligned_storage<
|
||||
sizeof( atomic_type), cache_alignment
|
||||
>::type storage_type;
|
||||
|
||||
std::size_t capacity_;
|
||||
storage_type * storage_;
|
||||
|
||||
public:
|
||||
array( std::size_t capacity) :
|
||||
capacity_{ capacity },
|
||||
storage_{ new storage_type[capacity_] } {
|
||||
for ( std::size_t i = 0; i < capacity_; ++i) {
|
||||
::new ( static_cast< void * >( std::addressof( storage_[i]) ) ) atomic_type{ nullptr };
|
||||
}
|
||||
}
|
||||
|
||||
~array() {
|
||||
for ( std::size_t i = 0; i < capacity_; ++i) {
|
||||
reinterpret_cast< atomic_type * >( std::addressof( storage_[i]) )->~atomic_type();
|
||||
}
|
||||
delete [] storage_;
|
||||
}
|
||||
|
||||
std::size_t capacity() const noexcept {
|
||||
return capacity_;
|
||||
}
|
||||
|
||||
void push( std::size_t bottom, context * ctx) noexcept {
|
||||
reinterpret_cast< atomic_type * >(
|
||||
std::addressof( storage_[bottom % capacity_]) )
|
||||
->store( ctx, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
context * pop( std::size_t top) noexcept {
|
||||
return reinterpret_cast< atomic_type * >(
|
||||
std::addressof( storage_[top % capacity_]) )
|
||||
->load( std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
array * resize( std::size_t bottom, std::size_t top) {
|
||||
std::unique_ptr< array > tmp{ new array{ 2 * capacity_ } };
|
||||
for ( std::size_t i = top; i != bottom; ++i) {
|
||||
tmp->push( i, pop( i) );
|
||||
}
|
||||
return tmp.release();
|
||||
}
|
||||
};
|
||||
|
||||
alignas(cache_alignment) std::atomic< std::size_t > top_{ 0 };
|
||||
alignas(cache_alignment) std::atomic< std::size_t > bottom_{ 0 };
|
||||
alignas(cache_alignment) std::atomic< array * > array_;
|
||||
std::vector< array * > old_arrays_{};
|
||||
char padding_[cacheline_length];
|
||||
|
||||
public:
|
||||
context_spmc_queue( std::size_t capacity = 4096) :
|
||||
array_{ new array{ capacity } } {
|
||||
old_arrays_.reserve( 32);
|
||||
}
|
||||
|
||||
~context_spmc_queue() {
|
||||
for ( array * a : old_arrays_) {
|
||||
delete a;
|
||||
}
|
||||
delete array_.load();
|
||||
}
|
||||
|
||||
context_spmc_queue( context_spmc_queue const&) = delete;
|
||||
context_spmc_queue & operator=( context_spmc_queue const&) = delete;
|
||||
|
||||
bool empty() const noexcept {
|
||||
std::size_t bottom = bottom_.load( std::memory_order_relaxed);
|
||||
std::size_t top = top_.load( std::memory_order_relaxed);
|
||||
return bottom <= top;
|
||||
}
|
||||
|
||||
void push( context * ctx) {
|
||||
std::size_t bottom = bottom_.load( std::memory_order_relaxed);
|
||||
std::size_t top = top_.load( std::memory_order_acquire);
|
||||
array * a = array_.load( std::memory_order_relaxed);
|
||||
if ( (a->capacity() - 1) < (bottom - top) ) {
|
||||
// queue is full
|
||||
// resize
|
||||
array * tmp = a->resize( bottom, top);
|
||||
old_arrays_.push_back( a);
|
||||
std::swap( a, tmp);
|
||||
array_.store( a, std::memory_order_relaxed);
|
||||
}
|
||||
a->push( bottom, ctx);
|
||||
std::atomic_thread_fence( std::memory_order_release);
|
||||
bottom_.store( bottom + 1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
context * pop() {
|
||||
std::size_t bottom = bottom_.load( std::memory_order_relaxed) - 1;
|
||||
array * a = array_.load( std::memory_order_relaxed);
|
||||
bottom_.store( bottom, std::memory_order_relaxed);
|
||||
std::atomic_thread_fence( std::memory_order_seq_cst);
|
||||
std::size_t top = top_.load( std::memory_order_relaxed);
|
||||
context * ctx = nullptr;
|
||||
if ( top <= bottom) {
|
||||
// queue is not empty
|
||||
ctx = a->pop( bottom);
|
||||
BOOST_ASSERT( nullptr != ctx);
|
||||
if ( top == bottom) {
|
||||
// last element dequeued
|
||||
if ( ! top_.compare_exchange_strong( top, top + 1,
|
||||
std::memory_order_seq_cst,
|
||||
std::memory_order_relaxed) ) {
|
||||
// lose the race
|
||||
ctx = nullptr;
|
||||
}
|
||||
bottom_.store( bottom + 1, std::memory_order_relaxed);
|
||||
}
|
||||
} else {
|
||||
// queue is empty
|
||||
bottom_.store( bottom + 1, std::memory_order_relaxed);
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
context * steal() {
|
||||
std::size_t top = top_.load( std::memory_order_acquire);
|
||||
std::atomic_thread_fence( std::memory_order_seq_cst);
|
||||
std::size_t bottom = bottom_.load( std::memory_order_acquire);
|
||||
context * ctx = nullptr;
|
||||
if ( top < bottom) {
|
||||
// queue is not empty
|
||||
array * a = array_.load( std::memory_order_consume);
|
||||
ctx = a->pop( top);
|
||||
BOOST_ASSERT( nullptr != ctx);
|
||||
// do not steal pinned context (e.g. main-/dispatcher-context)
|
||||
if ( ctx->is_context( type::pinned_context) ) {
|
||||
return nullptr;
|
||||
}
|
||||
if ( ! top_.compare_exchange_strong( top, top + 1,
|
||||
std::memory_order_seq_cst,
|
||||
std::memory_order_relaxed) ) {
|
||||
// lose the race
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#if BOOST_COMP_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_CONTEXT_SPMC_QUEUE_H
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2013.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_CONVERT_H
|
||||
#define BOOST_FIBERS_DETAIL_CONVERT_H
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
inline
|
||||
std::chrono::steady_clock::time_point convert(
|
||||
std::chrono::steady_clock::time_point const& timeout_time) noexcept {
|
||||
return timeout_time;
|
||||
}
|
||||
|
||||
template< typename Clock, typename Duration >
|
||||
std::chrono::steady_clock::time_point convert(
|
||||
std::chrono::time_point< Clock, Duration > const& timeout_time) {
|
||||
return std::chrono::steady_clock::now() + ( timeout_time - Clock::now() );
|
||||
}
|
||||
|
||||
// suggested by Howard Hinnant
|
||||
template< typename T >
|
||||
inline
|
||||
T * convert( T * p) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
template< typename Pointer >
|
||||
inline
|
||||
typename std::pointer_traits< Pointer >::element_type *
|
||||
convert( Pointer p) noexcept {
|
||||
return nullptr != p
|
||||
? to_raw_pointer( p.operator->() )
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_CONVERT_H
|
||||
@@ -0,0 +1,82 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2016.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_CPU_RELAX_H
|
||||
#define BOOST_FIBERS_DETAIL_CPU_RELAX_H
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/predef.h>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#if BOOST_COMP_MSVC || BOOST_COMP_MSVC_EMULATED
|
||||
# include <Windows.h>
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
#if BOOST_ARCH_ARM
|
||||
# if BOOST_COMP_MSVC
|
||||
# define cpu_relax() YieldProcessor();
|
||||
# elif (defined(__ARM_ARCH_6K__) || \
|
||||
defined(__ARM_ARCH_6Z__) || \
|
||||
defined(__ARM_ARCH_6ZK__) || \
|
||||
defined(__ARM_ARCH_6T2__) || \
|
||||
defined(__ARM_ARCH_7__) || \
|
||||
defined(__ARM_ARCH_7A__) || \
|
||||
defined(__ARM_ARCH_7R__) || \
|
||||
defined(__ARM_ARCH_7M__) || \
|
||||
defined(__ARM_ARCH_7S__) || \
|
||||
defined(__ARM_ARCH_8A__) || \
|
||||
defined(__aarch64__))
|
||||
// http://groups.google.com/a/chromium.org/forum/#!msg/chromium-dev/YGVrZbxYOlU/Vpgy__zeBQAJ
|
||||
// mnemonic 'yield' is supported from ARMv6k onwards
|
||||
# define cpu_relax() asm volatile ("yield" ::: "memory");
|
||||
# else
|
||||
# define cpu_relax() asm volatile ("nop" ::: "memory");
|
||||
# endif
|
||||
#elif BOOST_ARCH_MIPS
|
||||
# define cpu_relax() asm volatile ("pause" ::: "memory");
|
||||
#elif BOOST_ARCH_PPC
|
||||
// http://code.metager.de/source/xref/gnu/glibc/sysdeps/powerpc/sys/platform/ppc.h
|
||||
// http://stackoverflow.com/questions/5425506/equivalent-of-x86-pause-instruction-for-ppc
|
||||
// mnemonic 'or' shared resource hints
|
||||
// or 27, 27, 27 This form of 'or' provides a hint that performance
|
||||
// will probably be imrpoved if shared resources dedicated
|
||||
// to the executing processor are released for use by other
|
||||
// processors
|
||||
// extended mnemonics (available with POWER7)
|
||||
// yield == or 27, 27, 27
|
||||
# define cpu_relax() asm volatile ("or 27,27,27" ::: "memory");
|
||||
#elif BOOST_ARCH_X86
|
||||
# if BOOST_COMP_MSVC || BOOST_COMP_MSVC_EMULATED
|
||||
# define cpu_relax() YieldProcessor();
|
||||
# else
|
||||
# define cpu_relax() asm volatile ("pause" ::: "memory");
|
||||
# endif
|
||||
#else
|
||||
# define cpu_relax() { \
|
||||
static constexpr std::chrono::microseconds us0{ 0 }; \
|
||||
std::this_thread::sleep_for( us0); \
|
||||
}
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_CPU_RELAX_H
|
||||
@@ -0,0 +1,71 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2013.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_DATA_H
|
||||
#define BOOST_FIBERS_DETAIL_DATA_H
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/spinlock.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
|
||||
class context;
|
||||
|
||||
namespace detail {
|
||||
|
||||
#if (BOOST_EXECUTION_CONTEXT==1)
|
||||
struct data_t {
|
||||
spinlock_lock * lk{ nullptr };
|
||||
context * ctx{ nullptr };
|
||||
|
||||
data_t() = default;
|
||||
|
||||
explicit data_t( spinlock_lock * lk_) noexcept :
|
||||
lk{ lk_ } {
|
||||
}
|
||||
|
||||
explicit data_t( context * ctx_) noexcept :
|
||||
ctx{ ctx_ } {
|
||||
}
|
||||
};
|
||||
#else
|
||||
struct data_t {
|
||||
spinlock_lock * lk{ nullptr };
|
||||
context * ctx{ nullptr };
|
||||
context * from;
|
||||
|
||||
explicit data_t( context * from_) noexcept :
|
||||
from{ from_ } {
|
||||
}
|
||||
|
||||
explicit data_t( spinlock_lock * lk_,
|
||||
context * from_) noexcept :
|
||||
lk{ lk_ },
|
||||
from{ from_ } {
|
||||
}
|
||||
|
||||
explicit data_t( context * ctx_,
|
||||
context * from_) noexcept :
|
||||
ctx{ ctx_ },
|
||||
from{ from_ } {
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_DATA_H
|
||||
@@ -0,0 +1,36 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2014.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBER_DETAIL_DECAY_COPY_H
|
||||
#define BOOST_FIBER_DETAIL_DECAY_COPY_H
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
template< typename T >
|
||||
typename std::decay< T >::type
|
||||
decay_copy( T && t) {
|
||||
return std::forward< T >( t);
|
||||
}
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBER_DETAIL_DECAY_COPY_H
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2014.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBER_DETAIL_DISABLE_OVERLOAD_H
|
||||
#define BOOST_FIBER_DETAIL_DISABLE_OVERLOAD_H
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/context/detail/disable_overload.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
template< typename X, typename Y >
|
||||
using disable_overload = boost::context::detail::disable_overload< X, Y >;
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBER_DETAIL_DISABLE_OVERLOAD_H
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2013.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// based on tss.hpp from boost.thread
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_FSS_H
|
||||
#define BOOST_FIBERS_DETAIL_FSS_H
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/intrusive_ptr.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class fss_cleanup_function {
|
||||
private:
|
||||
std::atomic< std::size_t > use_count_{ 0 };
|
||||
|
||||
public:
|
||||
typedef intrusive_ptr< fss_cleanup_function > ptr_t;
|
||||
|
||||
fss_cleanup_function() noexcept = default;
|
||||
|
||||
virtual ~fss_cleanup_function() = default;
|
||||
|
||||
virtual void operator()( void * data) = 0;
|
||||
|
||||
friend inline
|
||||
void intrusive_ptr_add_ref( fss_cleanup_function * p) noexcept {
|
||||
p->use_count_.fetch_add( 1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
friend inline
|
||||
void intrusive_ptr_release( fss_cleanup_function * p) noexcept {
|
||||
if ( 1 == p->use_count_.fetch_sub( 1, std::memory_order_release) ) {
|
||||
std::atomic_thread_fence( std::memory_order_acquire);
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_FSS_H
|
||||
@@ -0,0 +1,61 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2016.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_DETAIL_FUTEX_H
|
||||
#define BOOST_FIBERS_DETAIL_FUTEX_H
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/predef.h>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#if BOOST_OS_LINUX
|
||||
extern "C" {
|
||||
#include <linux/futex.h>
|
||||
#include <sys/syscall.h>
|
||||
}
|
||||
#elif BOOST_OS_WINDOWS
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
#if BOOST_OS_LINUX
|
||||
inline
|
||||
int sys_futex( void * addr, std::int32_t op, std::int32_t x) {
|
||||
return ::syscall( SYS_futex, addr, op, x, nullptr, nullptr, 0);
|
||||
}
|
||||
|
||||
inline
|
||||
int futex_wake( std::atomic< std::int32_t > * addr) {
|
||||
return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAKE_PRIVATE, 1) ? 0 : -1;
|
||||
}
|
||||
|
||||
inline
|
||||
int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
|
||||
return 0 <= sys_futex( static_cast< void * >( addr), FUTEX_WAIT_PRIVATE, x) ? 0 : -1;
|
||||
}
|
||||
#elif BOOST_OS_WINDOWS
|
||||
inline
|
||||
int futex_wake( std::atomic< std::int32_t > * addr) {
|
||||
::WakeByAddressSingle( static_cast< void * >( addr) );
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline
|
||||
int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
|
||||
::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
# warn "no futex support on this platform"
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
#endif // BOOST_FIBERS_DETAIL_FUTEX_H
|
||||
@@ -0,0 +1,65 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2013.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_SPINLOCK_H
|
||||
#define BOOST_FIBERS_SPINLOCK_H
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#if !defined(BOOST_FIBERS_NO_ATOMICS)
|
||||
# include <mutex>
|
||||
# include <boost/fiber/detail/spinlock_ttas.hpp>
|
||||
# include <boost/fiber/detail/spinlock_ttas_adaptive.hpp>
|
||||
# if defined(BOOST_FIBERS_HAS_FUTEX)
|
||||
# include <boost/fiber/detail/spinlock_ttas_futex.hpp>
|
||||
# include <boost/fiber/detail/spinlock_ttas_adaptive_futex.hpp>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
#if defined(BOOST_FIBERS_NO_ATOMICS)
|
||||
struct spinlock {
|
||||
constexpr spinlock() noexcept {}
|
||||
void lock() noexcept {}
|
||||
void unlock() noexcept {}
|
||||
};
|
||||
|
||||
struct spinlock_lock {
|
||||
constexpr spinlock_lock( spinlock &) noexcept {}
|
||||
void lock() noexcept {}
|
||||
void unlock() noexcept {}
|
||||
};
|
||||
#else
|
||||
# if defined(BOOST_FIBERS_SPINLOCK_STD_MUTEX)
|
||||
using spinlock = std::mutex;
|
||||
# elif defined(BOOST_FIBERS_SPINLOCK_TTAS_FUTEX)
|
||||
using spinlock = spinlock_ttas_futex;
|
||||
# elif defined(BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX)
|
||||
using spinlock = spinlock_ttas_adaptive_futex;
|
||||
# elif defined(BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE)
|
||||
using spinlock = spinlock_ttas_adaptive;
|
||||
# else
|
||||
using spinlock = spinlock_ttas;
|
||||
# endif
|
||||
using spinlock_lock = std::unique_lock< spinlock >;
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_SPINLOCK_H
|
||||
@@ -0,0 +1,115 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2016.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_SPINLOCK_TTAS_H
|
||||
#define BOOST_FIBERS_SPINLOCK_TTAS_H
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/cpu_relax.hpp>
|
||||
|
||||
// based on informations from:
|
||||
// https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops
|
||||
// https://software.intel.com/en-us/articles/long-duration-spin-wait-loops-on-hyper-threading-technology-enabled-intel-processors
|
||||
|
||||
#if BOOST_COMP_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-private-field"
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class spinlock_ttas {
|
||||
private:
|
||||
enum class spinlock_status {
|
||||
locked = 0,
|
||||
unlocked
|
||||
};
|
||||
|
||||
std::atomic< spinlock_status > state_{ spinlock_status::unlocked };
|
||||
|
||||
public:
|
||||
spinlock_ttas() noexcept = default;
|
||||
|
||||
spinlock_ttas( spinlock_ttas const&) = delete;
|
||||
spinlock_ttas & operator=( spinlock_ttas const&) = delete;
|
||||
|
||||
void lock() noexcept {
|
||||
std::size_t collisions = 0 ;
|
||||
for (;;) {
|
||||
// avoid using multiple pause instructions for a delay of a specific cycle count
|
||||
// the delay of cpu_relax() (pause on Intel) depends on the processor family
|
||||
// the cycle count can not guaranteed from one system to the next
|
||||
// -> check the shared variable 'state_' in between each cpu_relax() to prevent
|
||||
// unnecessarily long delays on some systems
|
||||
std::size_t tests = 0;
|
||||
// test shared variable 'status_'
|
||||
// first access to 'state_' -> chache miss
|
||||
// sucessive acccess to 'state_' -> cache hit
|
||||
// if 'state_' was released by other fiber
|
||||
// cached 'state_' is invalidated -> cache miss
|
||||
while ( spinlock_status::locked == state_.load( std::memory_order_relaxed) ) {
|
||||
#if !defined(BOOST_FIBERS_SPIN_SINGLE_CORE)
|
||||
if ( BOOST_FIBERS_SPIN_MAX_TESTS > tests) {
|
||||
++tests;
|
||||
// give CPU a hint that this thread is in a "spin-wait" loop
|
||||
// delays the next instruction's execution for a finite period of time (depends on processor family)
|
||||
// the CPU is not under demand, parts of the pipeline are no longer being used
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
} else {
|
||||
// std::this_thread::sleep_for( 0us) has a fairly long instruction path length,
|
||||
// combined with an expensive ring3 to ring 0 transition costing about 1000 cycles
|
||||
// std::this_thread::sleep_for( 0us) lets give up this_thread the remaining part of its time slice
|
||||
// if and only if a thread of equal or greater priority is ready to run
|
||||
static constexpr std::chrono::microseconds us0{ 0 };
|
||||
std::this_thread::sleep_for( us0);
|
||||
}
|
||||
#else
|
||||
std::this_thread::yield();
|
||||
#endif
|
||||
}
|
||||
// test-and-set shared variable 'status_'
|
||||
// everytime 'status_' is signaled over the bus, even if the test failes
|
||||
if ( spinlock_status::locked == state_.exchange( spinlock_status::locked, std::memory_order_acquire) ) {
|
||||
// spinlock now contended
|
||||
// utilize 'Binary Exponential Backoff' algorithm
|
||||
// linear_congruential_engine is a random number engine based on Linear congruential generator (LCG)
|
||||
static thread_local std::minstd_rand generator;
|
||||
static std::uniform_int_distribution< std::size_t > distribution{ 0, static_cast< std::size_t >( 1) << collisions };
|
||||
const std::size_t z = distribution( generator);
|
||||
++collisions;
|
||||
for ( std::size_t i = 0; i < z; ++i) {
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
}
|
||||
} else {
|
||||
// success, thread has acquired the lock
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void unlock() noexcept {
|
||||
state_.store( spinlock_status::unlocked, std::memory_order_release);
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#if BOOST_COMP_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBERS_SPINLOCK_TTAS_H
|
||||
@@ -0,0 +1,112 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2016.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_H
|
||||
#define BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_H
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/cpu_relax.hpp>
|
||||
|
||||
// based on informations from:
|
||||
// https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops
|
||||
// https://software.intel.com/en-us/articles/long-duration-spin-wait-loops-on-hyper-threading-technology-enabled-intel-processors
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class spinlock_ttas_adaptive {
|
||||
private:
|
||||
enum class spinlock_status {
|
||||
locked = 0,
|
||||
unlocked
|
||||
};
|
||||
|
||||
std::atomic< spinlock_status > state_{ spinlock_status::unlocked };
|
||||
std::atomic< std::size_t > tests_{ 0 };
|
||||
|
||||
public:
|
||||
spinlock_ttas_adaptive() noexcept = default;
|
||||
|
||||
spinlock_ttas_adaptive( spinlock_ttas_adaptive const&) = delete;
|
||||
spinlock_ttas_adaptive & operator=( spinlock_ttas_adaptive const&) = delete;
|
||||
|
||||
void lock() noexcept {
|
||||
std::size_t collisions = 0 ;
|
||||
for (;;) {
|
||||
std::size_t tests = 0;
|
||||
const std::size_t prev_tests = tests_.load( std::memory_order_relaxed);
|
||||
const std::size_t max_tests = (std::min)( static_cast< std::size_t >( BOOST_FIBERS_SPIN_MAX_TESTS), 2 * prev_tests + 10);
|
||||
// avoid using multiple pause instructions for a delay of a specific cycle count
|
||||
// the delay of cpu_relax() (pause on Intel) depends on the processor family
|
||||
// the cycle count can not guaranteed from one system to the next
|
||||
// -> check the shared variable 'state_' in between each cpu_relax() to prevent
|
||||
// unnecessarily long delays on some systems
|
||||
// test shared variable 'status_'
|
||||
// first access to 'state_' -> chache miss
|
||||
// sucessive acccess to 'state_' -> cache hit
|
||||
// if 'state_' was released by other fiber
|
||||
// cached 'state_' is invalidated -> cache miss
|
||||
while ( spinlock_status::locked == state_.load( std::memory_order_relaxed) ) {
|
||||
#if !defined(BOOST_FIBERS_SPIN_SINGLE_CORE)
|
||||
if ( max_tests > tests) {
|
||||
++tests;
|
||||
// give CPU a hint that this thread is in a "spin-wait" loop
|
||||
// delays the next instruction's execution for a finite period of time (depends on processor family)
|
||||
// the CPU is not under demand, parts of the pipeline are no longer being used
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
} else {
|
||||
++tests;
|
||||
// std::this_thread::sleep_for( 0us) has a fairly long instruction path length,
|
||||
// combined with an expensive ring3 to ring 0 transition costing about 1000 cycles
|
||||
// std::this_thread::sleep_for( 0us) lets give up this_thread the remaining part of its time slice
|
||||
// if and only if a thread of equal or greater priority is ready to run
|
||||
static constexpr std::chrono::microseconds us0{ 0 };
|
||||
std::this_thread::sleep_for( us0);
|
||||
}
|
||||
#else
|
||||
std::this_thread::yield();
|
||||
#endif
|
||||
}
|
||||
// test-and-set shared variable 'status_'
|
||||
// everytime 'status_' is signaled over the bus, even if the test failes
|
||||
if ( spinlock_status::locked == state_.exchange( spinlock_status::locked, std::memory_order_acquire) ) {
|
||||
// spinlock now contended
|
||||
// utilize 'Binary Exponential Backoff' algorithm
|
||||
// linear_congruential_engine is a random number engine based on Linear congruential generator (LCG)
|
||||
static thread_local std::minstd_rand generator;
|
||||
static std::uniform_int_distribution< std::size_t > distribution{ 0, static_cast< std::size_t >( 1) << collisions };
|
||||
const std::size_t z = distribution( generator);
|
||||
++collisions;
|
||||
for ( std::size_t i = 0; i < z; ++i) {
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
}
|
||||
} else {
|
||||
tests_.store( prev_tests + (tests - prev_tests) / 8, std::memory_order_relaxed);
|
||||
// success, thread has acquired the lock
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void unlock() noexcept {
|
||||
state_.store( spinlock_status::unlocked, std::memory_order_release);
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif // BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_H
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2016.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX_H
|
||||
#define BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX_H
|
||||
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/cpu_relax.hpp>
|
||||
#include <boost/fiber/detail/futex.hpp>
|
||||
|
||||
// based on informations from:
|
||||
// https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops
|
||||
// https://software.intel.com/en-us/articles/long-duration-spin-wait-loops-on-hyper-threading-technology-enabled-intel-processors
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class spinlock_ttas_adaptive_futex {
|
||||
private:
|
||||
std::atomic< std::int32_t > value_{ 0 };
|
||||
std::atomic< std::int32_t > tests_{ 0 };
|
||||
|
||||
public:
|
||||
spinlock_ttas_adaptive_futex() noexcept = default;
|
||||
|
||||
spinlock_ttas_adaptive_futex( spinlock_ttas_adaptive_futex const&) = delete;
|
||||
spinlock_ttas_adaptive_futex & operator=( spinlock_ttas_adaptive_futex const&) = delete;
|
||||
|
||||
void lock() noexcept {
|
||||
std::int32_t collisions = 0, tests = 0, expected = 0;
|
||||
const std::int32_t prev_tests = tests_.load( std::memory_order_relaxed);
|
||||
const std::int32_t max_tests = (std::min)( static_cast< std::int32_t >( BOOST_FIBERS_SPIN_MAX_TESTS), 2 * prev_tests + 10);
|
||||
// after max. spins or collisions suspend via futex
|
||||
while ( max_tests > tests && BOOST_FIBERS_SPIN_MAX_COLLISIONS > collisions) {
|
||||
// avoid using multiple pause instructions for a delay of a specific cycle count
|
||||
// the delay of cpu_relax() (pause on Intel) depends on the processor family
|
||||
// the cycle count can not guaranteed from one system to the next
|
||||
// -> check the shared variable 'value_' in between each cpu_relax() to prevent
|
||||
// unnecessarily long delays on some systems
|
||||
// test shared variable 'status_'
|
||||
// first access to 'value_' -> chache miss
|
||||
// sucessive acccess to 'value_' -> cache hit
|
||||
// if 'value_' was released by other fiber
|
||||
// cached 'value_' is invalidated -> cache miss
|
||||
if ( 0 != ( expected = value_.load( std::memory_order_relaxed) ) ) {
|
||||
++tests;
|
||||
#if !defined(BOOST_FIBERS_SPIN_SINGLE_CORE)
|
||||
// give CPU a hint that this thread is in a "spin-wait" loop
|
||||
// delays the next instruction's execution for a finite period of time (depends on processor family)
|
||||
// the CPU is not under demand, parts of the pipeline are no longer being used
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
#else
|
||||
// std::this_thread::yield() allows this_thread to give up the remaining part of its time slice,
|
||||
// but only to another thread on the same processor
|
||||
// instead of constant checking, a thread only checks if no other useful work is pending
|
||||
std::this_thread::yield();
|
||||
#endif
|
||||
} else if ( ! value_.compare_exchange_strong( expected, 1, std::memory_order_acquire, std::memory_order_release) ) {
|
||||
// spinlock now contended
|
||||
// utilize 'Binary Exponential Backoff' algorithm
|
||||
// linear_congruential_engine is a random number engine based on Linear congruential generator (LCG)
|
||||
static thread_local std::minstd_rand generator;
|
||||
static std::uniform_int_distribution< std::int32_t > distribution{ 0, static_cast< std::int32_t >( 1) << collisions };
|
||||
const std::int32_t z = distribution( generator);
|
||||
++collisions;
|
||||
for ( std::int32_t i = 0; i < z; ++i) {
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
}
|
||||
} else {
|
||||
// success, lock acquired
|
||||
tests_.store( prev_tests + (tests - prev_tests) / 8, std::memory_order_relaxed);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// failure, lock not acquired
|
||||
// pause via futex
|
||||
if ( 2 != expected) {
|
||||
expected = value_.exchange( 2, std::memory_order_acquire);
|
||||
}
|
||||
while ( 0 != expected) {
|
||||
futex_wait( & value_, 2);
|
||||
expected = value_.exchange( 2, std::memory_order_acquire);
|
||||
}
|
||||
// success, lock acquired
|
||||
tests_.store( prev_tests + (tests - prev_tests) / 8, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void unlock() noexcept {
|
||||
if ( 1 != value_.fetch_sub( 1, std::memory_order_acquire) ) {
|
||||
value_.store( 0, std::memory_order_release);
|
||||
futex_wake( & value_);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif // BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX_H
|
||||
@@ -0,0 +1,104 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2016.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBERS_spinlock_ttas_futex_FUTEX_H
|
||||
#define BOOST_FIBERS_spinlock_ttas_futex_FUTEX_H
|
||||
|
||||
#include <atomic>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/cpu_relax.hpp>
|
||||
#include <boost/fiber/detail/futex.hpp>
|
||||
|
||||
// based on informations from:
|
||||
// https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops
|
||||
// https://software.intel.com/en-us/articles/long-duration-spin-wait-loops-on-hyper-threading-technology-enabled-intel-processors
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
class spinlock_ttas_futex {
|
||||
private:
|
||||
std::atomic< std::int32_t > value_{ 0 };
|
||||
|
||||
public:
|
||||
spinlock_ttas_futex() noexcept = default;
|
||||
|
||||
spinlock_ttas_futex( spinlock_ttas_futex const&) = delete;
|
||||
spinlock_ttas_futex & operator=( spinlock_ttas_futex const&) = delete;
|
||||
|
||||
void lock() noexcept {
|
||||
std::int32_t collisions = 0, tests = 0, expected = 0;
|
||||
// after max. spins or collisions suspend via futex
|
||||
while ( BOOST_FIBERS_SPIN_MAX_TESTS > tests && BOOST_FIBERS_SPIN_MAX_COLLISIONS > collisions) {
|
||||
// avoid using multiple pause instructions for a delay of a specific cycle count
|
||||
// the delay of cpu_relax() (pause on Intel) depends on the processor family
|
||||
// the cycle count can not guaranteed from one system to the next
|
||||
// -> check the shared variable 'value_' in between each cpu_relax() to prevent
|
||||
// unnecessarily long delays on some systems
|
||||
// test shared variable 'status_'
|
||||
// first access to 'value_' -> chache miss
|
||||
// sucessive acccess to 'value_' -> cache hit
|
||||
// if 'value_' was released by other fiber
|
||||
// cached 'value_' is invalidated -> cache miss
|
||||
if ( 0 != ( expected = value_.load( std::memory_order_relaxed) ) ) {
|
||||
++tests;
|
||||
#if !defined(BOOST_FIBERS_SPIN_SINGLE_CORE)
|
||||
// give CPU a hint that this thread is in a "spin-wait" loop
|
||||
// delays the next instruction's execution for a finite period of time (depends on processor family)
|
||||
// the CPU is not under demand, parts of the pipeline are no longer being used
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
#else
|
||||
// std::this_thread::yield() allows this_thread to give up the remaining part of its time slice,
|
||||
// but only to another thread on the same processor
|
||||
// instead of constant checking, a thread only checks if no other useful work is pending
|
||||
std::this_thread::yield();
|
||||
#endif
|
||||
} else if ( ! value_.compare_exchange_strong( expected, 1, std::memory_order_acquire, std::memory_order_release) ) {
|
||||
// spinlock now contended
|
||||
// utilize 'Binary Exponential Backoff' algorithm
|
||||
// linear_congruential_engine is a random number engine based on Linear congruential generator (LCG)
|
||||
static thread_local std::minstd_rand generator;
|
||||
static std::uniform_int_distribution< std::int32_t > distribution{ 0, static_cast< std::int32_t >( 1) << collisions };
|
||||
const std::int32_t z = distribution( generator);
|
||||
++collisions;
|
||||
for ( std::int32_t i = 0; i < z; ++i) {
|
||||
// -> reduces the power consumed by the CPU
|
||||
// -> prevent pipeline stalls
|
||||
cpu_relax();
|
||||
}
|
||||
} else {
|
||||
// success, lock acquired
|
||||
return;
|
||||
}
|
||||
}
|
||||
// failure, lock not acquired
|
||||
// pause via futex
|
||||
if ( 2 != expected) {
|
||||
expected = value_.exchange( 2, std::memory_order_acquire);
|
||||
}
|
||||
while ( 0 != expected) {
|
||||
futex_wait( & value_, 2);
|
||||
expected = value_.exchange( 2, std::memory_order_acquire);
|
||||
}
|
||||
}
|
||||
|
||||
void unlock() noexcept {
|
||||
if ( 1 != value_.fetch_sub( 1, std::memory_order_acquire) ) {
|
||||
value_.store( 0, std::memory_order_release);
|
||||
futex_wake( & value_);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif // BOOST_FIBERS_spinlock_ttas_futex_FUTEX_H
|
||||
@@ -0,0 +1,131 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2014.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_FIBER_DETAIL_WRAP_H
|
||||
#define BOOST_FIBER_DETAIL_WRAP_H
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#if defined(BOOST_NO_CXX17_STD_INVOKE)
|
||||
#include <boost/context/detail/invoke.hpp>
|
||||
#endif
|
||||
#if (BOOST_EXECUTION_CONTEXT==1)
|
||||
# include <boost/context/execution_context.hpp>
|
||||
#else
|
||||
# include <boost/context/continuation.hpp>
|
||||
#endif
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
#include <boost/fiber/detail/data.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace fibers {
|
||||
namespace detail {
|
||||
|
||||
#if (BOOST_EXECUTION_CONTEXT==1)
|
||||
template< typename Fn1, typename Fn2, typename Tpl >
|
||||
class wrapper {
|
||||
private:
|
||||
typename std::decay< Fn1 >::type fn1_;
|
||||
typename std::decay< Fn2 >::type fn2_;
|
||||
typename std::decay< Tpl >::type tpl_;
|
||||
boost::context::execution_context ctx_;
|
||||
|
||||
public:
|
||||
wrapper( Fn1 && fn1, Fn2 && fn2, Tpl && tpl,
|
||||
boost::context::execution_context const& ctx) :
|
||||
fn1_{ std::move( fn1) },
|
||||
fn2_{ std::move( fn2) },
|
||||
tpl_{ std::move( tpl) },
|
||||
ctx_{ ctx } {
|
||||
}
|
||||
|
||||
wrapper( wrapper const&) = delete;
|
||||
wrapper & operator=( wrapper const&) = delete;
|
||||
|
||||
wrapper( wrapper && other) = default;
|
||||
wrapper & operator=( wrapper && other) = default;
|
||||
|
||||
void operator()( void * vp) {
|
||||
#if defined(BOOST_NO_CXX17_STD_INVOKE)
|
||||
boost::context::detail::invoke( std::move( fn1_), fn2_, tpl_, ctx_, vp);
|
||||
#else
|
||||
std::invoke( std::move( fn1_), fn2_, tpl_, ctx_, vp);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
template< typename Fn1, typename Fn2, typename Tpl >
|
||||
wrapper< Fn1, Fn2, Tpl >
|
||||
wrap( Fn1 && fn1, Fn2 && fn2, Tpl && tpl,
|
||||
boost::context::execution_context const& ctx) {
|
||||
return wrapper< Fn1, Fn2, Tpl >{
|
||||
std::forward< Fn1 >( fn1),
|
||||
std::forward< Fn2 >( fn2),
|
||||
std::forward< Tpl >( tpl),
|
||||
ctx };
|
||||
}
|
||||
#else
|
||||
template< typename Fn1, typename Fn2, typename Tpl >
|
||||
class wrapper {
|
||||
private:
|
||||
typename std::decay< Fn1 >::type fn1_;
|
||||
typename std::decay< Fn2 >::type fn2_;
|
||||
typename std::decay< Tpl >::type tpl_;
|
||||
|
||||
public:
|
||||
wrapper( Fn1 && fn1, Fn2 && fn2, Tpl && tpl) :
|
||||
fn1_{ std::move( fn1) },
|
||||
fn2_{ std::move( fn2) },
|
||||
tpl_{ std::move( tpl) } {
|
||||
}
|
||||
|
||||
wrapper( wrapper const&) = delete;
|
||||
wrapper & operator=( wrapper const&) = delete;
|
||||
|
||||
wrapper( wrapper && other) = default;
|
||||
wrapper & operator=( wrapper && other) = default;
|
||||
|
||||
boost::context::continuation
|
||||
operator()( boost::context::continuation && c) {
|
||||
#if defined(BOOST_NO_CXX17_STD_INVOKE)
|
||||
return boost::context::detail::invoke(
|
||||
std::move( fn1_),
|
||||
fn2_,
|
||||
tpl_,
|
||||
std::forward< boost::context::continuation >( c) );
|
||||
#else
|
||||
return std::invoke(
|
||||
std::move( fn1_),
|
||||
fn2_,
|
||||
tpl_,
|
||||
std::forward< boost::context::continuation >( c) );
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
template< typename Fn1, typename Fn2, typename Tpl >
|
||||
wrapper< Fn1, Fn2, Tpl >
|
||||
wrap( Fn1 && fn1, Fn2 && fn2, Tpl && tpl) {
|
||||
return wrapper< Fn1, Fn2, Tpl >{
|
||||
std::forward< Fn1 >( fn1),
|
||||
std::forward< Fn2 >( fn2),
|
||||
std::forward< Tpl >( tpl) };
|
||||
}
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
#include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_FIBER_DETAIL_WRAP_H
|
||||
Reference in New Issue
Block a user