2013-01-16 13:15:38 -05:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 2009-2018 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-01-16 13:15:38 -05:00
|
|
|
|
2016-01-13 23:00:02 -05:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2015-12-28 10:14:17 -05:00
|
|
|
#include "pbd/compose.h"
|
2016-01-13 21:13:55 -05:00
|
|
|
#include "pbd/debug.h"
|
2009-12-21 13:23:07 -05:00
|
|
|
#include "pbd/event_loop.h"
|
2015-12-28 10:14:17 -05:00
|
|
|
#include "pbd/error.h"
|
2018-08-07 10:42:44 -04:00
|
|
|
#include "pbd/pthread_utils.h"
|
2009-12-21 13:23:07 -05:00
|
|
|
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2015-12-28 10:14:17 -05:00
|
|
|
|
2009-12-21 13:23:07 -05:00
|
|
|
using namespace PBD;
|
2010-03-30 11:18:43 -04:00
|
|
|
using namespace std;
|
2009-12-21 13:23:07 -05:00
|
|
|
|
|
|
|
static void do_not_delete_the_loop_pointer (void*) { }
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
Glib::Threads::Private<EventLoop> EventLoop::thread_event_loop (do_not_delete_the_loop_pointer);
|
2012-07-25 13:48:55 -04:00
|
|
|
|
2023-04-21 15:43:46 -04:00
|
|
|
Glib::Threads::Mutex EventLoop::thread_buffer_requests_lock;
|
2015-12-28 10:14:17 -05:00
|
|
|
EventLoop::ThreadRequestBufferList EventLoop::thread_buffer_requests;
|
|
|
|
EventLoop::RequestBufferSuppliers EventLoop::request_buffer_suppliers;
|
|
|
|
|
2015-12-12 10:55:40 -05:00
|
|
|
EventLoop::EventLoop (string const& name)
|
|
|
|
: _name (name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-20 07:46:11 -05:00
|
|
|
EventLoop::~EventLoop ()
|
|
|
|
{
|
|
|
|
trash.sort();
|
|
|
|
trash.unique();
|
|
|
|
for (std::list<InvalidationRecord*>::iterator r = trash.begin(); r != trash.end(); ++r) {
|
|
|
|
if (!(*r)->in_use ()) {
|
|
|
|
delete *r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trash.clear ();
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
EventLoop*
|
2015-12-28 10:14:17 -05:00
|
|
|
EventLoop::get_event_loop_for_thread()
|
|
|
|
{
|
2009-12-21 13:23:07 -05:00
|
|
|
return thread_event_loop.get ();
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
void
|
|
|
|
EventLoop::set_event_loop_for_thread (EventLoop* loop)
|
2009-12-21 13:23:07 -05:00
|
|
|
{
|
2012-07-25 13:48:55 -04:00
|
|
|
thread_event_loop.set (loop);
|
2009-12-21 13:23:07 -05:00
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
void*
|
2010-03-30 11:18:43 -04:00
|
|
|
EventLoop::invalidate_request (void* data)
|
|
|
|
{
|
2016-12-13 17:46:55 -05:00
|
|
|
InvalidationRecord* ir = (InvalidationRecord*) data;
|
2010-03-30 11:18:43 -04:00
|
|
|
|
2012-04-24 12:45:38 -04:00
|
|
|
/* Some of the requests queued with an EventLoop may involve functors
|
|
|
|
* that make method calls to objects whose lifetime is shorter
|
|
|
|
* than the EventLoop's. We do not want to make those calls if the
|
2015-10-04 14:51:05 -04:00
|
|
|
* object involve has been destroyed. To prevent this, we
|
2012-04-24 12:45:38 -04:00
|
|
|
* provide a way to invalidate those requests when the object is
|
|
|
|
* destroyed.
|
|
|
|
*
|
|
|
|
* An object was passed to __invalidator() which added a callback to
|
|
|
|
* EventLoop::invalidate_request() to its "notify when destroyed"
|
|
|
|
* list. __invalidator() returned an InvalidationRecord that has been
|
|
|
|
* to passed to this function as data.
|
|
|
|
*
|
|
|
|
* The object is currently being destroyed and so we want to
|
|
|
|
* mark all requests involving this object that are queued with
|
2015-10-04 14:51:05 -04:00
|
|
|
* any EventLoop as invalid.
|
2012-04-24 12:45:38 -04:00
|
|
|
*
|
|
|
|
* As of April 2012, we are usign sigc::trackable as the base object
|
|
|
|
* used to queue calls to ::invalidate_request() to be made upon
|
|
|
|
* destruction, via its ::add_destroy_notify_callback() API. This is
|
|
|
|
* not necessarily ideal, but it is very close to precisely what we
|
|
|
|
* want, and many of the objects we want to do this with already
|
|
|
|
* inherit (indirectly) from sigc::trackable.
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-12-13 20:25:13 -05:00
|
|
|
if (ir->event_loop) {
|
2018-08-07 10:42:44 -04:00
|
|
|
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));
|
2023-04-21 14:02:22 -04:00
|
|
|
Glib::Threads::RWLock::WriterLock lm (ir->event_loop->slot_invalidation_rwlock());
|
2016-12-15 00:11:20 -05:00
|
|
|
ir->invalidate ();
|
2016-12-14 16:38:37 -05:00
|
|
|
ir->event_loop->trash.push_back(ir);
|
2016-12-13 20:25:13 -05:00
|
|
|
}
|
2010-03-30 11:18:43 -04:00
|
|
|
|
2016-12-13 20:25:13 -05:00
|
|
|
return 0;
|
2010-03-30 11:18:43 -04:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:14:38 -04:00
|
|
|
/** Create a PBD::EventLoop::InvalidationRecord and attach a callback
|
|
|
|
* to a given sigc::trackable so that PBD::EventLoop::invalidate_request
|
|
|
|
* is called when that trackable is destroyed.
|
|
|
|
*/
|
|
|
|
EventLoop::InvalidationRecord*
|
|
|
|
EventLoop::__invalidator (sigc::trackable& trackable, const char* file, int line)
|
|
|
|
{
|
|
|
|
PBD::EventLoop::InvalidationRecord* ir = new PBD::EventLoop::InvalidationRecord;
|
|
|
|
|
|
|
|
ir->file = file;
|
|
|
|
ir->line = line;
|
|
|
|
|
|
|
|
trackable.add_destroy_notify_callback (ir, PBD::EventLoop::invalidate_request);
|
|
|
|
|
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-28 10:14:17 -05:00
|
|
|
vector<EventLoop::ThreadBufferMapping>
|
|
|
|
EventLoop::get_request_buffers_for_target_thread (const std::string& target_thread)
|
|
|
|
{
|
|
|
|
vector<ThreadBufferMapping> ret;
|
2023-04-21 15:43:46 -04:00
|
|
|
Glib::Threads::Mutex::Lock lm (thread_buffer_requests_lock);
|
2015-12-28 10:14:17 -05:00
|
|
|
|
2023-04-24 07:40:36 -04:00
|
|
|
DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("%1 look for request buffers via %2\n", pthread_name(), target_thread));
|
2023-04-21 14:12:20 -04:00
|
|
|
|
2023-04-21 15:43:46 -04:00
|
|
|
for (auto const & tbr : thread_buffer_requests) {
|
2023-04-23 13:26:00 -04:00
|
|
|
DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("for thread \"%1\", request buffer for %2 (%3) thread %4\n", target_thread, DEBUG_THREAD_PRINT(tbr.emitting_thread), tbr.num_requests));
|
2023-04-21 15:43:46 -04:00
|
|
|
ret.push_back (tbr);
|
2015-12-28 10:14:17 -05:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:13:55 -05:00
|
|
|
DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("for thread \"%1\", found %2 request buffers\n", target_thread, ret.size()));
|
|
|
|
|
2015-12-28 10:14:17 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::pre_register (const string& emitting_thread_name, uint32_t num_requests)
|
|
|
|
{
|
|
|
|
/* Threads that need to emit signals "towards" other threads, but with
|
|
|
|
RT safe behavior may be created before the receiving threads
|
|
|
|
exist. This makes it impossible for them to use the
|
|
|
|
ThreadCreatedWithRequestSize signal to notify receiving threads of
|
|
|
|
their existence.
|
|
|
|
|
|
|
|
This function creates a request buffer for them to use with
|
|
|
|
the (not yet) created threads, and stores it where the receiving
|
|
|
|
thread can find it later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ThreadBufferMapping mapping;
|
2023-04-21 15:43:46 -04:00
|
|
|
Glib::Threads::Mutex::Lock lm (thread_buffer_requests_lock);
|
2015-12-28 10:14:17 -05:00
|
|
|
|
2023-04-21 15:43:46 -04:00
|
|
|
mapping.emitting_thread = pthread_self();
|
|
|
|
mapping.num_requests = num_requests;
|
2015-12-28 10:14:17 -05:00
|
|
|
|
2023-04-21 15:43:46 -04:00
|
|
|
/* now store it where the receiving thread (trs->name) can find
|
|
|
|
it if and when it is created. (Discovery happens in the
|
|
|
|
AbstractUI constructor. Note that if
|
|
|
|
*/
|
2015-12-28 10:14:17 -05:00
|
|
|
|
2023-04-21 15:43:46 -04:00
|
|
|
/* management of the thread_request_buffers map works as
|
|
|
|
* follows:
|
|
|
|
*
|
|
|
|
* An entry will remain in the map after the thread exits.
|
|
|
|
*
|
|
|
|
* The receiving thread may (if it receives requests from other
|
|
|
|
* threads) notice the dead buffer. If it does, it will delete
|
|
|
|
* the request buffer, and call
|
|
|
|
* ::remove_request_buffer_from_map() to get rid of it from the map.
|
|
|
|
*
|
|
|
|
* This does mean that the lifetime of the request buffer is
|
|
|
|
* indeterminate: if the receiving thread were to receive no
|
|
|
|
* further requests, the request buffer will live on
|
|
|
|
* forever. But this is OK, because if there are no requests
|
|
|
|
* arriving, the receiving thread is not attempting to use the
|
|
|
|
* request buffer(s) in any way.
|
|
|
|
*
|
|
|
|
* Note, however, that *if* an emitting thread is recreated
|
|
|
|
* with the same name (e.g. when a control surface is
|
|
|
|
* enabled/disabled/enabled), then the request buffer for the
|
|
|
|
* new thread will replace the map entry for the key, because
|
|
|
|
* of the matching thread names. This does mean that
|
|
|
|
* potentially the request buffer can leak in this case, but
|
|
|
|
* (a) these buffers are not really that large anyway (b) the
|
|
|
|
* scenario is not particularly common (c) the buffers would
|
|
|
|
* typically last across a session instance if not program
|
|
|
|
* lifetime anyway.
|
|
|
|
*/
|
2015-12-28 10:14:17 -05:00
|
|
|
|
2023-04-21 15:43:46 -04:00
|
|
|
thread_buffer_requests.push_back (mapping);
|
|
|
|
DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("pre-registered thread \"%1\"\n", emitting_thread_name));
|
2015-12-28 10:14:17 -05:00
|
|
|
}
|
2016-01-13 23:00:02 -05:00
|
|
|
|
2016-01-14 10:53:32 -05:00
|
|
|
void
|
2023-04-21 15:43:46 -04:00
|
|
|
EventLoop::remove_request_buffer_from_map (pthread_t pth)
|
2016-01-14 10:53:32 -05:00
|
|
|
{
|
2023-04-21 15:43:46 -04:00
|
|
|
Glib::Threads::Mutex::Lock lm (thread_buffer_requests_lock);
|
2016-01-14 10:53:32 -05:00
|
|
|
|
|
|
|
for (ThreadRequestBufferList::iterator x = thread_buffer_requests.begin(); x != thread_buffer_requests.end(); ++x) {
|
2023-04-23 13:26:00 -04:00
|
|
|
if (pthread_equal (x->emitting_thread, pth)) {
|
2016-01-14 10:53:32 -05:00
|
|
|
thread_buffer_requests.erase (x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 14:14:38 -04:00
|
|
|
|