Skip to content

Commit

Permalink
Add native system semaphore and Windows shared memory (#7212)
Browse files Browse the repository at this point in the history
  • Loading branch information
DomClark committed Apr 20, 2024
1 parent df11a98 commit bda042e
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 118 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ jobs:
name: msvc-${{ matrix.arch }}
runs-on: windows-2019
env:
qt-version: '5.15.2'
CCACHE_MAXSIZE: 0
CCACHE_NOCOMPRESS: 1
steps:
Expand Down Expand Up @@ -246,22 +245,21 @@ jobs:
path: ~\AppData\Local\ccache
- name: Install tools
run: choco install ccache
- name: Install 64-bit Qt
if: matrix.arch == 'x64'
- name: Install Qt
uses: jurplel/install-qt-action@b3ea5275e37b734d027040e2c7fe7a10ea2ef946
with:
version: ${{ env.qt-version }}
arch: win64_msvc2019_64
version: '5.15.2'
arch: |-
${{
fromJSON('
{
"x86": "win32_msvc2019",
"x64": "win64_msvc2019_64"
}
')[matrix.arch]
}}
archives: qtbase qtsvg qttools
cache: true
- name: Install 32-bit Qt
uses: jurplel/install-qt-action@b3ea5275e37b734d027040e2c7fe7a10ea2ef946
with:
version: ${{ env.qt-version }}
arch: win32_msvc2019
archives: qtbase qtsvg qttools
cache: true
set-env: ${{ matrix.arch == 'x86' }}
- name: Set up build environment
uses: ilammy/msvc-dev-cmd@cec98b9d092141f74527d0afa6feb2af698cfe89
with:
Expand Down
31 changes: 11 additions & 20 deletions include/RemotePluginBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
#ifdef LMMS_HAVE_PROCESS_H
#include <process.h>
#endif

#include <QtGlobal>
#include <QSystemSemaphore>
#include <QUuid>
#else // !(LMMS_HAVE_SYS_IPC_H && LMMS_HAVE_SEMAPHORE_H)
#ifdef LMMS_HAVE_UNISTD_H
#include <unistd.h>
Expand Down Expand Up @@ -75,6 +71,7 @@
#include <QProcess>
#include <QThread>
#include <QString>
#include <QUuid>

#ifndef SYNC_WITH_SHM_FIFO
#include <poll.h>
Expand All @@ -85,6 +82,7 @@

#ifdef SYNC_WITH_SHM_FIFO
#include "SharedMemory.h"
#include "SystemSemaphore.h"
#endif

namespace lmms
Expand Down Expand Up @@ -120,39 +118,33 @@ class shmFifo
} ;

public:
#ifndef BUILD_REMOTE_PLUGIN_CLIENT
// constructor for master-side
shmFifo() :
m_invalid( false ),
m_master( true ),
m_dataSem( QString() ),
m_messageSem( QString() ),
m_lockDepth( 0 )
{
m_data.create(QUuid::createUuid().toString().toStdString());
m_data->startPtr = m_data->endPtr = 0;
static int k = 0;
m_data->dataSem.semKey = ( getpid()<<10 ) + ++k;
m_data->messageSem.semKey = ( getpid()<<10 ) + ++k;
m_dataSem.setKey( QString::number( m_data->dataSem.semKey ),
1, QSystemSemaphore::Create );
m_messageSem.setKey( QString::number(
m_data->messageSem.semKey ),
0, QSystemSemaphore::Create );
m_dataSem = SystemSemaphore{std::to_string(m_data->dataSem.semKey), 1u};
m_messageSem = SystemSemaphore{std::to_string(m_data->messageSem.semKey), 0u};
}
#endif

// constructor for remote-/client-side - use _shm_key for making up
// the connection to master
shmFifo(const std::string& shmKey) :
m_invalid( false ),
m_master( false ),
m_dataSem( QString() ),
m_messageSem( QString() ),
m_lockDepth( 0 )
{
m_data.attach(shmKey);
m_dataSem.setKey( QString::number( m_data->dataSem.semKey ) );
m_messageSem.setKey( QString::number(
m_data->messageSem.semKey ) );
m_dataSem = SystemSemaphore{std::to_string(m_data->dataSem.semKey)};
m_messageSem = SystemSemaphore{std::to_string(m_data->messageSem.semKey)};
}

inline bool isInvalid() const
Expand Down Expand Up @@ -336,11 +328,10 @@ class shmFifo
volatile bool m_invalid;
bool m_master;
SharedMemory<shmData> m_data;
QSystemSemaphore m_dataSem;
QSystemSemaphore m_messageSem;
SystemSemaphore m_dataSem;
SystemSemaphore m_messageSem;
std::atomic_int m_lockDepth;

} ;
};
#endif // SYNC_WITH_SHM_FIFO


Expand Down
61 changes: 61 additions & 0 deletions include/SystemSemaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* SystemSemaphore.h
*
* Copyright (c) 2024 Dominic Clark
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/

#ifndef LMMS_SYSTEM_SEMAPHORE_H
#define LMMS_SYSTEM_SEMAPHORE_H

#include <memory>
#include <string>

namespace lmms {

namespace detail {

class SystemSemaphoreImpl;

} // namespace detail

class SystemSemaphore
{
public:
SystemSemaphore() noexcept;
SystemSemaphore(std::string key, unsigned int value);
explicit SystemSemaphore(std::string key);
~SystemSemaphore();

SystemSemaphore(SystemSemaphore&& other) noexcept;
auto operator=(SystemSemaphore&& other) noexcept -> SystemSemaphore&;

auto acquire() noexcept -> bool;
auto release() noexcept -> bool;

auto key() const noexcept -> const std::string& { return m_key; }

private:
std::string m_key;
std::unique_ptr<detail::SystemSemaphoreImpl> m_impl;
};

} // namespace lmms

#endif // LMMS_SYSTEM_SEMAPHORE_H
13 changes: 6 additions & 7 deletions plugins/VstBase/RemoteVstPlugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ FOREACH( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
ENDFOREACH()

# Import of windows.h breaks min()/max()
add_definitions(-DNOMINMAX)

ADD_SUBDIRECTORY("${LMMS_SOURCE_DIR}/src/common" common)

if(NOT IS_WIN)
Expand Down Expand Up @@ -64,12 +67,6 @@ if(MSVC)
)
endif()


if(WIN32)
find_package(Qt5Core REQUIRED)
target_link_libraries(${EXE_NAME} Qt5::Core)
endif()

if(IS_MINGW)
SET(CMAKE_REQUIRED_FLAGS "-std=c++17")

Expand All @@ -96,7 +93,9 @@ if(LMMS_BUILD_WIN32)
set(NOOP_COMMAND "${CMAKE_COMMAND}" "-E" "echo")
endif()
if(STRIP)
set(STRIP_COMMAND "$<IF:$<TARGET:Debug,RelWithDebInfo>,${NOOP_COMMAND},${STRIP}>")
# TODO CMake 3.19: Now that CONFIG generator expressions support testing for
# multiple configurations, combine the OR into a single CONFIG expression.
set(STRIP_COMMAND "$<IF:$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>,${NOOP_COMMAND},${STRIP}>")
else()
set(STRIP_COMMAND "${NOOP_COMMAND}")
endif()
Expand Down
18 changes: 0 additions & 18 deletions plugins/VstBase/RemoteVstPlugin32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ ENDMACRO()
IF(LMMS_BUILD_WIN32 AND NOT LMMS_BUILD_WIN64)
ADD_SUBDIRECTORY(RemoteVstPlugin)
ELSEIF(LMMS_BUILD_WIN64 AND MSVC)
IF(NOT QT_32_PREFIX)
SET(LMMS_MSVC_YEAR_FOR_QT ${LMMS_MSVC_YEAR})

if(LMMS_MSVC_YEAR_FOR_QT EQUAL 2019 AND Qt5_VERSION VERSION_LESS "5.15")
SET(LMMS_MSVC_YEAR_FOR_QT 2017) # Qt only provides binaries for MSVC 2017, but 2019 is binary compatible
endif()

GET_FILENAME_COMPONENT(QT_BIN_DIR ${QT_QMAKE_EXECUTABLE} DIRECTORY)
SET(QT_32_PREFIX "${QT_BIN_DIR}/../../msvc${LMMS_MSVC_YEAR_FOR_QT}")
ENDIF()

#TODO: qt5 installed using vcpkg: I don't know how to detect if the user built the x86 version of qt5 from here. At least not cleanly.
#So for the moment, we'll allow the built.
IF(NOT (IS_DIRECTORY ${QT_32_PREFIX} AND EXISTS ${QT_32_PREFIX}/bin/qmake.exe))
MESSAGE(WARNING "No Qt 32 bit installation found at ${QT_32_PREFIX}. If you're using VCPKG you can ignore this message if you've built x86-windows version of qt5")
ENDIF()

ExternalProject_Add(RemoteVstPlugin32
"${EXTERNALPROJECT_ARGS}"
CMAKE_GENERATOR "${LMMS_MSVC_GENERATOR}"
Expand All @@ -42,7 +25,6 @@ ELSEIF(LMMS_BUILD_WIN64 AND MSVC)
CMAKE_ARGS
"${EXTERNALPROJECT_CMAKE_ARGS}"
"-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
"-DCMAKE_PREFIX_PATH=${QT_32_PREFIX}"
)

INSTALL_EXTERNAL_PROJECT(RemoteVstPlugin32)
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(COMMON_SRCS
RemotePluginBase.cpp
SharedMemory.cpp
SystemSemaphore.cpp
)

foreach(SRC ${COMMON_SRCS})
Expand Down

0 comments on commit bda042e

Please sign in to comment.