diff options
author | David Robillard <d@drobilla.net> | 2018-01-21 20:52:32 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-01-22 00:20:19 +0100 |
commit | d9c5d89d230b204a90cca4dee4165ba6ebdf00fd (patch) | |
tree | bfef33474927a6c22f723fcc849d8df801f618e9 /ingen/QueuedInterface.hpp | |
parent | 7150b9bc10511e17abdd6e528fd1317522f64eae (diff) | |
download | ingen-d9c5d89d230b204a90cca4dee4165ba6ebdf00fd.tar.gz ingen-d9c5d89d230b204a90cca4dee4165ba6ebdf00fd.tar.bz2 ingen-d9c5d89d230b204a90cca4dee4165ba6ebdf00fd.zip |
Only enqueue messages when the engine is remote
When the engine is local, messages are emitted in the Gtk thread and applied
immediately. This should make the GUI more responsive.
Diffstat (limited to 'ingen/QueuedInterface.hpp')
-rw-r--r-- | ingen/QueuedInterface.hpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/ingen/QueuedInterface.hpp b/ingen/QueuedInterface.hpp new file mode 100644 index 00000000..bf424edd --- /dev/null +++ b/ingen/QueuedInterface.hpp @@ -0,0 +1,66 @@ +/* + This file is part of Ingen. + Copyright 2018 David Robillard <http://drobilla.net/> + + Ingen is free software: you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free + Software Foundation, either version 3 of the License, or any later version. + + Ingen is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for details. + + You should have received a copy of the GNU Affero General Public License + along with Ingen. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef INGEN_ENGINE_QUEUEDINTERFACE_HPP +#define INGEN_ENGINE_QUEUEDINTERFACE_HPP + +#include <mutex> +#include <vector> + +#include "ingen/Interface.hpp" +#include "ingen/Message.hpp" + +namespace Ingen { + +/** Stores all messages and emits them to a sink on demand. + * + * This can be used to make an interface thread-safe. + */ +class QueuedInterface : public Interface +{ +public: + explicit QueuedInterface(SPtr<Interface> sink) : _sink(std::move(sink)) {} + + URI uri() const override { return URI("ingen:/QueuedInterface"); } + + void message(const Message& message) override { + std::lock_guard<std::mutex> lock(_mutex); + _messages.emplace_back(message); + } + + void emit() { + std::vector<Message> messages; + { + std::lock_guard<std::mutex> lock(_mutex); + _messages.swap(messages); + } + + for (const auto& i : messages) { + _sink->message(i); + } + } + + const SPtr<Interface>& sink() const { return _sink; } + +private: + std::mutex _mutex; + SPtr<Interface> _sink; + std::vector<Message> _messages; +}; + +} // namespace Ingen + +#endif // INGEN_ENGINE_QUEUEDINTERFACE_HPP |