Try to fix crash on drop()ping JobManager. More debugging when resampler fails.
[dcpomatic.git] / src / lib / job_manager.cc
index 507bf6791ba33e608e94bfe49f53b9066d409f39..056b9f925e1b237946122d804241614bb87f416c 100644 (file)
 
 using std::string;
 using std::list;
+using std::cout;
 using boost::shared_ptr;
+using boost::weak_ptr;
 
 JobManager* JobManager::_instance = 0;
 
 JobManager::JobManager ()
-       : _last_active_jobs (false)
+       : _terminate (false)
+       , _last_active_jobs (false)
+       , _scheduler (new boost::thread (boost::bind (&JobManager::scheduler, this)))
 {
-       boost::thread (boost::bind (&JobManager::scheduler, this));
+       
 }
 
-shared_ptr<Job>
-JobManager::add (shared_ptr<Job> 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<Job> after, shared_ptr<Job> j)
+shared_ptr<Job>
+JobManager::add (shared_ptr<Job> j)
 {
-       boost::mutex::scoped_lock lm (_mutex);
-       list<shared_ptr<Job> >::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<Job> (j)));
+       }
+       
+       return j;
 }
 
 list<shared_ptr<Job> >
@@ -99,6 +112,10 @@ JobManager::scheduler ()
 
                {
                        boost::mutex::scoped_lock lm (_mutex);
+                       if (_terminate) {
+                               return;
+                       }
+                       
                        for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
 
                                if (!(*i)->finished ()) {
@@ -111,23 +128,22 @@ JobManager::scheduler ()
                                }
                                
                                if ((*i)->is_new()) {
-                                       shared_ptr<Job> 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;
                                }
                        }
                }
 
                if (active_jobs != _last_active_jobs) {
                        _last_active_jobs = active_jobs;
-                       ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
+                       if (ui_signaller) {
+                               ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
+                       }
                }
 
-               dvdomatic_sleep (1);
+               dcpomatic_sleep (1);
        }
 }
 
@@ -140,3 +156,10 @@ JobManager::instance ()
 
        return _instance;
 }
+
+void
+JobManager::drop ()
+{
+       delete _instance;
+       _instance = 0;
+}