Browse Source

Implement IP semaphores for OSX, fix nsecs calculation

tags/1.9.7
falkTX 8 years ago
parent
commit
8ec10051d0
9 changed files with 115 additions and 39 deletions
  1. +5
    -2
      source/backend/engine/CarlaEngineBridge.cpp
  2. +2
    -2
      source/backend/plugin/CarlaPluginBridge.cpp
  3. +3
    -2
      source/jackbridge/JackBridge.hpp
  4. +15
    -4
      source/jackbridge/JackBridge2.cpp
  5. +1
    -0
      source/jackbridge/JackBridge3.cpp
  6. +7
    -2
      source/jackbridge/JackBridgeExport.cpp
  7. +4
    -2
      source/jackbridge/JackBridgeExport.hpp
  8. +4
    -4
      source/utils/CarlaBridgeUtils.hpp
  9. +74
    -21
      source/utils/CarlaSemUtils.hpp

+ 5
- 2
source/backend/engine/CarlaEngineBridge.cpp View File

@@ -151,6 +151,9 @@ struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
{ {
CARLA_SAFE_ASSERT(data->midiOut[0] == 0); CARLA_SAFE_ASSERT(data->midiOut[0] == 0);
setRingBuffer(&data->ringBuffer, false); 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; return true;
} }


@@ -175,12 +178,12 @@ struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {


WaitHelper(BridgeRtClientControl& c) noexcept WaitHelper(BridgeRtClientControl& c) noexcept
: data(c.data), : data(c.data),
ok(jackbridge_sem_timedwait(&data->sem.server, 5000)) {}
ok(jackbridge_sem_timedwait(&data->sem.server, 5000, false)) {}


~WaitHelper() noexcept ~WaitHelper() noexcept
{ {
if (ok) if (ok)
jackbridge_sem_post(&data->sem.client);
jackbridge_sem_post(&data->sem.client, false);
} }


CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper) CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper)


+ 2
- 2
source/backend/plugin/CarlaPluginBridge.cpp View File

@@ -244,9 +244,9 @@ struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
{ {
CARLA_SAFE_ASSERT_RETURN(data != nullptr, false); 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 void writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept


+ 3
- 2
source/jackbridge/JackBridge.hpp View File

@@ -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 bool jackbridge_sem_init(void* sem) noexcept;
JACKBRIDGE_API void jackbridge_sem_destroy(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 bool jackbridge_shm_is_valid(const void* shm) noexcept;
JACKBRIDGE_API void jackbridge_shm_init(void* shm) noexcept; JACKBRIDGE_API void jackbridge_shm_init(void* shm) noexcept;


+ 15
- 4
source/jackbridge/JackBridge2.cpp View File

@@ -43,23 +43,34 @@ void jackbridge_sem_destroy(void* sem) noexcept
#endif #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,); CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);


#ifndef JACKBRIDGE_DUMMY #ifndef JACKBRIDGE_DUMMY
carla_sem_post(*(carla_sem_t*)sem);
carla_sem_post(*(carla_sem_t*)sem, server);
#endif #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); CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);


#ifdef JACKBRIDGE_DUMMY #ifdef JACKBRIDGE_DUMMY
return false; return false;
#else #else
return carla_sem_timedwait(*(carla_sem_t*)sem, msecs);
return carla_sem_timedwait(*(carla_sem_t*)sem, msecs, server);
#endif #endif
} }




+ 1
- 0
source/jackbridge/JackBridge3.cpp View File

@@ -128,6 +128,7 @@ const JackBridgeExportedFunctions* JACKBRIDGE_API jackbridge_get_exported_functi
funcs.set_property_change_callback_ptr = jackbridge_set_property_change_callback; funcs.set_property_change_callback_ptr = jackbridge_set_property_change_callback;
funcs.sem_init_ptr = jackbridge_sem_init; funcs.sem_init_ptr = jackbridge_sem_init;
funcs.sem_destroy_ptr = jackbridge_sem_destroy; funcs.sem_destroy_ptr = jackbridge_sem_destroy;
funcs.sem_connect_ptr = jackbridge_sem_connect;
funcs.sem_post_ptr = jackbridge_sem_post; funcs.sem_post_ptr = jackbridge_sem_post;
funcs.sem_timedwait_ptr = jackbridge_sem_timedwait; funcs.sem_timedwait_ptr = jackbridge_sem_timedwait;
funcs.shm_is_valid_ptr = jackbridge_shm_is_valid; funcs.shm_is_valid_ptr = jackbridge_shm_is_valid;


+ 7
- 2
source/jackbridge/JackBridgeExport.cpp View File

@@ -543,12 +543,17 @@ void jackbridge_sem_destroy(void* sem) noexcept
getBridgeInstance().sem_destroy_ptr(sem); 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); 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); return getBridgeInstance().sem_timedwait_ptr(sem, msecs);
} }


+ 4
- 2
source/jackbridge/JackBridgeExport.hpp View File

@@ -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_set_property_change_callback)(jack_client_t*, JackPropertyChangeCallback, void*);
typedef bool (JACKBRIDGE_API *jackbridgesym_sem_init)(void*); typedef bool (JACKBRIDGE_API *jackbridgesym_sem_init)(void*);
typedef void (JACKBRIDGE_API *jackbridgesym_sem_destroy)(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 bool (JACKBRIDGE_API *jackbridgesym_shm_is_valid)(const void*);
typedef void (JACKBRIDGE_API *jackbridgesym_shm_init)(void*); typedef void (JACKBRIDGE_API *jackbridgesym_shm_init)(void*);
typedef void (JACKBRIDGE_API *jackbridgesym_shm_attach)(void*, const char*); 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_set_property_change_callback set_property_change_callback_ptr;
jackbridgesym_sem_init sem_init_ptr; jackbridgesym_sem_init sem_init_ptr;
jackbridgesym_sem_destroy sem_destroy_ptr; jackbridgesym_sem_destroy sem_destroy_ptr;
jackbridgesym_sem_connect sem_connect_ptr;
jackbridgesym_sem_post sem_post_ptr; jackbridgesym_sem_post sem_post_ptr;
jackbridgesym_sem_timedwait sem_timedwait_ptr; jackbridgesym_sem_timedwait sem_timedwait_ptr;
jackbridgesym_shm_is_valid shm_is_valid_ptr; jackbridgesym_shm_is_valid shm_is_valid_ptr;


+ 4
- 4
source/utils/CarlaBridgeUtils.hpp View File

@@ -26,10 +26,10 @@
# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Global\\carla-bridge_shm_nonrtC_" # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Global\\carla-bridge_shm_nonrtC_"
# define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Global\\carla-bridge_shm_nonrtS_" # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Global\\carla-bridge_shm_nonrtS_"
#else #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 #endif


// ----------------------------------------------------------------------- // -----------------------------------------------------------------------


+ 74
- 21
source/utils/CarlaSemUtils.hpp View File

@@ -30,8 +30,10 @@
# endif # endif
struct carla_sem_t { HANDLE handle; }; struct carla_sem_t { HANDLE handle; };
#elif defined(CARLA_OS_MAC) #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) #elif defined(CARLA_USE_FUTEXES)
# include <cerrno> # include <cerrno>
# include <syscall.h> # include <syscall.h>
@@ -63,7 +65,23 @@ bool carla_sem_create2(carla_sem_t& sem) noexcept


return (sem.handle != INVALID_HANDLE_VALUE); return (sem.handle != INVALID_HANDLE_VALUE);
#elif defined(CARLA_OS_MAC) #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) #elif defined(CARLA_USE_FUTEXES)
sem.count = 0; sem.count = 0;
return true; return true;
@@ -98,13 +116,13 @@ void carla_sem_destroy2(carla_sem_t& sem) noexcept
#if defined(CARLA_OS_WIN) #if defined(CARLA_OS_WIN)
::CloseHandle(sem.handle); ::CloseHandle(sem.handle);
#elif defined(CARLA_OS_MAC) #elif defined(CARLA_OS_MAC)
// TODO
::semaphore_destroy(mach_task_self(), sem.sem);
#elif defined(CARLA_USE_FUTEXES) #elif defined(CARLA_USE_FUTEXES)
// nothing to do // nothing to do
(void)sem;
#else #else
::sem_destroy(&sem.sem); ::sem_destroy(&sem.sem);
#endif #endif
carla_zeroStruct(sem);
} }


/* /*
@@ -119,16 +137,39 @@ void carla_sem_destroy(carla_sem_t* const sem) noexcept
std::free(sem); 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). * Post semaphore (unlock).
*/ */
static inline 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 #ifdef CARLA_OS_WIN
::ReleaseSemaphore(sem.handle, 1, nullptr); ::ReleaseSemaphore(sem.handle, 1, nullptr);
#elif defined(CARLA_OS_MAC) #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) #elif defined(CARLA_USE_FUTEXES)
const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1); const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
CARLA_SAFE_ASSERT_RETURN(unlocked,); CARLA_SAFE_ASSERT_RETURN(unlocked,);
@@ -142,18 +183,24 @@ void carla_sem_post(carla_sem_t& sem) noexcept
* Wait for a semaphore (lock). * Wait for a semaphore (lock).
*/ */
static inline 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); CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);


#if defined(CARLA_OS_WIN) #if defined(CARLA_OS_WIN)
return (::WaitForSingleObject(sem.handle, msecs) == WAIT_OBJECT_0); 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 (;;) 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) if (::syscall(__NR_futex, &sem.count, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0 && errno != EWOULDBLOCK)
return false; return false;
} }
#else
# else
if (::sem_trywait(&sem.sem) == 0) if (::sem_trywait(&sem.sem) == 0)
return true; 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 { 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 #endif
} }




Loading…
Cancel
Save