Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / lib / job.cc
index 784defc91736074e53292fdbcdb4c306341d7d6f..7aaac748c115a1d9e65144027e7eacf96f1d6640 100644 (file)
@@ -31,6 +31,7 @@
 #include <dcp/exceptions.h>
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
+#include <iostream>
 
 #include "i18n.h"
 
@@ -39,6 +40,7 @@ using std::list;
 using std::cout;
 using boost::shared_ptr;
 using boost::optional;
+using boost::function;
 
 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
 
@@ -57,6 +59,7 @@ Job::~Job ()
 {
        if (_thread) {
                _thread->interrupt ();
+               DCPOMATIC_ASSERT (_thread->joinable ());
                _thread->join ();
        }
 
@@ -131,7 +134,7 @@ Job::run_wrapper ()
 
        } catch (std::bad_alloc& e) {
 
-               set_error (_("Out of memory"), _("There was not enough memory to do this."));
+               set_error (_("Out of memory"), _("There was not enough memory to do this.  If you are running a 32-bit operating system try reducing the number of encoding threads in the General tab of Preferences."));
                set_progress (1);
                set_state (FINISHED_ERROR);
 
@@ -414,7 +417,10 @@ Job::cancel ()
        }
 
        _thread->interrupt ();
+       DCPOMATIC_ASSERT (_thread->joinable ());
        _thread->join ();
+       delete _thread;
+       _thread = 0;
 }
 
 void
@@ -434,3 +440,14 @@ Job::resume ()
                _pause_changed.notify_all ();
        }
 }
+
+void
+Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
+{
+       boost::mutex::scoped_lock lm (_state_mutex);
+       if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
+               finished ();
+       } else {
+               connection = Finished.connect (finished);
+       }
+}