Merge branch '2.0' of git.carlh.net:git/dcpomatic into 2.0
[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
30 using std::string;
31 using std::list;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35
36 JobManager* JobManager::_instance = 0;
37
38 JobManager::JobManager ()
39         : _terminate (false)
40         , _last_active_jobs (false)
41         , _scheduler (new boost::thread (boost::bind (&JobManager::scheduler, this)))
42 {
43         
44 }
45
46 JobManager::~JobManager ()
47 {
48         {
49                 boost::mutex::scoped_lock lm (_mutex);
50                 _terminate = true;
51         }
52
53         if (_scheduler->joinable ()) {
54                 _scheduler->join ();
55         }
56 }
57
58 shared_ptr<Job>
59 JobManager::add (shared_ptr<Job> j)
60 {
61         {
62                 boost::mutex::scoped_lock lm (_mutex);
63                 _jobs.push_back (j);
64         }
65
66         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
67         
68         return j;
69 }
70
71 list<shared_ptr<Job> >
72 JobManager::get () const
73 {
74         boost::mutex::scoped_lock lm (_mutex);
75         return _jobs;
76 }
77
78 bool
79 JobManager::work_to_do () const
80 {
81         boost::mutex::scoped_lock lm (_mutex);
82         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
83         while (i != _jobs.end() && (*i)->finished()) {
84                 ++i;
85         }
86
87         return i != _jobs.end ();
88 }
89
90 bool
91 JobManager::errors () const
92 {
93         boost::mutex::scoped_lock lm (_mutex);
94         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
95                 if ((*i)->finished_in_error ()) {
96                         return true;
97                 }
98         }
99
100         return false;
101 }       
102
103 void
104 JobManager::scheduler ()
105 {
106         while (true) {
107
108                 bool active_jobs = false;
109
110                 {
111                         boost::mutex::scoped_lock lm (_mutex);
112                         if (_terminate) {
113                                 return;
114                         }
115                         
116                         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
117
118                                 if (!(*i)->finished ()) {
119                                         active_jobs = true;
120                                 }
121                                 
122                                 if ((*i)->running ()) {
123                                         /* Something is already happening */
124                                         break;
125                                 }
126                                 
127                                 if ((*i)->is_new()) {
128                                         (*i)->start ();
129                                         
130                                         /* Only start one job at once */
131                                         break;
132                                 }
133                         }
134                 }
135
136                 if (active_jobs != _last_active_jobs) {
137                         _last_active_jobs = active_jobs;
138                         emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
139                 }
140
141                 dcpomatic_sleep (1);
142         }
143 }
144
145 JobManager *
146 JobManager::instance ()
147 {
148         if (_instance == 0) {
149                 _instance = new JobManager ();
150         }
151
152         return _instance;
153 }
154
155 void
156 JobManager::drop ()
157 {
158         delete _instance;
159         _instance = 0;
160 }