update boost
This commit is contained in:
@@ -44,6 +44,17 @@
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
#if defined(__CET__) && defined(__unix__)
|
||||
# include <cet.h>
|
||||
# include <sys/mman.h>
|
||||
# define SHSTK_ENABLED (__CET__ & 0x2)
|
||||
# define BOOST_CONTEXT_SHADOW_STACK (SHSTK_ENABLED && SHADOW_STACK_SYSCALL)
|
||||
# define __NR_map_shadow_stack 451
|
||||
#ifndef SHADOW_STACK_SET_TOKEN
|
||||
# define SHADOW_STACK_SET_TOKEN 0x1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4702)
|
||||
@@ -62,6 +73,12 @@ transfer_t context_unwind( transfer_t t) {
|
||||
template< typename Rec >
|
||||
transfer_t context_exit( transfer_t t) noexcept {
|
||||
Rec * rec = static_cast< Rec * >( t.data);
|
||||
#if BOOST_CONTEXT_SHADOW_STACK
|
||||
// destory shadow stack
|
||||
std::size_t ss_size = *((unsigned long*)(reinterpret_cast< uintptr_t >( rec)- 16));
|
||||
long unsigned int ss_base = *((unsigned long*)(reinterpret_cast< uintptr_t >( rec)- 8));
|
||||
munmap((void *)ss_base, ss_size);
|
||||
#endif
|
||||
// destroy context stack
|
||||
rec->deallocate();
|
||||
return { nullptr, nullptr };
|
||||
@@ -80,9 +97,6 @@ void context_entry( transfer_t t) noexcept {
|
||||
t.fctx = rec->run( t.fctx);
|
||||
} catch ( forced_unwind const& ex) {
|
||||
t = { ex.fctx, nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
BOOST_ASSERT( nullptr != t.fctx);
|
||||
// destroy context-stack of `this`context on next context
|
||||
@@ -171,6 +185,25 @@ fcontext_t create_context1( StackAlloc && salloc, Fn && fn) {
|
||||
reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
|
||||
// create fast-context
|
||||
const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
|
||||
|
||||
#if BOOST_CONTEXT_SHADOW_STACK
|
||||
std::size_t ss_size = size >> 5;
|
||||
// align shadow stack to 8 bytes.
|
||||
ss_size = (ss_size + 7) & ~7;
|
||||
// Todo: shadow stack occupies at least 4KB
|
||||
ss_size = (ss_size > 4096) ? size : 4096;
|
||||
// create shadow stack
|
||||
void *ss_base = (void *)syscall(__NR_map_shadow_stack, 0, ss_size, SHADOW_STACK_SET_TOKEN);
|
||||
BOOST_ASSERT(ss_base != -1);
|
||||
unsigned long ss_sp = (unsigned long)ss_base + ss_size;
|
||||
/* pass the shadow stack pointer to make_fcontext
|
||||
i.e., link the new shadow stack with the new fcontext
|
||||
TODO should be a better way? */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( stack_top)- 8)) = ss_sp;
|
||||
/* Todo: place shadow stack info in 64byte gap */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 8)) = (unsigned long) ss_base;
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 16)) = ss_size;
|
||||
#endif
|
||||
const fcontext_t fctx = make_fcontext( stack_top, size, & context_entry< Record >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// transfer control structure to context-stack
|
||||
@@ -193,6 +226,25 @@ fcontext_t create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn)
|
||||
reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
|
||||
// create fast-context
|
||||
const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
|
||||
|
||||
#if BOOST_CONTEXT_SHADOW_STACK
|
||||
std::size_t ss_size = size >> 5;
|
||||
// align shadow stack to 8 bytes.
|
||||
ss_size = (ss_size + 7) & ~7;
|
||||
// Todo: shadow stack occupies at least 4KB
|
||||
ss_size = (ss_size > 4096) ? size : 4096;
|
||||
// create shadow stack
|
||||
void *ss_base = (void *)syscall(__NR_map_shadow_stack, 0, ss_size, SHADOW_STACK_SET_TOKEN);
|
||||
BOOST_ASSERT(ss_base != -1);
|
||||
unsigned long ss_sp = (unsigned long)ss_base + ss_size;
|
||||
/* pass the shadow stack pointer to make_fcontext
|
||||
i.e., link the new shadow stack with the new fcontext
|
||||
TODO should be a better way? */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( stack_top)- 8)) = ss_sp;
|
||||
/* Todo: place shadow stack info in 64byte gap */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 8)) = (unsigned long) ss_base;
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 16)) = ss_size;
|
||||
#endif
|
||||
const fcontext_t fctx = make_fcontext( stack_top, size, & context_entry< Record >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// transfer control structure to context-stack
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#ifndef BOOST_CONTEXT_CONTINUATION_H
|
||||
#define BOOST_CONTEXT_CONTINUATION_H
|
||||
|
||||
#include <boost/predef.h>
|
||||
#include <boost/predef/os.h>
|
||||
#if BOOST_OS_MACOS
|
||||
#define _XOPEN_SOURCE 600
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@ extern "C" {
|
||||
#include <ucontext.h>
|
||||
}
|
||||
|
||||
#include <boost/predef.h>
|
||||
#include <boost/context/detail/config.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -60,12 +61,19 @@ namespace detail {
|
||||
// tampoline function
|
||||
// entered if the execution context
|
||||
// is resumed for the first time
|
||||
template< typename Record >
|
||||
static void entry_func( void * data) noexcept {
|
||||
Record * record = static_cast< Record * >( data);
|
||||
BOOST_ASSERT( nullptr != record);
|
||||
// start execution of toplevel context-function
|
||||
record->run();
|
||||
template <typename Record>
|
||||
#ifdef BOOST_OS_MACOS
|
||||
static void entry_func(std::uint32_t data_high,
|
||||
std::uint32_t data_low) noexcept {
|
||||
auto data =
|
||||
reinterpret_cast<void *>(std::uint64_t(data_high) << 32 | data_low);
|
||||
#else
|
||||
static void entry_func(void *data) noexcept {
|
||||
#endif
|
||||
Record *record = static_cast<Record *>(data);
|
||||
BOOST_ASSERT(nullptr != record);
|
||||
// start execution of toplevel context-function
|
||||
record->run();
|
||||
}
|
||||
|
||||
struct BOOST_CONTEXT_DECL activation_record {
|
||||
@@ -210,19 +218,10 @@ struct BOOST_CONTEXT_DECL activation_record_initializer {
|
||||
|
||||
struct forced_unwind {
|
||||
activation_record * from{ nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
bool caught{ false };
|
||||
#endif
|
||||
|
||||
forced_unwind( activation_record * from_) noexcept :
|
||||
from{ from_ } {
|
||||
}
|
||||
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
~forced_unwind() {
|
||||
BOOST_ASSERT( caught);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn >
|
||||
@@ -268,9 +267,6 @@ public:
|
||||
#endif
|
||||
} catch ( forced_unwind const& ex) {
|
||||
c = Ctx{ ex.from };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
// this context has finished its task
|
||||
from = nullptr;
|
||||
@@ -310,7 +306,15 @@ static activation_record * create_context1( StackAlloc && salloc, Fn && fn) {
|
||||
record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) -
|
||||
reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64);
|
||||
record->uctx.uc_link = nullptr;
|
||||
::makecontext( & record->uctx, ( void (*)() ) & entry_func< capture_t >, 1, record);
|
||||
#ifdef BOOST_OS_MACOS
|
||||
const auto integer = std::uint64_t(record);
|
||||
::makecontext(&record->uctx, (void (*)()) & entry_func<capture_t>, 2,
|
||||
std::uint32_t((integer >> 32) & 0xFFFFFFFF),
|
||||
std::uint32_t(integer));
|
||||
#else
|
||||
::makecontext(&record->uctx, (void (*)()) & entry_func<capture_t>, 1,
|
||||
record);
|
||||
#endif
|
||||
#if defined(BOOST_USE_ASAN)
|
||||
record->stack_bottom = record->uctx.uc_stack.ss_sp;
|
||||
record->stack_size = record->uctx.uc_stack.ss_size;
|
||||
@@ -345,7 +349,15 @@ static activation_record * create_context2( preallocated palloc, StackAlloc && s
|
||||
record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) -
|
||||
reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64);
|
||||
record->uctx.uc_link = nullptr;
|
||||
::makecontext( & record->uctx, ( void (*)() ) & entry_func< capture_t >, 1, record);
|
||||
#ifdef BOOST_OS_MACOS
|
||||
const auto integer = std::uint64_t(record);
|
||||
::makecontext(&record->uctx, (void (*)()) & entry_func<capture_t>, 2,
|
||||
std::uint32_t((integer >> 32) & 0xFFFFFFFF),
|
||||
std::uint32_t(integer));
|
||||
#else
|
||||
::makecontext(&record->uctx, (void (*)()) & entry_func<capture_t>, 1,
|
||||
record);
|
||||
#endif
|
||||
#if defined(BOOST_USE_ASAN)
|
||||
record->stack_bottom = record->uctx.uc_stack.ss_sp;
|
||||
record->stack_size = record->uctx.uc_stack.ss_size;
|
||||
@@ -465,6 +477,8 @@ public:
|
||||
return ptr_ < other.ptr_;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
|
||||
@@ -475,11 +489,33 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other);
|
||||
|
||||
#endif
|
||||
|
||||
void swap( continuation & other) noexcept {
|
||||
std::swap( ptr_, other.ptr_);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
inline std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
|
||||
if ( nullptr != other.ptr_) {
|
||||
return os << other.ptr_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template<
|
||||
typename Fn,
|
||||
typename = detail::disable_overload< continuation, Fn >
|
||||
|
||||
@@ -186,19 +186,10 @@ struct BOOST_CONTEXT_DECL activation_record_initializer {
|
||||
|
||||
struct forced_unwind {
|
||||
activation_record * from{ nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
bool caught{ false };
|
||||
#endif
|
||||
|
||||
explicit forced_unwind( activation_record * from_) :
|
||||
from{ from_ } {
|
||||
}
|
||||
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
~forced_unwind() {
|
||||
BOOST_ASSERT( caught);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn >
|
||||
@@ -239,9 +230,6 @@ public:
|
||||
#endif
|
||||
} catch ( forced_unwind const& ex) {
|
||||
c = Ctx{ ex.from };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
// this context has finished its task
|
||||
from = nullptr;
|
||||
@@ -399,7 +387,9 @@ public:
|
||||
bool operator<( continuation const& other) const noexcept {
|
||||
return ptr_ < other.ptr_;
|
||||
}
|
||||
|
||||
|
||||
#if !defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
|
||||
@@ -410,11 +400,33 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other);
|
||||
|
||||
#endif
|
||||
|
||||
void swap( continuation & other) noexcept {
|
||||
std::swap( ptr_, other.ptr_);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
inline std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
|
||||
if ( nullptr != other.ptr_) {
|
||||
return os << other.ptr_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template<
|
||||
typename Fn,
|
||||
typename = detail::disable_overload< continuation, Fn >
|
||||
|
||||
@@ -30,6 +30,10 @@
|
||||
# define BOOST_CONTEXT_DECL
|
||||
#endif
|
||||
|
||||
#if ! defined(BOOST_USE_UCONTEXT) && defined(__CYGWIN__)
|
||||
# define BOOST_USE_UCONTEXT
|
||||
#endif
|
||||
|
||||
#if ! defined(BOOST_CONTEXT_SOURCE) && ! defined(BOOST_ALL_NO_LIB) && ! defined(BOOST_CONTEXT_NO_LIB)
|
||||
# define BOOST_LIB_NAME boost_context
|
||||
# if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_CONTEXT_DYN_LINK)
|
||||
@@ -124,4 +128,9 @@ static constexpr std::size_t prefetch_stride{ 4 * cacheline_length };
|
||||
# include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
// stacks need mmap(2) with MAP_STACK
|
||||
# define BOOST_CONTEXT_USE_MAP_STACK
|
||||
#endif
|
||||
|
||||
#endif // BOOST_CONTEXT_DETAIL_CONFIG_H
|
||||
|
||||
@@ -22,21 +22,12 @@ namespace detail {
|
||||
|
||||
struct forced_unwind {
|
||||
fcontext_t fctx{ nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
bool caught{ false };
|
||||
#endif
|
||||
|
||||
forced_unwind() = default;
|
||||
|
||||
forced_unwind( fcontext_t fctx_) :
|
||||
fctx( fctx_) {
|
||||
}
|
||||
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
~forced_unwind() {
|
||||
BOOST_ASSERT( caught);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
|
||||
#include <boost/context/detail/config.hpp>
|
||||
|
||||
#if defined(BOOST_CONTEXT_NO_CXX14_INTEGER_SEQUENCE)
|
||||
#include <boost/mp11/integer_sequence.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
@@ -29,38 +33,12 @@ using make_index_sequence = std::make_index_sequence< I >;
|
||||
template< typename ... T >
|
||||
using index_sequence_for = std::index_sequence_for< T ... >;
|
||||
#else
|
||||
//http://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence
|
||||
|
||||
template< std::size_t ... I >
|
||||
struct index_sequence {
|
||||
using type = index_sequence;
|
||||
using value_type = std::size_t;
|
||||
static constexpr std::size_t size() {
|
||||
return sizeof ... (I);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename Seq1, typename Seq2 >
|
||||
struct concat_sequence;
|
||||
|
||||
template< std::size_t ... I1, std::size_t ... I2 >
|
||||
struct concat_sequence< index_sequence< I1 ... >, index_sequence< I2 ... > > : public index_sequence< I1 ..., (sizeof ... (I1)+I2) ... > {
|
||||
};
|
||||
|
||||
using index_sequence = mp11::index_sequence< I ... >;
|
||||
template< std::size_t I >
|
||||
struct make_index_sequence : public concat_sequence< typename make_index_sequence< I/2 >::type,
|
||||
typename make_index_sequence< I-I/2 >::type > {
|
||||
};
|
||||
|
||||
template<>
|
||||
struct make_index_sequence< 0 > : public index_sequence<> {
|
||||
};
|
||||
template<>
|
||||
struct make_index_sequence< 1 > : public index_sequence< 0 > {
|
||||
};
|
||||
|
||||
using make_index_sequence = mp11::make_index_sequence< I >;
|
||||
template< typename ... T >
|
||||
using index_sequence_for = make_index_sequence< sizeof ... (T) >;
|
||||
using index_sequence_for = mp11::index_sequence_for< T ... >;
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include <immintrin.h>
|
||||
#endif
|
||||
|
||||
#if BOOST_COMP_MSVC
|
||||
#if BOOST_COMP_MSVC && !defined(_M_ARM) && !defined(_M_ARM64)
|
||||
#include <mmintrin.h>
|
||||
#endif
|
||||
|
||||
@@ -44,7 +44,7 @@ void prefetch( void * addr) {
|
||||
// L1 cache : hint == _MM_HINT_T0
|
||||
_mm_prefetch( (const char *)addr, _MM_HINT_T0);
|
||||
}
|
||||
#elif BOOST_COMP_MSVC
|
||||
#elif BOOST_COMP_MSVC && !defined(_M_ARM) && !defined(_M_ARM64)
|
||||
#define BOOST_HAS_PREFETCH 1
|
||||
BOOST_FORCEINLINE
|
||||
void prefetch( void * addr) {
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
|
||||
// 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)
|
||||
|
||||
#include <boost/context/detail/config.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_THREAD_LOCAL)
|
||||
# include <boost/context/execution_context_v1.hpp>
|
||||
#endif
|
||||
#include <boost/context/execution_context_v2.hpp>
|
||||
@@ -1,473 +0,0 @@
|
||||
|
||||
// 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_CONTEXT_EXECUTION_CONTEXT_V1_H
|
||||
#define BOOST_CONTEXT_EXECUTION_CONTEXT_V1_H
|
||||
|
||||
#include <boost/context/detail/config.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/intrusive_ptr.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
#include <boost/context/detail/apply.hpp>
|
||||
#endif
|
||||
#include <boost/context/detail/disable_overload.hpp>
|
||||
#include <boost/context/detail/externc.hpp>
|
||||
#include <boost/context/detail/fcontext.hpp>
|
||||
#include <boost/context/fixedsize_stack.hpp>
|
||||
#include <boost/context/flags.hpp>
|
||||
#include <boost/context/preallocated.hpp>
|
||||
#include <boost/context/segmented_stack.hpp>
|
||||
#include <boost/context/stack_context.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace context {
|
||||
namespace detail {
|
||||
|
||||
template< typename Fn >
|
||||
transfer_t ecv1_context_ontop( transfer_t);
|
||||
|
||||
struct ecv1_activation_record;
|
||||
|
||||
struct ecv1_data_t {
|
||||
ecv1_activation_record * from;
|
||||
void * data;
|
||||
};
|
||||
|
||||
struct BOOST_CONTEXT_DECL ecv1_activation_record {
|
||||
typedef boost::intrusive_ptr< ecv1_activation_record > ptr_t;
|
||||
|
||||
static ptr_t & current() noexcept;
|
||||
|
||||
std::atomic< std::size_t > use_count{ 0 };
|
||||
fcontext_t fctx{ nullptr };
|
||||
stack_context sctx{};
|
||||
bool main_ctx{ true };
|
||||
|
||||
// used for toplevel-context
|
||||
// (e.g. main context, thread-entry context)
|
||||
ecv1_activation_record() = default;
|
||||
|
||||
ecv1_activation_record( fcontext_t fctx_, stack_context sctx_) noexcept :
|
||||
fctx{ fctx_ },
|
||||
sctx( sctx_ ), // sctx{ sctx_ } - clang-3.6: no viable conversion from 'boost::context::stack_context' to 'std::size_t'
|
||||
main_ctx{ false } {
|
||||
}
|
||||
|
||||
virtual ~ecv1_activation_record() = default;
|
||||
|
||||
bool is_main_context() const noexcept {
|
||||
return main_ctx;
|
||||
}
|
||||
|
||||
void * resume( void * vp) {
|
||||
// store current activation record in local variable
|
||||
auto from = current().get();
|
||||
// store `this` in static, thread local pointer
|
||||
// `this` will become the active (running) context
|
||||
// returned by execution_context::current()
|
||||
current() = this;
|
||||
#if defined(BOOST_USE_SEGMENTED_STACKS)
|
||||
// adjust segmented stack properties
|
||||
__splitstack_getcontext( from->sctx.segments_ctx);
|
||||
__splitstack_setcontext( sctx.segments_ctx);
|
||||
#endif
|
||||
ecv1_data_t d = { from, vp };
|
||||
// context switch from parent context to `this`-context
|
||||
transfer_t t = jump_fcontext( fctx, & d);
|
||||
ecv1_data_t * dp = reinterpret_cast< ecv1_data_t * >( t.data);
|
||||
dp->from->fctx = t.fctx;
|
||||
// parent context resumed
|
||||
return dp->data;
|
||||
}
|
||||
|
||||
template< typename Fn >
|
||||
void * resume_ontop( void * data, Fn && fn) {
|
||||
// store current activation record in local variable
|
||||
ecv1_activation_record * from = current().get();
|
||||
// store `this` in static, thread local pointer
|
||||
// `this` will become the active (running) context
|
||||
// returned by execution_context::current()
|
||||
current() = this;
|
||||
#if defined(BOOST_USE_SEGMENTED_STACKS)
|
||||
// adjust segmented stack properties
|
||||
__splitstack_getcontext( from->sctx.segments_ctx);
|
||||
__splitstack_setcontext( sctx.segments_ctx);
|
||||
#endif
|
||||
std::tuple< void *, Fn > p = std::forward_as_tuple( data, fn);
|
||||
ecv1_data_t d = { from, & p };
|
||||
// context switch from parent context to `this`-context
|
||||
// execute Fn( Tpl) on top of `this`
|
||||
transfer_t t = ontop_fcontext( fctx, & d, ecv1_context_ontop< Fn >);
|
||||
ecv1_data_t * dp = reinterpret_cast< ecv1_data_t * >( t.data);
|
||||
dp->from->fctx = t.fctx;
|
||||
// parent context resumed
|
||||
return dp->data;
|
||||
}
|
||||
|
||||
virtual void deallocate() noexcept {
|
||||
}
|
||||
|
||||
friend void intrusive_ptr_add_ref( ecv1_activation_record * ar) noexcept {
|
||||
++ar->use_count;
|
||||
}
|
||||
|
||||
friend void intrusive_ptr_release( ecv1_activation_record * ar) noexcept {
|
||||
BOOST_ASSERT( nullptr != ar);
|
||||
if ( 0 == --ar->use_count) {
|
||||
ar->deallocate();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct BOOST_CONTEXT_DECL ecv1_activation_record_initializer {
|
||||
ecv1_activation_record_initializer() noexcept;
|
||||
~ecv1_activation_record_initializer();
|
||||
};
|
||||
|
||||
template< typename Fn >
|
||||
transfer_t ecv1_context_ontop( transfer_t t) {
|
||||
ecv1_data_t * dp = reinterpret_cast< ecv1_data_t * >( t.data);
|
||||
dp->from->fctx = t.fctx;
|
||||
auto tpl = reinterpret_cast< std::tuple< void *, Fn > * >( dp->data);
|
||||
BOOST_ASSERT( nullptr != tpl);
|
||||
auto data = std::get< 0 >( * tpl);
|
||||
typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 1 >( * tpl) );
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
dp->data = boost::context::detail::apply( fn, std::tie( data) );
|
||||
#else
|
||||
dp->data = std::apply( fn, std::tie( data) );
|
||||
#endif
|
||||
return { t.fctx, dp };
|
||||
}
|
||||
|
||||
template< typename StackAlloc, typename Fn, typename ... Args >
|
||||
class ecv1_capture_record : public ecv1_activation_record {
|
||||
private:
|
||||
typename std::decay< StackAlloc >::type salloc_;
|
||||
typename std::decay< Fn >::type fn_;
|
||||
std::tuple< typename std::decay< Args >::type ... > args_;
|
||||
ecv1_activation_record * caller_;
|
||||
|
||||
static void destroy( ecv1_capture_record * p) noexcept {
|
||||
typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
|
||||
stack_context sctx = p->sctx;
|
||||
// deallocate activation record
|
||||
p->~ecv1_capture_record();
|
||||
// destroy stack with stack allocator
|
||||
salloc.deallocate( sctx);
|
||||
}
|
||||
|
||||
public:
|
||||
ecv1_capture_record( stack_context sctx, StackAlloc && salloc,
|
||||
fcontext_t fctx,
|
||||
ecv1_activation_record * caller,
|
||||
Fn && fn, Args && ... args) noexcept :
|
||||
ecv1_activation_record{ fctx, sctx },
|
||||
salloc_{ std::forward< StackAlloc >( salloc) },
|
||||
fn_( std::forward< Fn >( fn) ),
|
||||
args_( std::forward< Args >( args) ... ),
|
||||
caller_{ caller } {
|
||||
}
|
||||
|
||||
void deallocate() noexcept override final {
|
||||
destroy( this);
|
||||
}
|
||||
|
||||
void run() {
|
||||
auto data = caller_->resume( nullptr);
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
boost::context::detail::apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
|
||||
#else
|
||||
std::apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
|
||||
#endif
|
||||
BOOST_ASSERT_MSG( ! main_ctx, "main-context does not execute activation-record::run()");
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace v1 {
|
||||
|
||||
class BOOST_CONTEXT_DECL execution_context {
|
||||
private:
|
||||
// tampoline function
|
||||
// entered if the execution context
|
||||
// is resumed for the first time
|
||||
template< typename AR >
|
||||
static void entry_func( detail::transfer_t t) noexcept {
|
||||
detail::ecv1_data_t * dp = reinterpret_cast< detail::ecv1_data_t * >( t.data);
|
||||
AR * ar = static_cast< AR * >( dp->data);
|
||||
BOOST_ASSERT( nullptr != ar);
|
||||
dp->from->fctx = t.fctx;
|
||||
// start execution of toplevel context-function
|
||||
ar->run();
|
||||
}
|
||||
|
||||
typedef boost::intrusive_ptr< detail::ecv1_activation_record > ptr_t;
|
||||
|
||||
ptr_t ptr_;
|
||||
|
||||
template< typename StackAlloc, typename Fn, typename ... Args >
|
||||
static detail::ecv1_activation_record * create_context( StackAlloc && salloc,
|
||||
Fn && fn, Args && ... args) {
|
||||
typedef detail::ecv1_capture_record<
|
||||
StackAlloc, Fn, Args ...
|
||||
> capture_t;
|
||||
|
||||
auto sctx = salloc.allocate();
|
||||
// reserve space for control structure
|
||||
#if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||
const std::size_t size = sctx.size - sizeof( capture_t);
|
||||
void * sp = static_cast< char * >( sctx.sp) - sizeof( capture_t);
|
||||
#else
|
||||
constexpr std::size_t func_alignment = 64; // alignof( capture_t);
|
||||
constexpr std::size_t func_size = sizeof( capture_t);
|
||||
// reserve space on stack
|
||||
void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
|
||||
// align sp pointer
|
||||
std::size_t space = func_size + func_alignment;
|
||||
sp = std::align( func_alignment, func_size, sp, space);
|
||||
BOOST_ASSERT( nullptr != sp);
|
||||
// calculate remaining size
|
||||
const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
|
||||
#endif
|
||||
// create fast-context
|
||||
const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// get current activation record
|
||||
auto curr = execution_context::current().ptr_;
|
||||
// placment new for control structure on fast-context stack
|
||||
return ::new ( sp) capture_t{
|
||||
sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
|
||||
}
|
||||
|
||||
template< typename StackAlloc, typename Fn, typename ... Args >
|
||||
static detail::ecv1_activation_record * create_context( preallocated palloc, StackAlloc && salloc,
|
||||
Fn && fn, Args && ... args) {
|
||||
typedef detail::ecv1_capture_record<
|
||||
StackAlloc, Fn, Args ...
|
||||
> capture_t;
|
||||
|
||||
// reserve space for control structure
|
||||
#if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||
const std::size_t size = palloc.size - sizeof( capture_t);
|
||||
void * sp = static_cast< char * >( palloc.sp) - sizeof( capture_t);
|
||||
#else
|
||||
constexpr std::size_t func_alignment = 64; // alignof( capture_t);
|
||||
constexpr std::size_t func_size = sizeof( capture_t);
|
||||
// reserve space on stack
|
||||
void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
|
||||
// align sp pointer
|
||||
std::size_t space = func_size + func_alignment;
|
||||
sp = std::align( func_alignment, func_size, sp, space);
|
||||
BOOST_ASSERT( nullptr != sp);
|
||||
// calculate remaining size
|
||||
const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
|
||||
#endif
|
||||
// create fast-context
|
||||
const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// get current activation record
|
||||
auto curr = execution_context::current().ptr_;
|
||||
// placment new for control structure on fast-context stack
|
||||
return ::new ( sp) capture_t{
|
||||
palloc.sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
|
||||
}
|
||||
|
||||
execution_context() noexcept :
|
||||
// default constructed with current ecv1_activation_record
|
||||
ptr_{ detail::ecv1_activation_record::current() } {
|
||||
}
|
||||
|
||||
public:
|
||||
static execution_context current() noexcept;
|
||||
|
||||
#if defined(BOOST_USE_SEGMENTED_STACKS)
|
||||
template< typename Fn,
|
||||
typename ... Args,
|
||||
typename = detail::disable_overload< execution_context, Fn >
|
||||
>
|
||||
execution_context( Fn && fn, Args && ... args) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
ptr_{ create_context( segmented_stack(),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Args >( args) ...) } {
|
||||
ptr_->resume( ptr_.get() );
|
||||
}
|
||||
|
||||
template< typename Fn,
|
||||
typename ... Args
|
||||
>
|
||||
execution_context( std::allocator_arg_t, segmented_stack salloc, Fn && fn, Args && ... args) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
ptr_{ create_context( salloc,
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Args >( args) ...) } {
|
||||
ptr_->resume( ptr_.get() );
|
||||
}
|
||||
|
||||
template< typename Fn,
|
||||
typename ... Args
|
||||
>
|
||||
execution_context( std::allocator_arg_t, preallocated palloc, segmented_stack salloc, Fn && fn, Args && ... args) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
ptr_{ create_context( palloc, salloc,
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Args >( args) ...) } {
|
||||
ptr_->resume( ptr_.get() );
|
||||
}
|
||||
#else
|
||||
template< typename Fn,
|
||||
typename ... Args,
|
||||
typename = detail::disable_overload< execution_context, Fn >
|
||||
>
|
||||
execution_context( Fn && fn, Args && ... args) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
ptr_{ create_context( fixedsize_stack(),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Args >( args) ...) } {
|
||||
ptr_->resume( ptr_.get() );
|
||||
}
|
||||
|
||||
template< typename StackAlloc,
|
||||
typename Fn,
|
||||
typename ... Args
|
||||
>
|
||||
execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Args && ... args) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
ptr_{ create_context( std::forward< StackAlloc >( salloc),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Args >( args) ...) } {
|
||||
ptr_->resume( ptr_.get() );
|
||||
}
|
||||
|
||||
template< typename StackAlloc,
|
||||
typename Fn,
|
||||
typename ... Args
|
||||
>
|
||||
execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Args && ... args) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
ptr_{ create_context( palloc, std::forward< StackAlloc >( salloc),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Args >( args) ...) } {
|
||||
ptr_->resume( ptr_.get() );
|
||||
}
|
||||
#endif
|
||||
|
||||
execution_context( execution_context const& other) noexcept :
|
||||
ptr_{ other.ptr_ } {
|
||||
}
|
||||
|
||||
execution_context( execution_context && other) noexcept :
|
||||
ptr_{ other.ptr_ } {
|
||||
other.ptr_.reset();
|
||||
}
|
||||
|
||||
execution_context & operator=( execution_context const& other) noexcept {
|
||||
// intrusive_ptr<> does not test for self-assignment
|
||||
if ( this == & other) return * this;
|
||||
ptr_ = other.ptr_;
|
||||
return * this;
|
||||
}
|
||||
|
||||
execution_context & operator=( execution_context && other) noexcept {
|
||||
if ( this == & other) return * this;
|
||||
execution_context tmp{ std::move( other) };
|
||||
swap( tmp);
|
||||
return * this;
|
||||
}
|
||||
|
||||
void * operator()( void * vp = nullptr) {
|
||||
return ptr_->resume( vp);
|
||||
}
|
||||
|
||||
template< typename Fn >
|
||||
void * operator()( exec_ontop_arg_t, Fn && fn, void * vp = nullptr) {
|
||||
return ptr_->resume_ontop( vp,
|
||||
std::forward< Fn >( fn) );
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
return nullptr != ptr_.get();
|
||||
}
|
||||
|
||||
bool operator!() const noexcept {
|
||||
return nullptr == ptr_.get();
|
||||
}
|
||||
|
||||
bool operator<( execution_context const& other) const noexcept {
|
||||
return ptr_ < other.ptr_;
|
||||
}
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
|
||||
if ( nullptr != other.ptr_) {
|
||||
return os << other.ptr_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
void swap( execution_context & other) noexcept {
|
||||
ptr_.swap( other.ptr_);
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
void swap( execution_context & l, execution_context & r) noexcept {
|
||||
l.swap( r);
|
||||
}
|
||||
|
||||
}}}
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_CONTEXT_EXECUTION_CONTEXT_V1_H
|
||||
@@ -1,486 +0,0 @@
|
||||
|
||||
// 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_CONTEXT_EXECUTION_CONTEXT_V2_H
|
||||
#define BOOST_CONTEXT_EXECUTION_CONTEXT_V2_H
|
||||
|
||||
#include <boost/context/detail/config.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/intrusive_ptr.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
#include <boost/context/detail/apply.hpp>
|
||||
#endif
|
||||
#include <boost/context/detail/disable_overload.hpp>
|
||||
#include <boost/context/detail/exception.hpp>
|
||||
#include <boost/context/detail/exchange.hpp>
|
||||
#include <boost/context/detail/fcontext.hpp>
|
||||
#include <boost/context/detail/tuple.hpp>
|
||||
#include <boost/context/fixedsize_stack.hpp>
|
||||
#include <boost/context/flags.hpp>
|
||||
#include <boost/context/preallocated.hpp>
|
||||
#include <boost/context/segmented_stack.hpp>
|
||||
#include <boost/context/stack_context.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4702)
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace context {
|
||||
namespace detail {
|
||||
|
||||
transfer_t ecv2_context_unwind( transfer_t);
|
||||
|
||||
template< typename Rec >
|
||||
transfer_t ecv2_context_exit( transfer_t) noexcept;
|
||||
|
||||
template< typename Rec >
|
||||
void ecv2_context_etry( transfer_t) noexcept;
|
||||
|
||||
template< typename Ctx, typename Fn, typename ... Args >
|
||||
transfer_t ecv2_context_ontop( transfer_t);
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create( StackAlloc &&, Fn &&, Params && ...);
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create( preallocated, StackAlloc &&, Fn &&, Params && ...);
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
class ecv2_record {
|
||||
private:
|
||||
typename std::decay< StackAlloc >::type salloc_;
|
||||
stack_context sctx_;
|
||||
typename std::decay< Fn >::type fn_;
|
||||
std::tuple< typename std::decay< Params >::type ... > params_;
|
||||
|
||||
static void destroy( ecv2_record * p) noexcept {
|
||||
typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
|
||||
stack_context sctx = p->sctx_;
|
||||
// deallocate ecv2_record
|
||||
p->~ecv2_record();
|
||||
// destroy stack with stack allocator
|
||||
salloc.deallocate( sctx);
|
||||
}
|
||||
|
||||
public:
|
||||
ecv2_record( stack_context sctx, StackAlloc && salloc,
|
||||
Fn && fn, Params && ... params) noexcept :
|
||||
salloc_( std::forward< StackAlloc >( salloc)),
|
||||
sctx_( sctx),
|
||||
fn_( std::forward< Fn >( fn) ),
|
||||
params_( std::forward< Params >( params) ... ) {
|
||||
}
|
||||
|
||||
ecv2_record( ecv2_record const&) = delete;
|
||||
ecv2_record & operator=( ecv2_record const&) = delete;
|
||||
|
||||
void deallocate() noexcept {
|
||||
destroy( this);
|
||||
}
|
||||
|
||||
transfer_t run( transfer_t t) {
|
||||
Ctx from{ t.fctx };
|
||||
typename Ctx::args_tpl_t args = std::move( std::get<1>( * static_cast< std::tuple< std::exception_ptr, typename Ctx::args_tpl_t > * >( t.data) ) );
|
||||
auto tpl = std::tuple_cat(
|
||||
params_,
|
||||
std::forward_as_tuple( std::move( from) ),
|
||||
std::move( args) );
|
||||
// invoke context-function
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
Ctx cc = boost::context::detail::apply( std::move( fn_), std::move( tpl) );
|
||||
#else
|
||||
Ctx cc = std::apply( std::move( fn_), std::move( tpl) );
|
||||
#endif
|
||||
return { exchange( cc.fctx_, nullptr), nullptr };
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
inline namespace v2 {
|
||||
|
||||
template< typename ... Args >
|
||||
class execution_context {
|
||||
private:
|
||||
friend class ontop_error;
|
||||
|
||||
typedef std::tuple< Args ... > args_tpl_t;
|
||||
typedef std::tuple< execution_context, typename std::decay< Args >::type ... > ret_tpl_t;
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
friend class detail::ecv2_record;
|
||||
|
||||
template< typename Ctx, typename Fn, typename ... ArgsT >
|
||||
friend detail::transfer_t detail::ecv2_context_ontop( detail::transfer_t);
|
||||
|
||||
detail::fcontext_t fctx_{ nullptr };
|
||||
|
||||
execution_context( detail::fcontext_t fctx) noexcept :
|
||||
fctx_( fctx) {
|
||||
}
|
||||
|
||||
public:
|
||||
execution_context() noexcept = default;
|
||||
|
||||
#if defined(BOOST_USE_SEGMENTED_STACKS)
|
||||
// segmented-stack requires to preserve the segments of the `current` context
|
||||
// which is not possible (no global pointer to current context)
|
||||
template< typename Fn, typename ... Params >
|
||||
execution_context( std::allocator_arg_t, segmented_stack, Fn &&, Params && ...) = delete;
|
||||
|
||||
template< typename Fn, typename ... Params >
|
||||
execution_context( std::allocator_arg_t, preallocated, segmented_stack, Fn &&, Params && ...) = delete;
|
||||
#else
|
||||
template< typename Fn,
|
||||
typename ... Params,
|
||||
typename = detail::disable_overload< execution_context, Fn >
|
||||
>
|
||||
execution_context( Fn && fn, Params && ... params) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
fctx_( detail::ecv2_context_create< execution_context >(
|
||||
fixedsize_stack(),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Params >( params) ... ) ) {
|
||||
}
|
||||
|
||||
template< typename StackAlloc,
|
||||
typename Fn,
|
||||
typename ... Params
|
||||
>
|
||||
execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Params && ... params) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
fctx_( detail::ecv2_context_create< execution_context >(
|
||||
std::forward< StackAlloc >( salloc),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Params >( params) ... ) ) {
|
||||
}
|
||||
|
||||
template< typename StackAlloc,
|
||||
typename Fn,
|
||||
typename ... Params
|
||||
>
|
||||
execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Params && ... params) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
fctx_( detail::ecv2_context_create< execution_context >(
|
||||
palloc, std::forward< StackAlloc >( salloc),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Params >( params) ... ) ) {
|
||||
}
|
||||
#endif
|
||||
|
||||
~execution_context() {
|
||||
if ( nullptr != fctx_) {
|
||||
detail::ontop_fcontext( detail::exchange( fctx_, nullptr), nullptr, detail::ecv2_context_unwind);
|
||||
}
|
||||
}
|
||||
|
||||
execution_context( execution_context && other) noexcept :
|
||||
fctx_( other.fctx_) {
|
||||
other.fctx_ = nullptr;
|
||||
}
|
||||
|
||||
execution_context & operator=( execution_context && other) noexcept {
|
||||
if ( this != & other) {
|
||||
execution_context tmp = std::move( other);
|
||||
swap( tmp);
|
||||
}
|
||||
return * this;
|
||||
}
|
||||
|
||||
execution_context( execution_context const& other) noexcept = delete;
|
||||
execution_context & operator=( execution_context const& other) noexcept = delete;
|
||||
|
||||
ret_tpl_t operator()( Args ... args);
|
||||
|
||||
template< typename Fn >
|
||||
ret_tpl_t operator()( exec_ontop_arg_t, Fn && fn, Args ... args);
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
return nullptr != fctx_;
|
||||
}
|
||||
|
||||
bool operator!() const noexcept {
|
||||
return nullptr == fctx_;
|
||||
}
|
||||
|
||||
bool operator<( execution_context const& other) const noexcept {
|
||||
return fctx_ < other.fctx_;
|
||||
}
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
|
||||
if ( nullptr != other.fctx_) {
|
||||
return os << other.fctx_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
void swap( execution_context & other) noexcept {
|
||||
std::swap( fctx_, other.fctx_);
|
||||
}
|
||||
};
|
||||
|
||||
class ontop_error : public std::exception {
|
||||
private:
|
||||
detail::fcontext_t fctx_;
|
||||
|
||||
public:
|
||||
ontop_error( detail::fcontext_t fctx) noexcept :
|
||||
fctx_{ fctx } {
|
||||
}
|
||||
|
||||
template< typename ... Args >
|
||||
execution_context< Args ... > get_context() const noexcept {
|
||||
return execution_context< Args ... >{ fctx_ };
|
||||
}
|
||||
};
|
||||
|
||||
template< typename ... Args >
|
||||
typename execution_context< Args ... >::ret_tpl_t
|
||||
execution_context< Args ... >::operator()( Args ... args) {
|
||||
BOOST_ASSERT( nullptr != fctx_);
|
||||
args_tpl_t data( std::forward< Args >( args) ... );
|
||||
auto p = std::make_tuple( std::exception_ptr{}, std::move( data) );
|
||||
detail::transfer_t t = detail::jump_fcontext( detail::exchange( fctx_, nullptr), & p);
|
||||
if ( nullptr != t.data) {
|
||||
auto p = static_cast< std::tuple< std::exception_ptr, args_tpl_t > * >( t.data);
|
||||
std::exception_ptr eptr = std::get< 0 >( * p);
|
||||
if ( eptr) {
|
||||
try {
|
||||
std::rethrow_exception( eptr);
|
||||
} catch (...) {
|
||||
std::throw_with_nested( ontop_error{ t.fctx } );
|
||||
}
|
||||
}
|
||||
data = std::move( std::get< 1 >( * p) );
|
||||
}
|
||||
return std::tuple_cat( std::forward_as_tuple( execution_context( t.fctx) ), std::move( data) );
|
||||
}
|
||||
|
||||
template< typename ... Args >
|
||||
template< typename Fn >
|
||||
typename execution_context< Args ... >::ret_tpl_t
|
||||
execution_context< Args ... >::operator()( exec_ontop_arg_t, Fn && fn, Args ... args) {
|
||||
BOOST_ASSERT( nullptr != fctx_);
|
||||
args_tpl_t data{ std::forward< Args >( args) ... };
|
||||
auto p = std::make_tuple( fn, std::make_tuple( std::exception_ptr{}, std::move( data) ) );
|
||||
detail::transfer_t t = detail::ontop_fcontext(
|
||||
detail::exchange( fctx_, nullptr),
|
||||
& p,
|
||||
detail::ecv2_context_ontop< execution_context, Fn, Args ... >);
|
||||
if ( nullptr != t.data) {
|
||||
auto p = static_cast< std::tuple< std::exception_ptr, args_tpl_t > * >( t.data);
|
||||
std::exception_ptr eptr = std::get< 0 >( * p);
|
||||
if ( eptr) {
|
||||
try {
|
||||
std::rethrow_exception( eptr);
|
||||
} catch (...) {
|
||||
std::throw_with_nested( ontop_error{ t.fctx } );
|
||||
}
|
||||
}
|
||||
data = std::move( std::get< 1 >( * p) );
|
||||
}
|
||||
return std::tuple_cat( std::forward_as_tuple( execution_context( t.fctx) ), std::move( data) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template< int N >
|
||||
struct helper {
|
||||
template< typename T >
|
||||
static T convert( T && t) noexcept {
|
||||
return std::forward< T >( t);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct helper< 1 > {
|
||||
template< typename T >
|
||||
static std::tuple< T > convert( T && t) noexcept {
|
||||
return std::make_tuple( std::forward< T >( t) );
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
transfer_t ecv2_context_unwind( transfer_t t) {
|
||||
throw forced_unwind( t.fctx);
|
||||
return { nullptr, nullptr };
|
||||
}
|
||||
|
||||
template< typename Rec >
|
||||
transfer_t ecv2_context_exit( transfer_t t) noexcept {
|
||||
Rec * rec = static_cast< Rec * >( t.data);
|
||||
// destroy context stack
|
||||
rec->deallocate();
|
||||
return { nullptr, nullptr };
|
||||
}
|
||||
|
||||
template< typename Rec >
|
||||
void ecv2_context_etry( transfer_t t_) noexcept {
|
||||
// transfer control structure to the context-stack
|
||||
Rec * rec = static_cast< Rec * >( t_.data);
|
||||
BOOST_ASSERT( nullptr != rec);
|
||||
transfer_t t = { nullptr, nullptr };
|
||||
try {
|
||||
// jump back to `ecv2_context_create()`
|
||||
t = jump_fcontext( t_.fctx, nullptr);
|
||||
// start executing
|
||||
t = rec->run( t);
|
||||
} catch ( forced_unwind const& ex) {
|
||||
t = { ex.fctx, nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
BOOST_ASSERT( nullptr != t.fctx);
|
||||
// destroy context-stack of `this`context on next context
|
||||
ontop_fcontext( t.fctx, rec, ecv2_context_exit< Rec >);
|
||||
BOOST_ASSERT_MSG( false, "context already terminated");
|
||||
}
|
||||
|
||||
template< typename Ctx, typename Fn, typename ... Args >
|
||||
transfer_t ecv2_context_ontop( transfer_t t) {
|
||||
auto p = static_cast< std::tuple< Fn, std::tuple< std::exception_ptr, std::tuple< Args ... > > > * >( t.data);
|
||||
BOOST_ASSERT( nullptr != p);
|
||||
typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 0 >( * p) );
|
||||
auto args = std::move( std::get< 1 >( std::get< 1 >( * p) ) );
|
||||
try {
|
||||
// execute function
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
std::get< 1 >( std::get< 1 >( * p) ) = helper< sizeof ... (Args) >::convert( boost::context::detail::apply( fn, std::move( args) ) );
|
||||
#else
|
||||
std::get< 1 >( std::get< 1 >( * p) ) = helper< sizeof ... (Args) >::convert( std::apply( fn, std::move( args) ) );
|
||||
#endif
|
||||
#if defined( BOOST_CONTEXT_HAS_CXXABI_H )
|
||||
} catch ( abi::__forced_unwind const&) {
|
||||
throw;
|
||||
#endif
|
||||
} catch (...) {
|
||||
std::get< 0 >( std::get< 1 >( * p) ) = std::current_exception();
|
||||
}
|
||||
// apply returned data
|
||||
return { t.fctx, & std::get< 1 >( * p) };
|
||||
}
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create( StackAlloc && salloc, Fn && fn, Params && ... params) {
|
||||
typedef ecv2_record< Ctx, StackAlloc, Fn, Params ... > ecv2_record_t;
|
||||
|
||||
auto sctx = salloc.allocate();
|
||||
// reserve space for control structure
|
||||
#if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||
const std::size_t size = sctx.size - sizeof( ecv2_record_t);
|
||||
void * sp = static_cast< char * >( sctx.sp) - sizeof( ecv2_record_t);
|
||||
#else
|
||||
constexpr std::size_t func_alignment = 64; // alignof( ecv2_record_t);
|
||||
constexpr std::size_t func_size = sizeof( ecv2_record_t);
|
||||
// reserve space on stack
|
||||
void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
|
||||
// align sp pointer
|
||||
std::size_t space = func_size + func_alignment;
|
||||
sp = std::align( func_alignment, func_size, sp, space);
|
||||
BOOST_ASSERT( nullptr != sp);
|
||||
// calculate remaining size
|
||||
const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
|
||||
#endif
|
||||
// create fast-context
|
||||
const fcontext_t fctx = make_fcontext( sp, size, & ecv2_context_etry< ecv2_record_t >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// placment new for control structure on context-stack
|
||||
auto rec = ::new ( sp) ecv2_record_t{
|
||||
sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn), std::forward< Params >( params) ... };
|
||||
// transfer control structure to context-stack
|
||||
return jump_fcontext( fctx, rec).fctx;
|
||||
}
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create( preallocated palloc, StackAlloc && salloc, Fn && fn, Params && ... params) {
|
||||
typedef ecv2_record< Ctx, StackAlloc, Fn, Params ... > ecv2_record_t;
|
||||
|
||||
// reserve space for control structure
|
||||
#if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||
const std::size_t size = palloc.size - sizeof( ecv2_record_t);
|
||||
void * sp = static_cast< char * >( palloc.sp) - sizeof( ecv2_record_t);
|
||||
#else
|
||||
constexpr std::size_t func_alignment = 64; // alignof( ecv2_record_t);
|
||||
constexpr std::size_t func_size = sizeof( ecv2_record_t);
|
||||
// reserve space on stack
|
||||
void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
|
||||
// align sp pointer
|
||||
std::size_t space = func_size + func_alignment;
|
||||
sp = std::align( func_alignment, func_size, sp, space);
|
||||
BOOST_ASSERT( nullptr != sp);
|
||||
// calculate remaining size
|
||||
const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
|
||||
#endif
|
||||
// create fast-context
|
||||
const fcontext_t fctx = make_fcontext( sp, size, & ecv2_context_etry< ecv2_record_t >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// placment new for control structure on context-stack
|
||||
auto rec = ::new ( sp) ecv2_record_t{
|
||||
palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn), std::forward< Params >( params) ... };
|
||||
// transfer control structure to context-stack
|
||||
return jump_fcontext( fctx, rec).fctx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include <boost/context/execution_context_v2_void.ipp>
|
||||
|
||||
inline namespace v2 {
|
||||
|
||||
template< typename ... Args >
|
||||
void swap( execution_context< Args ... > & l, execution_context< Args ... > & r) noexcept {
|
||||
l.swap( r);
|
||||
}
|
||||
|
||||
}}}
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_SUFFIX
|
||||
#endif
|
||||
|
||||
#endif // BOOST_CONTEXT_EXECUTION_CONTEXT_V2_H
|
||||
@@ -1,311 +0,0 @@
|
||||
|
||||
// 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)
|
||||
|
||||
namespace detail {
|
||||
|
||||
template< typename Ctx, typename Fn >
|
||||
transfer_t ecv2_context_ontop_void( transfer_t);
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create_void( StackAlloc &&, Fn &&, Params && ...);
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create_void( preallocated, StackAlloc &&, Fn &&, Params && ...);
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
class ecv2_record_void {
|
||||
private:
|
||||
typename std::decay< StackAlloc >::type salloc_;
|
||||
stack_context sctx_;
|
||||
typename std::decay< Fn >::type fn_;
|
||||
std::tuple< typename std::decay< Params >::type ... > params_;
|
||||
|
||||
static void destroy( ecv2_record_void * p) noexcept {
|
||||
typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
|
||||
stack_context sctx = p->sctx_;
|
||||
// deallocate record
|
||||
p->~ecv2_record_void();
|
||||
// destroy stack with stack allocator
|
||||
salloc.deallocate( sctx);
|
||||
}
|
||||
|
||||
public:
|
||||
ecv2_record_void( stack_context sctx, StackAlloc && salloc,
|
||||
Fn && fn, Params && ... params) noexcept :
|
||||
salloc_( std::forward< StackAlloc >( salloc) ),
|
||||
sctx_( sctx),
|
||||
fn_( std::forward< Fn >( fn) ),
|
||||
params_( std::forward< Params >( params) ... ) {
|
||||
}
|
||||
|
||||
ecv2_record_void( ecv2_record_void const&) = delete;
|
||||
ecv2_record_void & operator=( ecv2_record_void const&) = delete;
|
||||
|
||||
void deallocate() noexcept {
|
||||
destroy( this);
|
||||
}
|
||||
|
||||
transfer_t run( transfer_t t) {
|
||||
Ctx from{ t.fctx };
|
||||
// invoke context-function
|
||||
#if defined(BOOST_NO_CXX17_STD_APPLY)
|
||||
Ctx cc = boost::context::detail::apply( fn_, std::tuple_cat( params_, std::forward_as_tuple( std::move( from) ) ) );
|
||||
#else
|
||||
Ctx cc = std::apply( fn_, std::tuple_cat( params_, std::forward_as_tuple( std::move( from) ) ) );
|
||||
#endif
|
||||
return { exchange( cc.fctx_, nullptr), nullptr };
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
inline namespace v2 {
|
||||
|
||||
template<>
|
||||
class execution_context< void > {
|
||||
private:
|
||||
friend class ontop_error;
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
friend class detail::ecv2_record_void;
|
||||
|
||||
template< typename Ctx, typename Fn >
|
||||
friend detail::transfer_t detail::ecv2_context_ontop_void( detail::transfer_t);
|
||||
|
||||
detail::fcontext_t fctx_{ nullptr };
|
||||
|
||||
execution_context( detail::fcontext_t fctx) noexcept :
|
||||
fctx_( fctx) {
|
||||
}
|
||||
|
||||
public:
|
||||
execution_context() noexcept = default;
|
||||
|
||||
#if defined(BOOST_USE_SEGMENTED_STACKS)
|
||||
// segmented-stack requires to preserve the segments of the `current` context
|
||||
// which is not possible (no global pointer to current context)
|
||||
template< typename Fn, typename ... Params >
|
||||
execution_context( std::allocator_arg_t, segmented_stack, Fn &&, Params && ...) = delete;
|
||||
|
||||
template< typename Fn, typename ... Params >
|
||||
execution_context( std::allocator_arg_t, preallocated, segmented_stack, Fn &&, Params && ...) = delete;
|
||||
#else
|
||||
template< typename Fn,
|
||||
typename ... Params,
|
||||
typename = detail::disable_overload< execution_context, Fn >
|
||||
>
|
||||
execution_context( Fn && fn, Params && ... params) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
fctx_( detail::ecv2_context_create_void< execution_context >(
|
||||
fixedsize_stack(),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Params >( params) ... ) ) {
|
||||
}
|
||||
|
||||
template< typename StackAlloc,
|
||||
typename Fn,
|
||||
typename ... Params
|
||||
>
|
||||
execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Params && ... params) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
fctx_( detail::ecv2_context_create_void< execution_context >(
|
||||
std::forward< StackAlloc >( salloc),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Params >( params) ... ) ) {
|
||||
}
|
||||
|
||||
template< typename StackAlloc,
|
||||
typename Fn,
|
||||
typename ... Params
|
||||
>
|
||||
execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Params && ... params) :
|
||||
// deferred execution of fn and its arguments
|
||||
// arguments are stored in std::tuple<>
|
||||
// non-type template parameter pack via std::index_sequence_for<>
|
||||
// preserves the number of arguments
|
||||
// used to extract the function arguments from std::tuple<>
|
||||
fctx_( detail::ecv2_context_create_void< execution_context >(
|
||||
palloc, std::forward< StackAlloc >( salloc),
|
||||
std::forward< Fn >( fn),
|
||||
std::forward< Params >( params) ... ) ) {
|
||||
}
|
||||
#endif
|
||||
|
||||
~execution_context() {
|
||||
if ( nullptr != fctx_) {
|
||||
detail::ontop_fcontext( detail::exchange( fctx_, nullptr), nullptr, detail::ecv2_context_unwind);
|
||||
}
|
||||
}
|
||||
|
||||
execution_context( execution_context && other) noexcept :
|
||||
fctx_( other.fctx_) {
|
||||
other.fctx_ = nullptr;
|
||||
}
|
||||
|
||||
execution_context & operator=( execution_context && other) noexcept {
|
||||
if ( this != & other) {
|
||||
execution_context tmp = std::move( other);
|
||||
swap( tmp);
|
||||
}
|
||||
return * this;
|
||||
}
|
||||
|
||||
execution_context( execution_context const& other) noexcept = delete;
|
||||
execution_context & operator=( execution_context const& other) noexcept = delete;
|
||||
|
||||
execution_context operator()() {
|
||||
BOOST_ASSERT( nullptr != fctx_);
|
||||
detail::transfer_t t = detail::jump_fcontext( detail::exchange( fctx_, nullptr), nullptr);
|
||||
if ( nullptr != t.data) {
|
||||
std::exception_ptr * eptr = static_cast< std::exception_ptr * >( t.data);
|
||||
try {
|
||||
std::rethrow_exception( * eptr);
|
||||
} catch (...) {
|
||||
std::throw_with_nested( ontop_error{ t.fctx } );
|
||||
}
|
||||
}
|
||||
return execution_context( t.fctx);
|
||||
}
|
||||
|
||||
template< typename Fn >
|
||||
execution_context operator()( exec_ontop_arg_t, Fn && fn) {
|
||||
BOOST_ASSERT( nullptr != fctx_);
|
||||
auto p = std::make_tuple( fn, std::exception_ptr{} );
|
||||
detail::transfer_t t = detail::ontop_fcontext(
|
||||
detail::exchange( fctx_, nullptr),
|
||||
& p,
|
||||
detail::ecv2_context_ontop_void< execution_context, Fn >);
|
||||
if ( nullptr != t.data) {
|
||||
std::exception_ptr * eptr = static_cast< std::exception_ptr * >( t.data);
|
||||
try {
|
||||
std::rethrow_exception( * eptr);
|
||||
} catch (...) {
|
||||
std::throw_with_nested( ontop_error{ t.fctx } );
|
||||
}
|
||||
}
|
||||
return execution_context( t.fctx);
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
return nullptr != fctx_;
|
||||
}
|
||||
|
||||
bool operator!() const noexcept {
|
||||
return nullptr == fctx_;
|
||||
}
|
||||
|
||||
bool operator<( execution_context const& other) const noexcept {
|
||||
return fctx_ < other.fctx_;
|
||||
}
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
|
||||
if ( nullptr != other.fctx_) {
|
||||
return os << other.fctx_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
void swap( execution_context & other) noexcept {
|
||||
std::swap( fctx_, other.fctx_);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template< typename Ctx, typename Fn >
|
||||
transfer_t ecv2_context_ontop_void( transfer_t t) {
|
||||
auto p = static_cast< std::tuple< Fn, std::exception_ptr > * >( t.data);
|
||||
BOOST_ASSERT( nullptr != p);
|
||||
typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 0 >( * p) );
|
||||
try {
|
||||
// execute function
|
||||
fn();
|
||||
#if defined( BOOST_CONTEXT_HAS_CXXABI_H )
|
||||
} catch ( abi::__forced_unwind const&) {
|
||||
throw;
|
||||
#endif
|
||||
} catch (...) {
|
||||
std::get< 1 >( * p) = std::current_exception();
|
||||
return { t.fctx, & std::get< 1 >( * p ) };
|
||||
}
|
||||
return { exchange( t.fctx, nullptr), nullptr };
|
||||
}
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create_void( StackAlloc && salloc, Fn && fn, Params && ... params) {
|
||||
typedef ecv2_record_void< Ctx, StackAlloc, Fn, Params ... > record_t;
|
||||
|
||||
auto sctx = salloc.allocate();
|
||||
// reserve space for control structure
|
||||
#if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||
const std::size_t size = sctx.size - sizeof( record_t);
|
||||
void * sp = static_cast< char * >( sctx.sp) - sizeof( record_t);
|
||||
#else
|
||||
constexpr std::size_t func_alignment = 64; // alignof( record_t);
|
||||
constexpr std::size_t func_size = sizeof( record_t);
|
||||
// reserve space on stack
|
||||
void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
|
||||
// align sp pointer
|
||||
std::size_t space = func_size + func_alignment;
|
||||
sp = std::align( func_alignment, func_size, sp, space);
|
||||
BOOST_ASSERT( nullptr != sp);
|
||||
// calculate remaining size
|
||||
const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
|
||||
#endif
|
||||
// create fast-context
|
||||
const fcontext_t fctx = make_fcontext( sp, size, & ecv2_context_etry< record_t >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// placment new for control structure on context-stack
|
||||
auto rec = ::new ( sp) record_t{
|
||||
sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn), std::forward< Params >( params) ... };
|
||||
// transfer control structure to context-stack
|
||||
return jump_fcontext( fctx, rec).fctx;
|
||||
}
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
|
||||
fcontext_t ecv2_context_create_void( preallocated palloc, StackAlloc && salloc, Fn && fn, Params && ... params) {
|
||||
typedef ecv2_record_void< Ctx, StackAlloc, Fn, Params ... > record_t;
|
||||
|
||||
// reserve space for control structure
|
||||
#if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||
const std::size_t size = palloc.size - sizeof( record_t);
|
||||
void * sp = static_cast< char * >( palloc.sp) - sizeof( record_t);
|
||||
#else
|
||||
constexpr std::size_t func_alignment = 64; // alignof( record_t);
|
||||
constexpr std::size_t func_size = sizeof( record_t);
|
||||
// reserve space on stack
|
||||
void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
|
||||
// align sp pointer
|
||||
std::size_t space = func_size + func_alignment;
|
||||
sp = std::align( func_alignment, func_size, sp, space);
|
||||
BOOST_ASSERT( nullptr != sp);
|
||||
// calculate remaining size
|
||||
const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
|
||||
#endif
|
||||
// create fast-context
|
||||
const fcontext_t fctx = make_fcontext( sp, size, & ecv2_context_etry< record_t >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// placment new for control structure on context-stack
|
||||
auto rec = ::new ( sp) record_t{
|
||||
palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn), std::forward< Params >( params) ... };
|
||||
// transfer control structure to context-stack
|
||||
return jump_fcontext( fctx, rec).fctx;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,6 +44,17 @@
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
#if defined(__CET__) && defined(__unix__)
|
||||
# include <cet.h>
|
||||
# include <sys/mman.h>
|
||||
# define SHSTK_ENABLED (__CET__ & 0x2)
|
||||
# define BOOST_CONTEXT_SHADOW_STACK (SHSTK_ENABLED && SHADOW_STACK_SYSCALL)
|
||||
# define __NR_map_shadow_stack 451
|
||||
#ifndef SHADOW_STACK_SET_TOKEN
|
||||
# define SHADOW_STACK_SET_TOKEN 0x1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4702)
|
||||
@@ -62,6 +73,12 @@ transfer_t fiber_unwind( transfer_t t) {
|
||||
template< typename Rec >
|
||||
transfer_t fiber_exit( transfer_t t) noexcept {
|
||||
Rec * rec = static_cast< Rec * >( t.data);
|
||||
#if BOOST_CONTEXT_SHADOW_STACK
|
||||
// destory shadow stack
|
||||
std::size_t ss_size = *((unsigned long*)(reinterpret_cast< uintptr_t >( rec)- 16));
|
||||
long unsigned int ss_base = *((unsigned long*)(reinterpret_cast< uintptr_t >( rec)- 8));
|
||||
munmap((void *)ss_base, ss_size);
|
||||
#endif
|
||||
// destroy context stack
|
||||
rec->deallocate();
|
||||
return { nullptr, nullptr };
|
||||
@@ -80,9 +97,6 @@ void fiber_entry( transfer_t t) noexcept {
|
||||
t.fctx = rec->run( t.fctx);
|
||||
} catch ( forced_unwind const& ex) {
|
||||
t = { ex.fctx, nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
BOOST_ASSERT( nullptr != t.fctx);
|
||||
// destroy context-stack of `this`context on next context
|
||||
@@ -168,6 +182,25 @@ fcontext_t create_fiber1( StackAlloc && salloc, Fn && fn) {
|
||||
reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
|
||||
// create fast-context
|
||||
const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
|
||||
|
||||
#if BOOST_CONTEXT_SHADOW_STACK
|
||||
std::size_t ss_size = size >> 5;
|
||||
// align shadow stack to 8 bytes.
|
||||
ss_size = (ss_size + 7) & ~7;
|
||||
// Todo: shadow stack occupies at least 4KB
|
||||
ss_size = (ss_size > 4096) ? size : 4096;
|
||||
// create shadow stack
|
||||
void *ss_base = (void *)syscall(__NR_map_shadow_stack, 0, ss_size, SHADOW_STACK_SET_TOKEN);
|
||||
BOOST_ASSERT(ss_base != -1);
|
||||
unsigned long ss_sp = (unsigned long)ss_base + ss_size;
|
||||
/* pass the shadow stack pointer to make_fcontext
|
||||
i.e., link the new shadow stack with the new fcontext
|
||||
TODO should be a better way? */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( stack_top)- 8)) = ss_sp;
|
||||
/* Todo: place shadow stack info in 64byte gap */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 8)) = (unsigned long) ss_base;
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 16)) = ss_size;
|
||||
#endif
|
||||
const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// transfer control structure to context-stack
|
||||
@@ -190,6 +223,25 @@ fcontext_t create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
|
||||
reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
|
||||
// create fast-context
|
||||
const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
|
||||
|
||||
#if BOOST_CONTEXT_SHADOW_STACK
|
||||
std::size_t ss_size = size >> 5;
|
||||
// align shadow stack to 8 bytes.
|
||||
ss_size = (ss_size + 7) & ~7;
|
||||
// Todo: shadow stack occupies at least 4KB
|
||||
ss_size = (ss_size > 4096) ? size : 4096;
|
||||
// create shadow stack
|
||||
void *ss_base = (void *)syscall(__NR_map_shadow_stack, 0, ss_size, SHADOW_STACK_SET_TOKEN);
|
||||
BOOST_ASSERT(ss_base != -1);
|
||||
unsigned long ss_sp = (unsigned long)ss_base + ss_size;
|
||||
/* pass the shadow stack pointer to make_fcontext
|
||||
i.e., link the new shadow stack with the new fcontext
|
||||
TODO should be a better way? */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( stack_top)- 8)) = ss_sp;
|
||||
/* Todo: place shadow stack info in 64byte gap */
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 8)) = (unsigned long) ss_base;
|
||||
*((unsigned long*)(reinterpret_cast< uintptr_t >( storage)- 16)) = ss_size;
|
||||
#endif
|
||||
const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
|
||||
BOOST_ASSERT( nullptr != fctx);
|
||||
// transfer control structure to context-stack
|
||||
@@ -207,14 +259,6 @@ private:
|
||||
friend detail::transfer_t
|
||||
detail::fiber_ontop( detail::transfer_t);
|
||||
|
||||
template< typename StackAlloc, typename Fn >
|
||||
friend fiber
|
||||
callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
|
||||
|
||||
template< typename StackAlloc, typename Fn >
|
||||
friend fiber
|
||||
callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
|
||||
|
||||
detail::fcontext_t fctx_{ nullptr };
|
||||
|
||||
fiber( detail::fcontext_t fctx) noexcept :
|
||||
@@ -314,6 +358,8 @@ public:
|
||||
return fctx_ < other.fctx_;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
|
||||
@@ -324,11 +370,33 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
|
||||
|
||||
#endif
|
||||
|
||||
void swap( fiber & other) noexcept {
|
||||
std::swap( fctx_, other.fctx_);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
inline std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
|
||||
if ( nullptr != other.fctx_) {
|
||||
return os << other.fctx_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline
|
||||
void swap( fiber & l, fiber & r) noexcept {
|
||||
l.swap( r);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#ifndef BOOST_CONTEXT_FIBER_H
|
||||
#define BOOST_CONTEXT_FIBER_H
|
||||
|
||||
#include <boost/predef.h>
|
||||
#include <boost/predef/os.h>
|
||||
#if BOOST_OS_MACOS
|
||||
#define _XOPEN_SOURCE 600
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@ extern "C" {
|
||||
#include <ucontext.h>
|
||||
}
|
||||
|
||||
#include <boost/predef.h>
|
||||
#include <boost/context/detail/config.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -32,6 +33,7 @@ extern "C" {
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/predef.h>
|
||||
|
||||
#include <boost/context/detail/disable_overload.hpp>
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
@@ -53,6 +55,10 @@ extern "C" {
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_USE_TSAN
|
||||
#include <sanitizer/tsan_interface.h>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace context {
|
||||
namespace detail {
|
||||
@@ -60,12 +66,19 @@ namespace detail {
|
||||
// tampoline function
|
||||
// entered if the execution context
|
||||
// is resumed for the first time
|
||||
template< typename Record >
|
||||
static void fiber_entry_func( void * data) noexcept {
|
||||
Record * record = static_cast< Record * >( data);
|
||||
BOOST_ASSERT( nullptr != record);
|
||||
// start execution of toplevel context-function
|
||||
record->run();
|
||||
template <typename Record>
|
||||
#ifdef BOOST_OS_MACOS
|
||||
static void fiber_entry_func(std::uint32_t data_high,
|
||||
std::uint32_t data_low) noexcept {
|
||||
auto data =
|
||||
reinterpret_cast<void *>(std::uint64_t(data_high) << 32 | data_low);
|
||||
#else
|
||||
static void fiber_entry_func(void *data) noexcept {
|
||||
#endif
|
||||
Record *record = static_cast<Record *>(data);
|
||||
BOOST_ASSERT(nullptr != record);
|
||||
// start execution of toplevel context-function
|
||||
record->run();
|
||||
}
|
||||
|
||||
struct BOOST_CONTEXT_DECL fiber_activation_record {
|
||||
@@ -82,6 +95,11 @@ struct BOOST_CONTEXT_DECL fiber_activation_record {
|
||||
std::size_t stack_size{ 0 };
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_USE_TSAN)
|
||||
void * tsan_fiber{ nullptr };
|
||||
bool destroy_tsan_fiber{ true };
|
||||
#endif
|
||||
|
||||
static fiber_activation_record *& current() noexcept;
|
||||
|
||||
// used for toplevel-context
|
||||
@@ -92,6 +110,11 @@ struct BOOST_CONTEXT_DECL fiber_activation_record {
|
||||
std::error_code( errno, std::system_category() ),
|
||||
"getcontext() failed");
|
||||
}
|
||||
|
||||
#if defined(BOOST_USE_TSAN)
|
||||
tsan_fiber = __tsan_get_current_fiber();
|
||||
destroy_tsan_fiber = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
fiber_activation_record( stack_context sctx_) noexcept :
|
||||
@@ -100,6 +123,10 @@ struct BOOST_CONTEXT_DECL fiber_activation_record {
|
||||
}
|
||||
|
||||
virtual ~fiber_activation_record() {
|
||||
#if defined(BOOST_USE_TSAN)
|
||||
if (destroy_tsan_fiber)
|
||||
__tsan_destroy_fiber(tsan_fiber);
|
||||
#endif
|
||||
}
|
||||
|
||||
fiber_activation_record( fiber_activation_record const&) = delete;
|
||||
@@ -125,6 +152,9 @@ struct BOOST_CONTEXT_DECL fiber_activation_record {
|
||||
} else {
|
||||
__sanitizer_start_switch_fiber( & from->fake_stack, stack_bottom, stack_size);
|
||||
}
|
||||
#endif
|
||||
#if defined (BOOST_USE_TSAN)
|
||||
__tsan_switch_to_fiber(tsan_fiber, 0);
|
||||
#endif
|
||||
// context switch from parent context to `this`-context
|
||||
::swapcontext( & from->uctx, & uctx);
|
||||
@@ -184,6 +214,9 @@ struct BOOST_CONTEXT_DECL fiber_activation_record {
|
||||
#endif
|
||||
#if defined(BOOST_USE_ASAN)
|
||||
__sanitizer_start_switch_fiber( & from->fake_stack, stack_bottom, stack_size);
|
||||
#endif
|
||||
#if defined (BOOST_USE_TSAN)
|
||||
__tsan_switch_to_fiber(tsan_fiber, 0);
|
||||
#endif
|
||||
// context switch from parent context to `this`-context
|
||||
::swapcontext( & from->uctx, & uctx);
|
||||
@@ -210,19 +243,10 @@ struct BOOST_CONTEXT_DECL fiber_activation_record_initializer {
|
||||
|
||||
struct forced_unwind {
|
||||
fiber_activation_record * from{ nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
bool caught{ false };
|
||||
#endif
|
||||
|
||||
forced_unwind( fiber_activation_record * from_) noexcept :
|
||||
from{ from_ } {
|
||||
}
|
||||
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
~forced_unwind() {
|
||||
BOOST_ASSERT( caught);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn >
|
||||
@@ -268,9 +292,6 @@ public:
|
||||
#endif
|
||||
} catch ( forced_unwind const& ex) {
|
||||
c = Ctx{ ex.from };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
// this context has finished its task
|
||||
from = nullptr;
|
||||
@@ -305,15 +326,31 @@ static fiber_activation_record * create_fiber1( StackAlloc && salloc, Fn && fn)
|
||||
std::error_code( errno, std::system_category() ),
|
||||
"getcontext() failed");
|
||||
}
|
||||
#if BOOST_OS_BSD_FREE
|
||||
// because FreeBSD defines stack_t::ss_sp as char *
|
||||
record->uctx.uc_stack.ss_sp = static_cast< char * >( stack_bottom);
|
||||
#else
|
||||
record->uctx.uc_stack.ss_sp = stack_bottom;
|
||||
#endif
|
||||
// 64byte gap between control structure and stack top
|
||||
record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) -
|
||||
reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64);
|
||||
record->uctx.uc_link = nullptr;
|
||||
::makecontext( & record->uctx, ( void (*)() ) & fiber_entry_func< capture_t >, 1, record);
|
||||
#ifdef BOOST_OS_MACOS
|
||||
const auto integer = std::uint64_t(record);
|
||||
::makecontext(&record->uctx, (void (*)()) & fiber_entry_func<capture_t>, 2,
|
||||
std::uint32_t((integer >> 32) & 0xFFFFFFFF),
|
||||
std::uint32_t(integer));
|
||||
#else
|
||||
::makecontext(&record->uctx, (void (*)()) & fiber_entry_func<capture_t>, 1,
|
||||
record);
|
||||
#endif
|
||||
#if defined(BOOST_USE_ASAN)
|
||||
record->stack_bottom = record->uctx.uc_stack.ss_sp;
|
||||
record->stack_size = record->uctx.uc_stack.ss_size;
|
||||
#endif
|
||||
#if defined (BOOST_USE_TSAN)
|
||||
record->tsan_fiber = __tsan_create_fiber(0);
|
||||
#endif
|
||||
return record;
|
||||
}
|
||||
@@ -340,15 +377,31 @@ static fiber_activation_record * create_fiber2( preallocated palloc, StackAlloc
|
||||
std::error_code( errno, std::system_category() ),
|
||||
"getcontext() failed");
|
||||
}
|
||||
#if BOOST_OS_BSD_FREE
|
||||
// because FreeBSD defines stack_t::ss_sp as char *
|
||||
record->uctx.uc_stack.ss_sp = static_cast< char * >( stack_bottom);
|
||||
#else
|
||||
record->uctx.uc_stack.ss_sp = stack_bottom;
|
||||
#endif
|
||||
// 64byte gap between control structure and stack top
|
||||
record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) -
|
||||
reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64);
|
||||
record->uctx.uc_link = nullptr;
|
||||
::makecontext( & record->uctx, ( void (*)() ) & fiber_entry_func< capture_t >, 1, record);
|
||||
#ifdef BOOST_OS_MACOS
|
||||
const auto integer = std::uint64_t(record);
|
||||
::makecontext(&record->uctx, (void (*)()) & fiber_entry_func<capture_t>, 2,
|
||||
std::uint32_t((integer >> 32) & 0xFFFFFFFF),
|
||||
std::uint32_t(integer));
|
||||
#else
|
||||
::makecontext(&record->uctx, (void (*)()) & fiber_entry_func<capture_t>, 1,
|
||||
record);
|
||||
#endif
|
||||
#if defined(BOOST_USE_ASAN)
|
||||
record->stack_bottom = record->uctx.uc_stack.ss_sp;
|
||||
record->stack_size = record->uctx.uc_stack.ss_size;
|
||||
#endif
|
||||
#if defined (BOOST_USE_TSAN)
|
||||
record->tsan_fiber = __tsan_create_fiber(0);
|
||||
#endif
|
||||
return record;
|
||||
}
|
||||
@@ -368,14 +421,6 @@ private:
|
||||
template< typename Ctx, typename StackAlloc, typename Fn >
|
||||
friend detail::fiber_activation_record * detail::create_fiber2( preallocated, StackAlloc &&, Fn &&);
|
||||
|
||||
template< typename StackAlloc, typename Fn >
|
||||
friend fiber
|
||||
callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
|
||||
|
||||
template< typename StackAlloc, typename Fn >
|
||||
friend fiber
|
||||
callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
|
||||
|
||||
detail::fiber_activation_record * ptr_{ nullptr };
|
||||
|
||||
fiber( detail::fiber_activation_record * ptr) noexcept :
|
||||
@@ -482,6 +527,8 @@ public:
|
||||
return ptr_ < other.ptr_;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
|
||||
@@ -492,11 +539,33 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
|
||||
|
||||
#endif
|
||||
|
||||
void swap( fiber & other) noexcept {
|
||||
std::swap( ptr_, other.ptr_);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
inline std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
|
||||
if ( nullptr != other.ptr_) {
|
||||
return os << other.ptr_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline
|
||||
void swap( fiber & l, fiber & r) noexcept {
|
||||
l.swap( r);
|
||||
|
||||
@@ -185,19 +185,10 @@ struct BOOST_CONTEXT_DECL fiber_activation_record_initializer {
|
||||
|
||||
struct forced_unwind {
|
||||
fiber_activation_record * from{ nullptr };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
bool caught{ false };
|
||||
#endif
|
||||
|
||||
explicit forced_unwind( fiber_activation_record * from_) :
|
||||
from{ from_ } {
|
||||
}
|
||||
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
~forced_unwind() {
|
||||
BOOST_ASSERT( caught);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template< typename Ctx, typename StackAlloc, typename Fn >
|
||||
@@ -238,9 +229,6 @@ public:
|
||||
#endif
|
||||
} catch ( forced_unwind const& ex) {
|
||||
c = Ctx{ ex.from };
|
||||
#ifndef BOOST_ASSERT_IS_VOID
|
||||
const_cast< forced_unwind & >( ex).caught = true;
|
||||
#endif
|
||||
}
|
||||
// this context has finished its task
|
||||
from = nullptr;
|
||||
@@ -302,14 +290,6 @@ private:
|
||||
template< typename Ctx, typename StackAlloc, typename Fn >
|
||||
friend detail::fiber_activation_record * detail::create_fiber2( preallocated, StackAlloc &&, Fn &&);
|
||||
|
||||
template< typename StackAlloc, typename Fn >
|
||||
friend fiber
|
||||
callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
|
||||
|
||||
template< typename StackAlloc, typename Fn >
|
||||
friend fiber
|
||||
callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
|
||||
|
||||
detail::fiber_activation_record * ptr_{ nullptr };
|
||||
|
||||
fiber( detail::fiber_activation_record * ptr) noexcept :
|
||||
@@ -410,7 +390,9 @@ public:
|
||||
bool operator<( fiber const& other) const noexcept {
|
||||
return ptr_ < other.ptr_;
|
||||
}
|
||||
|
||||
|
||||
#if !defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
|
||||
@@ -421,11 +403,33 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
friend std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
|
||||
|
||||
#endif
|
||||
|
||||
void swap( fiber & other) noexcept {
|
||||
std::swap( ptr_, other.ptr_);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_EMBTC)
|
||||
|
||||
template< typename charT, class traitsT >
|
||||
inline std::basic_ostream< charT, traitsT > &
|
||||
operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
|
||||
if ( nullptr != other.ptr_) {
|
||||
return os << other.ptr_;
|
||||
} else {
|
||||
return os << "{not-a-context}";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline
|
||||
void swap( fiber & l, fiber & r) noexcept {
|
||||
l.swap( r);
|
||||
|
||||
@@ -18,6 +18,12 @@
|
||||
#include <boost/context/stack_context.hpp>
|
||||
#include <boost/context/stack_traits.hpp>
|
||||
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
extern "C" {
|
||||
#include <sys/mman.h>
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_USE_VALGRIND)
|
||||
#include <valgrind/valgrind.h>
|
||||
#endif
|
||||
@@ -42,10 +48,17 @@ public:
|
||||
}
|
||||
|
||||
stack_context allocate() {
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
void * vp = ::mmap( 0, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
|
||||
if ( vp == MAP_FAILED) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
#else
|
||||
void * vp = std::malloc( size_);
|
||||
if ( ! vp) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
#endif
|
||||
stack_context sctx;
|
||||
sctx.size = size_;
|
||||
sctx.sp = static_cast< char * >( vp) + sctx.size;
|
||||
@@ -62,7 +75,11 @@ public:
|
||||
VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
|
||||
#endif
|
||||
void * vp = static_cast< char * >( sctx.sp) - sctx.size;
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
::munmap( vp, sctx.size);
|
||||
#else
|
||||
std::free( vp);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -21,6 +21,13 @@
|
||||
#include <boost/context/stack_context.hpp>
|
||||
#include <boost/context/stack_traits.hpp>
|
||||
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
extern "C" {
|
||||
#include <sys/mman.h>
|
||||
#include <stdlib.h>
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_USE_VALGRIND)
|
||||
#include <valgrind/valgrind.h>
|
||||
#endif
|
||||
@@ -32,6 +39,31 @@
|
||||
namespace boost {
|
||||
namespace context {
|
||||
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
namespace detail {
|
||||
template< typename traitsT >
|
||||
struct map_stack_allocator {
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
|
||||
static char * malloc( const size_type bytes) {
|
||||
void * block;
|
||||
if ( ::posix_memalign( &block, traitsT::page_size(), bytes) != 0) {
|
||||
return 0;
|
||||
}
|
||||
if ( mmap( block, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_STACK, -1, 0) == MAP_FAILED) {
|
||||
std::free( block);
|
||||
return 0;
|
||||
}
|
||||
return reinterpret_cast< char * >( block);
|
||||
}
|
||||
static void free( char * const block) {
|
||||
std::free( block);
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
template< typename traitsT >
|
||||
class basic_pooled_fixedsize_stack {
|
||||
private:
|
||||
@@ -39,7 +71,11 @@ private:
|
||||
private:
|
||||
std::atomic< std::size_t > use_count_;
|
||||
std::size_t stack_size_;
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
boost::pool< detail::map_stack_allocator< traitsT > > storage_;
|
||||
#else
|
||||
boost::pool< boost::default_user_allocator_malloc_free > storage_;
|
||||
#endif
|
||||
|
||||
public:
|
||||
storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :
|
||||
|
||||
@@ -20,6 +20,7 @@ extern "C" {
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/core/ignore_unused.hpp>
|
||||
|
||||
#include <boost/context/detail/config.hpp>
|
||||
#include <boost/context/stack_context.hpp>
|
||||
@@ -50,15 +51,13 @@ public:
|
||||
|
||||
stack_context allocate() {
|
||||
// calculate how many pages are required
|
||||
const std::size_t pages(
|
||||
static_cast< std::size_t >(
|
||||
std::ceil(
|
||||
static_cast< float >( size_) / traits_type::page_size() ) ) );
|
||||
const std::size_t pages = (size_ + traits_type::page_size() - 1) / traits_type::page_size();
|
||||
// add one page at bottom that will be used as guard-page
|
||||
const std::size_t size__ = ( pages + 1) * traits_type::page_size();
|
||||
|
||||
// conform to POSIX.4 (POSIX.1b-1993, _POSIX_C_SOURCE=199309L)
|
||||
#if defined(MAP_ANON)
|
||||
#if defined(BOOST_CONTEXT_USE_MAP_STACK)
|
||||
void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
|
||||
#elif defined(MAP_ANON)
|
||||
void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||
#else
|
||||
void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
@@ -66,12 +65,9 @@ public:
|
||||
if ( MAP_FAILED == vp) throw std::bad_alloc();
|
||||
|
||||
// conforming to POSIX.1-2001
|
||||
#if defined(BOOST_DISABLE_ASSERTS)
|
||||
::mprotect( vp, traits_type::page_size(), PROT_NONE);
|
||||
#else
|
||||
const int result( ::mprotect( vp, traits_type::page_size(), PROT_NONE) );
|
||||
boost::ignore_unused(result);
|
||||
BOOST_ASSERT( 0 == result);
|
||||
#endif
|
||||
|
||||
stack_context sctx;
|
||||
sctx.size = size__;
|
||||
|
||||
@@ -15,7 +15,9 @@ extern "C" {
|
||||
#include <cstddef>
|
||||
#include <new>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/core/ignore_unused.hpp>
|
||||
|
||||
#include <boost/context/detail/config.hpp>
|
||||
#include <boost/context/stack_context.hpp>
|
||||
@@ -42,10 +44,7 @@ public:
|
||||
|
||||
stack_context allocate() {
|
||||
// calculate how many pages are required
|
||||
const std::size_t pages(
|
||||
static_cast< std::size_t >(
|
||||
std::ceil(
|
||||
static_cast< float >( size_) / traits_type::page_size() ) ) );
|
||||
const std::size_t pages = (size_ + traits_type::page_size() - 1) / traits_type::page_size();
|
||||
// add one page at bottom that will be used as guard-page
|
||||
const std::size_t size__ = ( pages + 1) * traits_type::page_size();
|
||||
|
||||
@@ -53,14 +52,10 @@ public:
|
||||
if ( ! vp) throw std::bad_alloc();
|
||||
|
||||
DWORD old_options;
|
||||
#if defined(BOOST_DISABLE_ASSERTS)
|
||||
::VirtualProtect(
|
||||
vp, traits_type::page_size(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
|
||||
#else
|
||||
const BOOL result = ::VirtualProtect(
|
||||
vp, traits_type::page_size(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
|
||||
boost::ignore_unused(result);
|
||||
BOOST_ASSERT( FALSE != result);
|
||||
#endif
|
||||
|
||||
stack_context sctx;
|
||||
sctx.size = size__;
|
||||
|
||||
Reference in New Issue
Block a user