2 Copyright (C) 2015-2021 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/>.
22 #ifndef DCPOMATIC_SIGNALLER_H
23 #define DCPOMATIC_SIGNALLER_H
26 #include "signal_manager.h"
27 #include <boost/thread/mutex.hpp>
33 virtual ~WrapperBase () {}
35 /* Can be called from any thread */
38 boost::mutex::scoped_lock lm (_mutex);
42 bool finished () const {
43 boost::mutex::scoped_lock lm (_mutex, boost::try_to_lock);
45 /* It's possible that emission of this
46 wrapper's signal causes another signal to
47 be emitted, which causes finished() on this
48 wrapper to be called (by Signaller::emit).
49 In this case, just say that the wrapper is
58 /* Protect _valid and _finished */
59 mutable boost::mutex _mutex;
61 bool _finished = false;
65 /** Helper class to manage lifetime of signals, specifically to address
66 * the problem where an object containing a signal is deleted before
67 * its signal is emitted.
70 class Wrapper : public WrapperBase
73 explicit Wrapper (T signal)
79 /* Called by the UI thread only */
82 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 (auto i: _wrappers) {
108 /* Can be called from any thread */
112 auto 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 auto i = _wrappers.begin ();
121 while (i != _wrappers.end ()) {
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;