From f5f6f0401631428239e1f85780e054f7063d2a35 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 20 Jun 2020 15:47:30 +0200 Subject: [PATCH] Rearrange the try-catch in the thread handlers It's a bit clearer this way. I would prefer using jthreads here, but we would still need this try-catch block since there's no way to cancel synchronous Boost.Asio socket operations other than closing the socket. --- src/plugin/plugin-bridge.cpp | 11 +++++---- src/wine-host/bridges/vst2.cpp | 44 ++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/plugin/plugin-bridge.cpp b/src/plugin/plugin-bridge.cpp index 93b75eca..59f6df3a 100644 --- a/src/plugin/plugin-bridge.cpp +++ b/src/plugin/plugin-bridge.cpp @@ -132,8 +132,8 @@ PluginBridge::PluginBridge(audioMasterCallback host_callback) // instead of asynchronous IO since communication has to be handled in // lockstep anyway host_callback_handler = std::thread([&]() { - try { - while (true) { + while (true) { + try { // TODO: Think of a nicer way to structure this and the similar // handler in `Vst2Bridge::handle_dispatch_midi_events` receive_event( @@ -159,10 +159,11 @@ PluginBridge::PluginBridge(audioMasterCallback host_callback) &plugin, host_callback_function)(event); } }); + } catch (const boost::system::system_error&) { + // This happens when the sockets got closed because the plugin + // is being shut down + break; } - } catch (const boost::system::system_error&) { - // This happens when the sockets got closed because the plugin - // is being shut down } }); diff --git a/src/wine-host/bridges/vst2.cpp b/src/wine-host/bridges/vst2.cpp index 7fd7379c..9d24ba59 100644 --- a/src/wine-host/bridges/vst2.cpp +++ b/src/wine-host/bridges/vst2.cpp @@ -149,8 +149,8 @@ bool Vst2Bridge::should_skip_message_loop() const { } void Vst2Bridge::handle_dispatch() { - try { - while (true) { + while (true) { + try { receive_event( host_vst_dispatch, std::nullopt, passthrough_event( @@ -174,16 +174,17 @@ void Vst2Bridge::handle_dispatch() { // separately on a timer return dispatch_result.get_future().get(); })); + } catch (const boost::system::system_error&) { + // The plugin has cut off communications, so we can shut down this + // host application + break; } - } catch (const boost::system::system_error&) { - // The plugin has cut off communications, so we can shut down this - // host application } } void Vst2Bridge::handle_dispatch_midi_events() { - try { - while (true) { + while (true) { + try { receive_event( host_vst_dispatch_midi_events, std::nullopt, [&](Event& event) { if (BOOST_LIKELY(event.opcode == effProcessEvents)) { @@ -230,16 +231,17 @@ void Vst2Bridge::handle_dispatch_midi_events() { _2, _3, _4, _5, _6))(event); } }); + } catch (const boost::system::system_error&) { + // The plugin has cut off communications, so we can shut down this + // host application + break; } - } catch (const boost::system::system_error&) { - // The plugin has cut off communications, so we can shut down this host - // application } } void Vst2Bridge::handle_parameters() { - try { - while (true) { + while (true) { + try { // Both `getParameter` and `setParameter` functions are passed // through on this socket since they have a lot of overlap. The // presence of the `value` field tells us which one we're dealing @@ -258,18 +260,19 @@ void Vst2Bridge::handle_parameters() { ParameterResult response{value}; write_object(host_vst_parameters, response); } + } catch (const boost::system::system_error&) { + // The plugin has cut off communications, so we can shut down this + // host application + break; } - } catch (const boost::system::system_error&) { - // The plugin has cut off communications, so we can shut down this host - // application } } void Vst2Bridge::handle_process_replacing() { std::vector> output_buffers(plugin->numOutputs); - try { - while (true) { + while (true) { + try { auto request = read_object(host_vst_process_replacing, process_buffer); @@ -320,10 +323,11 @@ void Vst2Bridge::handle_process_replacing() { AudioBuffers response{output_buffers, request.sample_frames}; write_object(host_vst_process_replacing, response, process_buffer); + } catch (const boost::system::system_error&) { + // The plugin has cut off communications, so we can shut down this + // host application + break; } - } catch (const boost::system::system_error&) { - // The plugin has cut off communications, so we can shut down this host - // application } }