X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fjob_manager.cc;h=056b9f925e1b237946122d804241614bb87f416c;hb=4e2e24a44857dd6dbd777cd28d851fd792ca3124;hp=2db91a177cc08c194644e7b35efc7095e577cddd;hpb=b5001080a3e5b414f6cad1c52926ed757f2d8574;p=dcpomatic.git diff --git a/src/lib/job_manager.cc b/src/lib/job_manager.cc index 2db91a177..056b9f925 100644 --- a/src/lib/job_manager.cc +++ b/src/lib/job_manager.cc @@ -26,33 +26,49 @@ #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)); + } -shared_ptr -JobManager::add (shared_ptr j) +JobManager::~JobManager () { - boost::mutex::scoped_lock lm (_mutex); - _jobs.push_back (j); - return j; + { + boost::mutex::scoped_lock lm (_mutex); + _terminate = true; + } + + if (_scheduler->joinable ()) { + _scheduler->join (); + } } -void -JobManager::add_after (shared_ptr after, shared_ptr j) +shared_ptr +JobManager::add (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); + { + boost::mutex::scoped_lock lm (_mutex); + _jobs.push_back (j); + } + + if (ui_signaller) { + ui_signaller->emit (boost::bind (boost::ref (JobAdded), weak_ptr (j))); + } + + return j; } list > @@ -87,32 +103,47 @@ JobManager::errors () const return false; } - void JobManager::scheduler () { while (1) { + + bool active_jobs = false; + { boost::mutex::scoped_lock lm (_mutex); + if (_terminate) { + return; + } + for (list >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) { + + if (!(*i)->finished ()) { + active_jobs = true; + } + if ((*i)->running ()) { /* Something is already happening */ break; } if ((*i)->is_new()) { - shared_ptr r = (*i)->required (); - if (!r || r->finished_ok ()) { - (*i)->start (); - - /* Only start one job at once */ - break; - } + (*i)->start (); + + /* Only start one job at once */ + break; } } } - dvdomatic_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); } } @@ -125,3 +156,10 @@ JobManager::instance () return _instance; } + +void +JobManager::drop () +{ + delete _instance; + _instance = 0; +}