Rename SafeStringStream -> locked_stringstream. Bump deps for removal of stringstream.
[dcpomatic.git] / src / lib / job_manager.cc
1 /*
2     Copyright (C) 2012-2015 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 }
57
58 JobManager::~JobManager ()
59 {
60         {
61                 boost::mutex::scoped_lock lm (_mutex);
62                 _terminate = true;
63         }
64
65         if (_scheduler) {
66                 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
67                    can't throw exceptions from a destructor.
68                 */
69                 if (_scheduler->joinable ()) {
70                         _scheduler->join ();
71                 }
72         }
73
74         delete _scheduler;
75 }
76
77 shared_ptr<Job>
78 JobManager::add (shared_ptr<Job> j)
79 {
80         {
81                 boost::mutex::scoped_lock lm (_mutex);
82                 _jobs.push_back (j);
83         }
84
85         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
86
87         return j;
88 }
89
90 list<shared_ptr<Job> >
91 JobManager::get () const
92 {
93         boost::mutex::scoped_lock lm (_mutex);
94         return _jobs;
95 }
96
97 bool
98 JobManager::work_to_do () const
99 {
100         boost::mutex::scoped_lock lm (_mutex);
101         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
102         while (i != _jobs.end() && (*i)->finished()) {
103                 ++i;
104         }
105
106         return i != _jobs.end ();
107 }
108
109 bool
110 JobManager::errors () const
111 {
112         boost::mutex::scoped_lock lm (_mutex);
113         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
114                 if ((*i)->finished_in_error ()) {
115                         return true;
116                 }
117         }
118
119         return false;
120 }
121
122 void
123 JobManager::scheduler ()
124 {
125         while (true) {
126
127                 optional<string> active_job;
128
129                 {
130                         boost::mutex::scoped_lock lm (_mutex);
131                         if (_terminate) {
132                                 return;
133                         }
134
135                         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
136
137                                 if (!i->finished ()) {
138                                         active_job = i->json_name ();
139                                 }
140
141                                 if (i->running ()) {
142                                         /* Something is already happening */
143                                         break;
144                                 }
145
146                                 if (i->is_new()) {
147                                         i->start ();
148                                         /* Only start one job at once */
149                                         break;
150                                 }
151                         }
152                 }
153
154                 if (active_job != _last_active_job) {
155                         emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
156                         _last_active_job = active_job;
157                 }
158
159                 dcpomatic_sleep (1);
160         }
161 }
162
163 JobManager *
164 JobManager::instance ()
165 {
166         if (_instance == 0) {
167                 _instance = new JobManager ();
168                 _instance->start ();
169         }
170
171         return _instance;
172 }
173
174 void
175 JobManager::drop ()
176 {
177         delete _instance;
178         _instance = 0;
179 }
180
181 void
182 JobManager::analyse_audio (
183         shared_ptr<const Film> film,
184         shared_ptr<const Playlist> playlist,
185         boost::signals2::connection& connection,
186         function<void()> ready
187         )
188 {
189         {
190                 boost::mutex::scoped_lock lm (_mutex);
191
192                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
193                         shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
194                         if (a && a->playlist () == playlist) {
195                                 i->when_finished (connection, ready);
196                                 return;
197                         }
198                 }
199         }
200
201         shared_ptr<AnalyseAudioJob> job;
202
203         {
204                 boost::mutex::scoped_lock lm (_mutex);
205
206                 job.reset (new AnalyseAudioJob (film, playlist));
207                 connection = job->Finished.connect (ready);
208                 _jobs.push_back (job);
209         }
210
211         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
212 }