3346 |
06 Nov 14 |
peter |
// $Id$ |
3346 |
06 Nov 14 |
peter |
2 |
|
3346 |
06 Nov 14 |
peter |
3 |
/* |
3851 |
12 Dec 19 |
peter |
Copyright (C) 2014, 2015, 2017, 2019 Peter Johansson |
3346 |
06 Nov 14 |
peter |
5 |
|
3346 |
06 Nov 14 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3346 |
06 Nov 14 |
peter |
7 |
|
3346 |
06 Nov 14 |
peter |
The yat library is free software; you can redistribute it and/or |
3346 |
06 Nov 14 |
peter |
modify it under the terms of the GNU General Public License as |
3346 |
06 Nov 14 |
peter |
published by the Free Software Foundation; either version 3 of the |
3346 |
06 Nov 14 |
peter |
License, or (at your option) any later version. |
3346 |
06 Nov 14 |
peter |
12 |
|
3346 |
06 Nov 14 |
peter |
The yat library is distributed in the hope that it will be useful, |
3346 |
06 Nov 14 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3346 |
06 Nov 14 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3346 |
06 Nov 14 |
peter |
General Public License for more details. |
3346 |
06 Nov 14 |
peter |
17 |
|
3346 |
06 Nov 14 |
peter |
You should have received a copy of the GNU General Public License |
3346 |
06 Nov 14 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3346 |
06 Nov 14 |
peter |
20 |
*/ |
3346 |
06 Nov 14 |
peter |
21 |
|
3346 |
06 Nov 14 |
peter |
22 |
#include <config.h> |
3346 |
06 Nov 14 |
peter |
23 |
|
3346 |
06 Nov 14 |
peter |
24 |
#include "Suite.h" |
3346 |
06 Nov 14 |
peter |
25 |
|
3346 |
06 Nov 14 |
peter |
26 |
#include "yat/utility/Scheduler.h" |
3346 |
06 Nov 14 |
peter |
27 |
|
3346 |
06 Nov 14 |
peter |
28 |
#include <boost/make_shared.hpp> |
3346 |
06 Nov 14 |
peter |
29 |
#include <boost/shared_ptr.hpp> |
3346 |
06 Nov 14 |
peter |
30 |
|
3346 |
06 Nov 14 |
peter |
31 |
#include <cstdlib> |
3346 |
06 Nov 14 |
peter |
32 |
#include <iostream> |
3346 |
06 Nov 14 |
peter |
33 |
#include <set> |
3346 |
06 Nov 14 |
peter |
34 |
#include <unistd.h> |
3346 |
06 Nov 14 |
peter |
35 |
|
3346 |
06 Nov 14 |
peter |
36 |
using namespace theplu::yat; |
3346 |
06 Nov 14 |
peter |
37 |
using namespace theplu::yat::utility; |
3346 |
06 Nov 14 |
peter |
38 |
|
3346 |
06 Nov 14 |
peter |
39 |
class Sleeper : public Scheduler::Job |
3346 |
06 Nov 14 |
peter |
40 |
{ |
3346 |
06 Nov 14 |
peter |
41 |
public: |
3346 |
06 Nov 14 |
peter |
42 |
Sleeper(int t) : time_(t) {} |
3346 |
06 Nov 14 |
peter |
43 |
void operator()(void) |
3346 |
06 Nov 14 |
peter |
44 |
{ |
3346 |
06 Nov 14 |
peter |
45 |
sleep(time_); |
3346 |
06 Nov 14 |
peter |
46 |
std::cout << "Have slept for " << time_ << " seconds" << std::endl; |
3346 |
06 Nov 14 |
peter |
47 |
} |
3346 |
06 Nov 14 |
peter |
48 |
private: |
3346 |
06 Nov 14 |
peter |
49 |
int time_; |
3346 |
06 Nov 14 |
peter |
50 |
}; |
3346 |
06 Nov 14 |
peter |
51 |
|
3346 |
06 Nov 14 |
peter |
52 |
|
3346 |
06 Nov 14 |
peter |
53 |
int main(int argc, char* argv[]) |
3346 |
06 Nov 14 |
peter |
54 |
{ |
3346 |
06 Nov 14 |
peter |
55 |
test::Suite suite(argc, argv); |
3346 |
06 Nov 14 |
peter |
56 |
|
3346 |
06 Nov 14 |
peter |
57 |
Scheduler scheduler(2); |
3826 |
18 Jul 19 |
peter |
58 |
int n_threads = scheduler.threads(); |
3826 |
18 Jul 19 |
peter |
59 |
if (n_threads != 2) { |
3826 |
18 Jul 19 |
peter |
60 |
suite.add(false); |
3826 |
18 Jul 19 |
peter |
61 |
suite.err() << "threads(): " << n_threads << " - expected: 2\n"; |
3826 |
18 Jul 19 |
peter |
62 |
} |
3851 |
12 Dec 19 |
peter |
63 |
for (size_t i=0; i<10; ++i) |
3851 |
12 Dec 19 |
peter |
64 |
scheduler.submit(boost::make_shared<Sleeper>(i < 4 ? 1 : 0)); |
3851 |
12 Dec 19 |
peter |
// sleep a sec to give the Scheduler chance to process submitted jobs |
3851 |
12 Dec 19 |
peter |
66 |
sleep(1); |
3851 |
12 Dec 19 |
peter |
67 |
int n = scheduler.jobs(); |
3851 |
12 Dec 19 |
peter |
68 |
suite.out() << "jobs: " << n << "\n"; |
3851 |
12 Dec 19 |
peter |
69 |
if (n < 8 || n > 10) { |
3851 |
12 Dec 19 |
peter |
70 |
suite.add(false); |
3851 |
12 Dec 19 |
peter |
71 |
suite.err() << "error: jobs() returned: " << n << "; expected 8-10\n"; |
3851 |
12 Dec 19 |
peter |
72 |
} |
3851 |
12 Dec 19 |
peter |
73 |
|
3346 |
06 Nov 14 |
peter |
74 |
std::vector<boost::shared_ptr<Sleeper> > sleeper; |
3346 |
06 Nov 14 |
peter |
75 |
sleeper.reserve(4); |
3346 |
06 Nov 14 |
peter |
76 |
sleeper.push_back(boost::make_shared<Sleeper>(1)); |
3346 |
06 Nov 14 |
peter |
77 |
sleeper.push_back(boost::make_shared<Sleeper>(10)); |
3348 |
13 Nov 14 |
peter |
78 |
scheduler.submit(sleeper[1]); |
3346 |
06 Nov 14 |
peter |
79 |
sleeper.push_back(boost::make_shared<Sleeper>(2)); |
3401 |
31 Mar 15 |
peter |
80 |
scheduler.add_dependency(sleeper[2], sleeper[0]); |
3346 |
06 Nov 14 |
peter |
81 |
sleeper.push_back(boost::make_shared<Sleeper>(3)); |
3346 |
06 Nov 14 |
peter |
82 |
|
3346 |
06 Nov 14 |
peter |
// not strictly needed since sleeper2 depends on sleeper0, |
3346 |
06 Nov 14 |
peter |
// but... this is a test |
3401 |
31 Mar 15 |
peter |
85 |
scheduler.add_dependency(sleeper[3], sleeper[0]); |
3401 |
31 Mar 15 |
peter |
86 |
scheduler.add_dependency(sleeper[3], sleeper[1]); |
3401 |
31 Mar 15 |
peter |
87 |
scheduler.add_dependency(sleeper[3], sleeper[2]); |
3346 |
06 Nov 14 |
peter |
88 |
|
3346 |
06 Nov 14 |
peter |
89 |
scheduler.submit(sleeper[3]); |
3348 |
13 Nov 14 |
peter |
90 |
scheduler.wait(); |
3346 |
06 Nov 14 |
peter |
91 |
return suite.return_value(); |
3346 |
06 Nov 14 |
peter |
92 |
} |