1/* 2 * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 27#include "config.h" 28#include "ThreadGlobalData.h" 29 30#include "CachedResourceRequestInitiators.h" 31#include "EventNames.h" 32#include "TextCodecICU.h" 33#include "ThreadTimers.h" 34#include <wtf/MainThread.h> 35#include <wtf/ThreadSpecific.h> 36#include <wtf/Threading.h> 37#include <wtf/WTFThreadData.h> 38#include <wtf/text/StringImpl.h> 39 40#if PLATFORM(MAC) 41#include "TextCodeCMac.h" 42#endif 43 44#if ENABLE(WEB_REPLAY) 45#include "ReplayInputTypes.h" 46#endif 47 48namespace WebCore { 49 50ThreadSpecific<ThreadGlobalData>* ThreadGlobalData::staticData; 51#if USE(WEB_THREAD) 52ThreadGlobalData* ThreadGlobalData::sharedMainThreadStaticData; 53#endif 54 55ThreadGlobalData::ThreadGlobalData() 56 : m_cachedResourceRequestInitiators(adoptPtr(new CachedResourceRequestInitiators)) 57 , m_eventNames(adoptPtr(new EventNames)) 58 , m_threadTimers(adoptPtr(new ThreadTimers)) 59#if ENABLE(WEB_REPLAY) 60 , m_inputTypes(std::make_unique<ReplayInputTypes>()) 61#endif 62#ifndef NDEBUG 63 , m_isMainThread(isMainThread()) 64#endif 65 , m_cachedConverterICU(adoptPtr(new ICUConverterWrapper)) 66#if PLATFORM(MAC) 67 , m_cachedConverterTEC(adoptPtr(new TECConverterWrapper)) 68#endif 69{ 70 // This constructor will have been called on the main thread before being called on 71 // any other thread, and is only called once per thread - this makes this a convenient 72 // point to call methods that internally perform a one-time initialization that is not 73 // threadsafe. 74 wtfThreadData(); 75 StringImpl::empty(); 76} 77 78ThreadGlobalData::~ThreadGlobalData() 79{ 80} 81 82void ThreadGlobalData::destroy() 83{ 84#if PLATFORM(MAC) 85 m_cachedConverterTEC.clear(); 86#endif 87 88 m_cachedConverterICU.clear(); 89 90#if ENABLE(WEB_REPLAY) 91 m_inputTypes = nullptr; 92#endif 93 94 m_eventNames.clear(); 95 m_threadTimers.clear(); 96} 97 98#if USE(WEB_THREAD) 99void ThreadGlobalData::setWebCoreThreadData() 100{ 101 ASSERT(isWebThread()); 102 ASSERT(&threadGlobalData() != ThreadGlobalData::sharedMainThreadStaticData); 103 104 // Set WebThread's ThreadGlobalData object to be the same as the main UI thread. 105 ThreadGlobalData::staticData->replace(ThreadGlobalData::sharedMainThreadStaticData); 106 107 ASSERT(&threadGlobalData() == ThreadGlobalData::sharedMainThreadStaticData); 108} 109#endif 110 111ThreadGlobalData& threadGlobalData() 112{ 113#if USE(WEB_THREAD) 114 if (UNLIKELY(!ThreadGlobalData::staticData)) { 115 ThreadGlobalData::staticData = new ThreadSpecific<ThreadGlobalData>; 116 // WebThread and main UI thread need to share the same object. Save it in a static 117 // here, the WebThread will pick it up in setWebCoreThreadData(). 118 if (pthread_main_np()) 119 ThreadGlobalData::sharedMainThreadStaticData = *ThreadGlobalData::staticData; 120 } 121 return **ThreadGlobalData::staticData; 122#else 123 if (!ThreadGlobalData::staticData) 124 ThreadGlobalData::staticData = new ThreadSpecific<ThreadGlobalData>; 125 return **ThreadGlobalData::staticData; 126#endif 127} 128 129} // namespace WebCore 130