ThreadGlobalData.cpp [plain text]
#include "config.h"
#include "ThreadGlobalData.h"
#include "CachedResourceRequestInitiators.h"
#include "EventNames.h"
#include "TextCodecICU.h"
#include "ThreadTimers.h"
#include <wtf/MainThread.h>
#include <wtf/ThreadSpecific.h>
#include <wtf/Threading.h>
#include <wtf/WTFThreadData.h>
#include <wtf/text/StringImpl.h>
#if PLATFORM(MAC)
#include "TextCodeCMac.h"
#endif
namespace WebCore {
ThreadSpecific<ThreadGlobalData>* ThreadGlobalData::staticData;
#if USE(WEB_THREAD)
ThreadGlobalData* ThreadGlobalData::sharedMainThreadStaticData;
#endif
ThreadGlobalData::ThreadGlobalData()
: m_cachedResourceRequestInitiators(std::make_unique<CachedResourceRequestInitiators>())
, m_eventNames(EventNames::create())
, m_threadTimers(std::make_unique<ThreadTimers>())
#ifndef NDEBUG
, m_isMainThread(isMainThread())
#endif
, m_cachedConverterICU(std::make_unique<ICUConverterWrapper>())
#if PLATFORM(MAC)
, m_cachedConverterTEC(std::make_unique<TECConverterWrapper>())
#endif
{
wtfThreadData();
StringImpl::empty();
}
ThreadGlobalData::~ThreadGlobalData()
{
}
void ThreadGlobalData::destroy()
{
#if PLATFORM(MAC)
m_cachedConverterTEC = nullptr;
#endif
m_cachedConverterICU = nullptr;
m_eventNames = nullptr;
m_threadTimers = nullptr;
}
#if USE(WEB_THREAD)
void ThreadGlobalData::setWebCoreThreadData()
{
ASSERT(isWebThread());
ASSERT(&threadGlobalData() != ThreadGlobalData::sharedMainThreadStaticData);
ThreadGlobalData::staticData->replace(ThreadGlobalData::sharedMainThreadStaticData);
ASSERT(&threadGlobalData() == ThreadGlobalData::sharedMainThreadStaticData);
}
#endif
ThreadGlobalData& threadGlobalData()
{
#if USE(WEB_THREAD)
if (UNLIKELY(!ThreadGlobalData::staticData)) {
ThreadGlobalData::staticData = new ThreadSpecific<ThreadGlobalData>;
if (pthread_main_np())
ThreadGlobalData::sharedMainThreadStaticData = *ThreadGlobalData::staticData;
}
return **ThreadGlobalData::staticData;
#else
if (!ThreadGlobalData::staticData)
ThreadGlobalData::staticData = new ThreadSpecific<ThreadGlobalData>;
return **ThreadGlobalData::staticData;
#endif
}
}