Significantly clean up mutual recursion workaround

This has much fewer moving parts, and it's probably more understandable.
There was also a race condition in the previous implementation, so
there's that.
This commit is contained in:
Robbert van der Helm
2020-12-23 15:31:32 +01:00
parent eae77d4dbf
commit 7a55fc3ec0
5 changed files with 146 additions and 191 deletions
@@ -47,80 +47,11 @@ Vst3PlugFrameProxyImpl::resizeView(Steinberg::IPlugView* /*view*/,
// assume `view` is the `IPlugView*` returned by the last call to
// `IEditController::createView()`
// HACK: This ia bit of a weird one and requires special handling. A
// plugin will call this function from the Win32 message loop so
// the call blocks the loop. The host will then check with the
// plugin if it can actually resize itself to `*newSize`, and it
// will then call `IPlugView::onSize()` with the new size. The
// issue is that the `IPlugView::onSize()` call also has to be
// called from within the UI thread, but that thread is currently
// being blocked by the call to this function.
// As a workaround, we'll send the message for the call to
// `IPlugFrame::resizeView()` on another thread. We then wait for
// either that request to finish immediately (meaning the host
// hasn't resized the window), or for `IPlugView::onSize()` to be
// called by the host. If the host does call `IPlugView::onSize()`
// while the other thread is handling `IPlugFrame::resizeView()`,
// then we'll awaken this thread using a condition variable so we
// can do the actual call to `IPlugView::onSize()` from here, from
// within the Win32 loop.
// TODO: Can we someone use Boost.Asio strands to make this cleaner?
{
std::lock_guard lock(on_size_interrupt_mutex);
on_size_interrupt_waiting = true;
on_size_interrupt.reset();
on_size_interrupt_result.reset();
}
std::promise<tresult> resize_result_promise{};
std::future<tresult> resize_result_future =
resize_result_promise.get_future();
Win32Thread resize_thread([&]() {
const tresult result = bridge.send_message(YaPlugFrame::ResizeView{
.owner_instance_id = owner_instance_id(),
.new_size = *newSize});
resize_result_promise.set_value(result);
{
std::lock_guard lock(on_size_interrupt_mutex);
if (BOOST_LIKELY(!on_size_interrupt_waiting)) {
return;
}
// If the call to `IPlugFrame::resizeView()` finish without the
// host calling `IPlugView::onSize()` (e.g. when the call
// failed), then we'll have to manually unblock the thread below
// by providing a noop function.
on_size_interrupt = []() { return Steinberg::kResultOk; };
}
on_size_interrupt_cv.notify_one();
// We don't need this result value, but we should still wait for
// it
std::unique_lock lock(on_size_interrupt_mutex);
on_size_interrupt_cv.wait(
lock, [&]() { return on_size_interrupt_result.has_value(); });
});
// Wait for `IPlugView::onSize()` to be called by the host and handle
// the call here on this thread, or wait for the call to
// `IPlugFrame::resizeView()` in the above thread to finish. In the last
// case we'll execute a noop function.
std::unique_lock lock(on_size_interrupt_mutex);
on_size_interrupt_cv.wait(
lock, [&]() { return on_size_interrupt.has_value(); });
// When we get a function through one of the two above means, we'll
// store the result and then awake the calling thread again so it can
// access the result
on_size_interrupt_result = (*on_size_interrupt)();
on_size_interrupt_waiting = false;
lock.unlock();
on_size_interrupt_cv.notify_one();
return resize_result_future.get();
// We have to use this special sending function here so we can handle
// calls to `IPlugView::onSize()` from this same thread (the UI thread).
// See the docstring for more information.
return bridge.send_mutually_recursive_message(YaPlugFrame::ResizeView{
.owner_instance_id = owner_instance_id(), .new_size = *newSize});
} else {
std::cerr
<< "WARNING: Null pointer passed to 'IPlugFrame::resizeView()'"