6#ifndef QSYSTEMSEMAPHORE_P_H
7#define QSYSTEMSEMAPHORE_P_H
22#if QT_CONFIG(systemsemaphore)
24#include "qcoreapplication.h"
25#include "qtipccommon_p.h"
26#include "private/qtcore-config_p.h"
29#if QT_CONFIG(posix_sem)
30# include <semaphore.h>
33# define SEM_FAILED nullptr
36#if QT_CONFIG(sysv_sem)
42class QSystemSemaphorePrivate;
44struct QSystemSemaphorePosix
46 static constexpr bool Enabled = QT_CONFIG(posix_sem);
47 static bool supports(QNativeIpcKey::Type type)
48 {
return type == QNativeIpcKey::Type::PosixRealtime; }
49 static bool runtimeSupportCheck();
51 bool handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode);
52 void cleanHandle(QSystemSemaphorePrivate *self);
53 bool modifySemaphore(QSystemSemaphorePrivate *self,
int count);
55 sem_t *semaphore = SEM_FAILED;
56 bool createdSemaphore =
false;
59struct QSystemSemaphoreSystemV
61 static constexpr bool Enabled = QT_CONFIG(sysv_sem);
62 static bool supports(QNativeIpcKey::Type type)
63 {
return quint16(type) <= 0xff; }
64 static bool runtimeSupportCheck();
66#if QT_CONFIG(sysv_sem)
67 key_t handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode);
68 void cleanHandle(QSystemSemaphorePrivate *self);
69 bool modifySemaphore(QSystemSemaphorePrivate *self,
int count);
71 QByteArray nativeKeyFile;
74 bool createdFile =
false;
75 bool createdSemaphore =
false;
79struct QSystemSemaphoreWin32
82 static constexpr bool Enabled =
true;
84 static constexpr bool Enabled =
false;
86 static bool supports(QNativeIpcKey::Type type)
87 {
return type == QNativeIpcKey::Type::Windows; }
88 static bool runtimeSupportCheck() {
return Enabled; }
91 Qt::HANDLE handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode);
92 void cleanHandle(QSystemSemaphorePrivate *self);
93 bool modifySemaphore(QSystemSemaphorePrivate *self,
int count);
95 Qt::HANDLE semaphore =
nullptr;
98class QSystemSemaphorePrivate
101 QSystemSemaphorePrivate(QNativeIpcKey::Type type) : nativeKey(type)
102 { constructBackend(); }
103 ~QSystemSemaphorePrivate() { destructBackend(); }
105 void setWindowsErrorString(QLatin1StringView function);
106 void setUnixErrorString(QLatin1StringView function);
107 inline void setError(QSystemSemaphore::SystemSemaphoreError e,
const QString &message)
108 { error = e; errorString = message; }
109 inline void clearError()
110 { setError(QSystemSemaphore::NoError, QString()); }
112 QNativeIpcKey nativeKey;
115 QSystemSemaphore::SystemSemaphoreError error = QSystemSemaphore::NoError;
120 QSystemSemaphorePosix posix;
121 QSystemSemaphoreSystemV sysv;
122 QSystemSemaphoreWin32 win32;
124 QtIpcCommon::IpcStorageVariant<&Backend::posix, &Backend::sysv, &Backend::win32> backend;
126 void constructBackend();
127 void destructBackend();
129 template <
typename Lambda>
auto visit(
const Lambda &lambda)
131 return backend.visit(nativeKey.type(), lambda);
134 void handle(QSystemSemaphore::AccessMode mode)
136 visit([&](
auto p) { p->handle(
this, mode); });
140 visit([&](
auto p) { p->cleanHandle(
this); });
142 bool modifySemaphore(
int count)
144 return visit([&](
auto p) {
return p->modifySemaphore(
this, count); });