Skip to content

Commit

Permalink
macos plugin details
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Mar 12, 2024
1 parent 989b660 commit 6b8808c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 34 deletions.
26 changes: 20 additions & 6 deletions src/plugin/ChildProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
START_NAMESPACE_DISTRHO

// FIXME
#define P "/home/falktx/Source/MOD/mod-app/build"
#if defined(DISTRHO_OS_MAC)
# define P "/Users/falktx/Source/MOD/mod-app/build/mod-desktop.app/Contents"
#elif defined(DISTRHO_OS_WINDOWS)
#else
# define P "/home/falktx/Source/MOD/mod-app/build"
#endif

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

class ChildProcess
{
#ifdef _WIN32
#else
pid_t pid = 0;
pid_t pid = -1;
#endif

public:
Expand All @@ -49,13 +54,22 @@ class ChildProcess
{
// FIXME
const char* const envp[] = {
"MOD_DESKTOP=1",
"LANG=en_US.UTF-8",
#if defined(DISTRHO_OS_MAC)
"DYLD_LIBRARY_PATH=" P "/MacOS",
"JACK_DRIVER_DIR=" P "/MacOS/jack",
"MOD_DATA_DIR=/Users/falktx/Documents/MOD Desktop",
"MOD_FACTORY_PEDALBOARDS_DIR=" P "/Resources/pedalboards",
"LV2_PATH=" P "/PlugIns/LV2", // FIXME
#elif defined(DISTRHO_OS_WINDOWS)
#else
"LD_LIBRARY_PATH=" P,
"JACK_DRIVER_DIR=" P "/jack",
"MOD_DATA_DIR=" P "/data",
"MOD_FACTORY_PEDALBOARDS_DIR=" P "/pedalboards",
"MOD_DESKTOP=1",
"LANG=en_US.UTF-8",
"LV2_PATH=" P "/plugins",
#endif
nullptr
};

Expand All @@ -82,14 +96,14 @@ class ChildProcess

void stop(const uint32_t timeoutInMilliseconds = 2000)
{
if (pid == 0)
if (pid <= 0)
return;

const uint32_t timeout = d_gettime_ms() + timeoutInMilliseconds;
const pid_t opid = pid;
pid_t ret;
bool sendTerminate = true;
pid = 0;
pid = -1;

for (;;)
{
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/DesktopPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ class DesktopPlugin : public Plugin
/* --------------------------------------------------------------------------------------------------------
* Process */

void activate()
void activate() override
{
shm.reset();

firstTimeProcessing = true;
numFramesInShmBuffer = numFramesInTmpBuffer = 0;
}

void deactivate()
void deactivate() override
{
}

Expand Down
4 changes: 1 addition & 3 deletions src/plugin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ NAME = MOD-Desktop
# Files to build

FILES_DSP = \
DesktopPlugin.cpp \
zita-resampler/resampler.cc \
zita-resampler/resampler-table.cc
DesktopPlugin.cpp

FILES_UI = \
DesktopUI.cpp
Expand Down
126 changes: 103 additions & 23 deletions src/plugin/SharedMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@

#include "DistrhoUtils.hpp"

#if defined(DISTRHO_OS_MAC)
#elif defined(DISTRHO_OS_WINDOWS)
#ifdef DISTRHO_OS_WINDOWS
#else
# include <cerrno>
# include <fcntl.h>
# include <syscall.h>
# include <linux/futex.h>
# include <sys/mman.h>
# include <sys/time.h>
# ifdef DISTRHO_OS_MAC
# include <mach/mach.h>
# include <mach/semaphore.h>
# include <servers/bootstrap.h>
# else
# include <cerrno>
# include <syscall.h>
# include <sys/time.h>
# include <linux/futex.h>
# endif
#endif

START_NAMESPACE_DISTRHO

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

class SharedMemory
{
Expand All @@ -34,40 +39,86 @@ class SharedMemory

bool init()
{
#if defined(DISTRHO_OS_MAC)
#elif defined(DISTRHO_OS_WINDOWS)
#ifdef DISTRHO_OS_WINDOWS
#else
fd = shm_open("/mod-desktop-test1", O_CREAT|O_EXCL|O_RDWR, 0600);
DISTRHO_SAFE_ASSERT_RETURN(fd >= 0, false);

{
const int ret = ::ftruncate(fd, static_cast<off_t>(kDataSize));
const int ret = ftruncate(fd, static_cast<off_t>(kDataSize));
DISTRHO_SAFE_ASSERT_RETURN(ret == 0, fail_deinit());
}

{
void* const ptr = ::mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fd, 0);
void* ptr;

#ifdef MAP_LOCKED
ptr = mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fd, 0);
if (ptr == nullptr || ptr == MAP_FAILED)
#endif
{
ptr = mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
}

DISTRHO_SAFE_ASSERT_RETURN(ptr != nullptr, fail_deinit());
DISTRHO_SAFE_ASSERT_RETURN(ptr != MAP_FAILED, fail_deinit());

data = static_cast<Data*>(ptr);

#ifndef MAP_LOCKED
mlock(data, kDataSize);
#endif
}
#endif

std::memset(data, 0, kDataSize);
data->magic = 1337;

#ifdef DISTRHO_OS_MAC
task = mach_task_self();

mach_port_t bootport1, bootport2;
DISTRHO_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport1) == KERN_SUCCESS, fail_deinit());
DISTRHO_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport2) == KERN_SUCCESS, fail_deinit());
DISTRHO_SAFE_ASSERT_RETURN(semaphore_create(task, &sem1, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS, fail_deinit());
DISTRHO_SAFE_ASSERT_RETURN(semaphore_create(task, &sem2, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS, fail_deinit());

static int bootcounter = 0;
std::snprintf(data->bootname1, 31, "mdskt_%d_%d_%p", ++bootcounter, getpid(), &sem1);
std::snprintf(data->bootname2, 31, "mdskt_%d_%d_%p", ++bootcounter, getpid(), &sem2);
data->bootname1[31] = data->bootname2[31] = '\0';

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
DISTRHO_SAFE_ASSERT_RETURN(bootstrap_register(bootport1, data->bootname1, sem1) == KERN_SUCCESS, fail_deinit());
DISTRHO_SAFE_ASSERT_RETURN(bootstrap_register(bootport2, data->bootname2, sem2) == KERN_SUCCESS, fail_deinit());
#pragma clang diagnostic pop
#endif

return true;
}

void deinit()
{
#if defined(DISTRHO_OS_MAC)
#elif defined(DISTRHO_OS_WINDOWS)
#ifdef DISTRHO_OS_MAC
if (sem1 != MACH_PORT_NULL)
{
semaphore_destroy(task, sem1);
sem1 = MACH_PORT_NULL;
}

if (sem2 != MACH_PORT_NULL)
{
semaphore_destroy(task, sem2);
sem2 = MACH_PORT_NULL;
}
#endif

#ifdef DISTRHO_OS_WINDOWS
#else
if (data != nullptr)
{
::munmap(data, kDataSize);
munmap(data, kDataSize);
data = nullptr;
}

Expand All @@ -80,6 +131,8 @@ class SharedMemory
#endif
}

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

void getAudioData(float* audio[2])
{
DISTRHO_SAFE_ASSERT_RETURN(data != nullptr,);
Expand Down Expand Up @@ -113,13 +166,22 @@ class SharedMemory
return true;
}

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

private:
struct Data {
uint32_t magic;
int32_t padding1;
#if defined(DISTRHO_OS_MAC)
char bootname1[32];
char bootname2[32];
#elif defined(DISTRHO_OS_WINDOWS)
HANDLE sem1;
HANDLE sem2;
#else
int32_t sem1;
int32_t sem2;
int32_t padding1;
// uint8_t sem[32];
#endif
uint16_t midiEventCount;
uint16_t midiFrames[511];
uint8_t midiData[511 * 4];
Expand All @@ -129,32 +191,50 @@ class SharedMemory

static constexpr const size_t kDataSize = sizeof(Data) + sizeof(float) * 128 * 2;

// ----------------------------------------------------------------------------------------------------------------
// shared memory details

#ifdef DISTRHO_OS_WINDOWS
#else
int fd = -1;
#endif

#ifdef DISTRHO_OS_MAC
mach_port_t task = MACH_PORT_NULL;
semaphore_t sem1 = MACH_PORT_NULL;
semaphore_t sem2 = MACH_PORT_NULL;
#endif

// ----------------------------------------------------------------------------------------------------------------
// semaphore details

#if defined(DISTRHO_OS_MAC)
void post()
{
semaphore_signal(sem1);
}

bool wait()
{
return false;
const mach_timespec timeout = { 1, 0 };
return semaphore_timedwait(sem2, timeout) == KERN_SUCCESS;
}
#elif defined(DISTRHO_OS_WINDOWS)
void post()
{
ReleaseSemaphore(data->sem1, 1, nullptr);
}

bool wait()
{
return false;
return WaitForSingleObject(data->sem2, 1000) == WAIT_OBJECT_0;
}
#else
int fd = -1;

void post()
{
const bool unlocked = __sync_bool_compare_and_swap(&data->sem1, 0, 1);
DISTRHO_SAFE_ASSERT_RETURN(unlocked,);
::syscall(__NR_futex, &data->sem1, FUTEX_WAKE, 1, nullptr, nullptr, 0);
syscall(__NR_futex, &data->sem1, FUTEX_WAKE, 1, nullptr, nullptr, 0);
}

bool wait()
Expand All @@ -166,7 +246,7 @@ class SharedMemory
if (__sync_bool_compare_and_swap(&data->sem2, 1, 0))
return true;

if (::syscall(__NR_futex, &data->sem2, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0)
if (syscall(__NR_futex, &data->sem2, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0)
if (errno != EAGAIN && errno != EINTR)
return false;
}
Expand All @@ -182,6 +262,6 @@ class SharedMemory
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SharedMemory)
};

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

END_NAMESPACE_DISTRHO

0 comments on commit 6b8808c

Please sign in to comment.