X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fjob_manager.cc;h=9105976280dd3b7d5ab58f81a362bdb72dcf5a9d;hb=8eabffda9a95de36df560c1b6eadd1a7baa852c7;hp=93fdbd27a8d43472a1a93b64aad2a05b4a978c67;hpb=076451f9f32e8052480968c004f6830403df7638;p=dcpomatic.git diff --git a/src/lib/job_manager.cc b/src/lib/job_manager.cc index 93fdbd27a..910597628 100644 --- a/src/lib/job_manager.cc +++ b/src/lib/job_manager.cc @@ -26,30 +26,42 @@ #include "job_manager.h" #include "job.h" #include "cross.h" +#include "ui_signaller.h" -using namespace std; -using namespace boost; +using std::string; +using std::list; +using boost::shared_ptr; JobManager* JobManager::_instance = 0; JobManager::JobManager () + : _last_active_jobs (false) { boost::thread (boost::bind (&JobManager::scheduler, this)); } -void +shared_ptr JobManager::add (shared_ptr j) { boost::mutex::scoped_lock lm (_mutex); - _jobs.push_back (j); + return j; +} + +void +JobManager::add_after (shared_ptr after, shared_ptr j) +{ + boost::mutex::scoped_lock lm (_mutex); + list >::iterator i = find (_jobs.begin(), _jobs.end(), after); + assert (i != _jobs.end ()); + ++i; + _jobs.insert (i, j); } list > JobManager::get () const { boost::mutex::scoped_lock lm (_mutex); - return _jobs; } @@ -65,28 +77,55 @@ JobManager::work_to_do () const return i != _jobs.end (); } +bool +JobManager::errors () const +{ + boost::mutex::scoped_lock lm (_mutex); + for (list >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) { + if ((*i)->finished_in_error ()) { + return true; + } + } + + return false; +} + void JobManager::scheduler () { while (1) { + + bool active_jobs = false; + { boost::mutex::scoped_lock lm (_mutex); - int running = 0; - shared_ptr first_new; for (list >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) { + + if (!(*i)->finished ()) { + active_jobs = true; + } + if ((*i)->running ()) { - ++running; - } else if (!(*i)->finished () && first_new == 0) { - first_new = *i; + /* Something is already happening */ + break; } - - if (running == 0 && first_new) { - first_new->start (); + + if ((*i)->is_new()) { + (*i)->start (); + + /* Only start one job at once */ break; } } } + if (active_jobs != _last_active_jobs) { + _last_active_jobs = active_jobs; + if (ui_signaller) { + ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs)); + } + } + dvdomatic_sleep (1); } }