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