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