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