Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defensive countermeasure to bug in Renoise #126

Merged
merged 9 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/detail/vst3/os/osutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,54 @@

#include <atomic>
#include <string>
#include <functional>
#define FMT_HEADER_ONLY 1
#include "fmt/format.h"
#include "fmt/ranges.h"

namespace os
{

class State
{
// the State class ensures that a specific function pair (like init/terminate) is only called in pairs.
// the bool _state reflects if the _on lambda has already been called
// on destruction, it makes sure that the _off lambda is definitely called.
//
// the construct should only be applied to when no state machine can cover this properly
// or your own code depends on correct calling sequences of an external code base and should
// help to implement defensive programming.

public:
State(std::function<void()> on, std::function<void()> off) : _on(on), _off(off)
{
}
~State()
{
off();
}
void on()
{
if (!_state)
{
_on();
}
_state = true;
}
void off()
{
if (_state)
{
_off();
}
_state = false;
}

private:
bool _state = false;
std::function<void()> _on, _off;
};

class IPlugObject
{
public:
Expand Down Expand Up @@ -90,4 +132,4 @@ class fixedqueue
std::atomic_uint32_t _head = 0u;
std::atomic_uint32_t _tail = 0u;
};
}; // namespace util
}; // namespace util
6 changes: 4 additions & 2 deletions src/wrapasvst3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ tresult PLUGIN_API ClapAsVst3::terminate()
{
if (_plugin)
{
_os_attached.off(); // ensure we are detached
_plugin->terminate();
_plugin.reset();
}
Expand Down Expand Up @@ -87,11 +88,12 @@ tresult PLUGIN_API ClapAsVst3::setActive(TBool state)
_expressionmap & clap_supported_note_expressions::AS_VST3_NOTE_EXPRESSION_TUNING);
updateAudioBusses();

os::attach(this);
_os_attached.on();
}
if (!state)
{
os::detach(this);
_os_attached.off();

if (_active)
{
_plugin->deactivate();
Expand Down
2 changes: 2 additions & 0 deletions src/wrapasvst3.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class ClapAsVst3 : public Steinberg::Vst::SingleComponentEffect,
, _library(lib)
, _libraryIndex(number)
, _creationcontext(context)
, _os_attached([this] { os::attach(this); }, [this] { os::detach(this); })
{
}

Expand Down Expand Up @@ -253,6 +254,7 @@ class ClapAsVst3 : public Steinberg::Vst::SingleComponentEffect,

// plugin state
bool _active = false;
os::State _os_attached;
bool _processing = false;
std::mutex _processingLock;
std::atomic_bool _requestedFlush = false;
Expand Down