Skip to content

Commit 5984d57

Browse files
committed
Simplify semaphore
Signed-off-by: falkTX <falktx@falktx.com>
1 parent 0e827c4 commit 5984d57

File tree

1 file changed

+5
-55
lines changed

1 file changed

+5
-55
lines changed

plugins/common/Semaphore.hpp

+5-55
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
22
* DISTRHO Plugin Framework (DPF)
3-
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
4-
*
5-
* Permission to use, copy, modify, and/or distribute this software for any purpose with
6-
* or without fee is hereby granted, provided that the above copyright notice and this
7-
* permission notice appear in all copies.
8-
*
9-
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10-
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11-
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12-
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13-
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14-
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3+
* Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
4+
* SPDX-License-Identifier: ISC
155
*/
166

177
#ifndef DISTRHO_SEMAPHORE_HPP_INCLUDED
@@ -28,20 +18,14 @@
2818
# endif
2919
# include <winsock2.h>
3020
# include <windows.h>
31-
#elif defined(__MOD_DEVICES__)
32-
# include <linux/futex.h>
33-
# include <sys/time.h>
34-
# include <errno.h>
35-
# include <syscall.h>
36-
# include <unistd.h>
3721
#else
3822
# include <semaphore.h>
3923
# include <sys/time.h>
4024
#endif
4125

4226
START_NAMESPACE_DISTRHO
4327

44-
// -----------------------------------------------------------------------
28+
// --------------------------------------------------------------------------------------------------------------------
4529

4630
class Semaphore
4731
{
@@ -56,8 +40,6 @@ class Semaphore
5640
#elif defined(DISTRHO_OS_WINDOWS)
5741
handle = ::CreateSemaphoreA(nullptr, initialValue, std::max(initialValue, 1), nullptr);
5842
DISTRHO_SAFE_ASSERT_RETURN(handle != INVALID_HANDLE_VALUE,);
59-
#elif defined(__MOD_DEVICES__)
60-
value = initialValue;
6143
#else
6244
::sem_init(&sem, 0, initialValue);
6345
#endif
@@ -69,8 +51,6 @@ class Semaphore
6951
::semaphore_destroy(mach_task_self(), sem);
7052
#elif defined(DISTRHO_OS_WINDOWS)
7153
::CloseHandle(handle);
72-
#elif defined(__MOD_DEVICES__)
73-
// nothing here
7454
#else
7555
::sem_destroy(&sem);
7656
#endif
@@ -82,10 +62,6 @@ class Semaphore
8262
::semaphore_signal(sem);
8363
#elif defined(DISTRHO_OS_WINDOWS)
8464
::ReleaseSemaphore(handle, 1, nullptr);
85-
#elif defined(__MOD_DEVICES__)
86-
// if already unlocked, do not wake futex
87-
if (::__sync_bool_compare_and_swap(&value, 0, 1))
88-
::syscall(__NR_futex, &value, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
8965
#else
9066
::sem_post(&sem);
9167
#endif
@@ -97,18 +73,6 @@ class Semaphore
9773
return ::semaphore_wait(sem) == KERN_SUCCESS;
9874
#elif defined(DISTRHO_OS_WINDOWS)
9975
return ::WaitForSingleObject(handle, INFINITE) == WAIT_OBJECT_0;
100-
#elif defined(__MOD_DEVICES__)
101-
for (;;)
102-
{
103-
if (::__sync_bool_compare_and_swap(&value, 1, 0))
104-
return true;
105-
106-
if (::syscall(__NR_futex, &value, FUTEX_WAIT_PRIVATE, 0, nullptr, nullptr, 0) != 0)
107-
{
108-
if (errno != EAGAIN && errno != EINTR)
109-
return false;
110-
}
111-
}
11276
#else
11377
return ::sem_wait(&sem) == 0;
11478
#endif
@@ -121,39 +85,25 @@ class Semaphore
12185
return ::semaphore_timedwait(sem, time) == KERN_SUCCESS;
12286
#elif defined(DISTRHO_OS_WINDOWS)
12387
return ::WaitForSingleObject(handle, numSecs * 1000) == WAIT_OBJECT_0;
124-
#elif defined(__MOD_DEVICES__)
125-
const struct timespec timeout = { static_cast<time_t>(numSecs), 0 };
126-
for (;;)
127-
{
128-
if (::__sync_bool_compare_and_swap(&value, 1, 0))
129-
return true;
130-
131-
if (::syscall(__NR_futex, &value, FUTEX_WAIT_PRIVATE, 0, &timeout, nullptr, 0) != 0)
132-
{
133-
if (errno != EAGAIN && errno != EINTR)
134-
return false;
135-
}
136-
}
13788
#else
13889
struct timespec timeout;
13990
::clock_gettime(CLOCK_REALTIME, &timeout);
14091
timeout.tv_sec += numSecs;
14192
return ::sem_timedwait(&sem, &timeout) == 0;
14293
#endif
14394
}
95+
14496
private:
14597
#if defined(DISTRHO_OS_MAC)
14698
::semaphore_t sem;
14799
#elif defined(DISTRHO_OS_WINDOWS)
148100
::HANDLE handle;
149-
#elif defined(__MOD_DEVICES__)
150-
int value;
151101
#else
152102
::sem_t sem;
153103
#endif
154104
};
155105

156-
// -----------------------------------------------------------------------
106+
// --------------------------------------------------------------------------------------------------------------------
157107

158108
END_NAMESPACE_DISTRHO
159109

0 commit comments

Comments
 (0)