aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/context/src/fiber.cpp
blob: 9bb4ba6e032c28efe93dd53722e2e0fdf684d9ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//          Copyright Oliver Kowalke 2017.
// 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/fiber_ucontext.hpp"
#elif defined(BOOST_USE_WINFIB)
#include "boost/context/fiber_winfib.hpp"
#endif

#include <boost/config.hpp>

#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif

namespace boost {
namespace context {
namespace detail {

// zero-initialization
thread_local fiber_activation_record * current_rec;
thread_local static std::size_t counter;

// schwarz counter
fiber_activation_record_initializer::fiber_activation_record_initializer() noexcept {
    if ( 0 == counter++) {
        current_rec = new fiber_activation_record();
    }
}

fiber_activation_record_initializer::~fiber_activation_record_initializer() {
    if ( 0 == --counter) {
        BOOST_ASSERT( current_rec->is_main_context() );
        delete current_rec;
    }
}

}

namespace detail {

fiber_activation_record *&
fiber_activation_record::current() noexcept {
    // initialized the first time control passes; per thread
    thread_local static fiber_activation_record_initializer initializer;
    return current_rec;
}

}

}}

#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif