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.
22 #include <folly/AtomicIntrusiveLinkedList.h>
23 #include <folly/CPortability.h>
24 #include <folly/Function.h>
25 #include <folly/IntrusiveList.h>
26 #include <folly/Portability.h>
27 #include <folly/fibers/BoostContextCompatibility.h>
28 #include <folly/io/async/Request.h>
38 * @brief Fiber object used by FiberManager to execute tasks.
40 * Each Fiber object can be executing at most one task at a time. In active
41 * phase it is running the task function and keeps its context.
42 * Fiber is also used to pass data to blocked task and thus unblock it.
43 * Each Fiber may be associated with a single FiberManager.
48 * Resume the blocked task
52 Fiber(const Fiber&) = delete;
53 Fiber& operator=(const Fiber&) = delete;
58 * Retrieve this fiber's base stack and stack size.
60 * @return This fiber's stack pointer and stack size.
62 std::pair<void*, size_t> getStack() const {
63 return {fiberStackLimit_, fiberStackSize_};
68 INVALID, /**< Does't have task function */
69 NOT_STARTED, /**< Has task function, not started */
70 READY_TO_RUN, /**< Was started, blocked, then unblocked */
71 RUNNING, /**< Is running right now */
72 AWAITING, /**< Is currently blocked */
73 AWAITING_IMMEDIATE, /**< Was preempted to run an immediate function,
74 and will be resumed right away */
75 YIELDED, /**< The fiber yielded execution voluntarily */
78 State state_{INVALID}; /**< current Fiber state */
81 friend class FiberManager;
83 explicit Fiber(FiberManager& fiberManager);
85 void init(bool recordStackUsed);
88 void setFunction(F&& func);
90 template <typename F, typename G>
91 void setFunctionFinally(F&& func, G&& finally);
96 * Switch out of fiber context into the main context,
97 * performing necessary housekeeping for the new state.
99 * @param state New state, must not be RUNNING.
101 void preempt(State state);
104 * Examines how much of the stack we used at this moment and
105 * registers with the FiberManager (for monitoring).
107 void recordStackPosition();
109 FiberManager& fiberManager_; /**< Associated FiberManager */
110 size_t fiberStackSize_;
111 unsigned char* fiberStackLimit_;
112 FiberImpl fiberImpl_; /**< underlying fiber implementation */
113 std::shared_ptr<RequestContext> rcontext_; /**< current RequestContext */
114 folly::Function<void()> func_; /**< task function */
115 bool recordStackUsed_{false};
116 bool stackFilledWithMagic_{false};
119 * Points to next fiber in remote ready list
121 folly::AtomicIntrusiveLinkedListHook<Fiber> nextRemoteReady_;
123 static constexpr size_t kUserBufferSize = 256;
124 std::aligned_storage<kUserBufferSize>::type userBuffer_;
126 void* getUserBuffer();
128 folly::Function<void()> resultFunc_;
129 folly::Function<void()> finallyFunc_;
134 LocalData(const LocalData& other);
135 LocalData& operator=(const LocalData& other);
137 template <typename T>
140 assert(*dataType_ == typeid(T));
141 return *reinterpret_cast<T*>(data_);
149 template <typename T>
150 FOLLY_NOINLINE T& getSlow();
152 static void* allocateHeapBuffer(size_t size);
153 static void freeHeapBuffer(void* buffer);
155 template <typename T>
156 static void dataCopyConstructor(void*, const void*);
157 template <typename T>
158 static void dataBufferDestructor(void*);
159 template <typename T>
160 static void dataHeapDestructor(void*);
162 static constexpr size_t kBufferSize = 128;
163 std::aligned_storage<kBufferSize>::type buffer_;
166 const std::type_info* dataType_;
167 void (*dataDestructor_)(void*);
168 void (*dataCopyConstructor_)(void*, const void*);
169 void* data_{nullptr};
172 LocalData localData_;
174 folly::IntrusiveListHook listHook_; /**< list hook for different FiberManager
176 folly::IntrusiveListHook globalListHook_; /**< list hook for global list */
177 std::thread::id threadId_{};
179 #ifdef FOLLY_SANITIZE_ADDRESS
180 void* asanFakeStack_{nullptr};
181 const void* asanMainStackBase_{nullptr};
182 size_t asanMainStackSize_{0};
188 #include <folly/fibers/Fiber-inl.h>