4#ifndef QSHAREDMEMORY_P_H
5#define QSHAREDMEMORY_P_H
20#include <QtCore/qstring.h>
22#if QT_CONFIG(sharedmemory)
23#include "qsystemsemaphore.h"
24#include "qtipccommon_p.h"
25#include "private/qobject_p.h"
27#if QT_CONFIG(posix_shm)
30#if QT_CONFIG(sysv_shm)
36class QSharedMemoryPrivate;
38#if QT_CONFIG(systemsemaphore)
40
41
42class QSharedMemoryLocker
44 Q_DISABLE_COPY(QSharedMemoryLocker)
46 Q_NODISCARD_CTOR
explicit QSharedMemoryLocker(QSharedMemory *sharedMemory)
52 Q_NODISCARD_CTOR QSharedMemoryLocker(QSharedMemoryLocker &&other)
noexcept
53 : q_sm{std::exchange(other.q_sm,
nullptr)}
56 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSharedMemoryLocker)
58 void swap(QSharedMemoryLocker &other)
noexcept
59 { qt_ptr_swap(q_sm, other.q_sm); }
61 inline ~QSharedMemoryLocker()
69 if (q_sm && q_sm->lock())
76 friend void swap(QSharedMemoryLocker &lhs, QSharedMemoryLocker &rhs)
noexcept
83class QSharedMemoryPosix
86 static constexpr bool Enabled = QT_CONFIG(posix_shm);
87 static bool supports(QNativeIpcKey::Type type)
88 {
return type == QNativeIpcKey::Type::PosixRealtime; }
89 static bool runtimeSupportCheck();
91 bool handle(QSharedMemoryPrivate *self);
92 bool cleanHandle(QSharedMemoryPrivate *self);
93 bool create(QSharedMemoryPrivate *self, qsizetype size);
94 bool attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode);
95 bool detach(QSharedMemoryPrivate *self);
100class QSharedMemorySystemV
103 static constexpr bool Enabled = QT_CONFIG(sysv_shm);
104 static bool supports(QNativeIpcKey::Type type)
105 {
return quint16(type) <= 0xff; }
106 static bool runtimeSupportCheck();
108#if QT_CONFIG(sysv_shm)
109 key_t handle(QSharedMemoryPrivate *self);
110 bool cleanHandle(QSharedMemoryPrivate *self);
111 bool create(QSharedMemoryPrivate *self, qsizetype size);
112 bool attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode);
113 bool detach(QSharedMemoryPrivate *self);
116 void updateNativeKeyFile(
const QNativeIpcKey &nativeKey);
118 QByteArray nativeKeyFile;
123class QSharedMemoryWin32
127 static constexpr bool Enabled =
true;
129 static constexpr bool Enabled =
false;
131 static bool runtimeSupportCheck() {
return Enabled; }
132 static bool supports(QNativeIpcKey::Type type)
133 {
return type == QNativeIpcKey::Type::Windows; }
135 Qt::HANDLE handle(QSharedMemoryPrivate *self);
136 bool cleanHandle(QSharedMemoryPrivate *self);
137 bool create(QSharedMemoryPrivate *self, qsizetype size);
138 bool attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode);
139 bool detach(QSharedMemoryPrivate *self);
141 Qt::HANDLE hand =
nullptr;
144class Q_AUTOTEST_EXPORT QSharedMemoryPrivate :
public QObjectPrivate
146 Q_DECLARE_PUBLIC(QSharedMemory)
149 QSharedMemoryPrivate(QNativeIpcKey::Type type) : nativeKey(type)
150 { constructBackend(); }
151 ~QSharedMemoryPrivate();
153 void *memory =
nullptr;
155 QNativeIpcKey nativeKey;
157#if QT_CONFIG(systemsemaphore)
158 using SemaphoreAccessMode = QSystemSemaphore::AccessMode;
159 QSystemSemaphore systemSemaphore{ QNativeIpcKey() };
160 bool lockedByMe =
false;
162 enum SemaphoreAccessMode {};
164 QSharedMemory::SharedMemoryError error = QSharedMemory::NoError;
169 QSharedMemoryPosix posix;
170 QSharedMemorySystemV sysv;
171 QSharedMemoryWin32 win32;
173 QtIpcCommon::IpcStorageVariant<&Backend::posix, &Backend::sysv, &Backend::win32> backend;
175 void constructBackend();
176 void destructBackend();
177 bool initKey(SemaphoreAccessMode mode);
179 template <
typename Lambda>
auto visit(
const Lambda &lambda)
181 return backend.visit(nativeKey.type(), lambda);
186 return visit([&](
auto p) {
return !!p->handle(
this); });
190 return visit([&](
auto p) {
return p->cleanHandle(
this); });
192 bool create(qsizetype sz)
194 return visit([&](
auto p) {
return p->create(
this, sz); });
196 bool attach(QSharedMemory::AccessMode mode)
198 return visit([&](
auto p) {
return p->attach(
this, mode); });
202 return visit([&](
auto p) {
return p->detach(
this); });
205 inline void setError(QSharedMemory::SharedMemoryError e,
const QString &message)
206 { error = e; errorString = message; }
207 void setUnixErrorString(QLatin1StringView function);
208 void setWindowsErrorString(QLatin1StringView function);
210#if QT_CONFIG(systemsemaphore)
211 bool tryLocker(QSharedMemoryLocker *locker,
const QString &function) {
212 if (!locker->lock()) {
213 errorString = QSharedMemory::tr(
"%1: unable to lock").arg(function);
214 error = QSharedMemory::LockError;
219 QNativeIpcKey semaphoreNativeKey()
const;
Combined button and popup list for selecting options.