generic: replace QThreadStorage with thread_local where possible

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-05-14 21:51:40 +00:00
parent 4cc545f43b
commit 896476b53a
6 changed files with 84 additions and 48 deletions

View file

@ -212,6 +212,23 @@ struct KDebugPrivate
~KDebugPrivate() ~KDebugPrivate()
{ {
delete config; delete config;
if (m_indentString) {
delete m_indentString;
m_indentString = 0;
}
if (syslogwriter) {
delete syslogwriter;
syslogwriter = 0;
}
if (filewriter) {
delete filewriter;
filewriter = 0;
}
if (messageboxwriter) {
delete messageboxwriter;
messageboxwriter = 0;
}
} }
void loadAreaNames() void loadAreaNames()
@ -402,18 +419,18 @@ struct KDebugPrivate
QDebug setupFileWriter(const QString &fileName) QDebug setupFileWriter(const QString &fileName)
{ {
if (!filewriter.hasLocalData()) if (!filewriter)
filewriter.setLocalData(new KFileDebugStream); filewriter = new KFileDebugStream();
filewriter.localData()->setFileName(fileName); filewriter->setFileName(fileName);
QDebug result(filewriter.localData()); QDebug result(filewriter);
return result; return result;
} }
QDebug setupMessageBoxWriter(QtMsgType type, const QByteArray &areaName) QDebug setupMessageBoxWriter(QtMsgType type, const QByteArray &areaName)
{ {
if (!messageboxwriter.hasLocalData()) if (!messageboxwriter)
messageboxwriter.setLocalData(new KMessageBoxDebugStream); messageboxwriter = new KMessageBoxDebugStream();
QDebug result(messageboxwriter.localData()); QDebug result(messageboxwriter);
QByteArray header; QByteArray header;
switch (type) { switch (type) {
@ -434,15 +451,15 @@ struct KDebugPrivate
header += areaName; header += areaName;
header += ')'; header += ')';
messageboxwriter.localData()->setCaption(QString::fromLatin1(header)); messageboxwriter->setCaption(QString::fromLatin1(header));
return result; return result;
} }
QDebug setupSyslogWriter(QtMsgType type) QDebug setupSyslogWriter(QtMsgType type)
{ {
if (!syslogwriter.hasLocalData()) if (!syslogwriter)
syslogwriter.setLocalData(new KSyslogDebugStream); syslogwriter = new KSyslogDebugStream();
QDebug result(syslogwriter.localData()); QDebug result(syslogwriter);
int level = 0; int level = 0;
switch (type) { switch (type) {
@ -460,7 +477,7 @@ struct KDebugPrivate
level = LOG_ERR; level = LOG_ERR;
break; break;
} }
syslogwriter.localData()->setPriority(level); syslogwriter->setPriority(level);
return result; return result;
} }
@ -516,8 +533,8 @@ struct KDebugPrivate
s << areaName.constData(); s << areaName.constData();
} }
if (m_indentString.hasLocalData()) { if (m_indentString) {
s << m_indentString.localData()->toLatin1().constData(); s << m_indentString->toLatin1().constData();
} }
if (printFileLine) { if (printFileLine) {
@ -652,13 +669,18 @@ struct KDebugPrivate
int m_nullOutputYesNoCache[8]; int m_nullOutputYesNoCache[8];
KNoDebugStream devnull; KNoDebugStream devnull;
QThreadStorage<QString*> m_indentString; static thread_local QString* m_indentString;
QThreadStorage<KSyslogDebugStream*> syslogwriter; static thread_local KSyslogDebugStream* syslogwriter;
QThreadStorage<KFileDebugStream*> filewriter; static thread_local KFileDebugStream* filewriter;
QThreadStorage<KMessageBoxDebugStream*> messageboxwriter; static thread_local KMessageBoxDebugStream* messageboxwriter;
KLineEndStrippingDebugStream lineendstrippingwriter; KLineEndStrippingDebugStream lineendstrippingwriter;
}; };
thread_local QString* KDebugPrivate::m_indentString = 0;
thread_local KSyslogDebugStream* KDebugPrivate::syslogwriter = 0;
thread_local KFileDebugStream* KDebugPrivate::filewriter = 0;
thread_local KMessageBoxDebugStream* KDebugPrivate::messageboxwriter = 0;
K_GLOBAL_STATIC(KDebugPrivate, kDebug_data) K_GLOBAL_STATIC(KDebugPrivate, kDebug_data)
#ifdef HAVE_BACKTRACE #ifdef HAVE_BACKTRACE
@ -850,11 +872,10 @@ KDebug::Block::Block(const char* label, int area)
kDebug(area) << "BEGIN:" << label; kDebug(area) << "BEGIN:" << label;
// The indent string is per thread // The indent string is per thread
QThreadStorage<QString*> & indentString = kDebug_data->m_indentString; if (!kDebug_data->m_indentString) {
if (!indentString.hasLocalData()) { kDebug_data->m_indentString = new QString();
indentString.setLocalData(new QString);
} }
*(indentString.localData()) += QLatin1String(" "); kDebug_data->m_indentString->append(QLatin1String(" "));
} }
} }
@ -862,8 +883,7 @@ KDebug::Block::~Block()
{ {
if (d) { if (d) {
const double duration = m_startTime.elapsed() / 1000.0; const double duration = m_startTime.elapsed() / 1000.0;
QThreadStorage<QString*> & indentString = kDebug_data->m_indentString; kDebug_data->m_indentString->chop(2);
indentString.localData()->chop(2);
// Print timing information, and a special message (DELAY) if the method took longer than 5s // Print timing information, and a special message (DELAY) if the method took longer than 5s
if (duration < 5.0) { if (duration < 5.0) {

View file

@ -102,23 +102,30 @@ class KSycocaSingleton
{ {
public: public:
KSycocaSingleton() { } KSycocaSingleton() { }
~KSycocaSingleton() { } ~KSycocaSingleton()
{
if (m_threadSycocas) {
delete m_threadSycocas;
m_threadSycocas = 0;
}
}
bool hasSycoca() const { bool hasSycoca() const {
return m_threadSycocas.hasLocalData(); return (m_threadSycocas != 0);
} }
KSycoca* sycoca() { KSycoca* sycoca() {
if (!m_threadSycocas.hasLocalData()) if (!m_threadSycocas)
m_threadSycocas.setLocalData(new KSycoca); m_threadSycocas = new KSycoca();
return m_threadSycocas.localData(); return m_threadSycocas;
} }
void setSycoca(KSycoca* s) { void setSycoca(KSycoca* s) {
m_threadSycocas.setLocalData(s); m_threadSycocas = s;
} }
private: private:
QThreadStorage<KSycoca*> m_threadSycocas; static thread_local KSycoca* m_threadSycocas;
}; };
thread_local KSycoca* KSycocaSingleton::m_threadSycocas = 0;
K_GLOBAL_STATIC(KSycocaSingleton, ksycocaInstance) K_GLOBAL_STATIC(KSycocaSingleton, ksycocaInstance)

View file

@ -28,7 +28,6 @@
#include <QSqlField> #include <QSqlField>
#include <QSqlQuery> #include <QSqlQuery>
#include <QSqlRecord> #include <QSqlRecord>
#include <QThreadStorage>
//KDE //KDE
#include <kdebug.h> #include <kdebug.h>

View file

@ -32,6 +32,8 @@
Q_GLOBAL_STATIC(Solid::DeviceManagerStorage, globalDeviceStorage) Q_GLOBAL_STATIC(Solid::DeviceManagerStorage, globalDeviceStorage)
thread_local Solid::DeviceManagerPrivate* Solid::DeviceManagerStorage::m_storage = 0;
Solid::DeviceManagerPrivate::DeviceManagerPrivate() Solid::DeviceManagerPrivate::DeviceManagerPrivate()
: m_nullDevice(new DevicePrivate(QString())) : m_nullDevice(new DevicePrivate(QString()))
{ {
@ -269,22 +271,30 @@ Solid::DeviceManagerStorage::DeviceManagerStorage()
} }
Solid::DeviceManagerStorage::~DeviceManagerStorage()
{
if (m_storage) {
delete m_storage;
m_storage = 0;
}
}
QList<QObject*> Solid::DeviceManagerStorage::managerBackends() QList<QObject*> Solid::DeviceManagerStorage::managerBackends()
{ {
ensureManagerCreated(); ensureManagerCreated();
return m_storage.localData()->managerBackends(); return m_storage->managerBackends();
} }
Solid::DeviceNotifier *Solid::DeviceManagerStorage::notifier() Solid::DeviceNotifier *Solid::DeviceManagerStorage::notifier()
{ {
ensureManagerCreated(); ensureManagerCreated();
return m_storage.localData(); return m_storage;
} }
void Solid::DeviceManagerStorage::ensureManagerCreated() void Solid::DeviceManagerStorage::ensureManagerCreated()
{ {
if (!m_storage.hasLocalData()) { if (!m_storage) {
m_storage.setLocalData(new DeviceManagerPrivate()); m_storage = new DeviceManagerPrivate();
} }
} }

View file

@ -28,7 +28,6 @@
#include <QtCore/QMap> #include <QtCore/QMap>
#include <QtCore/qsharedpointer.h> #include <QtCore/qsharedpointer.h>
#include <QtCore/QSharedData> #include <QtCore/QSharedData>
#include <QtCore/QThreadStorage>
namespace Solid namespace Solid
{ {
@ -65,6 +64,7 @@ namespace Solid
{ {
public: public:
DeviceManagerStorage(); DeviceManagerStorage();
~DeviceManagerStorage();
QList<QObject*> managerBackends(); QList<QObject*> managerBackends();
DeviceNotifier *notifier(); DeviceNotifier *notifier();
@ -72,7 +72,7 @@ namespace Solid
private: private:
void ensureManagerCreated(); void ensureManagerCreated();
QThreadStorage<DeviceManagerPrivate*> m_storage; static thread_local DeviceManagerPrivate* m_storage;
}; };
} }

View file

@ -31,7 +31,6 @@ void PredicateParse_mainParse(const char *_code);
#include <stdlib.h> #include <stdlib.h>
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include <QtCore/QThreadStorage>
namespace Solid namespace Solid
{ {
@ -51,12 +50,12 @@ struct ParsingData
} }
} }
Q_GLOBAL_STATIC(QThreadStorage<Solid::PredicateParse::ParsingData *>, s_parsingData) thread_local Solid::PredicateParse::ParsingData *s_parsingData = 0;
Solid::Predicate Solid::Predicate::fromString(const QString &predicate) Solid::Predicate Solid::Predicate::fromString(const QString &predicate)
{ {
Solid::PredicateParse::ParsingData *data = new Solid::PredicateParse::ParsingData(); Solid::PredicateParse::ParsingData *data = new Solid::PredicateParse::ParsingData();
s_parsingData()->setLocalData(data); s_parsingData = data;
data->buffer = predicate.toLatin1(); data->buffer = predicate.toLatin1();
PredicateParse_mainParse(data->buffer.constData()); PredicateParse_mainParse(data->buffer.constData());
Predicate result; Predicate result;
@ -65,26 +64,27 @@ Solid::Predicate Solid::Predicate::fromString(const QString &predicate)
result = Predicate(*data->result); result = Predicate(*data->result);
delete data->result; delete data->result;
} }
s_parsingData()->setLocalData(0); delete s_parsingData;
s_parsingData = 0;;
return result; return result;
} }
void PredicateParse_setResult(void *result) void PredicateParse_setResult(void *result)
{ {
Solid::PredicateParse::ParsingData *data = s_parsingData()->localData(); Solid::PredicateParse::ParsingData *data = s_parsingData;
data->result = (Solid::Predicate *) result; data->result = (Solid::Predicate *) result;
} }
void PredicateParse_errorDetected(const char* s) void PredicateParse_errorDetected(const char* s)
{ {
qWarning("ERROR from solid predicate parser: %s", s); qWarning("ERROR from solid predicate parser: %s", s);
s_parsingData()->localData()->result = 0; s_parsingData->result = 0;
} }
void PredicateParse_destroy(void *pred) void PredicateParse_destroy(void *pred)
{ {
Solid::PredicateParse::ParsingData *data = s_parsingData()->localData(); Solid::PredicateParse::ParsingData *data = s_parsingData;
Solid::Predicate *p = (Solid::Predicate *) pred; Solid::Predicate *p = (Solid::Predicate *) pred;
if (p != data->result) { if (p != data->result) {
delete p; delete p;
@ -138,7 +138,7 @@ void *PredicateParse_newAnd(void *pred1, void *pred2)
{ {
Solid::Predicate *result = new Solid::Predicate(); Solid::Predicate *result = new Solid::Predicate();
Solid::PredicateParse::ParsingData *data = s_parsingData()->localData(); Solid::PredicateParse::ParsingData *data = s_parsingData;
Solid::Predicate *p1 = (Solid::Predicate *)pred1; Solid::Predicate *p1 = (Solid::Predicate *)pred1;
Solid::Predicate *p2 = (Solid::Predicate *)pred2; Solid::Predicate *p2 = (Solid::Predicate *)pred2;
@ -159,7 +159,7 @@ void *PredicateParse_newOr(void *pred1, void *pred2)
{ {
Solid::Predicate *result = new Solid::Predicate(); Solid::Predicate *result = new Solid::Predicate();
Solid::PredicateParse::ParsingData *data = s_parsingData()->localData(); Solid::PredicateParse::ParsingData *data = s_parsingData;
Solid::Predicate *p1 = (Solid::Predicate *)pred1; Solid::Predicate *p1 = (Solid::Predicate *)pred1;
Solid::Predicate *p2 = (Solid::Predicate *)pred2; Solid::Predicate *p2 = (Solid::Predicate *)pred2;
@ -239,5 +239,5 @@ void *PredicateParse_appendStringListValue(char *name, void *list)
void PredicateLexer_unknownToken(const char* text) void PredicateLexer_unknownToken(const char* text)
{ {
qWarning("ERROR from solid predicate parser: unrecognized token '%s' in predicate '%s'\n", qWarning("ERROR from solid predicate parser: unrecognized token '%s' in predicate '%s'\n",
text, s_parsingData()->localData()->buffer.constData()); text, s_parsingData->buffer.constData());
} }