Merge master.
[dcpomatic.git] / src / lib / job_manager.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/job_manager.cc
21  *  @brief A simple scheduler for jobs.
22  */
23
24 #include <iostream>
25 #include <boost/thread.hpp>
26 #include "job_manager.h"
27 #include "job.h"
28 #include "cross.h"
29 #include "ui_signaller.h"
30
31 using std::string;
32 using std::list;
33 using boost::shared_ptr;
34
35 JobManager* JobManager::_instance = 0;
36
37 JobManager::JobManager ()
38         : _last_active_jobs (false)
39 {
40         boost::thread (boost::bind (&JobManager::scheduler, this));
41 }
42
43 shared_ptr<Job>
44 JobManager::add (shared_ptr<Job> j)
45 {
46         boost::mutex::scoped_lock lm (_mutex);
47         _jobs.push_back (j);
48         return j;
49 }
50
51 void
52 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
53 {
54         boost::mutex::scoped_lock lm (_mutex);
55         list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
56         assert (i != _jobs.end ());
57         ++i;
58         _jobs.insert (i, j);
59 }
60
61 list<shared_ptr<Job> >
62 JobManager::get () const
63 {
64         boost::mutex::scoped_lock lm (_mutex);
65         return _jobs;
66 }
67
68 bool
69 JobManager::work_to_do () const
70 {
71         boost::mutex::scoped_lock lm (_mutex);
72         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
73         while (i != _jobs.end() && (*i)->finished()) {
74                 ++i;
75         }
76
77         return i != _jobs.end ();
78 }
79
80 bool
81 JobManager::errors () const
82 {
83         boost::mutex::scoped_lock lm (_mutex);
84         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
85                 if ((*i)->finished_in_error ()) {
86                         return true;
87                 }
88         }
89
90         return false;
91 }       
92
93 void
94 JobManager::scheduler ()
95 {
96         while (1) {
97
98                 bool active_jobs = false;
99
100                 {
101                         boost::mutex::scoped_lock lm (_mutex);
102                         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
103
104                                 if (!(*i)->finished ()) {
105                                         active_jobs = true;
106                                 }
107                                 
108                                 if ((*i)->running ()) {
109                                         /* Something is already happening */
110                                         break;
111                                 }
112                                 
113                                 if ((*i)->is_new()) {
114                                         (*i)->start ();
115                                         
116                                         /* Only start one job at once */
117                                         break;
118                                 }
119                         }
120                 }
121
122                 if (active_jobs != _last_active_jobs) {
123                         _last_active_jobs = active_jobs;
124                         if (ui_signaller) {
125                                 ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
126                         }
127                 }
128
129                 dcpomatic_sleep (1);
130         }
131 }
132
133 JobManager *
134 JobManager::instance ()
135 {
136         if (_instance == 0) {
137                 _instance = new JobManager ();
138         }
139
140         return _instance;
141 }