Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsystemsemaphore.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsystemsemaphore.h"
6
7#if QT_CONFIG(systemsemaphore)
8#include <QtCore/q20memory.h>
9
11
12using namespace QtIpcCommon;
13using namespace Qt::StringLiterals;
14
15inline void QSystemSemaphorePrivate::constructBackend()
16{
17 visit([](auto p) { q20::construct_at(p); });
18}
19
20inline void QSystemSemaphorePrivate::destructBackend()
21{
22 visit([](auto p) { std::destroy_at(p); });
23}
24
71#if QT_DEPRECATED_SINCE(6, 10)
84QSystemSemaphore::QSystemSemaphore(const QString &key, int initialValue, AccessMode mode)
85 : QSystemSemaphore(legacyNativeKey(key), initialValue, mode)
86{
87 d->legacyKey = key;
88}
89#endif
90
127QSystemSemaphore::QSystemSemaphore(const QNativeIpcKey &key, int initialValue, AccessMode mode)
128 : d(new QSystemSemaphorePrivate(key.type()))
129{
130 setNativeKey(key, initialValue, mode);
131}
132
148QSystemSemaphore::~QSystemSemaphore()
149{
150 d->cleanHandle();
151}
152
190void QSystemSemaphore::setNativeKey(const QNativeIpcKey &key, int initialValue, AccessMode mode)
191{
192 if (key == d->nativeKey && mode == Open)
193 return;
194 if (!isKeyTypeSupported(key.type())) {
195 d->setError(KeyError, tr("%1: unsupported key type")
196 .arg("QSystemSemaphore::setNativeKey"_L1));
197 return;
198 }
199
200 d->clearError();
201 d->cleanHandle();
202 if (key.type() == d->nativeKey.type()) {
203 // we can reuse the backend
204 d->nativeKey = key;
205 } else {
206 // we must recreate the backend
207 d->destructBackend();
208 d->nativeKey = key;
209 d->constructBackend();
210 }
211 d->initialValue = initialValue;
212 d->handle(mode);
213
214 d->legacyKey.clear();
215}
216
227QNativeIpcKey QSystemSemaphore::nativeIpcKey() const
228{
229 return d->nativeKey;
230}
231
232#if QT_DEPRECATED_SINCE(6, 10)
244void QSystemSemaphore::setKey(const QString &key, int initialValue, AccessMode mode)
245{
246 setNativeKey(legacyNativeKey(key), initialValue, mode);
247 d->legacyKey = key;
248}
249
257QString QSystemSemaphore::key() const
258{
259 return d->legacyKey;
260}
261#endif
262
276bool QSystemSemaphore::acquire()
277{
278 return d->modifySemaphore(-1);
279}
280
302bool QSystemSemaphore::release(int n)
303{
304 if (n == 0)
305 return true;
306 if (n < 0) {
307 qWarning("QSystemSemaphore::release: n is negative.");
308 return false;
309 }
310 return d->modifySemaphore(n);
311}
312
319QSystemSemaphore::SystemSemaphoreError QSystemSemaphore::error() const
320{
321 return d->error;
322}
323
354QString QSystemSemaphore::errorString() const
355{
356 return d->errorString;
357}
358
359void QSystemSemaphorePrivate::setUnixErrorString(QLatin1StringView function)
360{
361 // EINVAL is handled in functions so they can give better error strings
362 switch (errno) {
363 case EPERM:
364 case EACCES:
365 errorString = QSystemSemaphore::tr("%1: permission denied").arg(function);
366 error = QSystemSemaphore::PermissionDenied;
367 break;
368 case EEXIST:
369 errorString = QSystemSemaphore::tr("%1: already exists").arg(function);
370 error = QSystemSemaphore::AlreadyExists;
371 break;
372 case ENOENT:
373 errorString = QSystemSemaphore::tr("%1: does not exist").arg(function);
374 error = QSystemSemaphore::NotFound;
375 break;
376 case ERANGE:
377 case ENOSPC:
378 case EMFILE:
379 errorString = QSystemSemaphore::tr("%1: out of resources").arg(function);
380 error = QSystemSemaphore::OutOfResources;
381 break;
382 case ENAMETOOLONG:
383 errorString = QSystemSemaphore::tr("%1: key too long").arg(function);
384 error = QSystemSemaphore::KeyError;
385 break;
386 default:
387 errorString = QSystemSemaphore::tr("%1: unknown error: %2")
388 .arg(function, qt_error_string(errno));
389 error = QSystemSemaphore::UnknownError;
390#if defined QSYSTEMSEMAPHORE_DEBUG
391 qDebug() << errorString << "key" << key << "errno" << errno << EINVAL;
392#endif
393 }
394}
395
396bool QSystemSemaphore::isKeyTypeSupported(QNativeIpcKey::Type type)
397{
398 if (!isIpcSupported(IpcType::SystemSemaphore, type))
399 return false;
400 using Variant = decltype(QSystemSemaphorePrivate::backend);
401 return Variant::staticVisit(type, [](auto ptr) {
402 using Impl = std::decay_t<decltype(*ptr)>;
403 return Impl::runtimeSupportCheck();
404 });
405}
406
407QNativeIpcKey QSystemSemaphore::platformSafeKey(const QString &key, QNativeIpcKey::Type type)
408{
409 return { QtIpcCommon::platformSafeKey(key, IpcType::SystemSemaphore, type), type };
410}
411
412QNativeIpcKey QSystemSemaphore::legacyNativeKey(const QString &key, QNativeIpcKey::Type type)
413{
414 return { legacyPlatformSafeKey(key, IpcType::SystemSemaphore, type), type };
415}
416
418
419#include "moc_qsystemsemaphore.cpp"
420
421#endif // QT_CONFIG(systemsemaphore)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
Combined button and popup list for selecting options.
Q_MULTIMEDIA_EXPORT QString errorString(HRESULT hr)
T * construct_at(T *ptr, Args &&... args)
Definition q20memory.h:41
DBusConnection const char DBusError * error
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
#define qDebug
[1]
Definition qlogging.h:160
#define qWarning
Definition qlogging.h:162
static ControlElement< T > * ptr(QWidget *widget)
GLenum mode
GLuint64 key
GLenum type
GLfloat n
GLfloat GLfloat p
[1]
SSL_CTX int(*) void arg)
#define tr(X)