C++11 tidying.
[dcpomatic.git] / src / lib / job_manager.cc
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file  src/job_manager.cc
23  *  @brief A simple scheduler for jobs.
24  */
25
26
27 #include "analyse_audio_job.h"
28 #include "analyse_subtitles_job.h"
29 #include "cross.h"
30 #include "film.h"
31 #include "job.h"
32 #include "job_manager.h"
33 #include <boost/thread.hpp>
34
35
36 using std::dynamic_pointer_cast;
37 using std::list;
38 using std::make_shared;
39 using std::shared_ptr;
40 using std::string;
41 using std::weak_ptr;
42 using boost::bind;
43 using boost::function;
44 using boost::optional;
45
46
47 JobManager* JobManager::_instance = nullptr;
48
49
50 JobManager::JobManager ()
51 {
52
53 }
54
55
56 void
57 JobManager::start ()
58 {
59         _scheduler = boost::thread (boost::bind(&JobManager::scheduler, this));
60 #ifdef DCPOMATIC_LINUX
61         pthread_setname_np (_scheduler.native_handle(), "job-scheduler");
62 #endif
63 }
64
65
66 JobManager::~JobManager ()
67 {
68         boost::this_thread::disable_interruption dis;
69
70         for (auto& i: _connections) {
71                 i.disconnect ();
72         }
73
74         {
75                 boost::mutex::scoped_lock lm (_mutex);
76                 _terminate = true;
77                 _empty_condition.notify_all ();
78         }
79
80         try {
81                 _scheduler.join();
82         } catch (...) {}
83 }
84
85
86 shared_ptr<Job>
87 JobManager::add (shared_ptr<Job> j)
88 {
89         {
90                 boost::mutex::scoped_lock lm (_mutex);
91                 _jobs.push_back (j);
92                 _empty_condition.notify_all ();
93         }
94
95         emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(j)));
96
97         return j;
98 }
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                 auto 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
118 list<shared_ptr<Job>>
119 JobManager::get () const
120 {
121         boost::mutex::scoped_lock lm (_mutex);
122         return _jobs;
123 }
124
125
126 bool
127 JobManager::work_to_do () const
128 {
129         boost::mutex::scoped_lock lm (_mutex);
130         auto i = _jobs.begin();
131         while (i != _jobs.end() && (*i)->finished()) {
132                 ++i;
133         }
134
135         return i != _jobs.end ();
136 }
137
138
139 bool
140 JobManager::errors () const
141 {
142         boost::mutex::scoped_lock lm (_mutex);
143         for (auto i: _jobs) {
144                 if (i->finished_in_error()) {
145                         return true;
146                 }
147         }
148
149         return false;
150 }
151
152
153 void
154 JobManager::scheduler ()
155 {
156         while (true) {
157
158                 boost::mutex::scoped_lock lm (_mutex);
159
160                 while (true) {
161                         bool have_new = false;
162                         bool have_running = false;
163                         for (auto i: _jobs) {
164                                 if (i->running()) {
165                                         have_running = true;
166                                 }
167                                 if (i->is_new()) {
168                                         have_new = true;
169                                 }
170                         }
171
172                         if ((!have_running && have_new) || _terminate) {
173                                 break;
174                         }
175
176                         _empty_condition.wait (lm);
177                 }
178
179                 if (_terminate) {
180                         break;
181                 }
182
183                 for (auto i: _jobs) {
184                         if (i->is_new()) {
185                                 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
186                                 i->start ();
187                                 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
188                                 _last_active_job = i->json_name ();
189                                 /* Only start one job at once */
190                                 break;
191                         }
192                 }
193         }
194 }
195
196
197 void
198 JobManager::job_finished ()
199 {
200         {
201                 boost::mutex::scoped_lock lm (_mutex);
202                 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
203                 _last_active_job = optional<string>();
204         }
205
206         _empty_condition.notify_all ();
207 }
208
209
210 JobManager *
211 JobManager::instance ()
212 {
213         if (!_instance) {
214                 _instance = new JobManager ();
215                 _instance->start ();
216         }
217
218         return _instance;
219 }
220
221
222 void
223 JobManager::drop ()
224 {
225         delete _instance;
226         _instance = nullptr;
227 }
228
229
230 void
231 JobManager::analyse_audio (
232         shared_ptr<const Film> film,
233         shared_ptr<const Playlist> playlist,
234         bool from_zero,
235         boost::signals2::connection& connection,
236         function<void()> ready
237         )
238 {
239         {
240                 boost::mutex::scoped_lock lm (_mutex);
241
242                 for (auto i: _jobs) {
243                         auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
244                         if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
245                                 i->when_finished (connection, ready);
246                                 return;
247                         }
248                 }
249         }
250
251         shared_ptr<AnalyseAudioJob> job;
252
253         {
254                 boost::mutex::scoped_lock lm (_mutex);
255
256                 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
257                 connection = job->Finished.connect (ready);
258                 _jobs.push_back (job);
259                 _empty_condition.notify_all ();
260         }
261
262         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
263 }
264
265
266 void
267 JobManager::analyse_subtitles (
268         shared_ptr<const Film> film,
269         shared_ptr<Content> content,
270         boost::signals2::connection& connection,
271         function<void()> ready
272         )
273 {
274         {
275                 boost::mutex::scoped_lock lm (_mutex);
276
277                 for (auto i: _jobs) {
278                         auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
279                         if (a && a->path() == film->subtitle_analysis_path(content)) {
280                                 i->when_finished (connection, ready);
281                                 return;
282                         }
283                 }
284         }
285
286         shared_ptr<AnalyseSubtitlesJob> job;
287
288         {
289                 boost::mutex::scoped_lock lm (_mutex);
290
291                 job = make_shared<AnalyseSubtitlesJob>(film, content);
292                 connection = job->Finished.connect (ready);
293                 _jobs.push_back (job);
294                 _empty_condition.notify_all ();
295         }
296
297         emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
298 }
299
300
301 void
302 JobManager::increase_priority (shared_ptr<Job> job)
303 {
304         bool changed = false;
305
306         {
307                 boost::mutex::scoped_lock lm (_mutex);
308                 auto last = _jobs.end ();
309                 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
310                         if (*i == job && last != _jobs.end()) {
311                                 swap (*i, *last);
312                                 changed = true;
313                                 break;
314                         }
315                         last = i;
316                 }
317         }
318
319         if (changed) {
320                 priority_changed ();
321         }
322 }
323
324
325 void
326 JobManager::priority_changed ()
327 {
328         {
329                 boost::mutex::scoped_lock lm (_mutex);
330
331                 bool first = true;
332                 for (auto i: _jobs) {
333                         if (first) {
334                                 if (i->is_new ()) {
335                                         i->start ();
336                                 } else if (i->paused_by_priority ()) {
337                                         i->resume ();
338                                 }
339                                 first = false;
340                         } else {
341                                 if (i->running ()) {
342                                         i->pause_by_priority ();
343                                 }
344                         }
345                 }
346         }
347
348         emit (boost::bind(boost::ref(JobsReordered)));
349 }
350
351
352 void
353 JobManager::decrease_priority (shared_ptr<Job> job)
354 {
355         bool changed = false;
356
357         {
358                 boost::mutex::scoped_lock lm (_mutex);
359                 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
360                         auto next = i;
361                         ++next;
362                         if (*i == job && next != _jobs.end()) {
363                                 swap (*i, *next);
364                                 changed = true;
365                                 break;
366                         }
367                 }
368         }
369
370         if (changed) {
371                 priority_changed ();
372         }
373 }
374
375
376 void
377 JobManager::pause ()
378 {
379         boost::mutex::scoped_lock lm (_mutex);
380
381         if (_paused) {
382                 return;
383         }
384
385         for (auto i: _jobs) {
386                 if (i->pause_by_user()) {
387                         _paused_job = i;
388                 }
389         }
390
391         _paused = true;
392 }
393
394
395 void
396 JobManager::resume ()
397 {
398         boost::mutex::scoped_lock lm (_mutex);
399         if (!_paused) {
400                 return;
401         }
402
403         if (_paused_job) {
404                 _paused_job->resume ();
405         }
406
407         _paused_job.reset ();
408         _paused = false;
409 }