clap

CLAP Audio Plugin API
Log | Files | Refs | README | LICENSE

commit c19a570e95d065a54da2923defc215743ca536fa
parent 9c6fcf93e4843da792d0c56fe1cddd1c29391098
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date:   Wed, 20 Oct 2021 21:26:18 +0200

Make that code more robust

Diffstat:
Mexamples/common/param-queue.hh | 20+++++++++++---------
Mexamples/plugins/core-plugin.cc | 14+++++++++++++-
2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/examples/common/param-queue.hh b/examples/common/param-queue.hh @@ -14,19 +14,22 @@ public: ParamQueue() = default; void push(const T &value) { - int w; // write element - int wn; // next write element + while (!tryPush(value)) + ; + } + + bool tryPush(const T &value) { + int w = _writeOffset.load(); // write element + int wn = (w + 1) % CAPACITY; // next write element - do { - w = _writeOffset.load(); - wn = (w + 1) % CAPACITY; - } while (wn == _readOffset); // wait until there is space + if (wn == _readOffset) + return false; _data[w] = value; _writeOffset = wn; } - bool tryPop(T& value) { + bool tryPop(T &value) { int r = _readOffset; if (r == _writeOffset) @@ -38,8 +41,7 @@ public: return true; } - void reset() - { + void reset() { _writeOffset = 0; _readOffset = 0; } diff --git a/examples/plugins/core-plugin.cc b/examples/plugins/core-plugin.cc @@ -2,6 +2,8 @@ #include <boost/archive/text_oarchive.hpp> #include <sstream> +#include <chrono> +#include <thread> #include "core-plugin.hh" #include "stream-helper.hh" @@ -171,7 +173,17 @@ namespace clap { } void CorePlugin::guiAdjust(clap_id paramId, double value, clap_event_param_flags flags) { - _guiToPluginQueue.push({paramId, value, flags}); + GuiToPluginValue item{paramId, value, flags}; + while (true) { + // very highly likely to succeed + if (_guiToPluginQueue.tryPush(item)) + return; + + if (canUseParams()) + _hostParams->request_flush(_host); + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } } void CorePlugin::processGuiEvents(const clap_process *process) {