Use a foreach.
[dcpomatic.git] / src / lib / job_manager.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  src/job_manager.cc
22  *  @brief A simple scheduler for jobs.
23  */
24
25 #include "job_manager.h"
26 #include "job.h"
27 #include "cross.h"
28 #include "analyse_audio_job.h"
29 #include "film.h"
30 #include <boost/thread.hpp>
31 #include <boost/foreach.hpp>
32 #include <iostream>
33
34 using std::string;
35 using std::list;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::function;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using boost::bind;
43
44 JobManager* JobManager::_instance = 0;
45
46 JobManager::JobManager ()
47         : _terminate (false)
48         , _paused (false)
49         , _scheduler (0)
50 {
51
52 }
53
54 void
55 JobManager::start ()
56 {
57         _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
58 #ifdef DCPOMATIC_LINUX
59         pthread_setname_np (_scheduler->native_handle(), "job-scheduler");
60 #endif
61 }
62
63 JobManager::~JobManager ()
64 {
65         BOOST_FOREACH (boost::signals2::connection& i, _connections) {
66                 i.disconnect ();
67         }
68
69         {
70                 boost::mutex::scoped_lock lm (_mutex);
71                 _terminate = true;
72                 _empty_condition.notify_all ();
73         }
74
75         if (_scheduler) {
76                 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
77                    can't throw exceptions from a destructor.
78                 */
79                 if (_scheduler->joinable ()) {
80                         _scheduler->join ();
81                 }
82         }
83
84         delete _scheduler;
85 }
86
87 shared_ptr<Job>
88 JobManager::add (shared_ptr<Job> j)
89 {
90         {
91                 boost::mutex::scoped_lock lm (_mutex);
92                 _jobs.push_back (j);
93                 _empty_condition.notify_all ();
94         }
95
96         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
97
98         return j;
99 }
100
101 shared_ptr<Job>
102 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
103 {
104         {
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());
108                 _jobs.insert (i, j);
109                 _empty_condition.notify_all ();
110         }
111
112         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
113
114         return j;
115 }
116
117 list<shared_ptr<Job> >
118 JobManager::get () const
119 {
120         boost::mutex::scoped_lock lm (_mutex);
121         return _jobs;
122 }
123
124 bool
125 JobManager::work_to_do () const
126 {
127         boost::mutex::scoped_lock lm (_mutex);
128         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
129         while (i != _jobs.end() && (*i)->finished()) {
130                 ++i;
131         }
132
133         return i != _jobs.end ();
134 }
135
136 bool
137 JobManager::errors () const
138 {
139         boost::mutex::scoped_lock lm (_mutex);
140         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
141                 if (i->finished_in_error ()) {
142                         return true;
143                 }
144         }
145
146         return false;
147 }
148
149 void
150 JobManager::scheduler ()
151 {
152         while (true) {
153
154                 boost::mutex::scoped_lock lm (_mutex);
155
156                 optional<string> active_job;
157
158                 while (true) {
159                         bool have_new = false;
160                         bool have_running = false;
161                         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
162                                 if (i->running()) {
163                                         have_running = true;
164                                         active_job = i->json_name();
165                                 }
166                                 if (i->is_new()) {
167                                         have_new = true;
168                                 }
169                         }
170
171                         if ((!have_running && have_new) || _terminate) {
172                                 break;
173                         }
174
175                         _empty_condition.wait (lm);
176                 }
177
178                 if (_terminate) {
179                         break;
180                 }
181
182                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
183                         if (i->is_new()) {
184                                 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
185                                 i->start ();
186                                 /* Only start one job at once */
187                                 break;
188                         }
189                 }
190
191                 lm.unlock ();
192
193                 if (active_job != _last_active_job) {
194                         emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
195                         _last_active_job = active_job;
196                 }
197         }
198 }
199
200 void
201 JobManager::job_finished ()
202 {
203         _empty_condition.notify_all ();
204 }
205
206 JobManager *
207 JobManager::instance ()
208 {
209         if (_instance == 0) {
210                 _instance = new JobManager ();
211                 _instance->start ();
212         }
213
214         return _instance;
215 }
216
217 void
218 JobManager::drop ()
219 {
220         delete _instance;
221         _instance = 0;
222 }
223
224 void
225 JobManager::analyse_audio (
226         shared_ptr<const Film> film,
227         shared_ptr<const Playlist> playlist,
228         bool from_zero,
229         boost::signals2::connection& connection,
230         function<void()> ready
231         )
232 {
233         {
234                 boost::mutex::scoped_lock lm (_mutex);
235
236                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
237                         shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
238                         if (a && a->playlist () == playlist) {
239                                 i->when_finished (connection, ready);
240                                 return;
241                         }
242                 }
243         }
244
245         shared_ptr<AnalyseAudioJob> job;
246
247         {
248                 boost::mutex::scoped_lock lm (_mutex);
249
250                 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
251                 connection = job->Finished.connect (ready);
252                 _jobs.push_back (job);
253                 _empty_condition.notify_all ();
254         }
255
256         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
257 }
258
259 void
260 JobManager::increase_priority (shared_ptr<Job> job)
261 {
262         bool changed = false;
263
264         {
265                 boost::mutex::scoped_lock lm (_mutex);
266                 list<shared_ptr<Job> >::iterator last = _jobs.end ();
267                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
268                         if (*i == job && last != _jobs.end()) {
269                                 swap (*i, *last);
270                                 changed = true;
271                                 break;
272                         }
273                         last = i;
274                 }
275         }
276
277         if (changed) {
278                 priority_changed ();
279         }
280 }
281
282 void
283 JobManager::priority_changed ()
284 {
285         {
286                 boost::mutex::scoped_lock lm (_mutex);
287
288                 bool first = true;
289                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
290                         if (first) {
291                                 if (i->is_new ()) {
292                                         i->start ();
293                                 } else if (i->paused_by_priority ()) {
294                                         i->resume ();
295                                 }
296                                 first = false;
297                         } else {
298                                 if (i->running ()) {
299                                         i->pause_by_priority ();
300                                 }
301                         }
302                 }
303         }
304
305         emit (boost::bind (boost::ref (JobsReordered)));
306 }
307
308 void
309 JobManager::decrease_priority (shared_ptr<Job> job)
310 {
311         bool changed = false;
312
313         {
314                 boost::mutex::scoped_lock lm (_mutex);
315                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
316                         list<shared_ptr<Job> >::iterator next = i;
317                         ++next;
318                         if (*i == job && next != _jobs.end()) {
319                                 swap (*i, *next);
320                                 changed = true;
321                                 break;
322                         }
323                 }
324         }
325
326         if (changed) {
327                 priority_changed ();
328         }
329 }
330
331 void
332 JobManager::pause ()
333 {
334         boost::mutex::scoped_lock lm (_mutex);
335
336         if (_paused) {
337                 return;
338         }
339
340         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
341                 if (i->pause_by_user()) {
342                         _paused_job = i;
343                 }
344         }
345
346         _paused = true;
347 }
348
349 void
350 JobManager::resume ()
351 {
352         boost::mutex::scoped_lock lm (_mutex);
353         if (!_paused) {
354                 return;
355         }
356
357         if (_paused_job) {
358                 _paused_job->resume ();
359         }
360
361         _paused_job.reset ();
362         _paused = false;
363 }