folly copyright 2015 -> copyright 2016
[folly.git] / folly / experimental / fibers / test / FibersTestApp.cpp
1 /*
2  * Copyright 2016 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 <iostream>
17 #include <queue>
18
19 #include <folly/Memory.h>
20
21 #include <folly/experimental/fibers/FiberManager.h>
22 #include <folly/experimental/fibers/SimpleLoopController.h>
23
24 using namespace folly::fibers;
25
26 struct Application {
27  public:
28   Application ()
29       : fiberManager(folly::make_unique<SimpleLoopController>()),
30         toSend(20),
31         maxOutstanding(5) {
32   }
33
34   void loop() {
35     if (pendingRequests.size() == maxOutstanding || toSend == 0) {
36       if (pendingRequests.empty()) {
37         return;
38       }
39       intptr_t value = rand()%1000;
40       std::cout << "Completing request with data = " << value << std::endl;
41
42       pendingRequests.front().setValue(value);
43       pendingRequests.pop();
44     } else {
45       static size_t id_counter = 1;
46       size_t id = id_counter++;
47       std::cout << "Adding new request with id = " << id << std::endl;
48
49       fiberManager.addTask([this, id]() {
50           std::cout << "Executing fiber with id = " << id << std::endl;
51
52           auto result1 = await(
53             [this](Promise<int> fiber) {
54               pendingRequests.push(std::move(fiber));
55             });
56
57           std::cout << "Fiber id = " << id
58                     << " got result1 = " << result1 << std::endl;
59
60           auto result2 = await
61             ([this](Promise<int> fiber) {
62               pendingRequests.push(std::move(fiber));
63             });
64           std::cout << "Fiber id = " << id
65                     << " got result2 = " << result2 << std::endl;
66          });
67
68       if (--toSend == 0) {
69         auto& loopController =
70           dynamic_cast<SimpleLoopController&>(fiberManager.loopController());
71         loopController.stop();
72       }
73     }
74   }
75
76   FiberManager fiberManager;
77
78   std::queue<Promise<int>> pendingRequests;
79   size_t toSend;
80   size_t maxOutstanding;
81 };
82
83 int main() {
84   Application app;
85
86   auto loop = [&app]() {
87     app.loop();
88   };
89
90   auto& loopController =
91     dynamic_cast<SimpleLoopController&>(app.fiberManager.loopController());
92
93   loopController.loop(std::move(loop));
94
95   return 0;
96 }