2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file src/job_manager.h
23 * @brief A simple scheduler for jobs.
27 #include "signaller.h"
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread.hpp>
30 #include <boost/signals2.hpp>
31 #include <boost/thread/condition.hpp>
42 extern bool wait_for_jobs ();
46 * @brief A simple scheduler for jobs.
48 class JobManager : public Signaller
51 JobManager (JobManager const&) = delete;
52 JobManager& operator= (JobManager const&) = delete;
54 std::shared_ptr<Job> add (std::shared_ptr<Job>);
55 std::shared_ptr<Job> add_after (std::shared_ptr<Job> after, std::shared_ptr<Job> j);
56 std::list<std::shared_ptr<Job>> get () const;
57 bool work_to_do () const;
59 void increase_priority (std::shared_ptr<Job>);
60 void decrease_priority (std::shared_ptr<Job>);
63 bool paused () const {
64 boost::mutex::scoped_lock lm (_mutex);
69 std::shared_ptr<const Film> film,
70 std::shared_ptr<const Playlist> playlist,
72 boost::signals2::connection& connection,
73 std::function<void()> ready
76 void analyse_subtitles (
77 std::shared_ptr<const Film> film,
78 std::shared_ptr<Content> content,
79 boost::signals2::connection& connection,
80 std::function<void()> ready
83 boost::signals2::signal<void (std::weak_ptr<Job>)> JobAdded;
84 boost::signals2::signal<void ()> JobsReordered;
85 boost::signals2::signal<void (boost::optional<std::string>, boost::optional<std::string>)> ActiveJobsChanged;
87 static JobManager* instance ();
91 /* This function is part of the test suite */
92 friend bool ::wait_for_jobs ();
93 friend struct threed_test7;
99 void priority_changed ();
100 void job_finished ();
102 mutable boost::mutex _mutex;
103 boost::condition _empty_condition;
104 /** List of jobs in the order that they will be executed */
105 std::list<std::shared_ptr<Job>> _jobs;
106 std::list<boost::signals2::connection> _connections;
107 bool _terminate = false;
108 bool _paused = false;
109 std::shared_ptr<Job> _paused_job;
111 boost::optional<std::string> _last_active_job;
112 boost::thread _scheduler;
114 static JobManager* _instance;