Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify semaphore
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Apr 15, 2023
1 parent 0e827c4 commit 5984d57
Showing 1 changed file with 5 additions and 55 deletions.
60 changes: 5 additions & 55 deletions plugins/common/Semaphore.hpp
@@ -1,17 +1,7 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
* SPDX-License-Identifier: ISC
*/

#ifndef DISTRHO_SEMAPHORE_HPP_INCLUDED
Expand All @@ -28,20 +18,14 @@
# endif
# include <winsock2.h>
# include <windows.h>
#elif defined(__MOD_DEVICES__)
# include <linux/futex.h>
# include <sys/time.h>
# include <errno.h>
# include <syscall.h>
# include <unistd.h>
#else
# include <semaphore.h>
# include <sys/time.h>
#endif

START_NAMESPACE_DISTRHO

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

class Semaphore
{
Expand All @@ -56,8 +40,6 @@ class Semaphore
#elif defined(DISTRHO_OS_WINDOWS)
handle = ::CreateSemaphoreA(nullptr, initialValue, std::max(initialValue, 1), nullptr);
DISTRHO_SAFE_ASSERT_RETURN(handle != INVALID_HANDLE_VALUE,);
#elif defined(__MOD_DEVICES__)
value = initialValue;
#else
::sem_init(&sem, 0, initialValue);
#endif
Expand All @@ -69,8 +51,6 @@ class Semaphore
::semaphore_destroy(mach_task_self(), sem);
#elif defined(DISTRHO_OS_WINDOWS)
::CloseHandle(handle);
#elif defined(__MOD_DEVICES__)
// nothing here
#else
::sem_destroy(&sem);
#endif
Expand All @@ -82,10 +62,6 @@ class Semaphore
::semaphore_signal(sem);
#elif defined(DISTRHO_OS_WINDOWS)
::ReleaseSemaphore(handle, 1, nullptr);
#elif defined(__MOD_DEVICES__)
// if already unlocked, do not wake futex
if (::__sync_bool_compare_and_swap(&value, 0, 1))
::syscall(__NR_futex, &value, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
#else
::sem_post(&sem);
#endif
Expand All @@ -97,18 +73,6 @@ class Semaphore
return ::semaphore_wait(sem) == KERN_SUCCESS;
#elif defined(DISTRHO_OS_WINDOWS)
return ::WaitForSingleObject(handle, INFINITE) == WAIT_OBJECT_0;
#elif defined(__MOD_DEVICES__)
for (;;)
{
if (::__sync_bool_compare_and_swap(&value, 1, 0))
return true;

if (::syscall(__NR_futex, &value, FUTEX_WAIT_PRIVATE, 0, nullptr, nullptr, 0) != 0)
{
if (errno != EAGAIN && errno != EINTR)
return false;
}
}
#else
return ::sem_wait(&sem) == 0;
#endif
Expand All @@ -121,39 +85,25 @@ class Semaphore
return ::semaphore_timedwait(sem, time) == KERN_SUCCESS;
#elif defined(DISTRHO_OS_WINDOWS)
return ::WaitForSingleObject(handle, numSecs * 1000) == WAIT_OBJECT_0;
#elif defined(__MOD_DEVICES__)
const struct timespec timeout = { static_cast<time_t>(numSecs), 0 };
for (;;)
{
if (::__sync_bool_compare_and_swap(&value, 1, 0))
return true;

if (::syscall(__NR_futex, &value, FUTEX_WAIT_PRIVATE, 0, &timeout, nullptr, 0) != 0)
{
if (errno != EAGAIN && errno != EINTR)
return false;
}
}
#else
struct timespec timeout;
::clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += numSecs;
return ::sem_timedwait(&sem, &timeout) == 0;
#endif
}

private:
#if defined(DISTRHO_OS_MAC)
::semaphore_t sem;
#elif defined(DISTRHO_OS_WINDOWS)
::HANDLE handle;
#elif defined(__MOD_DEVICES__)
int value;
#else
::sem_t sem;
#endif
};

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

END_NAMESPACE_DISTRHO

Expand Down

0 comments on commit 5984d57

Please sign in to comment.