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"
34 using boost::shared_ptr;
35 using boost::weak_ptr;
37 JobManager* JobManager::_instance = 0;
39 JobManager::JobManager ()
41 , _last_active_jobs (false)
42 , _scheduler (new boost::thread (boost::bind (&JobManager::scheduler, this)))
47 JobManager::~JobManager ()
50 boost::mutex::scoped_lock lm (_mutex);
54 if (_scheduler->joinable ()) {
60 JobManager::add (shared_ptr<Job> j)
63 boost::mutex::scoped_lock lm (_mutex);
68 ui_signaller->emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
74 list<shared_ptr<Job> >
75 JobManager::get () const
77 boost::mutex::scoped_lock lm (_mutex);
82 JobManager::work_to_do () const
84 boost::mutex::scoped_lock lm (_mutex);
85 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
86 while (i != _jobs.end() && (*i)->finished()) {
90 return i != _jobs.end ();
94 JobManager::errors () const
96 boost::mutex::scoped_lock lm (_mutex);
97 for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
98 if ((*i)->finished_in_error ()) {
107 JobManager::scheduler ()
111 bool active_jobs = false;
114 boost::mutex::scoped_lock lm (_mutex);
119 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
121 if (!(*i)->finished ()) {
125 if ((*i)->running ()) {
126 /* Something is already happening */
130 if ((*i)->is_new()) {
133 /* Only start one job at once */
139 if (active_jobs != _last_active_jobs) {
140 _last_active_jobs = active_jobs;
142 ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
151 JobManager::instance ()
153 if (_instance == 0) {
154 _instance = new JobManager ();