2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "FiberManager.h"
18 #include <sys/syscall.h>
24 #include <glog/logging.h>
26 #include <folly/experimental/fibers/Fiber.h>
27 #include <folly/experimental/fibers/LoopController.h>
29 namespace folly { namespace fibers {
31 __thread FiberManager* FiberManager::currentFiberManager_ = nullptr;
33 FiberManager::FiberManager(std::unique_ptr<LoopController> loopController,
35 loopController_(std::move(loopController)),
37 exceptionCallback_([](std::exception_ptr e, std::string context) {
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 << "'";
46 LOG(DFATAL) << "Unknown exception was thrown in FiberManager with "
47 << "context '" << context << "'";
51 timeoutManager_(std::make_shared<TimeoutController>(*loopController_)),
52 localType_(typeid(void)) {
53 loopController_->setFiberManager(this);
56 FiberManager::~FiberManager() {
57 if (isLoopScheduled_) {
58 loopController_->cancel();
63 while (!fibersPool_.empty()) {
64 fibersPool_.pop_front_and_dispose([] (Fiber* fiber) {
68 assert(readyFibers_.empty());
69 assert(fibersActive_ == 0);
72 LoopController& FiberManager::loopController() {
73 return *loopController_;
76 const LoopController& FiberManager::loopController() const {
77 return *loopController_;
80 bool FiberManager::hasTasks() const {
81 return fibersActive_ > 0 ||
82 !remoteReadyQueue_.empty() ||
83 !remoteTaskQueue_.empty();
86 Fiber* FiberManager::getFiber() {
87 Fiber* fiber = nullptr;
88 if (fibersPool_.empty()) {
89 fiber = new Fiber(*this);
92 fiber = &fibersPool_.front();
93 fibersPool_.pop_front();
94 assert(fibersPoolSize_ > 0);
100 bool recordStack = (options_.recordStackEvery != 0) &&
101 (fiberId_ % options_.recordStackEvery == 0);
102 fiber->init(recordStack);
106 void FiberManager::setExceptionCallback(FiberManager::ExceptionCallback ec) {
108 exceptionCallback_ = std::move(ec);
111 size_t FiberManager::fibersAllocated() const {
112 return fibersAllocated_;
115 size_t FiberManager::fibersPoolSize() const {
116 return fibersPoolSize_;
119 size_t FiberManager::stackHighWatermark() const {
120 return stackHighWatermark_;
123 void FiberManager::remoteReadyInsert(Fiber* fiber) {
124 if (remoteReadyQueue_.insertHead(fiber)) {
125 loopController_->scheduleThreadSafe();