Move mcrouter/lib/fibers to folly/experimental/fibers
[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   loopController_->setFiberManager(this);
53 }
54
55 FiberManager::~FiberManager() {
56   if (isLoopScheduled_) {
57     loopController_->cancel();
58   }
59
60   Fiber* fiberIt;
61   Fiber* fiberItNext;
62   while (!fibersPool_.empty()) {
63     fibersPool_.pop_front_and_dispose([] (Fiber* fiber) {
64       delete fiber;
65     });
66   }
67   assert(readyFibers_.empty());
68   assert(fibersActive_ == 0);
69 }
70
71 LoopController& FiberManager::loopController() {
72   return *loopController_;
73 }
74
75 const LoopController& FiberManager::loopController() const {
76   return *loopController_;
77 }
78
79 bool FiberManager::hasTasks() const {
80   return fibersActive_ > 0 ||
81          !remoteReadyQueue_.empty() ||
82          !remoteTaskQueue_.empty();
83 }
84
85 Fiber* FiberManager::getFiber() {
86   Fiber* fiber = nullptr;
87   if (fibersPool_.empty()) {
88     fiber = new Fiber(*this);
89     ++fibersAllocated_;
90   } else {
91     fiber = &fibersPool_.front();
92     fibersPool_.pop_front();
93     assert(fibersPoolSize_ > 0);
94     --fibersPoolSize_;
95   }
96   ++fibersActive_;
97   assert(fiber);
98   return fiber;
99 }
100
101 void FiberManager::setExceptionCallback(FiberManager::ExceptionCallback ec) {
102   assert(ec);
103   exceptionCallback_ = std::move(ec);
104 }
105
106 size_t FiberManager::fibersAllocated() const {
107   return fibersAllocated_;
108 }
109
110 size_t FiberManager::fibersPoolSize() const {
111   return fibersPoolSize_;
112 }
113
114 size_t FiberManager::stackHighWatermark() const {
115   return stackHighWatermark_;
116 }
117
118 void FiberManager::remoteReadyInsert(Fiber* fiber) {
119   if (remoteReadyQueue_.insertHead(fiber)) {
120     loopController_->scheduleThreadSafe();
121   }
122 }
123
124 }}