2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef DCPOMATIC_SIGNALLER_H
22 #define DCPOMATIC_SIGNALLER_H
24 #include "signal_manager.h"
25 #include <boost/thread/mutex.hpp>
35 virtual ~WrapperBase () {}
37 /* Can be called from any thread */
40 boost::mutex::scoped_lock lm (_mutex);
44 bool finished () const {
45 boost::mutex::scoped_lock lm (_mutex, boost::try_to_lock);
47 /* It's possible that emission of this
48 wrapper's signal causes another signal to
49 be emitted, which causes finished() on this
50 wrapper to be called (by Signaller::emit).
51 In this case, just say that the wrapper is
60 /* Protect _valid and _finished */
61 mutable boost::mutex _mutex;
66 /** Helper class to manage lifetime of signals, specifically to address
67 * the problem where an object containing a signal is deleted before
68 * its signal is emitted.
71 class Wrapper : public WrapperBase
80 /* Called by the UI thread only */
83 boost::mutex::scoped_lock lm (_mutex);
94 /** Parent for any class which needs to raise cross-thread signals (from non-UI
95 * to UI). Subclasses should call, e.g. emit (boost::bind (boost::ref (MySignal), foo, bar));
100 /* Can be called from any thread */
101 virtual ~Signaller () {
102 boost::mutex::scoped_lock lm (_signaller_mutex);
103 for (std::list<WrapperBase*>::iterator i = _wrappers.begin(); i != _wrappers.end(); ++i) {
108 /* Can be called from any thread */
112 Wrapper<T>* w = new Wrapper<T> (signal);
113 if (signal_manager) {
114 signal_manager->emit (boost::bind (&Wrapper<T>::signal, w));
117 boost::mutex::scoped_lock lm (_signaller_mutex);
119 /* Clean up finished Wrappers */
120 std::list<WrapperBase*>::iterator i = _wrappers.begin ();
121 while (i != _wrappers.end ()) {
122 std::list<WrapperBase*>::iterator tmp = i;
124 if ((*i)->finished ()) {
131 /* Add the new one */
132 _wrappers.push_back (w);
136 /* Protect _wrappers */
137 boost::mutex _signaller_mutex;
138 std::list<WrapperBase*> _wrappers;