Skip to content

Commit b200856

Browse files
committed
Update semaphore and dpf
Signed-off-by: falkTX <falktx@falktx.com>
1 parent ef92781 commit b200856

File tree

2 files changed

+78
-27
lines changed

2 files changed

+78
-27
lines changed

dpf

Submodule dpf updated 75 files

plugins/common/Semaphore.hpp

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DISTRHO Plugin Framework (DPF)
3-
* Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
3+
* Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
44
* SPDX-License-Identifier: ISC
55
*/
66

@@ -16,6 +16,15 @@
1616
# ifndef NOMINMAX
1717
# define NOMINMAX
1818
# endif
19+
# ifndef NOKERNEL
20+
# define NOKERNEL
21+
# endif
22+
# ifndef NOSERVICE
23+
# define NOSERVICE
24+
# endif
25+
# ifndef WIN32_LEAN_AND_MEAN
26+
# define WIN32_LEAN_AND_MEAN
27+
# endif
1928
# include <winsock2.h>
2029
# include <windows.h>
2130
#else
@@ -30,75 +39,117 @@ START_NAMESPACE_DISTRHO
3039
class Semaphore
3140
{
3241
public:
33-
Semaphore(const int initialValue = 0)
42+
Semaphore(const uint16_t initialValue = 0)
43+
#if defined(DISTRHO_OS_MAC)
44+
: task(mach_task_self()),
45+
sem(nullptr)
46+
#elif defined(DISTRHO_OS_WINDOWS)
47+
: handle(INVALID_HANDLE_VALUE)
48+
#else
49+
: sem()
50+
#endif
3451
{
3552
#if defined(DISTRHO_OS_MAC)
36-
task = mach_task_self();
37-
DISTRHO_SAFE_ASSERT_RETURN(semaphore_create(task, &sem, SYNC_POLICY_FIFO, initialValue) == KERN_SUCCESS,);
53+
DISTRHO_SAFE_ASSERT_RETURN(semaphore_create(task,
54+
&sem,
55+
SYNC_POLICY_FIFO,
56+
static_cast<int>(initialValue)) == KERN_SUCCESS,);
3857
#elif defined(DISTRHO_OS_WINDOWS)
39-
handle = ::CreateSemaphoreA(nullptr, initialValue, std::max(initialValue, 1), nullptr);
58+
handle = CreateSemaphoreA(nullptr, initialValue, LONG_MAX, nullptr);
4059
DISTRHO_SAFE_ASSERT_RETURN(handle != INVALID_HANDLE_VALUE,);
4160
#else
42-
DISTRHO_SAFE_ASSERT_RETURN(::sem_init(&sem, 0, initialValue) == 0,);
61+
DISTRHO_SAFE_ASSERT_RETURN(sem_init(&sem, 0, initialValue) == 0,);
4362
#endif
4463
}
4564

4665
~Semaphore()
4766
{
4867
#if defined(DISTRHO_OS_MAC)
49-
::semaphore_destroy(task, sem);
68+
semaphore_destroy(task, sem);
5069
#elif defined(DISTRHO_OS_WINDOWS)
51-
::CloseHandle(handle);
70+
CloseHandle(handle);
5271
#else
53-
::sem_destroy(&sem);
72+
sem_destroy(&sem);
5473
#endif
5574
}
5675

5776
void post()
5877
{
5978
#if defined(DISTRHO_OS_MAC)
60-
::semaphore_signal(sem);
79+
semaphore_signal(sem);
6180
#elif defined(DISTRHO_OS_WINDOWS)
62-
::ReleaseSemaphore(handle, 1, nullptr);
81+
ReleaseSemaphore(handle, 1, nullptr);
6382
#else
64-
::sem_post(&sem);
83+
sem_post(&sem);
6584
#endif
6685
}
6786

6887
bool wait()
6988
{
7089
#if defined(DISTRHO_OS_MAC)
71-
return ::semaphore_wait(sem) == KERN_SUCCESS;
90+
return semaphore_wait(sem) == KERN_SUCCESS;
7291
#elif defined(DISTRHO_OS_WINDOWS)
73-
return ::WaitForSingleObject(handle, INFINITE) == WAIT_OBJECT_0;
92+
return WaitForSingleObject(handle, INFINITE) == WAIT_OBJECT_0;
7493
#else
75-
return ::sem_wait(&sem) == 0;
94+
return sem_wait(&sem) == 0;
7695
#endif
7796
}
7897

79-
bool timedWait(const uint numSecs)
98+
bool tryWait()
8099
{
81100
#if defined(DISTRHO_OS_MAC)
82-
const struct mach_timespec time = { numSecs, 0 };
83-
return ::semaphore_timedwait(sem, time) == KERN_SUCCESS;
101+
const mach_timespec_t zero = { 0, 0 };
102+
return semaphore_timedwait(sem, zero) == KERN_SUCCESS;
84103
#elif defined(DISTRHO_OS_WINDOWS)
85-
return ::WaitForSingleObject(handle, numSecs * 1000) == WAIT_OBJECT_0;
104+
return WaitForSingleObject(handle, 0) == WAIT_OBJECT_0;
105+
#else
106+
return sem_trywait(&sem) == 0;
107+
#endif
108+
}
109+
110+
bool timedWait(const uint seconds, const uint nanoseconds)
111+
{
112+
#if defined(DISTRHO_OS_MAC)
113+
const mach_timespec_t time = { seconds, static_cast<clock_res_t>(nanoseconds) };
114+
return semaphore_timedwait(sem, time) == KERN_SUCCESS;
115+
#elif defined(DISTRHO_OS_WINDOWS)
116+
const uint milliseconds = seconds * 1000U + nanoseconds / 1000000U;
117+
return WaitForSingleObject(handle, milliseconds) == WAIT_OBJECT_0;
86118
#else
87119
struct timespec timeout;
88-
::clock_gettime(CLOCK_REALTIME, &timeout);
89-
timeout.tv_sec += numSecs;
90-
return ::sem_timedwait(&sem, &timeout) == 0;
120+
DISTRHO_SAFE_ASSERT_RETURN(clock_gettime(CLOCK_REALTIME, &timeout) == 0, false);
121+
122+
timeout.tv_sec += seconds;
123+
timeout.tv_nsec += nanoseconds;
124+
if (timeout.tv_nsec >= 1000000000LL)
125+
{
126+
++timeout.tv_sec;
127+
timeout.tv_nsec -= 1000000000LL;
128+
}
129+
130+
for (int r;;)
131+
{
132+
r = sem_timedwait(&sem, &timeout);
133+
134+
if (r < 0)
135+
r = errno;
136+
137+
if (r == EINTR)
138+
continue;
139+
140+
return r == 0;
141+
}
91142
#endif
92143
}
93144

94145
private:
95146
#if defined(DISTRHO_OS_MAC)
96-
::mach_port_t task;
97-
::semaphore_t sem;
147+
mach_port_t task;
148+
semaphore_t sem;
98149
#elif defined(DISTRHO_OS_WINDOWS)
99-
::HANDLE handle;
150+
HANDLE handle;
100151
#else
101-
::sem_t sem;
152+
sem_t sem;
102153
#endif
103154
};
104155

0 commit comments

Comments
 (0)