3848 |
23 Sep 19 |
peter |
// $Id$ |
3848 |
23 Sep 19 |
peter |
2 |
|
3848 |
23 Sep 19 |
peter |
3 |
/* |
3848 |
23 Sep 19 |
peter |
Copyright (C) 2019 Peter Johansson |
3848 |
23 Sep 19 |
peter |
5 |
|
3848 |
23 Sep 19 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3848 |
23 Sep 19 |
peter |
7 |
|
3848 |
23 Sep 19 |
peter |
The yat library is free software; you can redistribute it and/or |
3848 |
23 Sep 19 |
peter |
modify it under the terms of the GNU General Public License as |
3848 |
23 Sep 19 |
peter |
published by the Free Software Foundation; either version 3 of the |
3848 |
23 Sep 19 |
peter |
License, or (at your option) any later version. |
3848 |
23 Sep 19 |
peter |
12 |
|
3848 |
23 Sep 19 |
peter |
The yat library is distributed in the hope that it will be useful, |
3848 |
23 Sep 19 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3848 |
23 Sep 19 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3848 |
23 Sep 19 |
peter |
General Public License for more details. |
3848 |
23 Sep 19 |
peter |
17 |
|
3848 |
23 Sep 19 |
peter |
You should have received a copy of the GNU General Public License |
3848 |
23 Sep 19 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3848 |
23 Sep 19 |
peter |
20 |
*/ |
3848 |
23 Sep 19 |
peter |
21 |
|
3848 |
23 Sep 19 |
peter |
// test that functionality to change the size of the Scheduler via the |
3848 |
23 Sep 19 |
peter |
// Scheduler::threads(unsigned int) function. |
3848 |
23 Sep 19 |
peter |
24 |
|
3848 |
23 Sep 19 |
peter |
25 |
|
3848 |
23 Sep 19 |
peter |
26 |
#include <config.h> |
3848 |
23 Sep 19 |
peter |
27 |
|
3848 |
23 Sep 19 |
peter |
28 |
#include "Suite.h" |
3848 |
23 Sep 19 |
peter |
29 |
|
3848 |
23 Sep 19 |
peter |
30 |
#include "yat/utility/Scheduler.h" |
3848 |
23 Sep 19 |
peter |
31 |
|
3848 |
23 Sep 19 |
peter |
32 |
#include <boost/make_shared.hpp> |
3848 |
23 Sep 19 |
peter |
33 |
#include <boost/shared_ptr.hpp> |
3848 |
23 Sep 19 |
peter |
34 |
|
3848 |
23 Sep 19 |
peter |
35 |
#include <iostream> |
3848 |
23 Sep 19 |
peter |
36 |
|
3848 |
23 Sep 19 |
peter |
37 |
using namespace theplu::yat; |
3848 |
23 Sep 19 |
peter |
38 |
using namespace theplu::yat::utility; |
3848 |
23 Sep 19 |
peter |
39 |
|
3848 |
23 Sep 19 |
peter |
40 |
class Sleeper : public Scheduler::Job |
3848 |
23 Sep 19 |
peter |
41 |
{ |
3848 |
23 Sep 19 |
peter |
42 |
public: |
3848 |
23 Sep 19 |
peter |
43 |
Sleeper(int t) : time_(t) {} |
3848 |
23 Sep 19 |
peter |
44 |
void operator()(void) |
3848 |
23 Sep 19 |
peter |
45 |
{ |
3848 |
23 Sep 19 |
peter |
46 |
sleep(time_); |
3848 |
23 Sep 19 |
peter |
47 |
std::cout << "Have slept for " << time_ << " seconds" << std::endl; |
3848 |
23 Sep 19 |
peter |
48 |
} |
3848 |
23 Sep 19 |
peter |
49 |
private: |
3848 |
23 Sep 19 |
peter |
50 |
int time_; |
3848 |
23 Sep 19 |
peter |
51 |
}; |
3848 |
23 Sep 19 |
peter |
52 |
|
3848 |
23 Sep 19 |
peter |
53 |
|
3848 |
23 Sep 19 |
peter |
54 |
int main(int argc, char* argv[]) |
3848 |
23 Sep 19 |
peter |
55 |
{ |
3848 |
23 Sep 19 |
peter |
56 |
test::Suite suite(argc, argv); |
3848 |
23 Sep 19 |
peter |
57 |
|
3848 |
23 Sep 19 |
peter |
58 |
Scheduler scheduler(2); |
3848 |
23 Sep 19 |
peter |
59 |
for (size_t i=0; i<3; ++i) |
3848 |
23 Sep 19 |
peter |
60 |
scheduler.submit(boost::make_shared<Sleeper>(i+1)); |
3848 |
23 Sep 19 |
peter |
61 |
scheduler.threads(3); |
3848 |
23 Sep 19 |
peter |
62 |
scheduler.wait(); |
3848 |
23 Sep 19 |
peter |
63 |
suite.add(scheduler.threads() == 3); |
3848 |
23 Sep 19 |
peter |
64 |
|
3848 |
23 Sep 19 |
peter |
65 |
suite.out() << "enter single-thread phase\n"; |
3848 |
23 Sep 19 |
peter |
66 |
scheduler.threads(1); |
3848 |
23 Sep 19 |
peter |
67 |
for (size_t i=0; i<3; ++i) |
3848 |
23 Sep 19 |
peter |
68 |
scheduler.submit(boost::make_shared<Sleeper>(1)); |
3848 |
23 Sep 19 |
peter |
69 |
int threads = scheduler.threads(); |
3848 |
23 Sep 19 |
peter |
70 |
if (!suite.add(threads == 1)) |
3848 |
23 Sep 19 |
peter |
71 |
suite.err() << "error: threads() returned: " << threads |
3848 |
23 Sep 19 |
peter |
72 |
<< "; expected 1\n"; |
3848 |
23 Sep 19 |
peter |
73 |
scheduler.wait(); |
3848 |
23 Sep 19 |
peter |
74 |
suite.add(scheduler.threads() == 1); |
3848 |
23 Sep 19 |
peter |
75 |
|
3848 |
23 Sep 19 |
peter |
76 |
for (size_t i=0; i<5; ++i) |
3848 |
23 Sep 19 |
peter |
77 |
scheduler.submit(boost::make_shared<Sleeper>(1)); |
3848 |
23 Sep 19 |
peter |
78 |
suite.add(scheduler.threads() == 1); |
3848 |
23 Sep 19 |
peter |
79 |
scheduler.threads(5); |
3848 |
23 Sep 19 |
peter |
80 |
|
3848 |
23 Sep 19 |
peter |
81 |
suite.add(scheduler.threads() == 5); |
3848 |
23 Sep 19 |
peter |
82 |
scheduler.submit(boost::make_shared<Sleeper>(1)); |
3848 |
23 Sep 19 |
peter |
83 |
scheduler.submit(boost::make_shared<Sleeper>(1)); |
3848 |
23 Sep 19 |
peter |
84 |
suite.add(scheduler.threads() == 5); |
3848 |
23 Sep 19 |
peter |
85 |
scheduler.threads(1); |
3848 |
23 Sep 19 |
peter |
86 |
suite.add(scheduler.threads() == 1); |
3848 |
23 Sep 19 |
peter |
87 |
scheduler.wait(); |
3848 |
23 Sep 19 |
peter |
88 |
|
3848 |
23 Sep 19 |
peter |
89 |
suite.add(scheduler.threads() == 1); |
3848 |
23 Sep 19 |
peter |
90 |
return suite.return_value(); |
3848 |
23 Sep 19 |
peter |
91 |
} |