5#ifndef QSYSTEMSEMAPHORE_P_H
6#define QSYSTEMSEMAPHORE_P_H
21#if QT_CONFIG(systemsemaphore)
23#include "qcoreapplication.h"
24#include "qtipccommon_p.h"
25#include "private/qtcore-config_p.h"
28#if QT_CONFIG(posix_sem)
29# include <semaphore.h>
32# define SEM_FAILED nullptr
35#if QT_CONFIG(sysv_sem)
41class QSystemSemaphorePrivate;
43struct QSystemSemaphorePosix
45 static constexpr bool Enabled = QT_CONFIG(posix_sem);
46 static bool supports(QNativeIpcKey::Type type)
47 {
return type == QNativeIpcKey::Type::PosixRealtime; }
48 static bool runtimeSupportCheck();
50 bool handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode);
51 void cleanHandle(QSystemSemaphorePrivate *self);
52 bool modifySemaphore(QSystemSemaphorePrivate *self,
int count);
54 sem_t *semaphore = SEM_FAILED;
55 bool createdSemaphore =
false;
58struct QSystemSemaphoreSystemV
60 static constexpr bool Enabled = QT_CONFIG(sysv_sem);
61 static bool supports(QNativeIpcKey::Type type)
62 {
return quint16(type) <= 0xff; }
63 static bool runtimeSupportCheck();
65#if QT_CONFIG(sysv_sem)
66 key_t handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode);
67 void cleanHandle(QSystemSemaphorePrivate *self);
68 bool modifySemaphore(QSystemSemaphorePrivate *self,
int count);
70 QByteArray nativeKeyFile;
73 bool createdFile =
false;
74 bool createdSemaphore =
false;
78struct QSystemSemaphoreWin32
81 static constexpr bool Enabled =
true;
83 static constexpr bool Enabled =
false;
85 static bool supports(QNativeIpcKey::Type type)
86 {
return type == QNativeIpcKey::Type::Windows; }
87 static bool runtimeSupportCheck() {
return Enabled; }
90 Qt::HANDLE handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode);
91 void cleanHandle(QSystemSemaphorePrivate *self);
92 bool modifySemaphore(QSystemSemaphorePrivate *self,
int count);
94 Qt::HANDLE semaphore =
nullptr;
97class QSystemSemaphorePrivate
100 QSystemSemaphorePrivate(QNativeIpcKey::Type type) : nativeKey(type)
101 { constructBackend(); }
102 ~QSystemSemaphorePrivate() { destructBackend(); }
104 void setWindowsErrorString(QLatin1StringView function);
105 void setUnixErrorString(QLatin1StringView function);
106 inline void setError(QSystemSemaphore::SystemSemaphoreError e,
const QString &message)
107 { error = e; errorString = message; }
108 inline void clearError()
109 { setError(QSystemSemaphore::NoError, QString()); }
111 QNativeIpcKey nativeKey;
114 QSystemSemaphore::SystemSemaphoreError error = QSystemSemaphore::NoError;
119 QSystemSemaphorePosix posix;
120 QSystemSemaphoreSystemV sysv;
121 QSystemSemaphoreWin32 win32;
123 QtIpcCommon::IpcStorageVariant<&Backend::posix, &Backend::sysv, &Backend::win32> backend;
125 void constructBackend();
126 void destructBackend();
128 template <
typename Lambda>
auto visit(
const Lambda &lambda)
130 return backend.visit(nativeKey.type(), lambda);
133 void handle(QSystemSemaphore::AccessMode mode)
135 visit([&](
auto p) { p->handle(
this, mode); });
139 visit([&](
auto p) { p->cleanHandle(
this); });
141 bool modifySemaphore(
int count)
143 return visit([&](
auto p) {
return p->modifySemaphore(
this, count); });