5#ifndef QTHREADSTORAGE_H
6#define QTHREADSTORAGE_H
8#include <QtCore/qglobal.h>
18template <
bool ShouldWarn>
struct QThreadStorageTraits
20 static constexpr void warnAboutTrivial() {}
22template <>
struct QThreadStorageTraits<
true>
24#ifndef Q_NO_THREAD_STORAGE_TRIVIAL_WARNING
25 Q_DECL_DEPRECATED_X(
"QThreadStorage used with a trivial non-pointer type; consider using thread_local")
27 static constexpr void warnAboutTrivial()
noexcept {}
30class Q_CORE_EXPORT QThreadStorageData
33 explicit QThreadStorageData(
void (*func)(
void *));
34 ~QThreadStorageData();
45T *&qThreadStorage_localData(QThreadStorageData &d, T **)
48 if (!v) v = d.set(
nullptr);
49 return *(
reinterpret_cast<T**>(v));
54T *qThreadStorage_localData_const(
const QThreadStorageData &d, T **)
57 return v ? *(
reinterpret_cast<T**>(v)) : 0;
62void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
67void qThreadStorage_deleteData(
void *d, T **)
68{
delete static_cast<T *>(d); }
73T &qThreadStorage_localData(QThreadStorageData &d, T *)
76 if (!v) v = d.set(
new T());
77 return *(
reinterpret_cast<T*>(*v));
82T qThreadStorage_localData_const(
const QThreadStorageData &d, T *)
85 return v ? *(
reinterpret_cast<T*>(*v)) : T();
90void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
91{ (
void) d.set(
new T(*t)); }
95void qThreadStorage_deleteData(
void *d, T *)
96{
delete static_cast<T *>(d); }
102 using Trait = QThreadStorageTraits<std::is_trivially_default_constructible_v<T> &&
103 std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>>;
104 QThreadStorageData d;
106 Q_DISABLE_COPY(QThreadStorage)
108 static inline void deleteData(
void *x)
109 { qThreadStorage_deleteData(x,
reinterpret_cast<T*>(0)); }
112 inline QThreadStorage() : d(deleteData) { Trait::warnAboutTrivial(); }
113 inline ~QThreadStorage() { }
115 inline bool hasLocalData()
const
116 {
return d.get() !=
nullptr; }
118 inline T& localData()
119 {
return qThreadStorage_localData(d,
reinterpret_cast<T*>(0)); }
120 inline T localData()
const
121 {
return qThreadStorage_localData_const(d,
reinterpret_cast<T*>(0)); }
123 inline void setLocalData(T t)
124 { qThreadStorage_setLocalData(d, &t); }
129template <
typename T,
typename U>
130inline bool qThreadStorage_hasLocalData(
const std::unique_ptr<T, U> &data)
135template <
typename T,
typename U>
138 return !!data ? *data !=
nullptr :
false;
158 struct ScopedPointerThreadStorageDeleter
160 void operator()(T *t)
const noexcept
164 qThreadStorage_deleteLocalData(t);
167 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
177 return qThreadStorage_hasLocalData(data);
189 return !!data ? *data : T();
194 data.reset(
new T(t));
QDeferredDeleteEvent(int loopLevel, int scopeLevel)
Constructs a deferred delete event with the given loop and scope level.
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
void setLocalData(T t)
Sets the local data for the calling thread to data.
~QThreadStorage()=default
Destroys the per-thread data storage object.
QThreadStorage()=default
Constructs a new per-thread data storage object.
T localData() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & localData()
Returns a reference to the data that was set by the calling thread.
QThreadStorage(const QThreadStorage &rhs)=delete
bool hasLocalData() const
If T is a pointer type, returns true if the calling thread has non-zero data available.
QThreadStorage & operator=(const QThreadStorage &rhs)=delete
Q_TRACE_POINT(qtcore, QCoreApplication_notify_exit, bool consumed, bool filtered)
void QT_MANGLE_NAMESPACE qt_startup_hook()
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
QString qtTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_exit)
static void qt_call_pre_routines()
qsizetype qGlobalPostedEventsCount()
static Q_CONSTINIT bool preRoutinesCalled
QList< QtCleanUpFunction > QVFuncList
static void replacePercentN(QString *result, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_event_compressed, QObject *receiver, QEvent *event)
QString qTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_entry, QObject *receiver, QEvent *event, QEvent::Type type)
Q_TRACE_PREFIX(qtcore, "#include <qcoreevent.h>")
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
#define Q_EVENT_DISABLE_COPY(Class)
#define Q_DECL_EVENT_COMMON(Class)
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)
QString applicationVersion
QCoreApplicationData() noexcept
bool applicationVersionSet