mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 10:22:48 +00:00
kdecore: replace QThreadStorage with thread_local
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
parent
896476b53a
commit
b0c48bbe79
9 changed files with 46 additions and 97 deletions
|
@ -52,7 +52,6 @@
|
|||
#include <QtCore/QObject>
|
||||
#include <QtCore/QChar>
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QtCore/QThreadStorage>
|
||||
#include <QtCore/QMutex>
|
||||
|
||||
#include <stdlib.h> // abort
|
||||
|
|
|
@ -24,23 +24,26 @@
|
|||
|
||||
extern int servicesDebugArea();
|
||||
|
||||
K_GLOBAL_STATIC(KSycocaFactorySingleton<KMimeTypeFactory>, kMimeTypeFactoryInstance)
|
||||
thread_local KMimeTypeFactory* kMimeTypeFactoryInstance = 0;
|
||||
|
||||
KMimeTypeFactory::KMimeTypeFactory()
|
||||
: KSycocaFactory( KST_KMimeTypeFactory )
|
||||
{
|
||||
kMimeTypeFactoryInstance->instanceCreated(this);
|
||||
kMimeTypeFactoryInstance = this;
|
||||
}
|
||||
|
||||
KMimeTypeFactory::~KMimeTypeFactory()
|
||||
{
|
||||
if (kMimeTypeFactoryInstance.exists())
|
||||
kMimeTypeFactoryInstance->instanceDestroyed(this);
|
||||
if (kMimeTypeFactoryInstance) {
|
||||
kMimeTypeFactoryInstance = 0;
|
||||
}
|
||||
}
|
||||
|
||||
KMimeTypeFactory * KMimeTypeFactory::self()
|
||||
{
|
||||
return kMimeTypeFactoryInstance->self();
|
||||
if (!kMimeTypeFactoryInstance)
|
||||
kMimeTypeFactoryInstance = new KMimeTypeFactory();
|
||||
return kMimeTypeFactoryInstance;
|
||||
}
|
||||
|
||||
int KMimeTypeFactory::entryOffset(const QString& mimeTypeName)
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
|
||||
extern int servicesDebugArea();
|
||||
|
||||
K_GLOBAL_STATIC(KSycocaFactorySingleton<KServiceFactory>, kServiceFactoryInstance)
|
||||
thread_local KServiceFactory* kServiceFactoryInstance = 0;
|
||||
|
||||
KServiceFactory::KServiceFactory()
|
||||
: KSycocaFactory( KST_KServiceFactory ),
|
||||
|
@ -37,7 +37,7 @@ KServiceFactory::KServiceFactory()
|
|||
m_relNameDict(0),
|
||||
m_menuIdDict(0)
|
||||
{
|
||||
kServiceFactoryInstance->instanceCreated(this);
|
||||
kServiceFactoryInstance = this;
|
||||
m_offerListOffset = 0;
|
||||
m_nameDictOffset = 0;
|
||||
m_relNameDictOffset = 0;
|
||||
|
@ -71,8 +71,9 @@ KServiceFactory::KServiceFactory()
|
|||
|
||||
KServiceFactory::~KServiceFactory()
|
||||
{
|
||||
if (kServiceFactoryInstance.exists())
|
||||
kServiceFactoryInstance->instanceDestroyed(this);
|
||||
if (kServiceFactoryInstance) {
|
||||
kServiceFactoryInstance = 0;
|
||||
}
|
||||
delete m_nameDict;
|
||||
delete m_relNameDict;
|
||||
delete m_menuIdDict;
|
||||
|
@ -80,7 +81,9 @@ KServiceFactory::~KServiceFactory()
|
|||
|
||||
KServiceFactory * KServiceFactory::self()
|
||||
{
|
||||
return kServiceFactoryInstance->self();
|
||||
if (!kServiceFactoryInstance)
|
||||
kServiceFactoryInstance = new KServiceFactory();
|
||||
return kServiceFactoryInstance;
|
||||
}
|
||||
|
||||
KService::Ptr KServiceFactory::findServiceByName(const QString &_name)
|
||||
|
|
|
@ -29,12 +29,12 @@
|
|||
#include <kglobal.h>
|
||||
#include <kstandarddirs.h>
|
||||
|
||||
K_GLOBAL_STATIC(KSycocaFactorySingleton<KServiceGroupFactory>, kServiceGroupFactoryInstance)
|
||||
thread_local KServiceGroupFactory* kServiceGroupFactoryInstance = 0;
|
||||
|
||||
KServiceGroupFactory::KServiceGroupFactory()
|
||||
: KSycocaFactory( KST_KServiceGroupFactory )
|
||||
{
|
||||
kServiceGroupFactoryInstance->instanceCreated(this);
|
||||
kServiceGroupFactoryInstance = this;
|
||||
m_baseGroupDictOffset = 0;
|
||||
if (!KSycoca::self()->isBuilding()) {
|
||||
QDataStream* str = stream();
|
||||
|
@ -53,13 +53,16 @@ KServiceGroupFactory::KServiceGroupFactory()
|
|||
KServiceGroupFactory::~KServiceGroupFactory()
|
||||
{
|
||||
delete m_baseGroupDict;
|
||||
if (kServiceGroupFactoryInstance.exists())
|
||||
kServiceGroupFactoryInstance->instanceDestroyed(this);
|
||||
if (kServiceGroupFactoryInstance) {
|
||||
kServiceGroupFactoryInstance = 0;
|
||||
}
|
||||
}
|
||||
|
||||
KServiceGroupFactory * KServiceGroupFactory::self()
|
||||
{
|
||||
return kServiceGroupFactoryInstance->self();
|
||||
if (!kServiceGroupFactoryInstance)
|
||||
kServiceGroupFactoryInstance = new KServiceGroupFactory();
|
||||
return kServiceGroupFactoryInstance;
|
||||
}
|
||||
|
||||
KServiceGroup::Ptr KServiceGroupFactory::findGroupByDesktopPath(const QString &_name, bool deep)
|
||||
|
|
|
@ -25,12 +25,12 @@
|
|||
#include <kdebug.h>
|
||||
#include <assert.h>
|
||||
|
||||
K_GLOBAL_STATIC(KSycocaFactorySingleton<KServiceTypeFactory>, kServiceTypeFactoryInstance)
|
||||
thread_local KServiceTypeFactory* kServiceTypeFactoryInstance = 0;
|
||||
|
||||
KServiceTypeFactory::KServiceTypeFactory()
|
||||
: KSycocaFactory( KST_KServiceTypeFactory )
|
||||
{
|
||||
kServiceTypeFactoryInstance->instanceCreated(this);
|
||||
kServiceTypeFactoryInstance = this;
|
||||
if (!KSycoca::self()->isBuilding()) {
|
||||
QDataStream* str = stream();
|
||||
Q_ASSERT(str);
|
||||
|
@ -56,13 +56,16 @@ KServiceTypeFactory::KServiceTypeFactory()
|
|||
KServiceTypeFactory::~KServiceTypeFactory()
|
||||
{
|
||||
KServiceTypeProfile::clearCache();
|
||||
if (kServiceTypeFactoryInstance.exists())
|
||||
kServiceTypeFactoryInstance->instanceDestroyed(this);
|
||||
if (kServiceTypeFactoryInstance) {
|
||||
kServiceTypeFactoryInstance = 0;
|
||||
}
|
||||
}
|
||||
|
||||
KServiceTypeFactory * KServiceTypeFactory::self()
|
||||
{
|
||||
return kServiceTypeFactoryInstance->self();
|
||||
if (!kServiceTypeFactoryInstance)
|
||||
kServiceTypeFactoryInstance = new KServiceTypeFactory();
|
||||
return kServiceTypeFactoryInstance;
|
||||
}
|
||||
|
||||
KServiceType::Ptr KServiceTypeFactory::findServiceTypeByName(const QString &_name)
|
||||
|
|
|
@ -24,17 +24,18 @@
|
|||
#include <ksycoca.h>
|
||||
#include <ksycocadict_p.h>
|
||||
|
||||
K_GLOBAL_STATIC(KSycocaFactorySingleton<KProtocolInfoFactory>, kProtocolInfoFactoryInstance)
|
||||
thread_local KProtocolInfoFactory* kProtocolInfoFactoryInstance = 0;
|
||||
|
||||
KProtocolInfoFactory::KProtocolInfoFactory() : KSycocaFactory( KST_KProtocolInfoFactory )
|
||||
{
|
||||
kProtocolInfoFactoryInstance->instanceCreated(this);
|
||||
kProtocolInfoFactoryInstance = this;
|
||||
}
|
||||
|
||||
KProtocolInfoFactory::~KProtocolInfoFactory()
|
||||
{
|
||||
if (kProtocolInfoFactoryInstance.exists())
|
||||
kProtocolInfoFactoryInstance->instanceDestroyed(this);
|
||||
if (kProtocolInfoFactoryInstance) {
|
||||
kProtocolInfoFactoryInstance = 0;
|
||||
}
|
||||
}
|
||||
|
||||
KProtocolInfo*
|
||||
|
@ -109,5 +110,7 @@ KProtocolInfo::Ptr KProtocolInfoFactory::findProtocol(const QString &protocol)
|
|||
|
||||
KProtocolInfoFactory* KProtocolInfoFactory::self()
|
||||
{
|
||||
return kProtocolInfoFactoryInstance->self();
|
||||
if (!kProtocolInfoFactoryInstance)
|
||||
kProtocolInfoFactoryInstance = new KProtocolInfoFactory();
|
||||
return kProtocolInfoFactoryInstance;
|
||||
}
|
||||
|
|
|
@ -22,13 +22,12 @@
|
|||
|
||||
#include <kdecore_export.h>
|
||||
#include <ksycocatype.h>
|
||||
#include <ksycocafactory.h>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtCore/QDataStream>
|
||||
|
||||
#include <QDataStream>
|
||||
class KSycocaFactory;
|
||||
class KSycocaFactoryList;
|
||||
class KSycocaPrivate;
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include "ksycocaentry.h"
|
||||
|
||||
#include <QList>
|
||||
|
||||
#include <QString>
|
||||
#include <QDataStream>
|
||||
|
||||
|
|
|
@ -22,11 +22,12 @@
|
|||
#include <ksycocaentry.h>
|
||||
|
||||
#include <QString>
|
||||
class KSycocaDict;
|
||||
class KSycocaResourceList;
|
||||
#include <QList>
|
||||
#include <QHash>
|
||||
|
||||
class KSycocaDict;
|
||||
class KSycocaResourceList;
|
||||
|
||||
typedef QHash<QString, KSycocaEntry::Ptr> KSycocaEntryDict;
|
||||
|
||||
/**
|
||||
|
@ -134,70 +135,6 @@ private:
|
|||
class Private;
|
||||
Private* const d;
|
||||
};
|
||||
|
||||
/**
|
||||
* This, instead of a typedef, allows to declare "class ..." in header files.
|
||||
* @internal
|
||||
*/
|
||||
class KDECORE_EXPORT KSycocaFactoryList : public QList<KSycocaFactory*> //krazy:exclude=dpointer (acts as a typedef)
|
||||
{
|
||||
public:
|
||||
KSycocaFactoryList() { }
|
||||
};
|
||||
|
||||
#include <QThreadStorage>
|
||||
/**
|
||||
* Workaround for the lack of QThreadStorage::setAutoDelete(false).
|
||||
* Container for KSycocaFactory that doesn't delete it when it is deleted.
|
||||
*/
|
||||
template <typename F> class KSycocaFactoryContainer
|
||||
{
|
||||
public:
|
||||
KSycocaFactoryContainer(F* factory) : m_factory(factory) {}
|
||||
F* factory() { return m_factory; }
|
||||
private:
|
||||
F* m_factory;
|
||||
};
|
||||
|
||||
/**
|
||||
* Template for making it easier to define a threadsafe singleton
|
||||
* for each factory, with support for kbuildsycoca providing a
|
||||
* subclass of the factory.
|
||||
*
|
||||
* @since 4.3
|
||||
* @internal
|
||||
*/
|
||||
template <typename F> class KSycocaFactorySingleton
|
||||
{
|
||||
public:
|
||||
typedef KSycocaFactoryContainer<F> C;
|
||||
KSycocaFactorySingleton() {
|
||||
}
|
||||
~KSycocaFactorySingleton() {
|
||||
// Do not delete the factory here.
|
||||
// All factories are owned by KSycoca, and deleted by it.
|
||||
}
|
||||
void instanceCreated(F* newFactory) {
|
||||
// This can also register a subclass created by kbuildsycoca
|
||||
Q_ASSERT(!m_factories.hasLocalData());
|
||||
Q_ASSERT(newFactory);
|
||||
m_factories.setLocalData(new C(newFactory));
|
||||
}
|
||||
void instanceDestroyed(F* factory) {
|
||||
if (m_factories.hasLocalData()) { // could be false on thread exit
|
||||
Q_ASSERT(m_factories.localData()->factory() == factory); Q_UNUSED(factory)
|
||||
m_factories.setLocalData(0);
|
||||
}
|
||||
}
|
||||
F* self() {
|
||||
if (!m_factories.hasLocalData()) {
|
||||
new F; // calls instanceCreated, which calls setLocalData
|
||||
Q_ASSERT(m_factories.hasLocalData());
|
||||
}
|
||||
return m_factories.localData()->factory();
|
||||
}
|
||||
private:
|
||||
QThreadStorage<C*> m_factories;
|
||||
};
|
||||
typedef QList<KSycocaFactory*> KSycocaFactoryList;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue