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