2008-06-02 21:41:35 +00:00
|
|
|
#include <unistd.h>
|
2009-12-22 20:21:43 +00:00
|
|
|
#include <iostream>
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-12-09 03:05:14 +00:00
|
|
|
#include "pbd/stacktrace.h"
|
2009-02-25 18:26:51 +00:00
|
|
|
#include "pbd/abstract_ui.h"
|
|
|
|
|
#include "pbd/pthread_utils.h"
|
|
|
|
|
#include "pbd/failed_constructor.h"
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2009-12-22 20:21:43 +00:00
|
|
|
static void do_not_delete_the_request_buffer (void*) { }
|
|
|
|
|
|
|
|
|
|
template<typename R>
|
|
|
|
|
Glib::StaticPrivate<typename AbstractUI<R>::RequestBuffer> AbstractUI<R>::per_thread_request_buffer;
|
|
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
template <typename RequestObject>
|
2009-12-09 03:05:14 +00:00
|
|
|
AbstractUI<RequestObject>::AbstractUI (const string& name)
|
|
|
|
|
: BaseUI (name)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-12-22 20:21:43 +00:00
|
|
|
void (AbstractUI<RequestObject>::*pmf)(string,pthread_t,string,uint32_t) = &AbstractUI<RequestObject>::register_thread;
|
|
|
|
|
|
|
|
|
|
/* better to make this connect a handler that runs in the UI event loop but the syntax seems hard, and
|
|
|
|
|
register_thread() is thread safe anyway.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
PBD::ThreadCreatedWithRequestSize.connect_same_thread (new_thread_connection, boost::bind (pmf, this, _1, _2, _3, _4));
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename RequestObject> void
|
2009-12-20 16:50:41 +00:00
|
|
|
AbstractUI<RequestObject>::register_thread (string target_gui, pthread_t thread_id, string /*thread name*/, uint32_t num_requests)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-12-09 03:05:14 +00:00
|
|
|
if (target_gui != name()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
RequestBuffer* b = new RequestBuffer (num_requests);
|
|
|
|
|
|
|
|
|
|
{
|
2009-12-09 03:05:14 +00:00
|
|
|
Glib::Mutex::Lock lm (request_buffer_map_lock);
|
2008-06-02 21:41:35 +00:00
|
|
|
request_buffers[thread_id] = b;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-22 20:21:43 +00:00
|
|
|
per_thread_request_buffer.set (b, do_not_delete_the_request_buffer);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename RequestObject> RequestObject*
|
|
|
|
|
AbstractUI<RequestObject>::get_request (RequestType rt)
|
|
|
|
|
{
|
2009-12-09 03:05:14 +00:00
|
|
|
RequestBuffer* rbuf = per_thread_request_buffer.get ();
|
2008-06-02 21:41:35 +00:00
|
|
|
RequestBufferVector vec;
|
|
|
|
|
|
2009-12-09 03:05:14 +00:00
|
|
|
if (rbuf != 0) {
|
|
|
|
|
/* we have a per-thread FIFO, use it */
|
|
|
|
|
|
|
|
|
|
rbuf->get_write_vector (&vec);
|
|
|
|
|
|
|
|
|
|
if (vec.len[0] == 0) {
|
2008-06-02 21:41:35 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2009-12-09 03:05:14 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
vec.buf[0]->type = rt;
|
|
|
|
|
return vec.buf[0];
|
|
|
|
|
}
|
2009-12-09 03:05:14 +00:00
|
|
|
|
|
|
|
|
RequestObject* req = new RequestObject;
|
|
|
|
|
req->type = rt;
|
|
|
|
|
return req;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename RequestObject> void
|
|
|
|
|
AbstractUI<RequestObject>::handle_ui_requests ()
|
|
|
|
|
{
|
|
|
|
|
RequestBufferMapIterator i;
|
2009-12-09 03:05:14 +00:00
|
|
|
RequestBufferVector vec;
|
|
|
|
|
|
|
|
|
|
/* per-thread buffers first */
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
request_buffer_map_lock.lock ();
|
|
|
|
|
|
|
|
|
|
for (i = request_buffers.begin(); i != request_buffers.end(); ++i) {
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
|
|
/* we must process requests 1 by 1 because
|
|
|
|
|
the request may run a recursive main
|
|
|
|
|
event loop that will itself call
|
|
|
|
|
handle_ui_requests. when we return
|
|
|
|
|
from the request handler, we cannot
|
|
|
|
|
expect that the state of queued requests
|
|
|
|
|
is even remotely consistent with
|
|
|
|
|
the condition before we called it.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
i->second->get_read_vector (&vec);
|
|
|
|
|
|
|
|
|
|
if (vec.len[0] == 0) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
request_buffer_map_lock.unlock ();
|
|
|
|
|
do_request (vec.buf[0]);
|
|
|
|
|
request_buffer_map_lock.lock ();
|
|
|
|
|
i->second->increment_read_ptr (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request_buffer_map_lock.unlock ();
|
2009-12-09 03:05:14 +00:00
|
|
|
|
|
|
|
|
/* and now, the generic request buffer. same rules as above apply */
|
|
|
|
|
|
|
|
|
|
Glib::Mutex::Lock lm (request_list_lock);
|
|
|
|
|
|
|
|
|
|
while (!request_list.empty()) {
|
|
|
|
|
RequestObject* req = request_list.front ();
|
|
|
|
|
request_list.pop_front ();
|
|
|
|
|
lm.release ();
|
|
|
|
|
|
|
|
|
|
do_request (req);
|
|
|
|
|
|
|
|
|
|
delete req;
|
|
|
|
|
|
|
|
|
|
lm.acquire();
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename RequestObject> void
|
|
|
|
|
AbstractUI<RequestObject>::send_request (RequestObject *req)
|
|
|
|
|
{
|
|
|
|
|
if (base_instance() == 0) {
|
|
|
|
|
return; /* XXX is this the right thing to do ? */
|
|
|
|
|
}
|
2009-12-09 03:05:14 +00:00
|
|
|
|
|
|
|
|
if (caller_is_self ()) {
|
2008-06-02 21:41:35 +00:00
|
|
|
do_request (req);
|
|
|
|
|
} else {
|
2009-12-09 03:05:14 +00:00
|
|
|
RequestBuffer* rbuf = per_thread_request_buffer.get ();
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-12-09 03:05:14 +00:00
|
|
|
if (rbuf != 0) {
|
|
|
|
|
rbuf->increment_write_ptr (1);
|
|
|
|
|
} else {
|
|
|
|
|
/* no per-thread buffer, so just use a list with a lock so that it remains
|
|
|
|
|
single-reader/single-writer semantics
|
2008-06-02 21:41:35 +00:00
|
|
|
*/
|
2009-12-09 03:05:14 +00:00
|
|
|
Glib::Mutex::Lock lm (request_list_lock);
|
|
|
|
|
request_list.push_back (req);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
2009-12-09 03:05:14 +00:00
|
|
|
request_channel.wakeup ();
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-09 03:05:14 +00:00
|
|
|
template<typename RequestObject> void
|
2009-12-11 23:30:48 +00:00
|
|
|
AbstractUI<RequestObject>::call_slot (const boost::function<void()>& f)
|
2009-12-09 03:05:14 +00:00
|
|
|
{
|
|
|
|
|
if (caller_is_self()) {
|
2009-12-22 20:21:43 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
if (getenv ("DEBUG_THREADED_SIGNALS")) {
|
|
|
|
|
std::cerr << "functor called in correct thread for " << name() << " , execute ...\n";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2009-12-11 23:30:48 +00:00
|
|
|
f ();
|
2009-12-09 03:05:14 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RequestObject *req = get_request (BaseUI::CallSlot);
|
|
|
|
|
|
|
|
|
|
if (req == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 23:30:48 +00:00
|
|
|
req->the_slot = f;
|
2009-12-22 20:21:43 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
if (getenv ("DEBUG_THREADED_SIGNALS")) {
|
|
|
|
|
std::cerr << "functor called in wrong thread for " << name() << " (from " << pthread_name() << ") send request ...\n";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2009-12-09 03:05:14 +00:00
|
|
|
send_request (req);
|
|
|
|
|
}
|
|
|
|
|
|