blob: 93fdbd27a8d43472a1a93b64aad2a05b4a978c67 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file src/job_manager.cc
* @brief A simple scheduler for jobs.
*/
#include <iostream>
#include <boost/thread.hpp>
#include "job_manager.h"
#include "job.h"
#include "cross.h"
using namespace std;
using namespace boost;
JobManager* JobManager::_instance = 0;
JobManager::JobManager ()
{
boost::thread (boost::bind (&JobManager::scheduler, this));
}
void
JobManager::add (shared_ptr<Job> j)
{
boost::mutex::scoped_lock lm (_mutex);
_jobs.push_back (j);
}
list<shared_ptr<Job> >
JobManager::get () const
{
boost::mutex::scoped_lock lm (_mutex);
return _jobs;
}
bool
JobManager::work_to_do () const
{
boost::mutex::scoped_lock lm (_mutex);
list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
while (i != _jobs.end() && (*i)->finished()) {
++i;
}
return i != _jobs.end ();
}
void
JobManager::scheduler ()
{
while (1) {
{
boost::mutex::scoped_lock lm (_mutex);
int running = 0;
shared_ptr<Job> first_new;
for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
if ((*i)->running ()) {
++running;
} else if (!(*i)->finished () && first_new == 0) {
first_new = *i;
}
if (running == 0 && first_new) {
first_new->start ();
break;
}
}
}
dvdomatic_sleep (1);
}
}
JobManager *
JobManager::instance ()
{
if (_instance == 0) {
_instance = new JobManager ();
}
return _instance;
}
|