X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fjob_manager.cc;h=f31a00a18b33f0bce674f0406b06197f8fbea691;hb=e60bb3e51bd1508b149e6b8f6608f09b5196ae26;hp=a166b5924dd0e3c472c89bdcc24bdd378c07797a;hpb=c0ed407fb02891f0dd364e78b6192f0e6dbe1d8d;p=dcpomatic.git diff --git a/src/lib/job_manager.cc b/src/lib/job_manager.cc index a166b5924..f31a00a18 100644 --- a/src/lib/job_manager.cc +++ b/src/lib/job_manager.cc @@ -27,31 +27,45 @@ #include "job.h" #include "cross.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::add (shared_ptr j) +JobManager::~JobManager () { - boost::mutex::scoped_lock lm (_mutex); - _jobs.push_back (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); + } + + emit (boost::bind (boost::ref (JobAdded), weak_ptr (j))); + + return j; } list > @@ -73,29 +87,58 @@ 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; } } } - dvdomatic_sleep (1); + if (active_jobs != _last_active_jobs) { + _last_active_jobs = active_jobs; + emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs)); + } + + dcpomatic_sleep (1); } } @@ -108,3 +151,10 @@ JobManager::instance () return _instance; } + +void +JobManager::drop () +{ + delete _instance; + _instance = 0; +}