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