| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Intel Corporation. |
| 3 | // Copyright (C) 2012 Olivier Goffart <ogoffart@woboq.com> |
| 4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 5 | // Qt-Security score:significant reason:default |
| 6 | |
| 7 | #ifndef QMUTEX_P_H |
| 8 | #define QMUTEX_P_H |
| 9 | |
| 10 | // |
| 11 | // W A R N I N G |
| 12 | // ------------- |
| 13 | // |
| 14 | // This file is not part of the Qt API. It exists for the convenience of |
| 15 | // qmutex.cpp and qmutex_unix.cpp. This header file may change from version to |
| 16 | // version without notice, or even be removed. |
| 17 | // |
| 18 | // We mean it. |
| 19 | // |
| 20 | |
| 21 | #include <QtCore/private/qglobal_p.h> |
| 22 | #include <QtCore/qnamespace.h> |
| 23 | #include <QtCore/qmutex.h> |
| 24 | #include <QtCore/qatomic.h> |
| 25 | #include <QtCore/qdeadlinetimer.h> |
| 26 | |
| 27 | #include "qplatformdefs.h" // _POSIX_VERSION |
| 28 | |
| 29 | #if defined(Q_OS_DARWIN) |
| 30 | # include <mach/semaphore.h> |
| 31 | #elif defined(Q_OS_UNIX) |
| 32 | # include <semaphore.h> |
| 33 | #endif |
| 34 | |
| 35 | struct timespec; |
| 36 | |
| 37 | QT_BEGIN_NAMESPACE |
| 38 | |
| 39 | // ### Qt7 remove this comment block |
| 40 | // We manipulate the pointer to this class in inline, atomic code, |
| 41 | // so syncqt mustn't mark them as private, so ELFVERSION:ignore-next |
| 42 | class QMutexPrivate |
| 43 | { |
| 44 | public: |
| 45 | ~QMutexPrivate(); |
| 46 | QMutexPrivate(); |
| 47 | |
| 48 | bool wait(QDeadlineTimer timeout = QDeadlineTimer::Forever); |
| 49 | void wakeUp() noexcept; |
| 50 | |
| 51 | // Control the lifetime of the privates |
| 52 | QAtomicInt refCount; |
| 53 | int id; |
| 54 | |
| 55 | bool ref() |
| 56 | { |
| 57 | Q_ASSERT(refCount.loadRelaxed() >= 0); |
| 58 | int c; |
| 59 | do { |
| 60 | c = refCount.loadRelaxed(); |
| 61 | if (c == 0) |
| 62 | return false; |
| 63 | } while (!refCount.testAndSetRelaxed(expectedValue: c, newValue: c + 1)); |
| 64 | Q_ASSERT(refCount.loadRelaxed() >= 0); |
| 65 | return true; |
| 66 | } |
| 67 | void deref() |
| 68 | { |
| 69 | Q_ASSERT(refCount.loadRelaxed() >= 0); |
| 70 | if (!refCount.deref()) |
| 71 | release(); |
| 72 | Q_ASSERT(refCount.loadRelaxed() >= 0); |
| 73 | } |
| 74 | void release(); |
| 75 | static QMutexPrivate *allocate(); |
| 76 | |
| 77 | QAtomicInt waiters; // Number of threads waiting on this mutex. (may be offset by -BigNumber) |
| 78 | QAtomicInt possiblyUnlocked; /* Boolean indicating that a timed wait timed out. |
| 79 | When it is true, a reference is held. |
| 80 | It is there to avoid a race that happens if unlock happens right |
| 81 | when the mutex is unlocked. |
| 82 | */ |
| 83 | enum { BigNumber = 0x100000 }; //Must be bigger than the possible number of waiters (number of threads) |
| 84 | void derefWaiters(int value) noexcept; |
| 85 | |
| 86 | //platform specific stuff |
| 87 | #if defined(Q_OS_DARWIN) |
| 88 | semaphore_t mach_semaphore; |
| 89 | #elif defined(Q_OS_UNIX) |
| 90 | sem_t semaphore; |
| 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | QT_END_NAMESPACE |
| 95 | |
| 96 | #endif // QMUTEX_P_H |
| 97 | |