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