2
0
mirror of https://github.com/boostorg/context.git synced 2026-01-26 06:22:42 +00:00
Files
context/src/execution_context.cpp
2017-06-01 18:20:02 +02:00

85 lines
2.3 KiB
C++

// Copyright Oliver Kowalke 2009.
// 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)
#if defined(BOOST_USE_UCONTEXT)
#include "boost/context/continuation_ucontext.hpp"
#else
#include "boost/context/execution_context.hpp"
#endif
#include <boost/config.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
#if defined(BOOST_USE_UCONTEXT) || (defined(BOOST_EXECUTION_CONTEXT) && (BOOST_EXECUTION_CONTEXT == 1))
namespace boost {
namespace context {
namespace detail {
thread_local
#if defined(BOOST_USE_UCONTEXT)
activation_record *
#elif defined(BOOST_EXECUTION_CONTEXT) && (BOOST_EXECUTION_CONTEXT == 1)
activation_record::ptr_t
#endif
activation_record::current_rec;
// zero-initialization
thread_local static std::size_t counter;
// schwarz counter
activation_record_initializer::activation_record_initializer() noexcept {
if ( 0 == counter++) {
#if defined(BOOST_USE_UCONTEXT)
activation_record::current_rec = new activation_record();
#elif defined(BOOST_EXECUTION_CONTEXT) && (BOOST_EXECUTION_CONTEXT == 1)
activation_record::current_rec.reset( new activation_record() );
#endif
}
}
activation_record_initializer::~activation_record_initializer() {
if ( 0 == --counter) {
BOOST_ASSERT( activation_record::current_rec->is_main_context() );
#if defined(BOOST_USE_UCONTEXT)
delete activation_record::current_rec;
#elif defined(BOOST_EXECUTION_CONTEXT) && (BOOST_EXECUTION_CONTEXT == 1)
delete activation_record::current_rec.detach();
#endif
}
}
}
#if defined(BOOST_USE_UCONTEXT)
namespace detail {
activation_record *&
activation_record::current() noexcept {
// initialized the first time control passes; per thread
thread_local static activation_record_initializer initializer;
return activation_record::current_rec;
}
}
#elif defined(BOOST_EXECUTION_CONTEXT) && (BOOST_EXECUTION_CONTEXT == 1)
execution_context
execution_context::current() noexcept {
// initialized the first time control passes; per thread
thread_local static detail::activation_record_initializer initializer;
return execution_context();
}
#endif
}}
#endif
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif