Small bits of tidying up.
[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 list<shared_ptr<Job> >
52 JobManager::get () const
53 {
54         boost::mutex::scoped_lock lm (_mutex);
55         return _jobs;
56 }
57
58 bool
59 JobManager::work_to_do () const
60 {
61         boost::mutex::scoped_lock lm (_mutex);
62         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
63         while (i != _jobs.end() && (*i)->finished()) {
64                 ++i;
65         }
66
67         return i != _jobs.end ();
68 }
69
70 bool
71 JobManager::errors () const
72 {
73         boost::mutex::scoped_lock lm (_mutex);
74         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
75                 if ((*i)->finished_in_error ()) {
76                         return true;
77                 }
78         }
79
80         return false;
81 }       
82
83 void
84 JobManager::scheduler ()
85 {
86         while (1) {
87
88                 bool active_jobs = false;
89
90                 {
91                         boost::mutex::scoped_lock lm (_mutex);
92                         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
93
94                                 if (!(*i)->finished ()) {
95                                         active_jobs = true;
96                                 }
97                                 
98                                 if ((*i)->running ()) {
99                                         /* Something is already happening */
100                                         break;
101                                 }
102                                 
103                                 if ((*i)->is_new()) {
104                                         (*i)->start ();
105                                         
106                                         /* Only start one job at once */
107                                         break;
108                                 }
109                         }
110                 }
111
112                 if (active_jobs != _last_active_jobs) {
113                         _last_active_jobs = active_jobs;
114                         if (ui_signaller) {
115                                 ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
116                         }
117                 }
118
119                 dcpomatic_sleep (1);
120         }
121 }
122
123 JobManager *
124 JobManager::instance ()
125 {
126         if (_instance == 0) {
127                 _instance = new JobManager ();
128         }
129
130         return _instance;
131 }