From e19652cf551a04467d2bcdfe613fa8eed86f739b Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 4 Feb 2014 16:51:21 +0000 Subject: [PATCH] Use separate thread for osc input --- source/backend/CarlaEngine.hpp | 5 --- source/backend/engine/CarlaEngine.cpp | 8 ----- source/backend/engine/CarlaEngineOsc.cpp | 38 ++++++++------------- source/backend/engine/CarlaEngineOsc.hpp | 8 +++-- source/backend/engine/CarlaEngineThread.cpp | 3 +- source/backend/engine/Makefile | 2 +- 6 files changed, 22 insertions(+), 42 deletions(-) diff --git a/source/backend/CarlaEngine.hpp b/source/backend/CarlaEngine.hpp index 12ae4bcfc..c03669ba3 100644 --- a/source/backend/CarlaEngine.hpp +++ b/source/backend/CarlaEngine.hpp @@ -983,11 +983,6 @@ public: bool isOscControlRegistered() const noexcept; #endif - /*! - * Idle OSC. - */ - void idleOsc() const noexcept; - /*! * Get OSC TCP server path. */ diff --git a/source/backend/engine/CarlaEngine.cpp b/source/backend/engine/CarlaEngine.cpp index ea27f2372..170db1bbe 100644 --- a/source/backend/engine/CarlaEngine.cpp +++ b/source/backend/engine/CarlaEngine.cpp @@ -1601,7 +1601,6 @@ float CarlaEngine::getOutputPeak(const unsigned int pluginId, const bool isLeft) void CarlaEngine::callback(const EngineCallbackOpcode action, const unsigned int pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept { carla_debug("CarlaEngine::callback(%s, %i, %i, %i, %f, \"%s\")", EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr); - CARLA_ENGINE_THREAD_SAFE_SECTION if (pData->callback != nullptr) { @@ -2013,13 +2012,6 @@ bool CarlaEngine::isOscControlRegistered() const noexcept } #endif -void CarlaEngine::idleOsc() const noexcept -{ - try { - pData->osc.idle(); - } catch(...) {} -} - const char* CarlaEngine::getOscServerPathTCP() const noexcept { return pData->osc.getServerPathTCP(); diff --git a/source/backend/engine/CarlaEngineOsc.cpp b/source/backend/engine/CarlaEngineOsc.cpp index 5f4b33887..ea2783f57 100644 --- a/source/backend/engine/CarlaEngineOsc.cpp +++ b/source/backend/engine/CarlaEngineOsc.cpp @@ -71,32 +71,34 @@ void CarlaEngineOsc::init(const char* const name) fName = name; fName.toBasic(); - fServerTCP = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP); + fServerTCP = lo_server_thread_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP); if (fServerTCP != nullptr) { - if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP)) + if (char* const tmpServerPathTCP = lo_server_thread_get_url(fServerTCP)) { fServerPathTCP = tmpServerPathTCP; fServerPathTCP += fName; std::free(tmpServerPathTCP); } - lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this); + lo_server_thread_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this); + lo_server_thread_start(fServerTCP); } - fServerUDP = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP); + fServerUDP = lo_server_thread_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP); if (fServerUDP != nullptr) { - if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP)) + if (char* const tmpServerPathUDP = lo_server_thread_get_url(fServerUDP)) { fServerPathUDP = tmpServerPathUDP; fServerPathUDP += fName; std::free(tmpServerPathUDP); } - lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this); + lo_server_thread_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this); + lo_server_thread_start(fServerUDP); } CARLA_ASSERT(fName.isNotEmpty()); @@ -106,19 +108,6 @@ void CarlaEngineOsc::init(const char* const name) CARLA_SAFE_ASSERT(fServerUDP != nullptr); } -void CarlaEngineOsc::idle() const -{ - if (fServerTCP != nullptr) - { - while (lo_server_recv_noblock(fServerTCP, 0) != 0) {} - } - - if (fServerUDP != nullptr) - { - while (lo_server_recv_noblock(fServerUDP, 0) != 0) {} - } -} - void CarlaEngineOsc::close() { CARLA_ASSERT(fName.isNotEmpty()); @@ -132,15 +121,17 @@ void CarlaEngineOsc::close() if (fServerTCP != nullptr) { - lo_server_del_method(fServerTCP, nullptr, nullptr); - lo_server_free(fServerTCP); + lo_server_thread_stop(fServerTCP); + lo_server_thread_del_method(fServerTCP, nullptr, nullptr); + lo_server_thread_free(fServerTCP); fServerTCP = nullptr; } if (fServerUDP != nullptr) { - lo_server_del_method(fServerUDP, nullptr, nullptr); - lo_server_free(fServerUDP); + lo_server_thread_stop(fServerUDP); + lo_server_thread_del_method(fServerUDP, nullptr, nullptr); + lo_server_thread_free(fServerUDP); fServerUDP = nullptr; } @@ -165,6 +156,7 @@ int CarlaEngineOsc::handleMessage(const bool isTCP, const char* const path, cons CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1); CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', 1); carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg); + const CarlaCriticalSection::Scope _ccsl(_cs); if (isTCP) { diff --git a/source/backend/engine/CarlaEngineOsc.hpp b/source/backend/engine/CarlaEngineOsc.hpp index 8e9dad2e8..be3b86269 100644 --- a/source/backend/engine/CarlaEngineOsc.hpp +++ b/source/backend/engine/CarlaEngineOsc.hpp @@ -20,6 +20,7 @@ #include "CarlaBackend.h" #include "CarlaOscUtils.hpp" +#include "CarlaMutex.hpp" #include "CarlaString.hpp" #define CARLA_ENGINE_OSC_HANDLE_ARGS1 CarlaPlugin* const plugin @@ -61,7 +62,6 @@ public: ~CarlaEngineOsc(); void init(const char* const name); - void idle() const; void close(); // ------------------------------------------------------------------- @@ -99,8 +99,10 @@ private: CarlaString fServerPathTCP; CarlaString fServerPathUDP; - lo_server fServerTCP; - lo_server fServerUDP; + lo_server_thread fServerTCP; + lo_server_thread fServerUDP; + + CarlaCriticalSection _cs; #ifndef BUILD_BRIDGE CarlaOscData fControlData; // for carla-control diff --git a/source/backend/engine/CarlaEngineThread.cpp b/source/backend/engine/CarlaEngineThread.cpp index ba3f52b5e..4a9ed41b4 100644 --- a/source/backend/engine/CarlaEngineThread.cpp +++ b/source/backend/engine/CarlaEngineThread.cpp @@ -111,8 +111,7 @@ void CarlaEngineThread::run() } } - fEngine->idleOsc(); - carla_msleep(oscRegisted ? 30 : 50); + carla_msleep(25); } } diff --git a/source/backend/engine/Makefile b/source/backend/engine/Makefile index 49a344cba..f63b67c3c 100644 --- a/source/backend/engine/Makefile +++ b/source/backend/engine/Makefile @@ -8,7 +8,7 @@ include ../Makefile.mk # ---------------------------------------------------------------------------------------------------------------------------- -CARLA_ENGINE_OSC_HPP = CarlaEngineOsc.hpp $(CARLA_BACKEND_H) $(CARLA_OSC_UTILS_HPP) $(CARLA_STRING_HPP) +CARLA_ENGINE_OSC_HPP = CarlaEngineOsc.hpp $(CARLA_BACKEND_H) $(CARLA_OSC_UTILS_HPP) $(CARLA_MUTEX_HPP) $(CARLA_STRING_HPP) CARLA_ENGINE_THREAD_HPP = CarlaEngineThread.hpp $(CARLA_BACKEND_H) $(CARLA_THREAD_HPP) CARLA_ENGINE_INTERNAL_HPP = CarlaEngineInternal.hpp $(CARLA_ENGINE_OSC_HPP) $(CARLA_ENGINE_THREAD_HPP) $(CARLA_ENGINE_HPP) $(CARLA_MUTEX_HPP) $(LINKED_LIST_HPP)