proxygen
Fiber.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present 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 
23 #include <folly/CPortability.h>
24 #include <folly/Function.h>
25 #include <folly/IntrusiveList.h>
26 #include <folly/Portability.h>
28 #include <folly/io/async/Request.h>
29 
30 namespace folly {
31 namespace fibers {
32 
33 class Baton;
34 class FiberManager;
35 
45 class Fiber {
46  public:
50  void resume();
51 
52  Fiber(const Fiber&) = delete;
53  Fiber& operator=(const Fiber&) = delete;
54 
55  ~Fiber();
56 
62  std::pair<void*, size_t> getStack() const {
64  }
65 
66  private:
67  enum State {
76  };
77 
80  friend class Baton;
81  friend class FiberManager;
82 
83  explicit Fiber(FiberManager& fiberManager);
84 
85  void init(bool recordStackUsed);
86 
87  template <typename F>
88  void setFunction(F&& func);
89 
90  template <typename F, typename G>
91  void setFunctionFinally(F&& func, G&& finally);
92 
93  [[noreturn]] void fiberFunc();
94 
101  void preempt(State state);
102 
107  void recordStackPosition();
108 
111  unsigned char* fiberStackLimit_;
113  std::shared_ptr<RequestContext> rcontext_;
115  bool recordStackUsed_{false};
117 
122 
123  static constexpr size_t kUserBufferSize = 256;
125 
126  void* getUserBuffer();
127 
130 
131  class LocalData {
132  public:
134  ~LocalData();
135  LocalData(const LocalData& other);
136  LocalData& operator=(const LocalData& other);
137 
138  template <typename T>
139  T& get() {
140  if (data_) {
141  assert(*dataType_ == typeid(T));
142  return *reinterpret_cast<T*>(data_);
143  }
144  return getSlow<T>();
145  }
146 
147  void reset();
148 
149  // private:
150  template <typename T>
152 
153  static void* allocateHeapBuffer(size_t size);
154  static void freeHeapBuffer(void* buffer);
155 
156  template <typename T>
157  static void dataCopyConstructor(void*, const void*);
158  template <typename T>
159  static void dataBufferDestructor(void*);
160  template <typename T>
161  static void dataHeapDestructor(void*);
162 
163  static constexpr size_t kBufferSize = 128;
165  size_t dataSize_;
166 
167  const std::type_info* dataType_;
168  void (*dataDestructor_)(void*);
169  void (*dataCopyConstructor_)(void*, const void*);
170  void* data_{nullptr};
171  };
172 
174 
178  std::thread::id threadId_{};
179 
180 #ifdef FOLLY_SANITIZE_ADDRESS
181  void* asanFakeStack_{nullptr};
182  const void* asanMainStackBase_{nullptr};
183  size_t asanMainStackSize_{0};
184 #endif
185 };
186 } // namespace fibers
187 } // namespace folly
188 
189 #include <folly/fibers/Fiber-inl.h>
void setFunctionFinally(F &&func, G &&finally)
Definition: Fiber-inl.h:31
std::pair< void *, size_t > getStack() const
Definition: Fiber.h:62
std::vector< uint8_t > buffer(kBufferSize+16)
std::aligned_storage< kUserBufferSize >::type userBuffer_
Definition: Fiber.h:124
static void freeHeapBuffer(void *buffer)
Definition: Fiber.cpp:240
folly::IntrusiveListHook listHook_
Definition: Fiber.h:175
FiberImpl fiberImpl_
Definition: Fiber.h:112
void(* dataDestructor_)(void *)
Definition: Fiber.h:168
static void dataBufferDestructor(void *)
Definition: Fiber-inl.h:66
static void dataCopyConstructor(void *, const void *)
Definition: Fiber-inl.h:61
unsigned char * fiberStackLimit_
Definition: Fiber.h:111
PskType type
void * getUserBuffer()
Definition: Fiber-inl.h:38
size_t fiberStackSize_
Definition: Fiber.h:110
std::aligned_storage< kBufferSize >::type buffer_
Definition: Fiber.h:164
const std::type_info * dataType_
Definition: Fiber.h:167
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
bool stackFilledWithMagic_
Definition: Fiber.h:116
static void * allocateHeapBuffer(size_t size)
Definition: Fiber.cpp:236
void setFunction(F &&func)
Definition: Fiber-inl.h:24
folly::Function< void()> resultFunc_
Definition: Fiber.h:128
Fiber(const Fiber &)=delete
static constexpr size_t kBufferSize
Definition: Fiber.h:163
Single-threaded task execution engine.
#define FOLLY_NOINLINE
Definition: CPortability.h:142
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
Fiber object used by FiberManager to execute tasks.
Definition: Fiber.h:45
std::shared_ptr< RequestContext > rcontext_
Definition: Fiber.h:113
FOLLY_NOINLINE T & getSlow()
boost::intrusive::list_member_hook< boost::intrusive::link_mode< boost::intrusive::auto_unlink >> IntrusiveListHook
Definition: IntrusiveList.h:32
void recordStackPosition()
Definition: Fiber.cpp:114
void preempt(State state)
Definition: Fiber.cpp:172
static constexpr size_t kUserBufferSize
Definition: Fiber.h:123
void init(bool recordStackUsed)
Definition: Fiber.cpp:78
FiberManager & fiberManager_
Definition: Fiber.h:109
folly::Function< void()> func_
Definition: Fiber.h:114
folly::Function< void()> finallyFunc_
Definition: Fiber.h:129
std::thread::id threadId_
Definition: Fiber.h:178
bool recordStackUsed_
Definition: Fiber.h:115
void(* dataCopyConstructor_)(void *, const void *)
Definition: Fiber.h:169
static void dataHeapDestructor(void *)
Definition: Fiber-inl.h:71
LocalData localData_
Definition: Fiber.h:173
LocalData & operator=(const LocalData &other)
Definition: Fiber.cpp:205
folly::IntrusiveListHook globalListHook_
Definition: Fiber.h:177
state
Definition: http_parser.c:272
Fiber & operator=(const Fiber &)=delete
folly::AtomicIntrusiveLinkedListHook< Fiber > nextRemoteReady_
Definition: Fiber.h:121