summaryrefslogtreecommitdiffstats
path: root/src/corelib/ipc/qsystemsemaphore.h
blob: df6fd28342c5784e2ace65078808853313757a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QSYSTEMSEMAPHORE_H
#define QSYSTEMSEMAPHORE_H

#include <QtCore/qcoreapplication.h>
#include <QtCore/qtipccommon.h>
#include <QtCore/qstring.h>
#include <QtCore/qscopedpointer.h>

QT_BEGIN_NAMESPACE

#if QT_CONFIG(systemsemaphore)

class QSystemSemaphorePrivate;

class Q_CORE_EXPORT QSystemSemaphore
{
    Q_GADGET
    Q_DECLARE_TR_FUNCTIONS(QSystemSemaphore)
public:
    enum AccessMode
    {
        Open,
        Create
    };
    Q_ENUM(AccessMode)

    enum SystemSemaphoreError
    {
        NoError,
        PermissionDenied,
        KeyError,
        AlreadyExists,
        NotFound,
        OutOfResources,
        UnknownError
    };

    QSystemSemaphore(const QNativeIpcKey &key, int initialValue = 0, AccessMode = Open);
    ~QSystemSemaphore();

    void setNativeKey(const QNativeIpcKey &key, int initialValue = 0, AccessMode = Open);
    void setNativeKey(const QString &key, int initialValue = 0, AccessMode mode = Open,
                      QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs())
    { setNativeKey({ key, type }, initialValue, mode); }
    QNativeIpcKey nativeIpcKey() const;

    QSystemSemaphore(const QString &key, int initialValue = 0, AccessMode mode = Open);
    void setKey(const QString &key, int initialValue = 0, AccessMode mode = Open);
    QString key() const;

    bool acquire();
    bool release(int n = 1);

    SystemSemaphoreError error() const;
    QString errorString() const;

    static bool isKeyTypeSupported(QNativeIpcKey::Type type) Q_DECL_CONST_FUNCTION;
    static QNativeIpcKey platformSafeKey(const QString &key,
            QNativeIpcKey::Type type = QNativeIpcKey::DefaultTypeForOs);
    static QNativeIpcKey legacyNativeKey(const QString &key,
            QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs());

private:
    Q_DISABLE_COPY(QSystemSemaphore)
    QScopedPointer<QSystemSemaphorePrivate> d;
};

#endif // QT_CONFIG(systemsemaphore)

QT_END_NAMESPACE

#endif // QSYSTEMSEMAPHORE_H