• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 COMPUTER, 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 COMPUTER, 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 "EventNames.h"
31 #include "StringImpl.h"
32 #include "ThreadTimers.h"
33 #include <wtf/UnusedParam.h>
34 
35 #if USE(ICU_UNICODE) || USE(GLIB_ICU_UNICODE_HYBRID)
36 #include "TextCodecICU.h"
37 #endif
38 
39 #if PLATFORM(MAC)
40 #include "TextCodecMac.h"
41 #endif
42 
43 #if ENABLE(WORKERS)
44 #include <wtf/Threading.h>
45 #include <wtf/ThreadSpecific.h>
46 using namespace WTF;
47 #endif
48 
49 namespace WebCore {
50 
threadGlobalData()51 ThreadGlobalData& threadGlobalData()
52 {
53     // FIXME: Workers are not necessarily the only feature that make per-thread global data necessary.
54     // We need to check for e.g. database objects manipulating strings on secondary threads.
55 #if ENABLE(WORKERS)
56     // ThreadGlobalData is used on main thread before it could possibly be used on secondary ones, so there is no need for synchronization here.
57     static ThreadSpecific<ThreadGlobalData>* threadGlobalData = new ThreadSpecific<ThreadGlobalData>;
58     return **threadGlobalData;
59 #else
60     static ThreadGlobalData* staticData;
61     if (!staticData) {
62         staticData = static_cast<ThreadGlobalData*>(fastMalloc(sizeof(ThreadGlobalData)));
63         // ThreadGlobalData constructor indirectly uses staticData, so we need to set up the memory before invoking it.
64         new (staticData) ThreadGlobalData;
65     }
66     return *staticData;
67 #endif
68 }
69 
ThreadGlobalData()70 ThreadGlobalData::ThreadGlobalData()
71     : m_emptyString(new StringImpl)
72     , m_atomicStringTable(new HashSet<StringImpl*>)
73     , m_eventNames(new EventNames)
74     , m_threadTimers(new ThreadTimers)
75 #ifndef NDEBUG
76     , m_isMainThread(isMainThread())
77 #endif
78 #if USE(ICU_UNICODE) || USE(GLIB_ICU_UNICODE_HYBRID)
79     , m_cachedConverterICU(new ICUConverterWrapper)
80 #endif
81 #if PLATFORM(MAC)
82     , m_cachedConverterTEC(new TECConverterWrapper)
83 #endif
84 {
85 }
86 
~ThreadGlobalData()87 ThreadGlobalData::~ThreadGlobalData()
88 {
89 #if PLATFORM(MAC)
90     delete m_cachedConverterTEC;
91 #endif
92 #if USE(ICU_UNICODE) || USE(GLIB_ICU_UNICODE_HYBRID)
93     delete m_cachedConverterICU;
94 #endif
95 
96     delete m_eventNames;
97     delete m_atomicStringTable;
98     delete m_threadTimers;
99 
100     // Using member variable m_isMainThread instead of calling WTF::isMainThread() directly
101     // to avoid issues described in https://bugs.webkit.org/show_bug.cgi?id=25973.
102     // In short, some pthread-based platforms and ports can not use WTF::CurrentThread() and WTF::isMainThread()
103     // in destructors of thread-specific data.
104     ASSERT(m_isMainThread || m_emptyString->hasOneRef()); // We intentionally don't clean up static data on application quit, so there will be many strings remaining on the main thread.
105 
106     delete m_emptyString;
107 }
108 
109 } // namespace WebCore
110