2 Copyright (C) 2012 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "pbd/compose.h"
25 #include "pbd/debug.h"
26 #include "pbd/event_loop.h"
27 #include "pbd/error.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/stacktrace.h"
36 static void do_not_delete_the_loop_pointer (void*) { }
38 Glib::Threads::Private<EventLoop> EventLoop::thread_event_loop (do_not_delete_the_loop_pointer);
40 Glib::Threads::RWLock EventLoop::thread_buffer_requests_lock;
41 EventLoop::ThreadRequestBufferList EventLoop::thread_buffer_requests;
42 EventLoop::RequestBufferSuppliers EventLoop::request_buffer_suppliers;
44 EventLoop::EventLoop (string const& name)
49 EventLoop::~EventLoop ()
53 for (std::list<InvalidationRecord*>::iterator r = trash.begin(); r != trash.end(); ++r) {
54 if (!(*r)->in_use ()) {
62 EventLoop::get_event_loop_for_thread()
64 return thread_event_loop.get ();
68 EventLoop::set_event_loop_for_thread (EventLoop* loop)
70 thread_event_loop.set (loop);
74 EventLoop::invalidate_request (void* data)
76 InvalidationRecord* ir = (InvalidationRecord*) data;
78 /* Some of the requests queued with an EventLoop may involve functors
79 * that make method calls to objects whose lifetime is shorter
80 * than the EventLoop's. We do not want to make those calls if the
81 * object involve has been destroyed. To prevent this, we
82 * provide a way to invalidate those requests when the object is
85 * An object was passed to __invalidator() which added a callback to
86 * EventLoop::invalidate_request() to its "notify when destroyed"
87 * list. __invalidator() returned an InvalidationRecord that has been
88 * to passed to this function as data.
90 * The object is currently being destroyed and so we want to
91 * mark all requests involving this object that are queued with
92 * any EventLoop as invalid.
94 * As of April 2012, we are usign sigc::trackable as the base object
95 * used to queue calls to ::invalidate_request() to be made upon
96 * destruction, via its ::add_destroy_notify_callback() API. This is
97 * not necessarily ideal, but it is very close to precisely what we
98 * want, and many of the objects we want to do this with already
99 * inherit (indirectly) from sigc::trackable.
102 if (ir->event_loop) {
103 DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("%1: invalidating request from %2 (%3) @ %4\n", pthread_name(), ir->event_loop, ir->event_loop->event_loop_name(), ir));
104 Glib::Threads::Mutex::Lock lm (ir->event_loop->slot_invalidation_mutex());
106 ir->event_loop->trash.push_back(ir);
112 vector<EventLoop::ThreadBufferMapping>
113 EventLoop::get_request_buffers_for_target_thread (const std::string& target_thread)
115 vector<ThreadBufferMapping> ret;
116 Glib::Threads::RWLock::WriterLock lm (thread_buffer_requests_lock);
118 for (ThreadRequestBufferList::const_iterator x = thread_buffer_requests.begin();
119 x != thread_buffer_requests.end(); ++x) {
121 if (x->second.target_thread_name == target_thread) {
122 ret.push_back (x->second);
126 DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("for thread \"%1\", found %2 request buffers\n", target_thread, ret.size()));
132 EventLoop::register_request_buffer_factory (const string& target_thread_name,
133 void* (*factory)(uint32_t))
136 RequestBufferSupplier trs;
137 trs.name = target_thread_name;
138 trs.factory = factory;
141 Glib::Threads::RWLock::WriterLock lm (thread_buffer_requests_lock);
142 request_buffer_suppliers.push_back (trs);
147 EventLoop::pre_register (const string& emitting_thread_name, uint32_t num_requests)
149 /* Threads that need to emit signals "towards" other threads, but with
150 RT safe behavior may be created before the receiving threads
151 exist. This makes it impossible for them to use the
152 ThreadCreatedWithRequestSize signal to notify receiving threads of
155 This function creates a request buffer for them to use with
156 the (not yet) created threads, and stores it where the receiving
157 thread can find it later.
160 ThreadBufferMapping mapping;
161 Glib::Threads::RWLock::WriterLock lm (thread_buffer_requests_lock);
163 for (RequestBufferSuppliers::iterator trs = request_buffer_suppliers.begin(); trs != request_buffer_suppliers.end(); ++trs) {
166 /* no factory - no request buffer required or expected */
170 if (emitting_thread_name == trs->name) {
171 /* no need to register an emitter with itself */
175 mapping.emitting_thread = pthread_self();
176 mapping.target_thread_name = trs->name;
178 /* Allocate a suitably sized request buffer. This will set the
179 * thread-local variable that holds a pointer to this request
182 mapping.request_buffer = trs->factory (num_requests);
184 /* now store it where the receiving thread (trs->name) can find
185 it if and when it is created. (Discovery happens in the
186 AbstractUI constructor. Note that if
189 const string key = string_compose ("%1/%2", emitting_thread_name, mapping.target_thread_name);
191 /* management of the thread_request_buffers map works as
194 * when the factory method was called above, the pointer to the
195 * created buffer is set as a thread-local-storage (TLS) value
196 * for this (the emitting) thread.
198 * The TLS value is set up with a destructor that marks the
199 * request buffer as "dead" when the emitting thread exits.
201 * An entry will remain in the map after the thread exits.
203 * The receiving thread may (if it receives requests from other
204 * threads) notice the dead buffer. If it does, it will delete
205 * the request buffer, and call
206 * ::remove_request_buffer_from_map() to get rid of it from the map.
208 * This does mean that the lifetime of the request buffer is
209 * indeterminate: if the receiving thread were to receive no
210 * further requests, the request buffer will live on
211 * forever. But this is OK, because if there are no requests
212 * arriving, the receiving thread is not attempting to use the
213 * request buffer(s) in any way.
215 * Note, however, that *if* an emitting thread is recreated
216 * with the same name (e.g. when a control surface is
217 * enabled/disabled/enabled), then the request buffer for the
218 * new thread will replace the map entry for the key, because
219 * of the matching thread names. This does mean that
220 * potentially the request buffer can leak in this case, but
221 * (a) these buffers are not really that large anyway (b) the
222 * scenario is not particularly common (c) the buffers would
223 * typically last across a session instance if not program
227 thread_buffer_requests[key] = mapping;
228 DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("pre-registered request buffer for \"%1\" to send to \"%2\", buffer @ %3 (key was %4)\n",
229 emitting_thread_name, trs->name, mapping.request_buffer, key));
234 EventLoop::remove_request_buffer_from_map (void* ptr)
236 Glib::Threads::RWLock::WriterLock lm (thread_buffer_requests_lock);
238 for (ThreadRequestBufferList::iterator x = thread_buffer_requests.begin(); x != thread_buffer_requests.end(); ++x) {
239 if (x->second.request_buffer == ptr) {
240 thread_buffer_requests.erase (x);