2 * Copyright 2016 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.
19 #include <folly/Memory.h>
21 #include <folly/fibers/FiberManager.h>
22 #include <folly/fibers/SimpleLoopController.h>
24 using namespace folly::fibers;
29 : fiberManager(folly::make_unique<SimpleLoopController>()),
34 if (pendingRequests.size() == maxOutstanding || toSend == 0) {
35 if (pendingRequests.empty()) {
38 intptr_t value = rand() % 1000;
39 std::cout << "Completing request with data = " << value << std::endl;
41 pendingRequests.front().setValue(value);
42 pendingRequests.pop();
44 static size_t id_counter = 1;
45 size_t id = id_counter++;
46 std::cout << "Adding new request with id = " << id << std::endl;
48 fiberManager.addTask([this, id]() {
49 std::cout << "Executing fiber with id = " << id << std::endl;
51 auto result1 = await([this](Promise<int> fiber) {
52 pendingRequests.push(std::move(fiber));
55 std::cout << "Fiber id = " << id << " got result1 = " << result1
58 auto result2 = await([this](Promise<int> fiber) {
59 pendingRequests.push(std::move(fiber));
61 std::cout << "Fiber id = " << id << " got result2 = " << result2
66 auto& loopController =
67 dynamic_cast<SimpleLoopController&>(fiberManager.loopController());
68 loopController.stop();
73 FiberManager fiberManager;
75 std::queue<Promise<int>> pendingRequests;
77 size_t maxOutstanding;
83 auto loop = [&app]() { app.loop(); };
85 auto& loopController =
86 dynamic_cast<SimpleLoopController&>(app.fiberManager.loopController());
88 loopController.loop(std::move(loop));