Skip to content

Commit

Permalink
src/core/vscore.cpp, vscore.h: fix data race with concurrent dynamic …
Browse files Browse the repository at this point in the history
…filter creations

also introduce $VSC_DISABLE_LAZY_PLUGIN.

Signed-off-by: akarin <[email protected]>
  • Loading branch information
AkarinVS committed Feb 14, 2023
1 parent 0e7a79e commit 5cec5ab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/core/vscore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ void VSPlugin::load(const std::string &relFilename, bool lazy) {
throw VSException("Core only supports API R" + std::to_string(VAPOURSYNTH_API_MAJOR) + "." + std::to_string(VAPOURSYNTH_API_MINOR) + " but the loaded plugin requires API R" + std::to_string(apiMajor) + "." + std::to_string(apiMinor) + "; Filename: " + relFilename + "; Name: " + fullname);
}

if (lazy) {
if (lazy && getenv("VSC_DISABLE_LAZY_PLUGIN") == nullptr) {
unload(true);
hasConfig = false;
readOnly = false;
Expand Down Expand Up @@ -2600,8 +2600,11 @@ bool VSPlugin::registerFunction(const std::string &name, const std::string &args
VSMap *VSPlugin::invoke(const std::string &funcName, const VSMap &args) {
auto it = funcs.find(funcName);
if (it != funcs.end()) {
if (it->second.isLazy())
load(filename, false);
std::call_once(lazyOnce, [&it, this]() {
if (it->second.isLazy()) {
load(filename, false);
}
});
return it->second.invoke(args);
} else {
VSMap *v = new VSMap();
Expand Down
6 changes: 4 additions & 2 deletions src/core/vscore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ struct VSPluginFunction {
std::string getV3ArgString() const;
bool rename(const std::string &newname); // 'vs-c'

// 'vs-c' lazy plugin support
void setLazy() { func = nullptr; functionData = nullptr; }
bool isLazy() const { return func == nullptr; }
void setFunc(VSPublicFunction f, void *d) { func = f; functionData = d; }
Expand Down Expand Up @@ -1036,11 +1037,12 @@ struct VSPlugin {
std::map<std::string, VSPluginFunction> funcs;
std::mutex functionLock;
VSCore *core;
std::once_flag lazyOnce;
public:
explicit VSPlugin(VSCore *core);
VSPlugin(const std::string &relFilename, const std::string &forcedNamespace, const std::string &forcedId, bool altSearchPath, VSCore *core, bool lazy = false);
void load(const std::string &relFilename, bool lazy = false);
void unload(bool lazy = false);
void load(const std::string &relFilename, bool lazy = false); // 'vs-c'
void unload(bool lazy = false); // 'vs-c'
~VSPlugin();
void lock() { readOnly = true; }
void unlock() { readOnly = false; } // 'vs-c'
Expand Down

0 comments on commit 5cec5ab

Please sign in to comment.