Merge.
[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 void
41 JobManager::add (shared_ptr<Job> j)
42 {
43         boost::mutex::scoped_lock lm (_mutex);
44         
45         _jobs.push_back (j);
46 }
47
48 list<shared_ptr<Job> >
49 JobManager::get () const
50 {
51         boost::mutex::scoped_lock lm (_mutex);
52         
53         return _jobs;
54 }
55
56 bool
57 JobManager::work_to_do () const
58 {
59         boost::mutex::scoped_lock lm (_mutex);
60         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
61         while (i != _jobs.end() && (*i)->finished()) {
62                 ++i;
63         }
64
65         return i != _jobs.end ();
66 }
67
68 void
69 JobManager::scheduler ()
70 {
71         while (1) {
72                 {
73                         boost::mutex::scoped_lock lm (_mutex);
74                         int running = 0;
75                         shared_ptr<Job> first_new;
76                         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
77                                 if ((*i)->running ()) {
78                                         ++running;
79                                 } else if (!(*i)->finished () && first_new == 0) {
80                                         first_new = *i;
81                                 }
82
83                                 if (running == 0 && first_new) {
84                                         first_new->start ();
85                                         break;
86                                 }
87                         }
88                 }
89
90                 dvdomatic_sleep (1);
91         }
92 }
93
94 JobManager *
95 JobManager::instance ()
96 {
97         if (_instance == 0) {
98                 _instance = new JobManager ();
99         }
100
101         return _instance;
102 }