2 * Copyright 2015 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 <sys/syscall.h>
26 #include <folly/Likely.h>
27 #include <folly/Portability.h>
28 #include <folly/experimental/fibers/BoostContextCompatibility.h>
29 #include <folly/experimental/fibers/FiberManager.h>
31 namespace folly { namespace fibers {
34 static const uint64_t kMagic8Bytes = 0xfaceb00cfaceb00c;
36 pid_t localThreadId() {
37 static thread_local pid_t threadId = syscall(SYS_gettid);
41 /* Size of the region from p + nBytes down to the last non-magic value */
42 static size_t nonMagicInBytes(const FContext& context) {
43 uint64_t* begin = static_cast<uint64_t*>(context.stackLimit());
44 uint64_t* end = static_cast<uint64_t*>(context.stackBase());
46 auto firstNonMagic = std::find_if(
49 return val != kMagic8Bytes;
53 return (end - firstNonMagic) * sizeof(uint64_t);
56 } // anonymous namespace
58 void Fiber::setData(intptr_t data) {
59 assert(state_ == AWAITING);
61 state_ = READY_TO_RUN;
63 if (LIKELY(threadId_ == localThreadId())) {
64 fiberManager_.readyFibers_.push_back(*this);
65 fiberManager_.ensureLoopScheduled();
67 fiberManager_.remoteReadyInsert(this);
71 Fiber::Fiber(FiberManager& fiberManager) :
72 fiberManager_(fiberManager) {
74 auto size = fiberManager_.options_.stackSize;
75 auto limit = fiberManager_.stackAllocator_.allocate(size);
77 fcontext_ = makeContext(limit, size, &Fiber::fiberFuncHelper);
80 void Fiber::init(bool recordStackUsed) {
81 // It is necessary to disable the logic for ASAN because we change
83 #ifndef FOLLY_SANITIZE_ADDRESS
84 recordStackUsed_ = recordStackUsed;
85 if (UNLIKELY(recordStackUsed_ && !stackFilledWithMagic_)) {
86 auto limit = fcontext_.stackLimit();
87 auto base = fcontext_.stackBase();
89 std::fill(static_cast<uint64_t*>(limit),
90 static_cast<uint64_t*>(base),
93 // newer versions of boost allocate context on fiber stack,
94 // need to create a new one
95 auto size = fiberManager_.options_.stackSize;
96 fcontext_ = makeContext(limit, size, &Fiber::fiberFuncHelper);
98 stackFilledWithMagic_ = true;
104 fiberManager_.stackAllocator_.deallocate(
105 static_cast<unsigned char*>(fcontext_.stackLimit()),
106 fiberManager_.options_.stackSize);
109 void Fiber::recordStackPosition() {
111 auto currentPosition = static_cast<size_t>(
112 static_cast<unsigned char*>(fcontext_.stackBase()) -
113 static_cast<unsigned char*>(static_cast<void*>(&stackDummy)));
114 fiberManager_.stackHighWatermark_ =
115 std::max(fiberManager_.stackHighWatermark_, currentPosition);
116 VLOG(4) << "Stack usage: " << currentPosition;
119 void Fiber::fiberFuncHelper(intptr_t fiber) {
120 reinterpret_cast<Fiber*>(fiber)->fiberFunc();
124 * Some weird bug in ASAN causes fiberFunc to allocate boundless amounts of
125 * memory inside __asan_handle_no_return. Work around this in ASAN builds by
126 * tricking the compiler into thinking it may, someday, return.
128 #ifdef FOLLY_SANITIZE_ADDRESS
129 volatile bool loopForever = true;
131 static constexpr bool loopForever = true;
134 void Fiber::fiberFunc() {
135 while (loopForever) {
136 assert(state_ == NOT_STARTED);
138 threadId_ = localThreadId();
143 assert(finallyFunc_);
152 fiberManager_.exceptionCallback_(std::current_exception(),
153 "running Fiber func_/resultFunc_");
156 if (UNLIKELY(recordStackUsed_)) {
157 fiberManager_.stackHighWatermark_ =
158 std::max(fiberManager_.stackHighWatermark_,
159 nonMagicInBytes(fcontext_));
160 VLOG(3) << "Max stack usage: " << fiberManager_.stackHighWatermark_;
161 CHECK(fiberManager_.stackHighWatermark_ <
162 fiberManager_.options_.stackSize - 64) << "Fiber stack overflow";
167 fiberManager_.activeFiber_ = nullptr;
169 auto fiber = reinterpret_cast<Fiber*>(
170 jumpContext(&fcontext_, &fiberManager_.mainContext_, 0));
171 assert(fiber == this);
175 intptr_t Fiber::preempt(State state) {
176 assert(fiberManager_.activeFiber_ == this);
177 assert(state_ == RUNNING);
178 assert(state != RUNNING);
180 fiberManager_.activeFiber_ = nullptr;
183 recordStackPosition();
185 auto ret = jumpContext(&fcontext_, &fiberManager_.mainContext_, 0);
187 assert(fiberManager_.activeFiber_ == this);
188 assert(state_ == READY_TO_RUN);
194 Fiber::LocalData::LocalData(const LocalData& other) : data_(nullptr) {
198 Fiber::LocalData& Fiber::LocalData::operator=(const LocalData& other) {
204 dataSize_ = other.dataSize_;
205 dataType_ = other.dataType_;
206 dataDestructor_ = other.dataDestructor_;
207 dataCopyConstructor_ = other.dataCopyConstructor_;
209 if (dataSize_ <= kBufferSize) {
212 data_ = allocateHeapBuffer(dataSize_);
215 dataCopyConstructor_(data_, other.data_);
220 void Fiber::LocalData::reset() {
225 dataDestructor_(data_);
229 void* Fiber::LocalData::allocateHeapBuffer(size_t size) {
230 return new char[size];
233 void Fiber::LocalData::freeHeapBuffer(void* buffer) {
234 delete[] reinterpret_cast<char*>(buffer);