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"
31 using namespace boost;
33 JobManager* JobManager::_instance = 0;
35 JobManager::JobManager ()
37 boost::thread (boost::bind (&JobManager::scheduler, this));
41 JobManager::add (shared_ptr<Job> j)
43 boost::mutex::scoped_lock lm (_mutex);
48 list<shared_ptr<Job> >
49 JobManager::get () const
51 boost::mutex::scoped_lock lm (_mutex);
57 JobManager::work_to_do () const
59 boost::mutex::scoped_lock lm (_mutex);
60 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
61 while (i != _jobs.end() && (*i)->finished()) {
65 return i != _jobs.end ();
69 JobManager::scheduler ()
73 boost::mutex::scoped_lock lm (_mutex);
75 shared_ptr<Job> first_new;
76 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
77 if ((*i)->running ()) {
79 } else if (!(*i)->finished () && first_new == 0) {
83 if (running == 0 && first_new) {
95 JobManager::instance ()
98 _instance = new JobManager ();