Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmutex_p.h
Go to the documentation of this file.
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
6#ifndef QMUTEX_P_H
7#define QMUTEX_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists for the convenience of
14// qmutex.cpp and qmutex_unix.cpp. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtCore/private/qglobal_p.h>
21#include <QtCore/qnamespace.h>
22#include <QtCore/qmutex.h>
23#include <QtCore/qatomic.h>
24#include <QtCore/qdeadlinetimer.h>
25
26#include "qplatformdefs.h" // _POSIX_VERSION
27
28#if defined(Q_OS_DARWIN)
29# include <mach/semaphore.h>
30#elif defined(Q_OS_UNIX)
31# include <semaphore.h>
32#endif
33
34struct timespec;
35
36QT_BEGIN_NAMESPACE
37
38// ### Qt7 remove this comment block
39// We manipulate the pointer to this class in inline, atomic code,
40// so syncqt mustn't mark them as private, so ELFVERSION:ignore-next
41class QMutexPrivate
42{
43public:
44 ~QMutexPrivate();
45 QMutexPrivate();
46
47 bool wait(QDeadlineTimer timeout = QDeadlineTimer::Forever);
48 void wakeUp() noexcept;
49
50 // Control the lifetime of the privates
51 QAtomicInt refCount;
52 int id;
53
54 bool ref()
55 {
56 Q_ASSERT(refCount.loadRelaxed() >= 0);
57 int c;
58 do {
59 c = refCount.loadRelaxed();
60 if (c == 0)
61 return false;
62 } while (!refCount.testAndSetRelaxed(c, c + 1));
63 Q_ASSERT(refCount.loadRelaxed() >= 0);
64 return true;
65 }
66 void deref()
67 {
68 Q_ASSERT(refCount.loadRelaxed() >= 0);
69 if (!refCount.deref())
70 release();
71 Q_ASSERT(refCount.loadRelaxed() >= 0);
72 }
73 void release();
74 static QMutexPrivate *allocate();
75
76 QAtomicInt waiters; // Number of threads waiting on this mutex. (may be offset by -BigNumber)
77 QAtomicInt possiblyUnlocked; /* Boolean indicating that a timed wait timed out.
78 When it is true, a reference is held.
79 It is there to avoid a race that happens if unlock happens right
80 when the mutex is unlocked.
81 */
82 enum { BigNumber = 0x100000 }; //Must be bigger than the possible number of waiters (number of threads)
83 void derefWaiters(int value) noexcept;
84
85 //platform specific stuff
86#if defined(Q_OS_DARWIN)
87 semaphore_t mach_semaphore;
88#elif defined(Q_OS_UNIX)
89 sem_t semaphore;
90#endif
91};
92
94
95#endif // QMUTEX_P_H
\inmodule QtCore
Definition qmutex.h:328
Combined button and popup list for selecting options.
constexpr bool futexAvailable()
Definition qfutex_p.h:24
void futexWakeAll(Atomic &)
Definition qfutex_p.h:30
bool futexWait(Atomic &, typename Atomic::Type, QDeadlineTimer={})
Definition qfutex_p.h:26
void futexWakeOne(Atomic &)
Definition qfutex_p.h:28
static QMutexPrivate * dummyFutexValue()
Definition qmutex.cpp:22
QMutex QBasicMutex
Definition qmutex.h:346