4155 |
12 Mar 22 |
peter |
// $Id$ |
4155 |
12 Mar 22 |
peter |
2 |
|
4155 |
12 Mar 22 |
peter |
3 |
/* |
4155 |
12 Mar 22 |
peter |
Copyright (C) 2022 Peter Johansson |
4155 |
12 Mar 22 |
peter |
5 |
|
4155 |
12 Mar 22 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
4155 |
12 Mar 22 |
peter |
7 |
|
4155 |
12 Mar 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4155 |
12 Mar 22 |
peter |
modify it under the terms of the GNU General Public License as |
4155 |
12 Mar 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4155 |
12 Mar 22 |
peter |
License, or (at your option) any later version. |
4155 |
12 Mar 22 |
peter |
12 |
|
4155 |
12 Mar 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4155 |
12 Mar 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4155 |
12 Mar 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4155 |
12 Mar 22 |
peter |
General Public License for more details. |
4155 |
12 Mar 22 |
peter |
17 |
|
4155 |
12 Mar 22 |
peter |
You should have received a copy of the GNU General Public License |
4155 |
12 Mar 22 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
4155 |
12 Mar 22 |
peter |
20 |
*/ |
4155 |
12 Mar 22 |
peter |
21 |
|
4155 |
12 Mar 22 |
peter |
// test that it is safe for the Scheduler to go out of scope after a |
4155 |
12 Mar 22 |
peter |
// call to ::interrupt(). |
4155 |
12 Mar 22 |
peter |
24 |
|
4155 |
12 Mar 22 |
peter |
25 |
#include <config.h> |
4155 |
12 Mar 22 |
peter |
26 |
|
4155 |
12 Mar 22 |
peter |
27 |
#include "Suite.h" |
4155 |
12 Mar 22 |
peter |
28 |
|
4155 |
12 Mar 22 |
peter |
29 |
|
4155 |
12 Mar 22 |
peter |
30 |
#include "yat/utility/Exception.h" |
4155 |
12 Mar 22 |
peter |
31 |
#include "yat/utility/Queue.h" |
4155 |
12 Mar 22 |
peter |
32 |
#include "yat/utility/Scheduler.h" |
4155 |
12 Mar 22 |
peter |
33 |
|
4155 |
12 Mar 22 |
peter |
34 |
#include <boost/shared_ptr.hpp> |
4155 |
12 Mar 22 |
peter |
35 |
#include <boost/exception/all.hpp> |
4155 |
12 Mar 22 |
peter |
36 |
|
4155 |
12 Mar 22 |
peter |
37 |
#include <chrono> |
4155 |
12 Mar 22 |
peter |
38 |
#include <iostream> |
4155 |
12 Mar 22 |
peter |
39 |
|
4155 |
12 Mar 22 |
peter |
40 |
using namespace theplu::yat; |
4155 |
12 Mar 22 |
peter |
41 |
using namespace theplu::yat::utility; |
4155 |
12 Mar 22 |
peter |
42 |
|
4155 |
12 Mar 22 |
peter |
43 |
class Sleeper : public Scheduler::Job |
4155 |
12 Mar 22 |
peter |
44 |
{ |
4155 |
12 Mar 22 |
peter |
45 |
public: |
4155 |
12 Mar 22 |
peter |
46 |
Sleeper(int x, utility::Queue<int>& result) |
4155 |
12 Mar 22 |
peter |
47 |
: time_(x), result_(result) {} |
4155 |
12 Mar 22 |
peter |
48 |
|
4155 |
12 Mar 22 |
peter |
49 |
void operator()(void) |
4155 |
12 Mar 22 |
peter |
50 |
{ |
4155 |
12 Mar 22 |
peter |
51 |
std::this_thread::sleep_for(std::chrono::seconds(time_)); |
4155 |
12 Mar 22 |
peter |
52 |
std::cerr << "sleeping " << time_ << " seconds\n"; |
4155 |
12 Mar 22 |
peter |
53 |
result_.push(time_); |
4155 |
12 Mar 22 |
peter |
54 |
} |
4155 |
12 Mar 22 |
peter |
55 |
private: |
4155 |
12 Mar 22 |
peter |
56 |
int time_; |
4155 |
12 Mar 22 |
peter |
57 |
utility::Queue<int>& result_; |
4155 |
12 Mar 22 |
peter |
58 |
}; |
4155 |
12 Mar 22 |
peter |
59 |
|
4155 |
12 Mar 22 |
peter |
60 |
|
4155 |
12 Mar 22 |
peter |
61 |
void run(test::Suite& suite) |
4155 |
12 Mar 22 |
peter |
62 |
{ |
4155 |
12 Mar 22 |
peter |
63 |
utility::Queue<int> result; |
4155 |
12 Mar 22 |
peter |
64 |
Scheduler scheduler(2); |
4155 |
12 Mar 22 |
peter |
65 |
std::vector<boost::shared_ptr<Sleeper> > task; |
4155 |
12 Mar 22 |
peter |
66 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(1, result))); |
4155 |
12 Mar 22 |
peter |
67 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(2, result))); |
4155 |
12 Mar 22 |
peter |
68 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(3, result))); |
4155 |
12 Mar 22 |
peter |
69 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(1, result))); |
4155 |
12 Mar 22 |
peter |
70 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(1, result))); |
4156 |
12 Mar 22 |
peter |
71 |
for (auto job : task) |
4156 |
12 Mar 22 |
peter |
72 |
scheduler.submit(job); |
4155 |
12 Mar 22 |
peter |
73 |
if (true) { |
4155 |
12 Mar 22 |
peter |
74 |
scheduler.interrupt(); |
4155 |
12 Mar 22 |
peter |
75 |
int count=0; |
4155 |
12 Mar 22 |
peter |
76 |
int x; |
4155 |
12 Mar 22 |
peter |
77 |
while (result.try_pop(x)) { |
4155 |
12 Mar 22 |
peter |
78 |
suite.out() << "received result: " << x << "\n"; |
4155 |
12 Mar 22 |
peter |
79 |
++count; |
4155 |
12 Mar 22 |
peter |
80 |
} |
4155 |
12 Mar 22 |
peter |
81 |
suite.out() << "received " << count << " results\n"; |
4155 |
12 Mar 22 |
peter |
82 |
suite.add(count <= 2); |
4155 |
12 Mar 22 |
peter |
83 |
return; |
4155 |
12 Mar 22 |
peter |
84 |
} |
4155 |
12 Mar 22 |
peter |
85 |
scheduler.wait(); |
4155 |
12 Mar 22 |
peter |
86 |
} |
4155 |
12 Mar 22 |
peter |
87 |
|
4155 |
12 Mar 22 |
peter |
88 |
|
4155 |
12 Mar 22 |
peter |
89 |
int main(int argc, char* argv[]) |
4155 |
12 Mar 22 |
peter |
90 |
{ |
4155 |
12 Mar 22 |
peter |
91 |
test::Suite suite(argc, argv); |
4155 |
12 Mar 22 |
peter |
92 |
|
4155 |
12 Mar 22 |
peter |
93 |
try { |
4155 |
12 Mar 22 |
peter |
94 |
run(suite); |
4155 |
12 Mar 22 |
peter |
95 |
} |
4155 |
12 Mar 22 |
peter |
96 |
catch (std::runtime_error& e) { |
4155 |
12 Mar 22 |
peter |
97 |
suite.out() << "error: " << e.what() << "\n"; |
4155 |
12 Mar 22 |
peter |
98 |
suite.add(false); |
4155 |
12 Mar 22 |
peter |
99 |
} |
4155 |
12 Mar 22 |
peter |
100 |
return suite.return_value(); |
4155 |
12 Mar 22 |
peter |
101 |
} |