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.
19 #include <folly/Memory.h>
21 #include <folly/experimental/fibers/FiberManager.h>
22 #include <folly/experimental/fibers/SimpleLoopController.h>
24 using namespace folly::fibers;
29 : fiberManager(folly::make_unique<SimpleLoopController>()),
35 if (pendingRequests.size() == maxOutstanding || toSend == 0) {
36 if (pendingRequests.empty()) {
39 intptr_t value = rand()%1000;
40 std::cout << "Completing request with data = " << value << std::endl;
42 pendingRequests.front().setValue(value);
43 pendingRequests.pop();
45 static size_t id_counter = 1;
46 size_t id = id_counter++;
47 std::cout << "Adding new request with id = " << id << std::endl;
49 fiberManager.addTask([this, id]() {
50 std::cout << "Executing fiber with id = " << id << std::endl;
53 [this](Promise<int> fiber) {
54 pendingRequests.push(std::move(fiber));
57 std::cout << "Fiber id = " << id
58 << " got result1 = " << result1 << std::endl;
61 ([this](Promise<int> fiber) {
62 pendingRequests.push(std::move(fiber));
64 std::cout << "Fiber id = " << id
65 << " got result2 = " << result2 << std::endl;
69 auto& loopController =
70 dynamic_cast<SimpleLoopController&>(fiberManager.loopController());
71 loopController.stop();
76 FiberManager fiberManager;
78 std::queue<Promise<int>> pendingRequests;
80 size_t maxOutstanding;
86 auto loop = [&app]() {
90 auto& loopController =
91 dynamic_cast<SimpleLoopController&>(app.fiberManager.loopController());
93 loopController.loop(std::move(loop));