ATBUpdateTool/DCPlugin/src/shared_mem_buffer.cpp

40 lines
1.2 KiB
C++
Raw Normal View History

Squashed 'DCPlugin/' changes from 3e3e1ef..652ad9b 652ad9b Included changes of TS for version 3.5. c85b090 Added ulong2uchar (version 3.5). 87a6ed0 Include changes of Thomas for version 3.5. 99c88c7 Included changes of Thomas for version 3.5. prn_getPrintResult(), 712ea0f Included changes of Thomas for version 3.5 of library: datif_templatePrintFinished_OK(), datif_templatePrintFinished_Err(), datif_gotNewCoin(). 86311de Add use of shared memory. Add changes for version 3.4. of library. cb44127 Added check4freeFDshortCmd. a95e174 Included some changes of Thomas. c724b5b Shift several device controller related variables to shared memory. ef79321 Massive change: using shared memory containing most device controller realted variables. a7aa75a Massive change: using shared memory containing devcie controller variables. dc00c69 Added check4freeFDlongCmd. eeb3519 Adding shared memory containing all variables necessary to check device controller related variables. 30338e2 Changed struct T_moduleCondition. Changed struct T_dynamicCondition. Added struct T_extTime. Added log_chkIfVaultRecordAvailable(). Added sys_getDeviceConditions(). set version to 3.4. 58fdea4 Added m_sharedMem. Added cash_getAmountInVault. Added cash_getNrCoinsInVault. Added sys_getDynMachineConditions() with new interface. Added sys_getDeviceConditions() with new interface. Added log_chkIfVaultRecordAvailable(). df760f1 Added shared_mem_buffer.h/.cpp. git-subtree-dir: DCPlugin git-subtree-split: 652ad9b43d24bddbc3c2636c4ff4b5d74e081e46
2023-04-18 13:43:37 +02:00
#include "shared_mem_buffer.h"
#include <QDebug>
#include <atomic>
#ifdef QT_POSIX_IPC
// The POSIX backend can be explicitly selected using the -feature-ipc_posix
// option to the Qt configure script. If it is enabled, the QT_POSIX_IPC
// macro will be defined. -> we use SystemV shared memory
#error "QT_POSIX_IPC defined"
#else
#include <sys/ipc.h> // ftok
#endif
// std::atomic_bool SharedMemBuffer::__sharedMemLocked{false};
QSharedMemory *SharedMemBuffer::getShm(std::size_t size) {
static QSharedMemory shMem;
if (size > 0) {
static const long nativeKey = ftok("/etc/os-release", 'H');
static const QString fkey = std::to_string(nativeKey).c_str();
shMem.setKey(fkey);
if (!shMem.isAttached()) {
if (shMem.create(size)) {
return &shMem;
} else {
if (shMem.error() == QSharedMemory::AlreadyExists) {
if (shMem.attach()) {
return &shMem;
}
}
}
qCritical() << shMem.nativeKey() << shMem.key() << shMem.data()
<< shMem.error() << shMem.errorString();
return nullptr;
}
}
return &shMem;
}