@@ -151,6 +151,9 @@ struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> { | |||
{ | |||
CARLA_SAFE_ASSERT(data->midiOut[0] == 0); | |||
setRingBuffer(&data->ringBuffer, false); | |||
CARLA_SAFE_ASSERT_RETURN(jackbridge_sem_connect(&data->sem.server), false); | |||
CARLA_SAFE_ASSERT_RETURN(jackbridge_sem_connect(&data->sem.client), false); | |||
return true; | |||
} | |||
@@ -175,12 +178,12 @@ struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> { | |||
WaitHelper(BridgeRtClientControl& c) noexcept | |||
: data(c.data), | |||
ok(jackbridge_sem_timedwait(&data->sem.server, 5000)) {} | |||
ok(jackbridge_sem_timedwait(&data->sem.server, 5000, false)) {} | |||
~WaitHelper() noexcept | |||
{ | |||
if (ok) | |||
jackbridge_sem_post(&data->sem.client); | |||
jackbridge_sem_post(&data->sem.client, false); | |||
} | |||
CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper) | |||
@@ -244,9 +244,9 @@ struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> { | |||
{ | |||
CARLA_SAFE_ASSERT_RETURN(data != nullptr, false); | |||
jackbridge_sem_post(&data->sem.server); | |||
jackbridge_sem_post(&data->sem.server, true); | |||
return jackbridge_sem_timedwait(&data->sem.client, msecs); | |||
return jackbridge_sem_timedwait(&data->sem.client, msecs, true); | |||
} | |||
void writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept | |||
@@ -396,8 +396,9 @@ JACKBRIDGE_API bool jackbridge_set_property_change_callback(jack_client_t* clien | |||
JACKBRIDGE_API bool jackbridge_sem_init(void* sem) noexcept; | |||
JACKBRIDGE_API void jackbridge_sem_destroy(void* sem) noexcept; | |||
JACKBRIDGE_API void jackbridge_sem_post(void* sem) noexcept; | |||
JACKBRIDGE_API bool jackbridge_sem_timedwait(void* sem, uint msecs) noexcept; | |||
JACKBRIDGE_API bool jackbridge_sem_connect(void* sem) noexcept; | |||
JACKBRIDGE_API void jackbridge_sem_post(void* sem, bool server) noexcept; | |||
JACKBRIDGE_API bool jackbridge_sem_timedwait(void* sem, uint msecs, bool server) noexcept; | |||
JACKBRIDGE_API bool jackbridge_shm_is_valid(const void* shm) noexcept; | |||
JACKBRIDGE_API void jackbridge_shm_init(void* shm) noexcept; | |||
@@ -43,23 +43,34 @@ void jackbridge_sem_destroy(void* sem) noexcept | |||
#endif | |||
} | |||
void jackbridge_sem_post(void* sem) noexcept | |||
bool jackbridge_sem_connect(void* sem) noexcept | |||
{ | |||
CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false); | |||
#ifdef JACKBRIDGE_DUMMY | |||
return false; | |||
#else | |||
return carla_sem_connect(*(carla_sem_t*)sem); | |||
#endif | |||
} | |||
void jackbridge_sem_post(void* sem, bool server) noexcept | |||
{ | |||
CARLA_SAFE_ASSERT_RETURN(sem != nullptr,); | |||
#ifndef JACKBRIDGE_DUMMY | |||
carla_sem_post(*(carla_sem_t*)sem); | |||
carla_sem_post(*(carla_sem_t*)sem, server); | |||
#endif | |||
} | |||
bool jackbridge_sem_timedwait(void* sem, uint msecs) noexcept | |||
bool jackbridge_sem_timedwait(void* sem, uint msecs, bool server) noexcept | |||
{ | |||
CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false); | |||
#ifdef JACKBRIDGE_DUMMY | |||
return false; | |||
#else | |||
return carla_sem_timedwait(*(carla_sem_t*)sem, msecs); | |||
return carla_sem_timedwait(*(carla_sem_t*)sem, msecs, server); | |||
#endif | |||
} | |||
@@ -128,6 +128,7 @@ const JackBridgeExportedFunctions* JACKBRIDGE_API jackbridge_get_exported_functi | |||
funcs.set_property_change_callback_ptr = jackbridge_set_property_change_callback; | |||
funcs.sem_init_ptr = jackbridge_sem_init; | |||
funcs.sem_destroy_ptr = jackbridge_sem_destroy; | |||
funcs.sem_connect_ptr = jackbridge_sem_connect; | |||
funcs.sem_post_ptr = jackbridge_sem_post; | |||
funcs.sem_timedwait_ptr = jackbridge_sem_timedwait; | |||
funcs.shm_is_valid_ptr = jackbridge_shm_is_valid; | |||
@@ -543,12 +543,17 @@ void jackbridge_sem_destroy(void* sem) noexcept | |||
getBridgeInstance().sem_destroy_ptr(sem); | |||
} | |||
void jackbridge_sem_post(void* sem) noexcept | |||
bool jackbridge_sem_connect(void* sem) noexcept | |||
{ | |||
return getBridgeInstance().sem_connect_ptr(sem); | |||
} | |||
void jackbridge_sem_post(void* sem, bool server) noexcept | |||
{ | |||
getBridgeInstance().sem_post_ptr(sem); | |||
} | |||
bool jackbridge_sem_timedwait(void* sem, uint msecs) noexcept | |||
bool jackbridge_sem_timedwait(void* sem, uint msecs, bool server) noexcept | |||
{ | |||
return getBridgeInstance().sem_timedwait_ptr(sem, msecs); | |||
} | |||
@@ -111,8 +111,9 @@ typedef bool (JACKBRIDGE_API *jackbridgesym_remove_all_properties)(jack_client_t | |||
typedef bool (JACKBRIDGE_API *jackbridgesym_set_property_change_callback)(jack_client_t*, JackPropertyChangeCallback, void*); | |||
typedef bool (JACKBRIDGE_API *jackbridgesym_sem_init)(void*); | |||
typedef void (JACKBRIDGE_API *jackbridgesym_sem_destroy)(void*); | |||
typedef void (JACKBRIDGE_API *jackbridgesym_sem_post)(void*); | |||
typedef bool (JACKBRIDGE_API *jackbridgesym_sem_timedwait)(void*, uint); | |||
typedef bool (JACKBRIDGE_API *jackbridgesym_sem_connect)(void*); | |||
typedef void (JACKBRIDGE_API *jackbridgesym_sem_post)(void*, bool); | |||
typedef bool (JACKBRIDGE_API *jackbridgesym_sem_timedwait)(void*, uint, bool); | |||
typedef bool (JACKBRIDGE_API *jackbridgesym_shm_is_valid)(const void*); | |||
typedef void (JACKBRIDGE_API *jackbridgesym_shm_init)(void*); | |||
typedef void (JACKBRIDGE_API *jackbridgesym_shm_attach)(void*, const char*); | |||
@@ -216,6 +217,7 @@ struct _JackBridgeExportedFunctions { | |||
jackbridgesym_set_property_change_callback set_property_change_callback_ptr; | |||
jackbridgesym_sem_init sem_init_ptr; | |||
jackbridgesym_sem_destroy sem_destroy_ptr; | |||
jackbridgesym_sem_connect sem_connect_ptr; | |||
jackbridgesym_sem_post sem_post_ptr; | |||
jackbridgesym_sem_timedwait sem_timedwait_ptr; | |||
jackbridgesym_shm_is_valid shm_is_valid_ptr; | |||
@@ -26,10 +26,10 @@ | |||
# 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_" | |||
# define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "/crlbrdg_shm_ap_" | |||
# define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "/crlbrdg_shm_rtC_" | |||
# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "/crlbrdg_shm_nonrtC_" | |||
# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "/crlbrdg_shm_nonrtS_" | |||
#endif | |||
// ----------------------------------------------------------------------- | |||
@@ -30,8 +30,10 @@ | |||
# endif | |||
struct carla_sem_t { HANDLE handle; }; | |||
#elif defined(CARLA_OS_MAC) | |||
// TODO | |||
struct carla_sem_t { char dummy; }; | |||
# include <mach/mach.h> | |||
# include <mach/semaphore.h> | |||
# include <servers/bootstrap.h> | |||
struct carla_sem_t { char bootname[32]; semaphore_t sem; semaphore_t sem2; }; | |||
#elif defined(CARLA_USE_FUTEXES) | |||
# include <cerrno> | |||
# include <syscall.h> | |||
@@ -63,7 +65,23 @@ bool carla_sem_create2(carla_sem_t& sem) noexcept | |||
return (sem.handle != INVALID_HANDLE_VALUE); | |||
#elif defined(CARLA_OS_MAC) | |||
return false; // TODO | |||
const mach_port_t task = mach_task_self(); | |||
mach_port_t bootport; | |||
CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport) == KERN_SUCCESS, false); | |||
if (::semaphore_create(task, &sem.sem, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS) | |||
{ | |||
static int bootcounter = 0; | |||
std::snprintf(sem.bootname, 31, "crlsm_%i_%i_%p", ++bootcounter, getpid(), &sem); | |||
if (bootstrap_register(bootport, sem.bootname, sem.sem) == KERN_SUCCESS) | |||
return true; | |||
::semaphore_destroy(task, sem.sem); | |||
} | |||
return false; | |||
#elif defined(CARLA_USE_FUTEXES) | |||
sem.count = 0; | |||
return true; | |||
@@ -98,13 +116,13 @@ void carla_sem_destroy2(carla_sem_t& sem) noexcept | |||
#if defined(CARLA_OS_WIN) | |||
::CloseHandle(sem.handle); | |||
#elif defined(CARLA_OS_MAC) | |||
// TODO | |||
::semaphore_destroy(mach_task_self(), sem.sem); | |||
#elif defined(CARLA_USE_FUTEXES) | |||
// nothing to do | |||
(void)sem; | |||
#else | |||
::sem_destroy(&sem.sem); | |||
#endif | |||
carla_zeroStruct(sem); | |||
} | |||
/* | |||
@@ -119,16 +137,39 @@ void carla_sem_destroy(carla_sem_t* const sem) noexcept | |||
std::free(sem); | |||
} | |||
/* | |||
* Connect to semaphore. | |||
*/ | |||
static inline | |||
bool carla_sem_connect(carla_sem_t& sem) noexcept | |||
{ | |||
#ifdef CARLA_OS_MAC | |||
mach_port_t bootport; | |||
CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(mach_task_self(), &bootport) == KERN_SUCCESS, false); | |||
try { | |||
return (bootstrap_look_up(bootport, sem.bootname, &sem.sem2) == KERN_SUCCESS); | |||
} CARLA_SAFE_EXCEPTION_RETURN("carla_sem_connect", false); | |||
#else | |||
// nothing to do | |||
return true; | |||
// unused | |||
(void)sem; | |||
#endif | |||
} | |||
/* | |||
* Post semaphore (unlock). | |||
*/ | |||
static inline | |||
void carla_sem_post(carla_sem_t& sem) noexcept | |||
void carla_sem_post(carla_sem_t& sem, const bool server) noexcept | |||
{ | |||
#ifdef CARLA_OS_WIN | |||
::ReleaseSemaphore(sem.handle, 1, nullptr); | |||
#elif defined(CARLA_OS_MAC) | |||
// TODO | |||
try { | |||
::semaphore_signal(server ? sem.sem : sem.sem2); | |||
} CARLA_SAFE_EXCEPTION_RETURN("carla_sem_post",); | |||
#elif defined(CARLA_USE_FUTEXES) | |||
const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1); | |||
CARLA_SAFE_ASSERT_RETURN(unlocked,); | |||
@@ -142,18 +183,24 @@ void carla_sem_post(carla_sem_t& sem) noexcept | |||
* Wait for a semaphore (lock). | |||
*/ | |||
static inline | |||
bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs) noexcept | |||
bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs, const bool server) noexcept | |||
{ | |||
CARLA_SAFE_ASSERT_RETURN(msecs > 0, false); | |||
#if defined(CARLA_OS_WIN) | |||
return (::WaitForSingleObject(sem.handle, msecs) == WAIT_OBJECT_0); | |||
#elif defined(CARLA_OS_MAC) | |||
return false; // TODO | |||
#elif defined(CARLA_USE_FUTEXES) | |||
timespec timeout; | |||
timeout.tv_sec = static_cast<time_t>(msecs / 1000); | |||
timeout.tv_nsec = static_cast<time_t>(msecs % 1000); | |||
#else | |||
const uint secs = msecs / 1000; | |||
const uint nsecs = (msecs % 1000) * 1000000; | |||
# if defined(CARLA_OS_MAC) | |||
const mach_timespec timeout = { secs, static_cast<clock_res_t>(nsecs) }; | |||
try { | |||
return (::semaphore_timedwait(server ? sem.sem : sem.sem2, timeout) == KERN_SUCCESS); | |||
} CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false); | |||
# elif defined(CARLA_USE_FUTEXES) | |||
const timespec timeout = { secs, nsecs }; | |||
for (;;) | |||
{ | |||
@@ -163,18 +210,24 @@ bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs) noexcept | |||
if (::syscall(__NR_futex, &sem.count, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0 && errno != EWOULDBLOCK) | |||
return false; | |||
} | |||
#else | |||
# else | |||
if (::sem_trywait(&sem.sem) == 0) | |||
return true; | |||
timespec timeout; | |||
::clock_gettime(CLOCK_REALTIME, &timeout); | |||
timeout.tv_sec += static_cast<time_t>(msecs / 1000); | |||
timeout.tv_nsec += static_cast<time_t>(msecs % 1000); | |||
timespec now; | |||
::clock_gettime(CLOCK_REALTIME, &now); | |||
struct timespec delta = { secs, nsecs }; | |||
struct timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec }; | |||
if (end.tv_nsec >= 1000000000L) { | |||
++end.tv_sec; | |||
end.tv_nsec -= 1000000000L; | |||
} | |||
try { | |||
return (::sem_timedwait(&sem.sem, &timeout) == 0); | |||
} CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false); | |||
return (::sem_timedwait(&sem.sem, &end) == 0); | |||
} CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false); | |||
# endif | |||
#endif | |||
} | |||