void
Content::signal_changed (int p)
{
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
- }
+ emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
}
void
#define DCPOMATIC_CONTENT_H
#include "types.h"
+#include "signaller.h"
#include "dcpomatic_time.h"
#include <libxml++/libxml++.h>
#include <libcxml/cxml.h>
/** @class Content
* @brief A piece of content represented by one or more files on disk.
*/
-class Content : public boost::enable_shared_from_this<Content>, public boost::noncopyable
+class Content : public boost::enable_shared_from_this<Content>, public Signaller, public boost::noncopyable
{
public:
Content (boost::shared_ptr<const Film>);
#include "exceptions.h"
#include "examine_content_job.h"
#include "config.h"
-#include "ui_signaller.h"
#include "playlist.h"
#include "player.h"
#include "dcp_content_type.h"
break;
}
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (Changed), p));
- }
+ emit (boost::bind (boost::ref (Changed), p));
}
void
signal_changed (NAME);
}
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
- }
+ emit (boost::bind (boost::ref (ContentChanged), c, p));
}
void
#include "types.h"
#include "isdcf_metadata.h"
#include "frame_rate_change.h"
+#include "signaller.h"
#include "ratio.h"
#include <dcp/key.h>
#include <dcp/encrypted_kdm.h>
*
* The content of a Film is held in a Playlist (created and managed by the Film).
*/
-class Film : public boost::enable_shared_from_this<Film>, public boost::noncopyable
+class Film : public boost::enable_shared_from_this<Film>, public Signaller, public boost::noncopyable
{
public:
Film (boost::filesystem::path, bool log = true);
}
}
- if (finished && ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (Finished)));
+ if (finished) {
+ emit (boost::bind (boost::ref (Finished)));
}
}
_pause_changed.wait (lm2);
}
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (Progress)));
- }
+ emit (boost::bind (boost::ref (Progress)));
}
/** @return fractional progress of the current sub-job, if known */
_progress.reset ();
lm.unlock ();
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (Progress)));
- }
+ emit (boost::bind (boost::ref (Progress)));
}
/** @return Human-readable status of this job */
#ifndef DCPOMATIC_JOB_H
#define DCPOMATIC_JOB_H
+#include "signaller.h"
#include <boost/thread/mutex.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/signals2.hpp>
/** @class Job
* @brief A parent class to represent long-running tasks which are run in their own thread.
*/
-class Job : public boost::enable_shared_from_this<Job>, public boost::noncopyable
+class Job : public boost::enable_shared_from_this<Job>, public Signaller, public boost::noncopyable
{
public:
Job (boost::shared_ptr<const Film>);
_jobs.push_back (j);
}
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
- }
+ emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
return j;
}
if (active_jobs != _last_active_jobs) {
_last_active_jobs = active_jobs;
- if (ui_signaller) {
- ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
- }
+ emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
}
dcpomatic_sleep (1);
* @brief A simple scheduler for jobs.
*/
+#include "signaller.h"
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include <boost/signals2.hpp>
/** @class JobManager
* @brief A simple scheduler for jobs.
*/
-class JobManager : public boost::noncopyable
+class JobManager : public Signaller, public boost::noncopyable
{
public:
boost::mutex::scoped_lock lm (_mutex);
_servers.push_back (sd);
}
- ui_signaller->emit (boost::bind (boost::ref (ServerFound), sd));
+ emit (boost::bind (boost::ref (ServerFound), sd));
}
start_accept ();
*/
#include "server.h"
+#include "signaller.h"
#include <boost/signals2.hpp>
-class ServerFinder : public ExceptionStore
+class ServerFinder : public Signaller, public ExceptionStore
{
public:
boost::signals2::connection connect (boost::function<void (ServerDescription)>);
--- /dev/null
+/*
+ Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#ifndef DCPOMATIC_SIGNALLER_H
+#define DCPOMATIC_SIGNALLER_H
+
+#include "ui_signaller.h"
+#include <boost/thread/mutex.hpp>
+#include <boost/signals2.hpp>
+
+class WrapperBase
+{
+public:
+ WrapperBase ()
+ : _valid (true)
+ , _finished (false)
+ {}
+
+ virtual ~WrapperBase () {}
+
+ /* Can be called from any thread */
+ void invalidate ()
+ {
+ boost::mutex::scoped_lock lm (_mutex);
+ _valid = false;
+ }
+
+ bool finished () const {
+ boost::mutex::scoped_lock lm (_mutex);
+ return _finished;
+ }
+
+protected:
+ /* Protect _valid and _finished */
+ mutable boost::mutex _mutex;
+ bool _valid;
+ bool _finished;
+};
+
+/** Helper class to manage lifetime of signals, specifically to address
+ * the problem where an object containing a signal is deleted before
+ * its signal is emitted.
+ */
+template <class T>
+class Wrapper : public WrapperBase
+{
+public:
+ Wrapper (T signal)
+ : _signal (signal)
+ {
+
+ }
+
+ /* Called by the UI thread only */
+ void signal ()
+ {
+ boost::mutex::scoped_lock lm (_mutex);
+ if (_valid) {
+ _signal ();
+ }
+ _finished = true;
+ }
+
+private:
+ T _signal;
+};
+
+/** Parent for any class which needs to raise cross-thread signals (from non-UI
+ * to UI). Subclasses should call, e.g. emit (boost::bind (boost::ref (MySignal), foo, bar));
+ */
+class Signaller
+{
+public:
+ /* Can be called from any thread */
+ virtual ~Signaller () {
+ boost::mutex::scoped_lock lm (_mutex);
+ for (std::list<WrapperBase*>::iterator i = _wrappers.begin(); i != _wrappers.end(); ++i) {
+ (*i)->invalidate ();
+ }
+ }
+
+ /* Can be called from any thread */
+ template <class T>
+ void emit (T signal)
+ {
+ Wrapper<T>* w = new Wrapper<T> (signal);
+ if (ui_signaller) {
+ ui_signaller->emit (boost::bind (&Wrapper<T>::signal, w));
+ }
+
+ boost::mutex::scoped_lock lm (_mutex);
+
+ /* Clean up finished Wrappers */
+ std::list<WrapperBase*>::iterator i = _wrappers.begin ();
+ while (i != _wrappers.end ()) {
+ std::list<WrapperBase*>::iterator tmp = i;
+ ++tmp;
+ if ((*i)->finished ()) {
+ delete *i;
+ _wrappers.erase (i);
+ }
+ i = tmp;
+ }
+
+ /* Add the new one */
+ _wrappers.push_back (w);
+ }
+
+private:
+ /* Protect _wrappers */
+ boost::mutex _mutex;
+ std::list<WrapperBase*> _wrappers;
+};
+
+#endif
#include <boost/asio.hpp>
#include <boost/thread.hpp>
+class Signaller;
+
/** A class to allow signals to be emitted from non-UI threads and handled
* by a UI thread.
*/
_ui_thread = boost::this_thread::get_id ();
}
- /** Emit a signal from any thread whose handlers will be called in the UI
- * thread. Use something like:
- *
- * ui_signaller->emit (boost::bind (boost::ref (SomeSignal), parameter));
- */
- template <typename T>
- void emit (T f) {
- if (boost::this_thread::get_id() == _ui_thread) {
- /* already in the UI thread */
- f ();
- } else {
- /* non-UI thread; post to the service and wake up the UI */
- _service.post (f);
- wake_ui ();
- }
- }
-
/* Do something next time the UI is idle */
template <typename T>
void when_idle (T f) {
}
private:
+ /** Emit a signal from any thread whose handlers will be called in the UI
+ * thread. Use something like:
+ *
+ * ui_signaller->emit (boost::bind (boost::ref (SomeSignal), parameter));
+ */
+ template <typename T>
+ void emit (T f) {
+ if (boost::this_thread::get_id() == _ui_thread) {
+ /* already in the UI thread */
+ f ();
+ } else {
+ /* non-UI thread; post to the service and wake up the UI */
+ _service.post (f);
+ wake_ui ();
+ }
+ }
+
+ friend class Signaller;
+
/** A io_service which is used as the conduit for messages */
boost::asio::io_service _service;
/** Object required to keep io_service from stopping when it has nothing to do */
_emits++;
}
- ui_signaller->emit (boost::bind (boost::ref (StateChanged)));
+ emit (boost::bind (boost::ref (StateChanged)));
}
UpdateChecker *
* @brief UpdateChecker class.
*/
+#include "signaller.h"
#include <curl/curl.h>
#include <boost/signals2.hpp>
#include <boost/thread/mutex.hpp>
struct update_checker_test;
/** Class to check for the existance of an update for DCP-o-matic on a remote server */
-class UpdateChecker : public boost::noncopyable
+class UpdateChecker : public Signaller, public boost::noncopyable
{
public:
UpdateChecker ();