145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (C) 2008 Apple Inc. All Rights Reserved.
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
5a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org * modification, are permitted provided that the following conditions
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef WTFThreadData_h
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define WTFThreadData_h
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/HashMap.h"
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/HashSet.h"
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/Noncopyable.h"
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/ThreadSpecific.h"
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/Threading.h"
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/WTFExport.h"
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "wtf/text/StringHash.h"
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgnamespace WTF {
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgclass AtomicStringTable;
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstruct ICUConverterWrapper;
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef void (*AtomicStringTableDestructor)(AtomicStringTable*);
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgclass WTF_EXPORT WTFThreadData {
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    WTF_MAKE_NONCOPYABLE(WTFThreadData);
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgpublic:
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    WTFThreadData();
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    ~WTFThreadData();
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    AtomicStringTable* atomicStringTable()
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return m_atomicStringTable;
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    ICUConverterWrapper& cachedConverterICU() { return *m_cachedConverterICU; }
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgprivate:
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    AtomicStringTable* m_atomicStringTable;
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    AtomicStringTableDestructor m_atomicStringTableDestructor;
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    OwnPtr<ICUConverterWrapper> m_cachedConverterICU;
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    static ThreadSpecific<WTFThreadData>* staticData;
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    friend WTFThreadData& wtfThreadData();
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    friend class AtomicStringTable;
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
67a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginline WTFThreadData& wtfThreadData()
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    // WRT WebCore:
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    //    WTFThreadData is used on main thread before it could possibly be used
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    //    on secondary ones, so there is no need for synchronization here.
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    // WRT JavaScriptCore:
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    //    wtfThreadData() is initially called from initializeThreading(), ensuring
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    //    this is initially called in a pthread_once locked context.
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (!WTFThreadData::staticData)
77a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        WTFThreadData::staticData = new ThreadSpecific<WTFThreadData>;
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return **WTFThreadData::staticData;
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} // namespace WTF
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgusing WTF::WTFThreadData;
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgusing WTF::wtfThreadData;
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif // WTFThreadData_h
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org