Name threads on Linux.
[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
43 JobManager* JobManager::_instance = 0;
44
45 JobManager::JobManager ()
46         : _terminate (false)
47         , _scheduler (0)
48 {
49
50 }
51
52 void
53 JobManager::start ()
54 {
55         _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
56 #ifdef DCPOMATIC_LINUX
57         pthread_setname_np (_scheduler->native_handle(), "job-scheduler");
58 #endif
59 }
60
61 JobManager::~JobManager ()
62 {
63         {
64                 boost::mutex::scoped_lock lm (_mutex);
65                 _terminate = true;
66         }
67
68         if (_scheduler) {
69                 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
70                    can't throw exceptions from a destructor.
71                 */
72                 if (_scheduler->joinable ()) {
73                         _scheduler->join ();
74                 }
75         }
76
77         delete _scheduler;
78 }
79
80 shared_ptr<Job>
81 JobManager::add (shared_ptr<Job> j)
82 {
83         {
84                 boost::mutex::scoped_lock lm (_mutex);
85                 _jobs.push_back (j);
86         }
87
88         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
89
90         return j;
91 }
92
93 list<shared_ptr<Job> >
94 JobManager::get () const
95 {
96         boost::mutex::scoped_lock lm (_mutex);
97         return _jobs;
98 }
99
100 bool
101 JobManager::work_to_do () const
102 {
103         boost::mutex::scoped_lock lm (_mutex);
104         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
105         while (i != _jobs.end() && (*i)->finished()) {
106                 ++i;
107         }
108
109         return i != _jobs.end ();
110 }
111
112 bool
113 JobManager::errors () const
114 {
115         boost::mutex::scoped_lock lm (_mutex);
116         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
117                 if ((*i)->finished_in_error ()) {
118                         return true;
119                 }
120         }
121
122         return false;
123 }
124
125 void
126 JobManager::scheduler ()
127 {
128         while (true) {
129
130                 optional<string> active_job;
131
132                 {
133                         boost::mutex::scoped_lock lm (_mutex);
134                         if (_terminate) {
135                                 return;
136                         }
137
138                         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
139
140                                 if (!i->finished ()) {
141                                         active_job = i->json_name ();
142                                 }
143
144                                 if (i->running ()) {
145                                         /* Something is already happening */
146                                         break;
147                                 }
148
149                                 if (i->is_new()) {
150                                         i->start ();
151                                         /* Only start one job at once */
152                                         break;
153                                 }
154                         }
155                 }
156
157                 if (active_job != _last_active_job) {
158                         emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
159                         _last_active_job = active_job;
160                 }
161
162                 dcpomatic_sleep (1);
163         }
164 }
165
166 JobManager *
167 JobManager::instance ()
168 {
169         if (_instance == 0) {
170                 _instance = new JobManager ();
171                 _instance->start ();
172         }
173
174         return _instance;
175 }
176
177 void
178 JobManager::drop ()
179 {
180         delete _instance;
181         _instance = 0;
182 }
183
184 void
185 JobManager::analyse_audio (
186         shared_ptr<const Film> film,
187         shared_ptr<const Playlist> playlist,
188         bool from_zero,
189         boost::signals2::connection& connection,
190         function<void()> ready
191         )
192 {
193         {
194                 boost::mutex::scoped_lock lm (_mutex);
195
196                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
197                         shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
198                         if (a && a->playlist () == playlist) {
199                                 i->when_finished (connection, ready);
200                                 return;
201                         }
202                 }
203         }
204
205         shared_ptr<AnalyseAudioJob> job;
206
207         {
208                 boost::mutex::scoped_lock lm (_mutex);
209
210                 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
211                 connection = job->Finished.connect (ready);
212                 _jobs.push_back (job);
213         }
214
215         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
216 }
217
218 void
219 JobManager::increase_priority (shared_ptr<Job> job)
220 {
221         bool changed = false;
222
223         {
224                 boost::mutex::scoped_lock lm (_mutex);
225                 list<shared_ptr<Job> >::iterator last = _jobs.end ();
226                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
227                         if (*i == job && last != _jobs.end()) {
228                                 swap (*i, *last);
229                                 changed = true;
230                                 break;
231                         }
232                         last = i;
233                 }
234         }
235
236         if (changed) {
237                 priority_changed ();
238         }
239 }
240
241 void
242 JobManager::priority_changed ()
243 {
244         {
245                 boost::mutex::scoped_lock lm (_mutex);
246
247                 bool first = true;
248                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
249                         if (first) {
250                                 if (i->is_new ()) {
251                                         i->start ();
252                                 } else if (i->paused_by_priority ()) {
253                                         i->resume ();
254                                 }
255                                 first = false;
256                         } else {
257                                 if (i->running ()) {
258                                         i->pause_by_priority ();
259                                 }
260                         }
261                 }
262         }
263
264         emit (boost::bind (boost::ref (JobsReordered)));
265 }
266
267 void
268 JobManager::decrease_priority (shared_ptr<Job> job)
269 {
270         bool changed = false;
271
272         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
273                 list<shared_ptr<Job> >::iterator next = i;
274                 ++next;
275                 if (*i == job && next != _jobs.end()) {
276                         swap (*i, *next);
277                         changed = true;
278                         break;
279                 }
280         }
281
282         if (changed) {
283                 priority_changed ();
284         }
285 }