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// Qt-Security score:significant reason:default
4
5#ifndef QSYSTEMSEMAPHORE_H
6#define QSYSTEMSEMAPHORE_H
7
8#include <QtCore/qcoreapplication.h>
9#include <QtCore/qtipccommon.h>
10#include <QtCore/qstring.h>
11#include <QtCore/qscopedpointer.h>
12
13#include <memory>
14
15QT_BEGIN_NAMESPACE
16
17#if QT_CONFIG(systemsemaphore)
18
19class QSystemSemaphorePrivate;
20
21class Q_CORE_EXPORT QSystemSemaphore
22{
23 Q_GADGET
24 Q_DECLARE_TR_FUNCTIONS(QSystemSemaphore)
25public:
26 enum AccessMode
27 {
28 Open,
29 Create
30 };
31 Q_ENUM(AccessMode)
32
33 enum SystemSemaphoreError
34 {
35 NoError,
36 PermissionDenied,
37 KeyError,
38 AlreadyExists,
39 NotFound,
40 OutOfResources,
41 UnknownError
42 };
43
44 QSystemSemaphore(const QNativeIpcKey &key, int initialValue = 0, AccessMode = Open);
45 ~QSystemSemaphore();
46
47 void setNativeKey(const QNativeIpcKey &key, int initialValue = 0, AccessMode = Open);
48 void setNativeKey(const QString &key, int initialValue = 0, AccessMode mode = Open,
49 QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs())
50 { setNativeKey(key: { key, type }, initialValue, mode); }
51 QNativeIpcKey nativeIpcKey() const;
52
53 QSystemSemaphore(const QString &key, int initialValue = 0, AccessMode mode = Open);
54 void setKey(const QString &key, int initialValue = 0, AccessMode mode = Open);
55 QString key() const;
56
57 bool acquire();
58 bool release(int n = 1);
59
60 SystemSemaphoreError error() const;
61 QString errorString() const;
62
63 static bool isKeyTypeSupported(QNativeIpcKey::Type type) Q_DECL_CONST_FUNCTION;
64 static QNativeIpcKey platformSafeKey(const QString &key,
65 QNativeIpcKey::Type type = QNativeIpcKey::DefaultTypeForOs);
66 static QNativeIpcKey legacyNativeKey(const QString &key,
67 QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs());
68
69private:
70 Q_DISABLE_COPY(QSystemSemaphore)
71 std::unique_ptr<QSystemSemaphorePrivate> d;
72};
73
74#endif // QT_CONFIG(systemsemaphore)
75
76QT_END_NAMESPACE
77
78#endif // QSYSTEMSEMAPHORE_H
79

source code of qtbase/src/corelib/ipc/qsystemsemaphore.h