ThreadGlobalData.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef ThreadGlobalData_h
  27. #define ThreadGlobalData_h
  28. #include <wtf/HashMap.h>
  29. #include <wtf/HashSet.h>
  30. #include <wtf/Noncopyable.h>
  31. #include <wtf/OwnPtr.h>
  32. #include <wtf/text/StringHash.h>
  33. #if ENABLE(WORKERS)
  34. #include <wtf/ThreadSpecific.h>
  35. #include <wtf/Threading.h>
  36. using WTF::ThreadSpecific;
  37. #endif
  38. namespace WebCore {
  39. class EventNames;
  40. class ThreadLocalInspectorCounters;
  41. class ThreadTimers;
  42. struct CachedResourceRequestInitiators;
  43. struct ICUConverterWrapper;
  44. struct TECConverterWrapper;
  45. class ThreadGlobalData {
  46. WTF_MAKE_NONCOPYABLE(ThreadGlobalData);
  47. public:
  48. ThreadGlobalData();
  49. ~ThreadGlobalData();
  50. void destroy(); // called on workers to clean up the ThreadGlobalData before the thread exits.
  51. const CachedResourceRequestInitiators& cachedResourceRequestInitiators() { return *m_cachedResourceRequestInitiators; }
  52. EventNames& eventNames() { return *m_eventNames; }
  53. ThreadTimers& threadTimers() { return *m_threadTimers; }
  54. #if USE(ICU_UNICODE)
  55. ICUConverterWrapper& cachedConverterICU() { return *m_cachedConverterICU; }
  56. #endif
  57. #if PLATFORM(MAC)
  58. TECConverterWrapper& cachedConverterTEC() { return *m_cachedConverterTEC; }
  59. #endif
  60. #if ENABLE(INSPECTOR)
  61. ThreadLocalInspectorCounters& inspectorCounters() { return *m_inspectorCounters; }
  62. #endif
  63. private:
  64. OwnPtr<CachedResourceRequestInitiators> m_cachedResourceRequestInitiators;
  65. OwnPtr<EventNames> m_eventNames;
  66. OwnPtr<ThreadTimers> m_threadTimers;
  67. #ifndef NDEBUG
  68. bool m_isMainThread;
  69. #endif
  70. #if USE(ICU_UNICODE)
  71. OwnPtr<ICUConverterWrapper> m_cachedConverterICU;
  72. #endif
  73. #if PLATFORM(MAC)
  74. OwnPtr<TECConverterWrapper> m_cachedConverterTEC;
  75. #endif
  76. #if ENABLE(INSPECTOR)
  77. OwnPtr<ThreadLocalInspectorCounters> m_inspectorCounters;
  78. #endif
  79. #if ENABLE(WORKERS)
  80. static ThreadSpecific<ThreadGlobalData>* staticData;
  81. #else
  82. static ThreadGlobalData* staticData;
  83. #endif
  84. friend ThreadGlobalData& threadGlobalData();
  85. };
  86. inline ThreadGlobalData& threadGlobalData()
  87. {
  88. // FIXME: Workers are not necessarily the only feature that make per-thread global data necessary.
  89. // We need to check for e.g. database objects manipulating strings on secondary threads.
  90. #if ENABLE(WORKERS)
  91. // ThreadGlobalData is used on main thread before it could possibly be used on secondary ones, so there is no need for synchronization here.
  92. if (!ThreadGlobalData::staticData)
  93. ThreadGlobalData::staticData = new ThreadSpecific<ThreadGlobalData>;
  94. return **ThreadGlobalData::staticData;
  95. #else
  96. if (!ThreadGlobalData::staticData) {
  97. ThreadGlobalData::staticData = static_cast<ThreadGlobalData*>(fastMalloc(sizeof(ThreadGlobalData)));
  98. // ThreadGlobalData constructor indirectly uses staticData, so we need to set up the memory before invoking it.
  99. new (NotNull, ThreadGlobalData::staticData) ThreadGlobalData;
  100. }
  101. return *ThreadGlobalData::staticData;
  102. #endif
  103. }
  104. } // namespace WebCore
  105. #endif // ThreadGlobalData_h