mirror of
https://github.com/tnodir/fort
synced 2024-11-15 05:46:03 +00:00
UI: IocContainert: Extract ThreadStorage
This commit is contained in:
parent
500b024aa0
commit
adcd0ff134
@ -203,6 +203,7 @@ SOURCES += \
|
||||
util/startuputil.cpp \
|
||||
util/stringutil.cpp \
|
||||
util/textareautil.cpp \
|
||||
util/threadstorage.cpp \
|
||||
util/triggertimer.cpp \
|
||||
util/variantutil.cpp \
|
||||
util/window/basewindowstatewatcher.cpp \
|
||||
@ -413,6 +414,7 @@ HEADERS += \
|
||||
util/startuputil.h \
|
||||
util/stringutil.h \
|
||||
util/textareautil.h \
|
||||
util/threadstorage.h \
|
||||
util/triggertimer.h \
|
||||
util/variantutil.h \
|
||||
util/window/basewindowstatewatcher.h \
|
||||
|
@ -8,7 +8,7 @@ namespace {
|
||||
const QLoggingCategory LC("util.ioc.iocContainer");
|
||||
}
|
||||
|
||||
int IocContainer::g_tlsIndex = -1;
|
||||
ThreadStorage IocContainer::g_threadStorage;
|
||||
|
||||
IocContainer::IocContainer() = default;
|
||||
|
||||
@ -94,27 +94,7 @@ void IocContainer::autoDelete(int typeId)
|
||||
|
||||
bool IocContainer::pinToThread()
|
||||
{
|
||||
createTlsIndex();
|
||||
|
||||
return TlsSetValue(g_tlsIndex, this);
|
||||
}
|
||||
|
||||
void IocContainer::createTlsIndex()
|
||||
{
|
||||
if (g_tlsIndex == -1) {
|
||||
g_tlsIndex = TlsAlloc();
|
||||
if (g_tlsIndex == -1) {
|
||||
qCCritical(LC) << "TlsAlloc error";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IocContainer::deleteTlsIndex()
|
||||
{
|
||||
if (g_tlsIndex != -1) {
|
||||
TlsFree(g_tlsIndex);
|
||||
g_tlsIndex = -1;
|
||||
}
|
||||
return g_threadStorage.setValue(this);
|
||||
}
|
||||
|
||||
int IocContainer::getNextTypeId()
|
||||
|
@ -3,8 +3,7 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <qt_windows.h>
|
||||
#include <util/threadstorage.h>
|
||||
|
||||
#include "iocservice.h"
|
||||
|
||||
@ -80,7 +79,7 @@ public:
|
||||
|
||||
inline static IocContainer *getPinned()
|
||||
{
|
||||
return static_cast<IocContainer *>(TlsGetValue(g_tlsIndex));
|
||||
return static_cast<IocContainer *>(g_threadStorage.value());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
@ -104,13 +103,10 @@ private:
|
||||
void tearDown(int typeId);
|
||||
void autoDelete(int typeId);
|
||||
|
||||
static void createTlsIndex();
|
||||
static void deleteTlsIndex();
|
||||
|
||||
static int getNextTypeId();
|
||||
|
||||
private:
|
||||
static int g_tlsIndex;
|
||||
static ThreadStorage g_threadStorage;
|
||||
|
||||
int m_size = 0;
|
||||
|
||||
|
52
src/ui/util/threadstorage.cpp
Normal file
52
src/ui/util/threadstorage.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include "threadstorage.h"
|
||||
|
||||
#include <QLoggingCategory>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <qt_windows.h>
|
||||
|
||||
namespace {
|
||||
const QLoggingCategory LC("util.threadStorage");
|
||||
}
|
||||
|
||||
ThreadStorage::ThreadStorage()
|
||||
{
|
||||
createTlsIndex();
|
||||
}
|
||||
|
||||
ThreadStorage::~ThreadStorage()
|
||||
{
|
||||
deleteTlsIndex();
|
||||
}
|
||||
|
||||
void *ThreadStorage::value() const
|
||||
{
|
||||
return TlsGetValue(m_tlsIndex);
|
||||
}
|
||||
|
||||
bool ThreadStorage::setValue(void *v)
|
||||
{
|
||||
return TlsSetValue(m_tlsIndex, v);
|
||||
}
|
||||
|
||||
void ThreadStorage::createTlsIndex()
|
||||
{
|
||||
if (m_tlsIndex != BadTlsIndex)
|
||||
return;
|
||||
|
||||
m_tlsIndex = TlsAlloc();
|
||||
|
||||
if (m_tlsIndex == BadTlsIndex) {
|
||||
qCCritical(LC) << "TlsAlloc error";
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadStorage::deleteTlsIndex()
|
||||
{
|
||||
if (m_tlsIndex == BadTlsIndex)
|
||||
return;
|
||||
|
||||
TlsFree(m_tlsIndex);
|
||||
|
||||
m_tlsIndex = BadTlsIndex;
|
||||
}
|
22
src/ui/util/threadstorage.h
Normal file
22
src/ui/util/threadstorage.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef THREADSTORAGE_H
|
||||
#define THREADSTORAGE_H
|
||||
|
||||
class ThreadStorage final
|
||||
{
|
||||
public:
|
||||
explicit ThreadStorage();
|
||||
~ThreadStorage();
|
||||
|
||||
void *value() const;
|
||||
bool setValue(void *v);
|
||||
|
||||
private:
|
||||
void createTlsIndex();
|
||||
void deleteTlsIndex();
|
||||
|
||||
private:
|
||||
constexpr static int BadTlsIndex = -1;
|
||||
int m_tlsIndex = BadTlsIndex;
|
||||
};
|
||||
|
||||
#endif // THREADSTORAGE_H
|
Loading…
Reference in New Issue
Block a user