From 58482013029de9a8691187ae662af07549e56c06 Mon Sep 17 00:00:00 2001 From: falkTX Date: Fri, 8 Aug 2014 18:49:50 +0100 Subject: [PATCH] plugin-bridges now working for OSX --- source/modules/jackbridge/JackBridge2.cpp | 54 ++++++++++++++--------- source/tests/Makefile | 6 +++ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/source/modules/jackbridge/JackBridge2.cpp b/source/modules/jackbridge/JackBridge2.cpp index 86fa36950..7b1ac4e7a 100644 --- a/source/modules/jackbridge/JackBridge2.cpp +++ b/source/modules/jackbridge/JackBridge2.cpp @@ -79,23 +79,37 @@ void* jackbridge_shm_map(void*, size_t) noexcept } #else //JACKBRIDGE_DUMMY +#include #include #include #include +#include #ifdef CARLA_OS_MAC -# include -#else -# include +extern "C" { +#include "osx_sem_timedwait.c" +} #endif +// TODO - check noexcept on OSX + bool jackbridge_sem_init(void* sem) noexcept { #ifdef CARLA_OS_MAC - const dispatch_semaphore_t sema = dispatch_semaphore_create(0); - //std::memcpy(sem, sema, sizeof(dispatch_semaphore_t)); - *(dispatch_semaphore_t*)sem = sema; - return sema != nullptr; + static ulong sCounter = 0; + ++sCounter; + + std::srand(static_cast(std::time(nullptr))); + + char strBuf[0xff+1]; + carla_zeroChar(strBuf, 0xff+1); + std::sprintf(strBuf, "carla-sem-%i-%li", std::rand(), sCounter); + + sem_unlink(strBuf); + + sem_t* const sema = sem_open(strBuf, O_CREAT, O_RDWR, 0); + std::memcpy(sem, &sema, sizeof(sem_t*)); + return (sema != nullptr && sema != SEM_FAILED); #else return (sem_init((sem_t*)sem, 1, 0) == 0); #endif @@ -104,8 +118,7 @@ bool jackbridge_sem_init(void* sem) noexcept bool jackbridge_sem_destroy(void* sem) noexcept { #ifdef CARLA_OS_MAC - dispatch_release(*(dispatch_semaphore_t*)sem); - return true; + return (sem_close(*(sem_t**)sem) == 0); #else return (sem_destroy((sem_t*)sem) == 0); #endif @@ -114,36 +127,37 @@ bool jackbridge_sem_destroy(void* sem) noexcept bool jackbridge_sem_post(void* sem) noexcept { #ifdef CARLA_OS_MAC - return (dispatch_semaphore_signal(*(dispatch_semaphore_t*)sem) == 0); + sem_t* const sema = *(sem_t**)sem; #else - return (sem_post((sem_t*)sem) == 0); + sem_t* const sema = (sem_t*)sem; #endif + return (sem_post(sema) == 0); } bool jackbridge_sem_timedwait(void* sem, int secs) noexcept { CARLA_SAFE_ASSERT_RETURN(secs > 0, false); - #ifdef CARLA_OS_MAC - const dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, secs*1000); - return (dispatch_semaphore_wait(*(dispatch_semaphore_t*)sem, timeout) == 0); + sem_t* const sema = *(sem_t**)sem; #else + sem_t* const sema = (sem_t*)sem; +#endif + timespec timeout; -# ifdef CARLA_OS_WIN +#ifdef CARLA_OS_LINUX + clock_gettime(CLOCK_REALTIME, &timeout); +#else timeval now; gettimeofday(&now, nullptr); timeout.tv_sec = now.tv_sec; timeout.tv_nsec = now.tv_usec * 1000; -# else - clock_gettime(CLOCK_REALTIME, &timeout); -# endif +#endif timeout.tv_sec += secs; try { - return (sem_timedwait((sem_t*)sem, &timeout) == 0); + return (sem_timedwait(sema, &timeout) == 0); } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false); -#endif } bool jackbridge_shm_is_valid(const void* shm) noexcept diff --git a/source/tests/Makefile b/source/tests/Makefile index 84f573b65..99b11bcb3 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -105,6 +105,12 @@ RDF: RDF.cpp ../modules/ladspa_rdf.hpp ../modules/lv2_rdf.hpp # -------------------------------------------------------------- +sem: sem.cpp ../modules/jackbridge/* + $(CXX) $< -Wall -Wextra -O0 -g -I../utils -I../includes -DREAL_BUILD -o $@ + ./$@ + +# -------------------------------------------------------------- + ChildProcess: ChildProcess.cpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) ../modules/juce_core.a -ldl -lpthread -lrt -o $@ valgrind --leak-check=full ./$@