5fb6bb9573ab5be1a4bd90f8d6a503fc84486cdc
[folly.git] / folly / experimental / fibers / FiberManager.cpp
1 /*
2  * Copyright 2015 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 #include "FiberManager.h"
17
18 #include <sys/syscall.h>
19 #include <unistd.h>
20
21 #include <cassert>
22 #include <stdexcept>
23
24 #include <glog/logging.h>
25
26 #include <folly/experimental/fibers/Fiber.h>
27 #include <folly/experimental/fibers/LoopController.h>
28
29 namespace folly { namespace fibers {
30
31 __thread FiberManager* FiberManager::currentFiberManager_ = nullptr;
32
33 FiberManager::FiberManager(std::unique_ptr<LoopController> loopController,
34                            Options options) :
35     loopController_(std::move(loopController)),
36     options_(options),
37     exceptionCallback_([](std::exception_ptr e, std::string context) {
38         try {
39           std::rethrow_exception(e);
40         } catch (const std::exception& e) {
41           LOG(DFATAL) << "Exception " << typeid(e).name()
42                       << " with message '" << e.what() << "' was thrown in "
43                       << "FiberManager with context '" << context << "'";
44           throw;
45         } catch (...) {
46           LOG(DFATAL) << "Unknown exception was thrown in FiberManager with "
47                       << "context '" << context << "'";
48           throw;
49         }
50       }),
51     timeoutManager_(std::make_shared<TimeoutController>(*loopController_)),
52     localType_(typeid(void)) {
53   loopController_->setFiberManager(this);
54 }
55
56 FiberManager::~FiberManager() {
57   if (isLoopScheduled_) {
58     loopController_->cancel();
59   }
60
61   Fiber* fiberIt;
62   Fiber* fiberItNext;
63   while (!fibersPool_.empty()) {
64     fibersPool_.pop_front_and_dispose([] (Fiber* fiber) {
65       delete fiber;
66     });
67   }
68   assert(readyFibers_.empty());
69   assert(fibersActive_ == 0);
70 }
71
72 LoopController& FiberManager::loopController() {
73   return *loopController_;
74 }
75
76 const LoopController& FiberManager::loopController() const {
77   return *loopController_;
78 }
79
80 bool FiberManager::hasTasks() const {
81   return fibersActive_ > 0 ||
82          !remoteReadyQueue_.empty() ||
83          !remoteTaskQueue_.empty();
84 }
85
86 Fiber* FiberManager::getFiber() {
87   Fiber* fiber = nullptr;
88   if (fibersPool_.empty()) {
89     fiber = new Fiber(*this);
90     ++fibersAllocated_;
91   } else {
92     fiber = &fibersPool_.front();
93     fibersPool_.pop_front();
94     assert(fibersPoolSize_ > 0);
95     --fibersPoolSize_;
96   }
97   assert(fiber);
98   ++fibersActive_;
99   ++fiberId_;
100   bool recordStack = (options_.recordStackEvery != 0) &&
101                      (fiberId_ % options_.recordStackEvery == 0);
102   fiber->init(recordStack);
103   return fiber;
104 }
105
106 void FiberManager::setExceptionCallback(FiberManager::ExceptionCallback ec) {
107   assert(ec);
108   exceptionCallback_ = std::move(ec);
109 }
110
111 size_t FiberManager::fibersAllocated() const {
112   return fibersAllocated_;
113 }
114
115 size_t FiberManager::fibersPoolSize() const {
116   return fibersPoolSize_;
117 }
118
119 size_t FiberManager::stackHighWatermark() const {
120   return stackHighWatermark_;
121 }
122
123 void FiberManager::remoteReadyInsert(Fiber* fiber) {
124   if (remoteReadyQueue_.insertHead(fiber)) {
125     loopController_->scheduleThreadSafe();
126   }
127 }
128
129 }}