Add native system semaphore and Windows shared memory (#7212)

This commit is contained in:
Dominic Clark
2024-04-20 23:21:29 +01:00
committed by GitHub
parent df11a98902
commit bda042e1eb
8 changed files with 347 additions and 118 deletions

View File

@@ -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>
@@ -75,6 +71,7 @@
#include <QProcess>
#include <QThread>
#include <QString>
#include <QUuid>
#ifndef SYNC_WITH_SHM_FIFO
#include <poll.h>
@@ -85,6 +82,7 @@
#ifdef SYNC_WITH_SHM_FIFO
#include "SharedMemory.h"
#include "SystemSemaphore.h"
#endif
namespace lmms
@@ -120,12 +118,11 @@ 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());
@@ -133,26 +130,21 @@ public:
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
@@ -336,11 +328,10 @@ private:
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

61
include/SystemSemaphore.h Normal file
View File

@@ -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