2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 /** @file src/job_manager.cc
22 * @brief A simple scheduler for jobs.
25 #include "job_manager.h"
28 #include "analyse_audio_job.h"
30 #include <boost/thread.hpp>
31 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::function;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
44 JobManager* JobManager::_instance = 0;
46 JobManager::JobManager ()
57 _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
58 #ifdef DCPOMATIC_LINUX
59 pthread_setname_np (_scheduler->native_handle(), "job-scheduler");
63 JobManager::~JobManager ()
65 BOOST_FOREACH (boost::signals2::connection& i, _connections) {
70 boost::mutex::scoped_lock lm (_mutex);
72 _empty_condition.notify_all ();
76 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
77 can't throw exceptions from a destructor.
79 if (_scheduler->joinable ()) {
88 JobManager::add (shared_ptr<Job> j)
91 boost::mutex::scoped_lock lm (_mutex);
93 _empty_condition.notify_all ();
96 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
102 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
105 boost::mutex::scoped_lock lm (_mutex);
106 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
107 DCPOMATIC_ASSERT (i != _jobs.end());
109 _empty_condition.notify_all ();
112 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
117 list<shared_ptr<Job> >
118 JobManager::get () const
120 boost::mutex::scoped_lock lm (_mutex);
125 JobManager::work_to_do () const
127 boost::mutex::scoped_lock lm (_mutex);
128 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
129 while (i != _jobs.end() && (*i)->finished()) {
133 return i != _jobs.end ();
137 JobManager::errors () const
139 boost::mutex::scoped_lock lm (_mutex);
140 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
141 if (i->finished_in_error ()) {
150 JobManager::scheduler ()
154 boost::mutex::scoped_lock lm (_mutex);
157 bool have_new = false;
158 bool have_running = false;
159 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
168 if ((!have_running && have_new) || _terminate) {
172 _empty_condition.wait (lm);
179 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
181 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
183 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
184 _last_active_job = i->json_name ();
185 /* Only start one job at once */
193 JobManager::job_finished ()
196 boost::mutex::scoped_lock lm (_mutex);
197 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
198 _last_active_job = optional<string>();
201 _empty_condition.notify_all ();
205 JobManager::instance ()
207 if (_instance == 0) {
208 _instance = new JobManager ();
223 JobManager::analyse_audio (
224 shared_ptr<const Film> film,
225 shared_ptr<const Playlist> playlist,
227 boost::signals2::connection& connection,
228 function<void()> ready
232 boost::mutex::scoped_lock lm (_mutex);
234 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
235 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
236 if (a && a->path() == film->audio_analysis_path(playlist)) {
237 i->when_finished (connection, ready);
243 shared_ptr<AnalyseAudioJob> job;
246 boost::mutex::scoped_lock lm (_mutex);
248 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
249 connection = job->Finished.connect (ready);
250 _jobs.push_back (job);
251 _empty_condition.notify_all ();
254 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
258 JobManager::increase_priority (shared_ptr<Job> job)
260 bool changed = false;
263 boost::mutex::scoped_lock lm (_mutex);
264 list<shared_ptr<Job> >::iterator last = _jobs.end ();
265 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
266 if (*i == job && last != _jobs.end()) {
281 JobManager::priority_changed ()
284 boost::mutex::scoped_lock lm (_mutex);
287 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
291 } else if (i->paused_by_priority ()) {
297 i->pause_by_priority ();
303 emit (boost::bind (boost::ref (JobsReordered)));
307 JobManager::decrease_priority (shared_ptr<Job> job)
309 bool changed = false;
312 boost::mutex::scoped_lock lm (_mutex);
313 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
314 list<shared_ptr<Job> >::iterator next = i;
316 if (*i == job && next != _jobs.end()) {
332 boost::mutex::scoped_lock lm (_mutex);
338 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
339 if (i->pause_by_user()) {
348 JobManager::resume ()
350 boost::mutex::scoped_lock lm (_mutex);
356 _paused_job->resume ();
359 _paused_job.reset ();