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 <wtf/HashSet.h>
35#include <wtf/TCSpinLock.h>
36#include <wtf/Threading.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 {
54public:
55    GCThreadSharedData(VM*);
56    ~GCThreadSharedData();
57
58    void reset();
59
60    void didStartMarking();
61    void didFinishMarking();
62    void didStartCopying();
63    void didFinishCopying();
64
65#if ENABLE(PARALLEL_GC)
66    void resetChildren();
67    size_t childVisitCount();
68    size_t childDupStrings();
69#endif
70
71private:
72    friend class GCThread;
73    friend class SlotVisitor;
74    friend class CopyVisitor;
75
76    void getNextBlocksToCopy(size_t&, size_t&);
77    void startNextPhase(GCPhase);
78    void endCurrentPhase();
79
80    VM* m_vm;
81    CopiedSpace* m_copiedSpace;
82
83    bool m_shouldHashCons;
84
85    Vector<GCThread*> m_gcThreads;
86
87    Mutex m_markingLock;
88    ThreadCondition m_markingCondition;
89    MarkStackArray m_sharedMarkStack;
90    unsigned m_numberOfActiveParallelMarkers;
91    bool m_parallelMarkersShouldExit;
92
93    Mutex m_opaqueRootsLock;
94    HashSet<void*> m_opaqueRoots;
95
96    SpinLock m_copyLock;
97    Vector<CopiedBlock*> m_blocksToCopy;
98    size_t m_copyIndex;
99    static const size_t s_blockFragmentLength = 32;
100
101    Mutex m_phaseLock;
102    ThreadCondition m_phaseCondition;
103    ThreadCondition m_activityCondition;
104    unsigned m_numberOfActiveGCThreads;
105    bool m_gcThreadsShouldWait;
106    GCPhase m_currentPhase;
107
108    ListableHandler<WeakReferenceHarvester>::List m_weakReferenceHarvesters;
109    ListableHandler<UnconditionalFinalizer>::List m_unconditionalFinalizers;
110};
111
112inline void GCThreadSharedData::getNextBlocksToCopy(size_t& start, size_t& end)
113{
114    SpinLockHolder locker(&m_copyLock);
115    start = m_copyIndex;
116    end = std::min(m_blocksToCopy.size(), m_copyIndex + s_blockFragmentLength);
117    m_copyIndex = end;
118}
119
120} // namespace JSC
121
122#endif
123