2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <boost/context/all.hpp>
19 #include <boost/version.hpp>
22 * Wrappers for different versions of boost::context library
23 * API reference for different versions
24 * Boost 1.51: http://www.boost.org/doc/libs/1_51_0/libs/context/doc/html/context/context/boost_fcontext.html
25 * Boost 1.52: http://www.boost.org/doc/libs/1_52_0/libs/context/doc/html/context/context/boost_fcontext.html
26 * Boost 1.56: http://www.boost.org/doc/libs/1_56_0/libs/context/doc/html/context/context/boost_fcontext.html
29 namespace folly { namespace fibers {
34 #if BOOST_VERSION >= 105200
35 using ContextStruct = boost::context::fcontext_t;
37 using ContextStruct = boost::ctx::fcontext_t;
40 void* stackLimit() const {
44 void* stackBase() const {
52 #if BOOST_VERSION >= 105600
53 ContextStruct context_;
54 #elif BOOST_VERSION >= 105200
55 ContextStruct* context_;
57 ContextStruct context_;
60 friend intptr_t jumpContext(FContext* oldC, FContext::ContextStruct* newC,
62 friend intptr_t jumpContext(FContext::ContextStruct* oldC, FContext* newC,
64 friend FContext makeContext(void* stackLimit, size_t stackSize,
68 inline intptr_t jumpContext(FContext* oldC, FContext::ContextStruct* newC,
71 #if BOOST_VERSION >= 105600
72 return boost::context::jump_fcontext(&oldC->context_, *newC, p);
73 #elif BOOST_VERSION >= 105200
74 return boost::context::jump_fcontext(oldC->context_, newC, p);
76 return jump_fcontext(&oldC->context_, newC, p);
81 inline intptr_t jumpContext(FContext::ContextStruct* oldC, FContext* newC,
84 #if BOOST_VERSION >= 105200
85 return boost::context::jump_fcontext(oldC, newC->context_, p);
87 return jump_fcontext(oldC, &newC->context_, p);
92 inline FContext makeContext(void* stackLimit, size_t stackSize,
93 void(*fn)(intptr_t)) {
95 res.stackLimit_ = stackLimit;
96 res.stackBase_ = static_cast<unsigned char*>(stackLimit) + stackSize;
98 #if BOOST_VERSION >= 105200
99 res.context_ = boost::context::make_fcontext(res.stackBase_, stackSize, fn);
101 res.context_.fc_stack.limit = stackLimit;
102 res.context_.fc_stack.base = res.stackBase_;
103 make_fcontext(&res.context_, fn);