X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fjob_manager.cc;h=2b727b0aaa43985942674f07d4f881051369236b;hb=58c574239f98b03f253934d061d976bdb3e30899;hp=dd7c62c318a499b68ab1c8a2734115620097882e;hpb=bb767c7e338414beee132af3e96829c1448e214b;p=dcpomatic.git diff --git a/src/lib/job_manager.cc b/src/lib/job_manager.cc index dd7c62c31..2b727b0aa 100644 --- a/src/lib/job_manager.cc +++ b/src/lib/job_manager.cc @@ -25,30 +25,56 @@ #include #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 std::cout; +using boost::shared_ptr; +using boost::weak_ptr; JobManager* JobManager::_instance = 0; JobManager::JobManager () + : _terminate (false) + , _last_active_jobs (false) + , _scheduler (new boost::thread (boost::bind (&JobManager::scheduler, this))) { - boost::thread (boost::bind (&JobManager::scheduler, this)); + } -void +JobManager::~JobManager () +{ + { + boost::mutex::scoped_lock lm (_mutex); + _terminate = true; + } + + if (_scheduler->joinable ()) { + _scheduler->join (); + } +} + +shared_ptr JobManager::add (shared_ptr j) { - boost::mutex::scoped_lock lm (_mutex); + { + boost::mutex::scoped_lock lm (_mutex); + _jobs.push_back (j); + } + + if (ui_signaller) { + ui_signaller->emit (boost::bind (boost::ref (JobAdded), weak_ptr (j))); + } - _jobs.push_back (j); + return j; } list > JobManager::get () const { boost::mutex::scoped_lock lm (_mutex); - return _jobs; } @@ -64,29 +90,60 @@ 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) { + while (true) { + + bool active_jobs = false; + { boost::mutex::scoped_lock lm (_mutex); - int running = 0; - shared_ptr first_new; + if (_terminate) { + return; + } + 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; } } } - sleep (1); + if (active_jobs != _last_active_jobs) { + _last_active_jobs = active_jobs; + if (ui_signaller) { + ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs)); + } + } + + dcpomatic_sleep (1); } } @@ -99,3 +156,10 @@ JobManager::instance () return _instance; } + +void +JobManager::drop () +{ + delete _instance; + _instance = 0; +}