1/*
2 * Copyright (C) 2014-2018 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#include "config.h"
27#include "DFGSafepoint.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGPlan.h"
32#include "DFGScannable.h"
33#include "DFGThreadData.h"
34#include "JSCInlines.h"
35
36namespace JSC { namespace DFG {
37
38Safepoint::Result::~Result()
39{
40 RELEASE_ASSERT(m_wasChecked);
41}
42
43bool Safepoint::Result::didGetCancelled()
44{
45 m_wasChecked = true;
46 return m_didGetCancelled;
47}
48
49Safepoint::Safepoint(Plan& plan, Result& result)
50 : m_vm(plan.vm())
51 , m_plan(plan)
52 , m_didCallBegin(false)
53 , m_result(result)
54{
55 RELEASE_ASSERT(result.m_wasChecked);
56 result.m_wasChecked = false;
57 result.m_didGetCancelled = false;
58}
59
60Safepoint::~Safepoint()
61{
62 RELEASE_ASSERT(m_didCallBegin);
63 if (ThreadData* data = m_plan.threadData()) {
64 RELEASE_ASSERT(data->m_safepoint == this);
65 data->m_rightToRun.lock();
66 data->m_safepoint = nullptr;
67 }
68}
69
70void Safepoint::add(Scannable* scannable)
71{
72 RELEASE_ASSERT(!m_didCallBegin);
73 m_scannables.append(scannable);
74}
75
76void Safepoint::begin()
77{
78 RELEASE_ASSERT(!m_didCallBegin);
79 m_didCallBegin = true;
80 if (ThreadData* data = m_plan.threadData()) {
81 RELEASE_ASSERT(!data->m_safepoint);
82 data->m_safepoint = this;
83 data->m_rightToRun.unlockFairly();
84 }
85}
86
87void Safepoint::checkLivenessAndVisitChildren(SlotVisitor& visitor)
88{
89 RELEASE_ASSERT(m_didCallBegin);
90
91 if (m_result.m_didGetCancelled)
92 return; // We were cancelled during a previous GC!
93
94 if (!isKnownToBeLiveDuringGC())
95 return;
96
97 for (unsigned i = m_scannables.size(); i--;)
98 m_scannables[i]->visitChildren(visitor);
99}
100
101bool Safepoint::isKnownToBeLiveDuringGC()
102{
103 RELEASE_ASSERT(m_didCallBegin);
104
105 if (m_result.m_didGetCancelled)
106 return true; // We were cancelled during a previous GC, so let's not mess with it this time around - pretend it's live and move on.
107
108 return m_plan.isKnownToBeLiveDuringGC();
109}
110
111void Safepoint::cancel()
112{
113 RELEASE_ASSERT(m_didCallBegin);
114 RELEASE_ASSERT(!m_result.m_didGetCancelled); // We cannot get cancelled twice because subsequent GCs will think that we're alive and they will not do anything to us.
115
116 m_plan.cancel();
117 m_result.m_didGetCancelled = true;
118 m_vm = nullptr;
119}
120
121VM* Safepoint::vm() const
122{
123 return m_vm;
124}
125
126} } // namespace JSC::DFG
127
128#endif // ENABLE(DFG_JIT)
129
130