29269b282fc599e6b7c2ebcd5caefd1fc2e59f76
[folly.git] / folly / fibers / Fiber.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #pragma once
17
18 #include <functional>
19 #include <thread>
20 #include <typeinfo>
21
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>
29
30 namespace folly {
31 namespace fibers {
32
33 class Baton;
34 class FiberManager;
35
36 /**
37  * @class Fiber
38  * @brief Fiber object used by FiberManager to execute tasks.
39  *
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.
44  */
45 class Fiber {
46  public:
47   /**
48    * Sets data for the blocked task
49    *
50    * @param data this data will be returned by await() when task is resumed.
51    */
52   void setData(intptr_t data);
53
54   Fiber(const Fiber&) = delete;
55   Fiber& operator=(const Fiber&) = delete;
56
57   ~Fiber();
58
59   /**
60    * Retrieve this fiber's base stack and stack size.
61    *
62    * @return This fiber's stack pointer and stack size.
63    */
64   std::pair<void*, size_t> getStack() const {
65     void* const stack =
66         std::min<void*>(fcontext_.stackLimit(), fcontext_.stackBase());
67     const size_t size = std::abs<intptr_t>(
68         reinterpret_cast<intptr_t>(fcontext_.stackBase()) -
69         reinterpret_cast<intptr_t>(fcontext_.stackLimit()));
70     return {stack, size};
71   }
72
73  private:
74   enum State {
75     INVALID, /**< Does't have task function */
76     NOT_STARTED, /**< Has task function, not started */
77     READY_TO_RUN, /**< Was started, blocked, then unblocked */
78     RUNNING, /**< Is running right now */
79     AWAITING, /**< Is currently blocked */
80     AWAITING_IMMEDIATE, /**< Was preempted to run an immediate function,
81                              and will be resumed right away */
82     YIELDED, /**< The fiber yielded execution voluntarily */
83   };
84
85   State state_{INVALID}; /**< current Fiber state */
86
87   friend class Baton;
88   friend class FiberManager;
89
90   explicit Fiber(FiberManager& fiberManager);
91
92   void init(bool recordStackUsed);
93
94   template <typename F>
95   void setFunction(F&& func);
96
97   template <typename F, typename G>
98   void setFunctionFinally(F&& func, G&& finally);
99
100   static void fiberFuncHelper(intptr_t fiber);
101   void fiberFunc();
102
103   /**
104    * Switch out of fiber context into the main context,
105    * performing necessary housekeeping for the new state.
106    *
107    * @param state New state, must not be RUNNING.
108    *
109    * @return The value passed back from the main context.
110    */
111   intptr_t preempt(State state);
112
113   /**
114    * Examines how much of the stack we used at this moment and
115    * registers with the FiberManager (for monitoring).
116    */
117   void recordStackPosition();
118
119   FiberManager& fiberManager_; /**< Associated FiberManager */
120   FContext fcontext_; /**< current task execution context */
121   intptr_t data_; /**< Used to keep some data with the Fiber */
122   std::shared_ptr<RequestContext> rcontext_; /**< current RequestContext */
123   folly::Function<void()> func_; /**< task function */
124   bool recordStackUsed_{false};
125   bool stackFilledWithMagic_{false};
126
127   /**
128    * Points to next fiber in remote ready list
129    */
130   folly::AtomicIntrusiveLinkedListHook<Fiber> nextRemoteReady_;
131
132   static constexpr size_t kUserBufferSize = 256;
133   std::aligned_storage<kUserBufferSize>::type userBuffer_;
134
135   void* getUserBuffer();
136
137   folly::Function<void()> resultFunc_;
138   folly::Function<void()> finallyFunc_;
139
140   class LocalData {
141    public:
142     LocalData() {}
143     LocalData(const LocalData& other);
144     LocalData& operator=(const LocalData& other);
145
146     template <typename T>
147     T& get() {
148       if (data_) {
149         assert(*dataType_ == typeid(T));
150         return *reinterpret_cast<T*>(data_);
151       }
152       return getSlow<T>();
153     }
154
155     void reset();
156
157     // private:
158     template <typename T>
159     FOLLY_NOINLINE T& getSlow();
160
161     static void* allocateHeapBuffer(size_t size);
162     static void freeHeapBuffer(void* buffer);
163
164     template <typename T>
165     static void dataCopyConstructor(void*, const void*);
166     template <typename T>
167     static void dataBufferDestructor(void*);
168     template <typename T>
169     static void dataHeapDestructor(void*);
170
171     static constexpr size_t kBufferSize = 128;
172     std::aligned_storage<kBufferSize>::type buffer_;
173     size_t dataSize_;
174
175     const std::type_info* dataType_;
176     void (*dataDestructor_)(void*);
177     void (*dataCopyConstructor_)(void*, const void*);
178     void* data_{nullptr};
179   };
180
181   LocalData localData_;
182
183   folly::IntrusiveListHook listHook_; /**< list hook for different FiberManager
184                                            queues */
185   folly::IntrusiveListHook globalListHook_; /**< list hook for global list */
186   std::thread::id threadId_{};
187 };
188 }
189 }
190
191 #include <folly/fibers/Fiber-inl.h>