Use boost::signals2; fix bugs with x-thread signalling.
[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 boost::shared_ptr;
33
34 JobManager* JobManager::_instance = 0;
35
36 JobManager::JobManager ()
37 {
38         boost::thread (boost::bind (&JobManager::scheduler, this));
39 }
40
41 shared_ptr<Job>
42 JobManager::add (shared_ptr<Job> j)
43 {
44         boost::mutex::scoped_lock lm (_mutex);
45         _jobs.push_back (j);
46         return j;
47 }
48
49 void
50 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
51 {
52         boost::mutex::scoped_lock lm (_mutex);
53         list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
54         assert (i != _jobs.end ());
55         ++i;
56         _jobs.insert (i, j);
57 }
58
59 list<shared_ptr<Job> >
60 JobManager::get () const
61 {
62         boost::mutex::scoped_lock lm (_mutex);
63         return _jobs;
64 }
65
66 bool
67 JobManager::work_to_do () const
68 {
69         boost::mutex::scoped_lock lm (_mutex);
70         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
71         while (i != _jobs.end() && (*i)->finished()) {
72                 ++i;
73         }
74
75         return i != _jobs.end ();
76 }
77
78 bool
79 JobManager::errors () const
80 {
81         boost::mutex::scoped_lock lm (_mutex);
82         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
83                 if ((*i)->finished_in_error ()) {
84                         return true;
85                 }
86         }
87
88         return false;
89 }       
90
91
92 void
93 JobManager::scheduler ()
94 {
95         while (1) {
96                 {
97                         boost::mutex::scoped_lock lm (_mutex);
98                         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
99                                 if ((*i)->running ()) {
100                                         /* Something is already happening */
101                                         break;
102                                 }
103                                 
104                                 if ((*i)->is_new()) {
105                                         shared_ptr<Job> r = (*i)->required ();
106                                         if (!r || r->finished_ok ()) {
107                                                 (*i)->start ();
108
109                                                 /* Only start one job at once */
110                                                 break;
111                                         }
112                                 }
113                         }
114                 }
115
116                 dvdomatic_sleep (1);
117         }
118 }
119
120 JobManager *
121 JobManager::instance ()
122 {
123         if (_instance == 0) {
124                 _instance = new JobManager ();
125         }
126
127         return _instance;
128 }