Browse Source

Replace hacky macos semaphore with hidden futex api

Signed-off-by: falkTX <falktx@falktx.com>
pull/1882/head
falkTX 7 months ago
parent
commit
3c7d0e7b91
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
3 changed files with 39 additions and 82 deletions
  1. +5
    -5
      source/jackbridge/JackBridge2.cpp
  2. +1
    -1
      source/utils/CarlaLv2Utils.hpp
  3. +33
    -76
      source/utils/CarlaSemUtils.hpp

+ 5
- 5
source/jackbridge/JackBridge2.cpp View File

@@ -55,28 +55,28 @@ bool jackbridge_sem_connect(void* sem) noexcept
#ifdef JACKBRIDGE_DUMMY #ifdef JACKBRIDGE_DUMMY
return false; return false;
#else #else
return carla_sem_connect(*(carla_sem_t*)sem);
return true;
#endif #endif
} }


void jackbridge_sem_post(void* sem, bool server) noexcept
void jackbridge_sem_post(void* sem, bool) 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, server);
carla_sem_post(*(carla_sem_t*)sem);
#endif #endif
} }


#ifndef CARLA_OS_WASM #ifndef CARLA_OS_WASM
bool jackbridge_sem_timedwait(void* sem, uint msecs, bool server) noexcept
bool jackbridge_sem_timedwait(void* sem, uint msecs, bool) 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, server);
return carla_sem_timedwait(*(carla_sem_t*)sem, msecs);
#endif #endif
} }
#endif #endif


+ 1
- 1
source/utils/CarlaLv2Utils.hpp View File

@@ -2723,7 +2723,7 @@ const LV2_RDF_Descriptor* lv2_rdf_new(const LV2_URI uri, const bool loadPresets)
} }
// some plugins use rdfs:label, spec was not clear which one to use // some plugins use rdfs:label, spec was not clear which one to use
else if (LilvNode* const portGroupLabelNode = lilv_world_get(lv2World.me, portGroupNode, else if (LilvNode* const portGroupLabelNode = lilv_world_get(lv2World.me, portGroupNode,
lv2World.rdfs_label.me, nullptr))
lv2World.rdfs_label.me, nullptr))
{ {
portGroup.Name = carla_strdup_safe(lilv_node_as_string(portGroupLabelNode)); portGroup.Name = carla_strdup_safe(lilv_node_as_string(portGroupLabelNode));
lilv_node_free(portGroupLabelNode); lilv_node_free(portGroupLabelNode);


+ 33
- 76
source/utils/CarlaSemUtils.hpp View File

@@ -32,10 +32,15 @@
# endif # endif
struct carla_sem_t { HANDLE handle; }; struct carla_sem_t { HANDLE handle; };
#elif defined(CARLA_OS_MAC) #elif defined(CARLA_OS_MAC)
# 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; };
# include <cerrno>
#define UL_COMPARE_AND_WAIT 1
#define UL_COMPARE_AND_WAIT_SHARED 3
#define ULF_NO_ERRNO 0x01000000
extern "C" {
int __ulock_wait(uint32_t operation, void* addr, uint64_t value, uint32_t timeout_us);
int __ulock_wake(uint32_t operation, void* addr, uint64_t value);
}
struct carla_sem_t { int count; bool external; };
#elif defined(CARLA_USE_FUTEXES) #elif defined(CARLA_USE_FUTEXES)
# include <cerrno> # include <cerrno>
# include <syscall.h> # include <syscall.h>
@@ -66,32 +71,7 @@ bool carla_sem_create2(carla_sem_t& sem, const bool externalIPC) noexcept
sem.handle = ::CreateSemaphoreA(externalIPC ? &sa : nullptr, 0, 1, nullptr); sem.handle = ::CreateSemaphoreA(externalIPC ? &sa : nullptr, 0, 1, nullptr);


return (sem.handle != INVALID_HANDLE_VALUE); return (sem.handle != INVALID_HANDLE_VALUE);
#elif defined(CARLA_OS_MAC)
mach_port_t bootport;
const mach_port_t task = ::mach_task_self();

if (externalIPC) {
CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport) == KERN_SUCCESS, false);
}
CARLA_SAFE_ASSERT_RETURN(::semaphore_create(task, &sem.sem, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS, false);

if (! externalIPC)
return true;

static int bootcounter = 0;
std::snprintf(sem.bootname, 31, "crlsm_%i_%i_%p", ++bootcounter, ::getpid(), &sem);
sem.bootname[31] = '\0';

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (::bootstrap_register(bootport, sem.bootname, sem.sem) == KERN_SUCCESS)
#pragma clang diagnostic pop
return true;

sem.bootname[0] = '\0';
::semaphore_destroy(task, sem.sem);
return false;
#elif defined(CARLA_USE_FUTEXES)
#elif defined(CARLA_OS_MAC) || defined(CARLA_USE_FUTEXES)
sem.external = externalIPC; sem.external = externalIPC;
return true; return true;
#else #else
@@ -124,9 +104,7 @@ 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)
::semaphore_destroy(mach_task_self(), sem.sem);
#elif defined(CARLA_USE_FUTEXES)
#elif defined(CARLA_OS_MAC) || defined(CARLA_USE_FUTEXES)
// nothing to do // nothing to do
#else #else
::sem_destroy(&sem.sem); ::sem_destroy(&sem.sem);
@@ -146,40 +124,18 @@ void carla_sem_destroy(carla_sem_t* const sem) noexcept
std::free(sem); std::free(sem);
} }


/*
* Connect to semaphore.
* Used only on macOS for a client to connect to a server.
*/
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, const bool server = true) noexcept
void carla_sem_post(carla_sem_t& sem) 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)
try {
::semaphore_signal(server ? sem.sem : sem.sem2);
} CARLA_SAFE_EXCEPTION_RETURN("carla_sem_post",);
const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
CARLA_SAFE_ASSERT_RETURN(unlocked,);
__ulock_wake(ULF_NO_ERRNO | (sem.external ? UL_COMPARE_AND_WAIT_SHARED : UL_COMPARE_AND_WAIT), &sem.count, 0);
#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,);
@@ -187,8 +143,6 @@ void carla_sem_post(carla_sem_t& sem, const bool server = true) noexcept
#else #else
::sem_post(&sem.sem); ::sem_post(&sem.sem);
#endif #endif
// may be unused
return; (void)server;
} }


#ifndef CARLA_OS_WASM #ifndef CARLA_OS_WASM
@@ -196,23 +150,27 @@ void carla_sem_post(carla_sem_t& sem, const bool server = true) 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, const bool server = true) noexcept
bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs) 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);
#else
const uint secs = msecs / 1000;
const uint nsecs = (msecs % 1000) * 1000000;
#elif defined(CARLA_OS_MAC)
const uint32_t timeout = msecs * 1000;


# if defined(CARLA_OS_MAC)
const mach_timespec timeout = { secs, static_cast<int>(nsecs) };
for (;;)
{
if (__sync_bool_compare_and_swap(&sem.count, 1, 0))
return true;


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)
if (__ulock_wait(sem.external ? UL_COMPARE_AND_WAIT_SHARED : UL_COMPARE_AND_WAIT, &sem.count, 0, timeout) != 0)
if (errno != EAGAIN && errno != EINTR)
return false;
}
#elif defined(CARLA_USE_FUTEXES)
const uint secs = msecs / 1000;
const uint nsecs = (msecs % 1000) * 1000000;
const timespec timeout = { static_cast<time_t>(secs), static_cast<long>(nsecs) }; const timespec timeout = { static_cast<time_t>(secs), static_cast<long>(nsecs) };


for (;;) for (;;)
@@ -224,13 +182,15 @@ bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs, const bool server =
if (errno != EAGAIN && errno != EINTR) if (errno != EAGAIN && errno != EINTR)
return false; return false;
} }
# else
#else
if (::sem_trywait(&sem.sem) == 0) if (::sem_trywait(&sem.sem) == 0)
return true; return true;


timespec now; timespec now;
::clock_gettime(CLOCK_REALTIME, &now); ::clock_gettime(CLOCK_REALTIME, &now);


const uint secs = msecs / 1000;
const uint nsecs = (msecs % 1000) * 1000000;
const timespec delta = { static_cast<time_t>(secs), static_cast<long>(nsecs) }; const timespec delta = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
/* */ timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec }; /* */ timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec };
if (end.tv_nsec >= 1000000000L) { if (end.tv_nsec >= 1000000000L) {
@@ -246,13 +206,10 @@ bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs, const bool server =


if (ret == 0) if (ret == 0)
return true; return true;
if (errno != EINTR)
if (errno != EAGAIN && errno != EINTR)
return false; return false;
} }
# endif
#endif #endif
// may be unused
(void)server;
} }
#endif #endif




Loading…
Cancel
Save