dd7a28db208914c3c67e06d09f04d5acd1c96595
[dcpomatic.git] / src / lib / job_manager.h
1 /*
2     Copyright (C) 2012-2020 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.h
22  *  @brief A simple scheduler for jobs.
23  */
24
25 #include "signaller.h"
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread.hpp>
28 #include <boost/signals2.hpp>
29 #include <boost/thread/condition.hpp>
30 #include <list>
31
32 class Job;
33 class Film;
34 class Playlist;
35 class Content;
36 struct threed_test7;
37
38 extern bool wait_for_jobs ();
39
40 /** @class JobManager
41  *  @brief A simple scheduler for jobs.
42  */
43 class JobManager : public Signaller, public boost::noncopyable
44 {
45 public:
46         boost::shared_ptr<Job> add (boost::shared_ptr<Job>);
47         boost::shared_ptr<Job> add_after (boost::shared_ptr<Job> after, boost::shared_ptr<Job> j);
48         std::list<boost::shared_ptr<Job> > get () const;
49         bool work_to_do () const;
50         bool errors () const;
51         void increase_priority (boost::shared_ptr<Job>);
52         void decrease_priority (boost::shared_ptr<Job>);
53         void pause ();
54         void resume ();
55         bool paused () const {
56                 boost::mutex::scoped_lock lm (_mutex);
57                 return _paused;
58         }
59
60         void analyse_audio (
61                 boost::shared_ptr<const Film> film,
62                 boost::shared_ptr<const Playlist> playlist,
63                 bool from_zero,
64                 boost::signals2::connection& connection,
65                 boost::function<void()> ready
66                 );
67
68         void analyse_subtitles (
69                 boost::shared_ptr<const Film> film,
70                 boost::shared_ptr<Content> content,
71                 boost::signals2::connection& connection,
72                 boost::function<void()> ready
73                 );
74
75         boost::signals2::signal<void (boost::weak_ptr<Job>)> JobAdded;
76         boost::signals2::signal<void ()> JobsReordered;
77         boost::signals2::signal<void (boost::optional<std::string>, boost::optional<std::string>)> ActiveJobsChanged;
78
79         static JobManager* instance ();
80         static void drop ();
81
82 private:
83         /* This function is part of the test suite */
84         friend bool ::wait_for_jobs ();
85         friend struct threed_test7;
86
87         JobManager ();
88         ~JobManager ();
89         void scheduler ();
90         void start ();
91         void priority_changed ();
92         void job_finished ();
93
94         mutable boost::mutex _mutex;
95         boost::condition _empty_condition;
96         /** List of jobs in the order that they will be executed */
97         std::list<boost::shared_ptr<Job> > _jobs;
98         std::list<boost::signals2::connection> _connections;
99         bool _terminate;
100         bool _paused;
101         boost::shared_ptr<Job> _paused_job;
102
103         boost::optional<std::string> _last_active_job;
104         boost::thread _scheduler;
105
106         static JobManager* _instance;
107 };