2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/job_manager.cc
21 * @brief A simple scheduler for jobs.
25 #include <boost/thread.hpp>
26 #include "job_manager.h"
29 #include "ui_signaller.h"
33 using boost::shared_ptr;
35 JobManager* JobManager::_instance = 0;
37 JobManager::JobManager ()
38 : _last_active_jobs (false)
40 boost::thread (boost::bind (&JobManager::scheduler, this));
44 JobManager::add (shared_ptr<Job> j)
46 boost::mutex::scoped_lock lm (_mutex);
51 list<shared_ptr<Job> >
52 JobManager::get () const
54 boost::mutex::scoped_lock lm (_mutex);
59 JobManager::work_to_do () const
61 boost::mutex::scoped_lock lm (_mutex);
62 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
63 while (i != _jobs.end() && (*i)->finished()) {
67 return i != _jobs.end ();
71 JobManager::errors () const
73 boost::mutex::scoped_lock lm (_mutex);
74 for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
75 if ((*i)->finished_in_error ()) {
84 JobManager::scheduler ()
88 bool active_jobs = false;
91 boost::mutex::scoped_lock lm (_mutex);
92 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
94 if (!(*i)->finished ()) {
98 if ((*i)->running ()) {
99 /* Something is already happening */
103 if ((*i)->is_new()) {
106 /* Only start one job at once */
112 if (active_jobs != _last_active_jobs) {
113 _last_active_jobs = active_jobs;
115 ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
124 JobManager::instance ()
126 if (_instance == 0) {
127 _instance = new JobManager ();