Fix missing condition wake.
[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         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
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         }
254
255         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
256 }
257
258 void
259 JobManager::increase_priority (shared_ptr<Job> job)
260 {
261         bool changed = false;
262
263         {
264                 boost::mutex::scoped_lock lm (_mutex);
265                 list<shared_ptr<Job> >::iterator last = _jobs.end ();
266                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
267                         if (*i == job && last != _jobs.end()) {
268                                 swap (*i, *last);
269                                 changed = true;
270                                 break;
271                         }
272                         last = i;
273                 }
274         }
275
276         if (changed) {
277                 priority_changed ();
278         }
279 }
280
281 void
282 JobManager::priority_changed ()
283 {
284         {
285                 boost::mutex::scoped_lock lm (_mutex);
286
287                 bool first = true;
288                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
289                         if (first) {
290                                 if (i->is_new ()) {
291                                         i->start ();
292                                 } else if (i->paused_by_priority ()) {
293                                         i->resume ();
294                                 }
295                                 first = false;
296                         } else {
297                                 if (i->running ()) {
298                                         i->pause_by_priority ();
299                                 }
300                         }
301                 }
302         }
303
304         emit (boost::bind (boost::ref (JobsReordered)));
305 }
306
307 void
308 JobManager::decrease_priority (shared_ptr<Job> job)
309 {
310         bool changed = false;
311
312         {
313                 boost::mutex::scoped_lock lm (_mutex);
314                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
315                         list<shared_ptr<Job> >::iterator next = i;
316                         ++next;
317                         if (*i == job && next != _jobs.end()) {
318                                 swap (*i, *next);
319                                 changed = true;
320                                 break;
321                         }
322                 }
323         }
324
325         if (changed) {
326                 priority_changed ();
327         }
328 }
329
330 void
331 JobManager::pause ()
332 {
333         boost::mutex::scoped_lock lm (_mutex);
334
335         if (_paused) {
336                 return;
337         }
338
339         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
340                 if (i->pause_by_user()) {
341                         _paused_job = i;
342                 }
343         }
344
345         _paused = true;
346 }
347
348 void
349 JobManager::resume ()
350 {
351         boost::mutex::scoped_lock lm (_mutex);
352         if (!_paused) {
353                 return;
354         }
355
356         if (_paused_job) {
357                 _paused_job->resume ();
358         }
359
360         _paused_job.reset ();
361         _paused = false;
362 }