From e0d65117ed487a85eb7a202c9ba0c892230757c6 Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 1 Jan 2015 11:01:34 +0000 Subject: [PATCH] Shared memory win32 implementation --- source/backend/engine/CarlaEngine.cpp | 6 ++- source/utils/CarlaBridgeUtils.hpp | 15 +++++-- source/utils/CarlaSemUtils.hpp | 1 + source/utils/CarlaShmUtils.hpp | 64 ++++++++++++++------------- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/source/backend/engine/CarlaEngine.cpp b/source/backend/engine/CarlaEngine.cpp index 14d4f1377..39ea8903d 100644 --- a/source/backend/engine/CarlaEngine.cpp +++ b/source/backend/engine/CarlaEngine.cpp @@ -364,13 +364,15 @@ bool CarlaEngine::addPlugin(const BinaryType btype, const PluginType ptype, cons if (bridgeBinary.isNotEmpty()) { -# ifndef CARLA_OS_WIN if (btype == BINARY_NATIVE) { +#ifdef CARLA_OS_WIN + bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native.exe"; +#else bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native"; +#endif } else -# endif { switch (btype) { diff --git a/source/utils/CarlaBridgeUtils.hpp b/source/utils/CarlaBridgeUtils.hpp index aa050a5aa..6c9c98633 100644 --- a/source/utils/CarlaBridgeUtils.hpp +++ b/source/utils/CarlaBridgeUtils.hpp @@ -20,10 +20,17 @@ #include "CarlaRingBuffer.hpp" -#define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "/carla-bridge_shm_ap_" -#define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "/carla-bridge_shm_rtC_" -#define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "/carla-bridge_shm_nonrtC_" -#define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "/carla-bridge_shm_nonrtS_" +#ifdef CARLA_OS_WIN +# define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "Global\\carla-bridge_shm_ap_" +# define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "Global\\carla-bridge_shm_rtC_" +# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Global\\carla-bridge_shm_nonrtC_" +# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Global\\carla-bridge_shm_nonrtS_" +#else +# define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "/carla-bridge_shm_ap_" +# define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "/carla-bridge_shm_rtC_" +# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "/carla-bridge_shm_nonrtC_" +# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "/carla-bridge_shm_nonrtS_" +#endif // ----------------------------------------------------------------------- diff --git a/source/utils/CarlaSemUtils.hpp b/source/utils/CarlaSemUtils.hpp index bf1965f11..e93fb1561 100644 --- a/source/utils/CarlaSemUtils.hpp +++ b/source/utils/CarlaSemUtils.hpp @@ -26,6 +26,7 @@ #include #if defined(CARLA_OS_MAC) +# include extern "C" { # include "osx_sem_timedwait.c" }; diff --git a/source/utils/CarlaShmUtils.hpp b/source/utils/CarlaShmUtils.hpp index cc593731f..c28a94008 100644 --- a/source/utils/CarlaShmUtils.hpp +++ b/source/utils/CarlaShmUtils.hpp @@ -21,8 +21,8 @@ #include "CarlaUtils.hpp" #ifdef CARLA_OS_WIN -struct shm_t { HANDLE shm; HANDLE map; }; -# define shm_t_INIT { nullptr, nullptr } +struct shm_t { HANDLE map; bool isServer; const char* filename; }; +# define shm_t_INIT { INVALID_HANDLE_VALUE, true, nullptr } #else # include # include @@ -46,7 +46,7 @@ static inline bool carla_is_shm_valid(const shm_t& shm) noexcept { #ifdef CARLA_OS_WIN - return (shm.shm != nullptr && shm.shm != INVALID_HANDLE_VALUE); + return (shm.filename != nullptr); #else return (shm.fd >= 0); #endif @@ -72,11 +72,12 @@ shm_t carla_shm_create(const char* const filename) noexcept shm_t ret; - try { #ifdef CARLA_OS_WIN - ret.shm = nullptr; // TODO - ret.map = nullptr; + ret.map = INVALID_HANDLE_VALUE; + ret.isServer = true; + ret.filename = carla_strdup_safe(filename); #else + try { ret.fd = ::shm_open(filename, O_CREAT|O_EXCL|O_RDWR, 0600); ret.filename = (ret.fd >= 0) ? carla_strdup_safe(filename) : nullptr; ret.size = 0; @@ -87,12 +88,9 @@ shm_t carla_shm_create(const char* const filename) noexcept ::shm_unlink(filename); ret.fd = -1; } -#endif - } - catch(...) { - carla_safe_exception("carla_shm_create", __FILE__, __LINE__); - ret = gNullCarlaShm; } + CARLA_SAFE_EXCEPTION_RETURN("carla_shm_create", gNullCarlaShm); +#endif return ret; } @@ -107,20 +105,18 @@ shm_t carla_shm_attach(const char* const filename) noexcept shm_t ret; - try { #ifdef CARLA_OS_WIN - ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); - ret.map = nullptr; + ret.map = INVALID_HANDLE_VALUE; + ret.isServer = false; + ret.filename = carla_strdup_safe(filename); #else + try { ret.fd = ::shm_open(filename, O_RDWR, 0); ret.filename = nullptr; ret.size = 0; -#endif - } - catch(...) { - carla_safe_exception("carla_shm_attach", __FILE__, __LINE__); - ret = gNullCarlaShm; } + CARLA_SAFE_EXCEPTION_RETURN("carla_shm_attach", gNullCarlaShm); +#endif return ret; } @@ -133,13 +129,14 @@ void carla_shm_close(shm_t& shm) noexcept { CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),); #ifdef CARLA_OS_WIN - CARLA_SAFE_ASSERT(shm.map == nullptr); + CARLA_SAFE_ASSERT(shm.map == INVALID_HANDLE_VALUE); #endif - try { #ifdef CARLA_OS_WIN - ::CloseHandle(shm.shm); + if (shm.filename != nullptr) + delete[] shm.filename; #else + try { ::close(shm.fd); if (shm.filename != nullptr) @@ -147,8 +144,8 @@ void carla_shm_close(shm_t& shm) noexcept ::shm_unlink(shm.filename); delete[] shm.filename; } -#endif } CARLA_SAFE_EXCEPTION("carla_shm_close"); +#endif shm = gNullCarlaShm; } @@ -163,17 +160,23 @@ void* carla_shm_map(shm_t& shm, const std::size_t size) noexcept CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm), nullptr); CARLA_SAFE_ASSERT_RETURN(size > 0, nullptr); #ifdef CARLA_OS_WIN - CARLA_SAFE_ASSERT_RETURN(shm.map == nullptr, nullptr); + CARLA_SAFE_ASSERT_RETURN(shm.map == INVALID_HANDLE_VALUE, nullptr); #else CARLA_SAFE_ASSERT_RETURN(shm.size == 0, nullptr); #endif try { #ifdef CARLA_OS_WIN - const HANDLE map(::CreateFileMapping(shm.shm, nullptr, PAGE_READWRITE, size, size, nullptr)); - CARLA_SAFE_ASSERT_RETURN(map != nullptr, nullptr); + HANDLE map; + + if (shm.isServer) + map = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE|SEC_COMMIT, 0, size, shm.filename); + else + map = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm.filename); - void* const ptr(::MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size)); + CARLA_SAFE_ASSERT_RETURN(map != INVALID_HANDLE_VALUE, nullptr); + + void* const ptr(::MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, size)); if (ptr == nullptr) { @@ -214,7 +217,7 @@ void carla_shm_unmap(shm_t& shm, void* const ptr) noexcept CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm),); CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,); #ifdef CARLA_OS_WIN - CARLA_SAFE_ASSERT_RETURN(shm.map != nullptr,); + CARLA_SAFE_ASSERT_RETURN(shm.map != INVALID_HANDLE_VALUE,); #else CARLA_SAFE_ASSERT_RETURN(shm.size > 0,); #endif @@ -222,7 +225,7 @@ void carla_shm_unmap(shm_t& shm, void* const ptr) noexcept try { #ifdef CARLA_OS_WIN const HANDLE map(shm.map); - shm.map = nullptr; + shm.map = INVALID_HANDLE_VALUE; ::UnmapViewOfFile(ptr); ::CloseHandle(map); @@ -276,7 +279,8 @@ shm_t carla_shm_create_temp(char* const fileBase) noexcept // file already exists, keep trying #ifdef CARLA_OS_WIN - // TODO + if (::GetLastError() == ERROR_ALREADY_EXISTS) + continue; #else if (errno == EEXIST) continue;