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.
20 #include <glog/logging.h>
25 #include <folly/Likely.h>
26 #include <folly/Portability.h>
27 #include <folly/fibers/BoostContextCompatibility.h>
28 #include <folly/fibers/FiberManager.h>
29 #include <folly/portability/SysSyscall.h>
35 static const uint64_t kMagic8Bytes = 0xfaceb00cfaceb00c;
37 std::thread::id localThreadId() {
38 return std::this_thread::get_id();
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(
47 begin, end, [](uint64_t val) { return val != kMagic8Bytes; });
49 return (end - firstNonMagic) * sizeof(uint64_t);
52 } // anonymous namespace
54 void Fiber::setData(intptr_t data) {
55 DCHECK_EQ(state_, AWAITING);
57 state_ = READY_TO_RUN;
59 if (fiberManager_.observer_) {
60 fiberManager_.observer_->runnable(reinterpret_cast<uintptr_t>(this));
63 if (LIKELY(threadId_ == localThreadId())) {
64 fiberManager_.readyFibers_.push_back(*this);
65 fiberManager_.ensureLoopScheduled();
67 fiberManager_.remoteReadyInsert(this);
71 Fiber::Fiber(FiberManager& fiberManager) : fiberManager_(fiberManager) {
72 auto size = fiberManager_.options_.stackSize;
73 auto limit = fiberManager_.stackAllocator_.allocate(size);
75 fcontext_ = makeContext(limit, size, &Fiber::fiberFuncHelper);
77 fiberManager_.allFibers_.push_back(*this);
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();
90 static_cast<uint64_t*>(limit),
91 static_cast<uint64_t*>(base),
94 // newer versions of boost allocate context on fiber stack,
95 // need to create a new one
96 auto size = fiberManager_.options_.stackSize;
97 fcontext_ = makeContext(limit, size, &Fiber::fiberFuncHelper);
99 stackFilledWithMagic_ = true;
102 (void)recordStackUsed;
107 #ifdef FOLLY_SANITIZE_ADDRESS
108 fiberManager_.unpoisonFiberStack(this);
110 fiberManager_.stackAllocator_.deallocate(
111 static_cast<unsigned char*>(fcontext_.stackLimit()),
112 fiberManager_.options_.stackSize);
115 void Fiber::recordStackPosition() {
117 auto currentPosition = static_cast<size_t>(
118 static_cast<unsigned char*>(fcontext_.stackBase()) -
119 static_cast<unsigned char*>(static_cast<void*>(&stackDummy)));
120 fiberManager_.stackHighWatermark_ =
121 std::max(fiberManager_.stackHighWatermark_, currentPosition);
122 VLOG(4) << "Stack usage: " << currentPosition;
125 void Fiber::fiberFuncHelper(intptr_t fiber) {
126 reinterpret_cast<Fiber*>(fiber)->fiberFunc();
129 void Fiber::fiberFunc() {
131 DCHECK_EQ(state_, NOT_STARTED);
133 threadId_ = localThreadId();
138 DCHECK(finallyFunc_);
147 fiberManager_.exceptionCallback_(
148 std::current_exception(), "running Fiber func_/resultFunc_");
151 if (UNLIKELY(recordStackUsed_)) {
152 fiberManager_.stackHighWatermark_ = std::max(
153 fiberManager_.stackHighWatermark_, nonMagicInBytes(fcontext_));
154 VLOG(3) << "Max stack usage: " << fiberManager_.stackHighWatermark_;
156 fiberManager_.stackHighWatermark_ <
157 fiberManager_.options_.stackSize - 64)
158 << "Fiber stack overflow";
163 auto context = fiberManager_.deactivateFiber(this);
165 DCHECK_EQ(reinterpret_cast<Fiber*>(context), this);
169 intptr_t Fiber::preempt(State state) {
172 auto preemptImpl = [&]() mutable {
173 DCHECK_EQ(fiberManager_.activeFiber_, this);
174 DCHECK_EQ(state_, RUNNING);
175 DCHECK_NE(state, RUNNING);
179 recordStackPosition();
181 ret = fiberManager_.deactivateFiber(this);
183 DCHECK_EQ(fiberManager_.activeFiber_, this);
184 DCHECK_EQ(state_, READY_TO_RUN);
188 if (fiberManager_.preemptRunner_) {
189 fiberManager_.preemptRunner_->run(std::ref(preemptImpl));
197 Fiber::LocalData::LocalData(const LocalData& other) : data_(nullptr) {
201 Fiber::LocalData& Fiber::LocalData::operator=(const LocalData& other) {
207 dataSize_ = other.dataSize_;
208 dataType_ = other.dataType_;
209 dataDestructor_ = other.dataDestructor_;
210 dataCopyConstructor_ = other.dataCopyConstructor_;
212 if (dataSize_ <= kBufferSize) {
215 data_ = allocateHeapBuffer(dataSize_);
218 dataCopyConstructor_(data_, other.data_);
223 void Fiber::LocalData::reset() {
228 dataDestructor_(data_);
232 void* Fiber::LocalData::allocateHeapBuffer(size_t size) {
233 return new char[size];
236 void Fiber::LocalData::freeHeapBuffer(void* buffer) {
237 delete[] reinterpret_cast<char*>(buffer);