8#include <QtCore/qglobal.h>
9#include <QtCore/qatomic.h>
10#include <QtCore/qdeadlinetimer.h>
11#include <QtCore/qtsan_impl.h>
17#if QT_CONFIG(thread) || defined(Q_QDOC)
23class QT6_ONLY(Q_CORE_EXPORT) QBasicMutex
25 Q_DISABLE_COPY_MOVE(QBasicMutex)
27 static constexpr bool FutexAlwaysAvailable =
28#if defined(Q_OS_FREEBSD) || defined(Q_OS_LINUX) || defined(Q_OS_WIN)
36 constexpr QBasicMutex()
41 inline void lock()
noexcept(FutexAlwaysAvailable) {
42 QtTsan::mutexPreLock(
this, 0u);
47 QtTsan::mutexPostLock(
this, 0u, 0);
51 inline void unlock()
noexcept {
52 Q_ASSERT(d_ptr.loadRelaxed());
54 QtTsan::mutexPreUnlock(
this, 0u);
56 if constexpr (FutexAlwaysAvailable) {
58 if (QMutexPrivate *d = d_ptr.fetchAndStoreRelease(
nullptr); Q_UNLIKELY(d != dummyLocked()))
59 unlockInternalFutex(d);
62 if (QMutexPrivate *d; !d_ptr.testAndSetRelease(dummyLocked(),
nullptr, d))
66 QtTsan::mutexPostUnlock(
this, 0u);
69 bool tryLock()
noexcept {
70 unsigned tsanFlags = QtTsan::TryLock;
71 QtTsan::mutexPreLock(
this, tsanFlags);
73 const bool success = fastTryLock();
76 tsanFlags |= QtTsan::TryLockFailed;
77 QtTsan::mutexPostLock(
this, tsanFlags, 0);
83 bool try_lock()
noexcept {
return tryLock(); }
86 inline bool fastTryLock()
noexcept
88 if (Q_UNLIKELY(d_ptr.loadRelaxed() !=
nullptr))
90 return d_ptr.testAndSetAcquire(
nullptr, dummyLocked());
92#if QT_CORE_REMOVED_SINCE(6
, 10
)
93 inline bool fastTryUnlock()
noexcept {
94 return d_ptr.testAndSetRelease(dummyLocked(),
nullptr);
98 QT7_ONLY(Q_CORE_EXPORT)
99 void lockInternal()
noexcept(FutexAlwaysAvailable);
100 QT7_ONLY(Q_CORE_EXPORT)
101 bool lockInternal(QDeadlineTimer timeout)
noexcept(FutexAlwaysAvailable);
102#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
103 bool lockInternal(
int timeout)
noexcept(FutexAlwaysAvailable);
104 void unlockInternal()
noexcept;
106 QT7_ONLY(Q_CORE_EXPORT)
107 void unlockInternalFutex(
void *d)
noexcept;
108 QT7_ONLY(Q_CORE_EXPORT)
109 void unlockInternal(
void *d)
noexcept;
110#if QT_CORE_REMOVED_SINCE(6
, 9
)
111 void destroyInternal(QMutexPrivate *d);
113 QT7_ONLY(Q_CORE_EXPORT)
114 void destroyInternal(
void *d);
116 QBasicAtomicPointer<QMutexPrivate> d_ptr;
117 static inline QMutexPrivate *dummyLocked() {
118 return reinterpret_cast<QMutexPrivate *>(quintptr(1));
122 friend class QMutexPrivate;
125class QT6_ONLY(Q_CORE_EXPORT) QMutex :
public QBasicMutex
128 constexpr QMutex() =
default;
131 QMutexPrivate *d = d_ptr.loadRelaxed();
137 inline void lock()
noexcept(FutexAlwaysAvailable);
138 inline void unlock()
noexcept;
139 bool tryLock()
noexcept;
143 bool try_lock()
noexcept {
return tryLock(); }
146 using QBasicMutex::tryLock;
147 bool tryLock(
int timeout)
noexcept(FutexAlwaysAvailable)
149 return tryLock(QDeadlineTimer(timeout));
152 bool tryLock(QDeadlineTimer timeout)
noexcept(FutexAlwaysAvailable)
154 unsigned tsanFlags = QtTsan::TryLock;
155 QtTsan::mutexPreLock(
this, tsanFlags);
157 bool success = fastTryLock();
160 QtTsan::mutexPostLock(
this, tsanFlags, 0);
164 success = lockInternal(timeout);
167 tsanFlags |= QtTsan::TryLockFailed;
168 QtTsan::mutexPostLock(
this, tsanFlags, 0);
174 template <
class Rep,
class Period>
175 bool try_lock_for(std::chrono::duration<Rep, Period> duration)
177 return tryLock(QDeadlineTimer(duration));
181 template<
class Clock,
class Duration>
182 bool try_lock_until(std::chrono::time_point<Clock, Duration> timePoint)
184 return tryLock(QDeadlineTimer(timePoint));
188class QT6_ONLY(Q_CORE_EXPORT) QRecursiveMutex
190 Q_DISABLE_COPY_MOVE(QRecursiveMutex)
193 QAtomicPointer<
void> owner =
nullptr;
197 static constexpr bool LockIsNoexcept =
noexcept(std::declval<QMutex>().lock());
200 constexpr QRecursiveMutex() =
default;
201 QT7_ONLY(Q_CORE_EXPORT)
206 void lock()
noexcept(LockIsNoexcept)
207 { tryLock(QDeadlineTimer(QDeadlineTimer::Forever)); }
208 QT_CORE_INLINE_SINCE(6, 6)
209 bool tryLock(
int timeout)
noexcept(LockIsNoexcept);
210 QT7_ONLY(Q_CORE_EXPORT)
211 bool tryLock(QDeadlineTimer timer = {})
noexcept(LockIsNoexcept);
213 QT7_ONLY(Q_CORE_EXPORT)
214 void unlock()
noexcept;
217 bool try_lock()
noexcept(LockIsNoexcept) {
return tryLock(); }
220 template <
class Rep,
class Period>
221 bool try_lock_for(std::chrono::duration<Rep, Period> duration)
223 return tryLock(QDeadlineTimer(duration));
227 template<
class Clock,
class Duration>
228 bool try_lock_until(std::chrono::time_point<Clock, Duration> timePoint)
230 return tryLock(QDeadlineTimer(timePoint));
234#if QT_CORE_INLINE_IMPL_SINCE(6
, 6
)
235bool QRecursiveMutex::tryLock(
int timeout)
noexcept(LockIsNoexcept)
237 return tryLock(QDeadlineTimer(timeout));
241template <
typename Mutex>
246 static constexpr bool LockIsNoexcept =
false;
248 static constexpr bool LockIsNoexcept =
noexcept(std::declval<Mutex>().lock());
252 inline explicit QMutexLocker(Mutex *mutex)
noexcept(LockIsNoexcept)
255 if (Q_LIKELY(mutex)) {
262 inline QMutexLocker(QMutexLocker &&other)
noexcept
263 : m_mutex(std::exchange(other.m_mutex,
nullptr)),
264 m_isLocked(std::exchange(other.m_isLocked,
false))
267 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QMutexLocker)
269 inline ~QMutexLocker()
275 inline bool isLocked()
const noexcept
280 inline void unlock()
noexcept
282 Q_ASSERT(m_isLocked);
287 inline void relock()
noexcept(LockIsNoexcept)
289 Q_ASSERT(!m_isLocked);
294 inline void swap(QMutexLocker &other)
noexcept
296 qt_ptr_swap(m_mutex, other.m_mutex);
297 std::swap(m_isLocked, other.m_isLocked);
305 Q_DISABLE_COPY(QMutexLocker)
308 bool m_isLocked =
false;
317 constexpr QMutex()
noexcept { }
319 inline void lock()
noexcept {}
320 inline bool tryLock(
int timeout = 0)
noexcept { Q_UNUSED(timeout);
return true; }
321 inline bool try_lock()
noexcept {
return true; }
322 inline void unlock()
noexcept {}
324 template <
class Rep,
class Period>
325 inline bool try_lock_for(
std::chrono::duration<Rep, Period> duration)
noexcept
331 template<
class Clock,
class Duration>
332 inline bool try_lock_until(
std::chrono::time_point<Clock, Duration> timePoint)
noexcept
339 Q_DISABLE_COPY(QMutex)
344template <
typename Mutex>
354 inline Mutex *
mutex()
const noexcept {
return nullptr; }
QByteArray & operator*() noexcept
QByteArray::Base64DecodingStatus decodingStatus
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
void swap(QByteArray::FromBase64Result &other) noexcept
operator bool() const noexcept
\variable QByteArray::FromBase64Result::decoded
const QByteArray & operator*() const noexcept
Returns the decoded byte array.
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
\inmodule QtCore\reentrant
int initFrom(const QMessageLogContext &logContext)
void populateBacktrace(int frameCount)
QInternalMessageLogContext(const QMessageLogContext &logContext, const QLoggingCategory &categoryOverride)
std::optional< BacktraceStorage > backtrace
static constexpr int DefaultBacktraceDepth
constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
constexpr QMessageLogContext() noexcept=default
QDebug debug(CategoryFunction catFunc) const
QDebug debug(const QLoggingCategory &cat) const
Logs a debug message into category cat using a QDebug stream.
void void void void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION void QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION void QDebug debug() const
Logs a debug message using a QDebug stream.
QDebug info(const QLoggingCategory &cat) const
Logs an informational message into the category cat using a QDebug stream.
QDebug info() const
Logs an informational message using a QDebug stream.
QNoDebug noDebug(...) const noexcept
QDebug info(CategoryFunction catFunc) const
Mutex * mutex() const noexcept
Returns the mutex on which the QMutexLocker is operating.
void unlock() noexcept
Unlocks this mutex locker.
~QMutexLocker() noexcept
Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.
void relock() noexcept
Relocks an unlocked mutex locker.
static Q_CONSTINIT thread_local bool msgHandlerGrabbed
static const char ifCriticalTokenC[]
static bool grabMessageHandler()
void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
static const char emptyTokenC[]
static Q_NEVER_INLINE void qt_message(QtMsgType msgType, const QMessageLogContext &context, const char *msg, va_list ap)
static void preformattedMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static bool systemHasStderr()
Returns true if writing to stderr is supported.
static const char endifTokenC[]
static bool isDefaultCategory(const char *category)
static const char messageTokenC[]
static bool qt_append_thread_name_to(QString &message)
static constexpr SystemMessageSink systemMessageSink
static void qt_maybe_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message)
\inmodule QtCore \title Qt Logging Types
#define HANDLE_IF_TOKEN(LEVEL)
Q_DECLARE_TYPEINFO(QMessagePattern::BacktraceParams, Q_RELOCATABLE_TYPE)
static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &buf)
static const char timeTokenC[]
static bool isFatalCountDown(const char *varname, QBasicAtomicInt &n)
void qErrnoWarning(int code, const char *msg,...)
static const char qthreadptrTokenC[]
static const char fileTokenC[]
static const char ifDebugTokenC[]
static const char ifFatalTokenC[]
static const char categoryTokenC[]
static void stderr_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static const char lineTokenC[]
static const char typeTokenC[]
static void ungrabMessageHandler()
static void copyInternalContext(QInternalMessageLogContext *self, const QMessageLogContext &logContext) noexcept
static const char ifCategoryTokenC[]
static int checked_var_value(const char *varname)
static const char threadnameTokenC[]
static const char pidTokenC[]
Q_TRACE_POINT(qtcore, qt_message_print, int type, const char *category, const char *function, const char *file, int line, const QString &message)
static const char threadidTokenC[]
static QString formatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
static const char backtraceTokenC[]
void qErrnoWarning(const char *msg,...)
static const char functionTokenC[]
static const char ifWarningTokenC[]
static const char appnameTokenC[]
static bool isFatal(QtMsgType msgType)
static const char ifInfoTokenC[]
QtMessageHandler qInstallMessageHandler(QtMessageHandler h)
static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message)
static bool stderrHasConsoleAttached()
Returns true if writing to stderr will end up in a console/terminal visible to the user.
void qSetMessagePattern(const QString &pattern)
QDebug printAssociativeContainer(QDebug debug, const char *which, const AssociativeContainer &c)
bool shouldLogToStderr()
Returns true if logging stderr should be ensured.
QDebug printSequentialContainer(QDebug debug, const char *which, const SequentialContainer &c)
QByteArray operator""_ba(const char *str, size_t size) noexcept
QT_BEGIN_NAMESPACE Q_NORETURN void qAbort()
QByteArray operator+(const QByteArray &a1, const char *a2)
QByteArray qUncompress(const QByteArray &data)
QByteArray operator+(char a1, const QByteArray &a2)
QByteArray operator+(QByteArray &&lhs, char rhs)
QByteArray operator+(const QByteArray &a1, char a2)
QByteArray operator+(const char *a1, const QByteArray &a2)
QByteArray operator+(QByteArray &&lhs, const QByteArray &rhs)
qsizetype erase_if(QByteArray &ba, Predicate pred)
QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
QByteArray qCompress(const QByteArray &data, int compressionLevel=-1)
qsizetype erase(QByteArray &ba, const T &t)
QByteArray operator+(QByteArray &&lhs, const char *rhs)
#define __has_cpp_attribute(x)
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value)
Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
#define QT_MESSAGELOG_FUNC
#define QT_MESSAGELOG_FILE
#define QT_MESSAGE_LOGGER_NORETURN
#define QT_MESSAGELOG_LINE
Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern)
#define QT_MESSAGELOGCONTEXT
Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, const QString &message)
void(* QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &)
#define Q_LOGGING_CATEGORY(name,...)
#define QT_MESSAGE_LOGGER_COMMON(category, level)
#define Q_DECLARE_LOGGING_CATEGORY(name)
QString backtraceSeparator
void setPattern(const QString &pattern)
std::unique_ptr< std::unique_ptr< const char[]>[]> literals
std::chrono::steady_clock::time_point appStartTime
std::unique_ptr< const char *[]> tokens
QList< QString > timeArgs
static constexpr bool Value
static constexpr bool Value