1/*
2 * Copyright (C) 2009, 2011 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#ifndef GCThreadSharedData_h
27#define GCThreadSharedData_h
28
29#include "ListableHandler.h"
30#include "MarkStack.h"
31#include "MarkedBlock.h"
32#include "UnconditionalFinalizer.h"
33#include "WeakReferenceHarvester.h"
34#include <condition_variable>
35#include <wtf/HashSet.h>
36#include <wtf/TCSpinLock.h>
37#include <wtf/Vector.h>
38
39namespace JSC {
40
41class GCThread;
42class VM;
43class CopiedSpace;
44class CopyVisitor;
45
46enum GCPhase {
47    NoPhase,
48    Mark,
49    Copy,
50    Exit
51};
52
53class GCThreadSharedData {
54    WTF_MAKE_FAST_ALLOCATED;
55public:
56    GCThreadSharedData(VM*);
57    ~GCThreadSharedData();
58
59    void reset();
60
61    void didStartMarking();
62    void didFinishMarking();
63    void didStartCopying();
64    void didFinishCopying();
65
66#if ENABLE(PARALLEL_GC)
67    void resetChildren();
68    size_t childVisitCount();
69    size_t childBytesVisited();
70    size_t childBytesCopied();
71    size_t childDupStrings();
72#endif
73
74private:
75    friend class GCThread;
76    friend class SlotVisitor;
77    friend class CopyVisitor;
78
79    void getNextBlocksToCopy(size_t&, size_t&);
80    void startNextPhase(GCPhase);
81    void endCurrentPhase();
82
83    VM* m_vm;
84    CopiedSpace* m_copiedSpace;
85
86    bool m_shouldHashCons;
87
88    Vector<GCThread*> m_gcThreads;
89
90    std::mutex m_markingMutex;
91    std::condition_variable m_markingConditionVariable;
92    MarkStackArray m_sharedMarkStack;
93    unsigned m_numberOfActiveParallelMarkers;
94    bool m_parallelMarkersShouldExit;
95
96    std::mutex m_opaqueRootsMutex;
97    HashSet<void*> m_opaqueRoots;
98
99    SpinLock m_copyLock;
100    Vector<CopiedBlock*> m_blocksToCopy;
101    size_t m_copyIndex;
102    static const size_t s_blockFragmentLength = 32;
103
104    std::mutex m_phaseMutex;
105    std::condition_variable m_phaseConditionVariable;
106    std::condition_variable m_activityConditionVariable;
107    unsigned m_numberOfActiveGCThreads;
108    bool m_gcThreadsShouldWait;
109    GCPhase m_currentPhase;
110
111    ListableHandler<WeakReferenceHarvester>::List m_weakReferenceHarvesters;
112    ListableHandler<UnconditionalFinalizer>::List m_unconditionalFinalizers;
113};
114
115inline void GCThreadSharedData::getNextBlocksToCopy(size_t& start, size_t& end)
116{
117    SpinLockHolder locker(&m_copyLock);
118    start = m_copyIndex;
119    end = std::min(m_blocksToCopy.size(), m_copyIndex + s_blockFragmentLength);
120    m_copyIndex = end;
121}
122
123} // namespace JSC
124
125#endif
126