1/*
2 * Copyright (C) 2013, 2016 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#pragma once
27
28#include "DeferGC.h"
29#include <wtf/Lock.h>
30#include <wtf/NoLock.h>
31#include <wtf/Optional.h>
32
33namespace JSC {
34
35#if ENABLE(CONCURRENT_JS)
36typedef Lock ConcurrentJSLock;
37typedef LockHolder ConcurrentJSLockerImpl;
38#else
39typedef NoLock ConcurrentJSLock;
40typedef NoLockLocker ConcurrentJSLockerImpl;
41#endif
42
43static_assert(sizeof(ConcurrentJSLock) == 1, "Regardless of status of concurrent JS flag, size of ConurrentJSLock is always one byte.");
44
45class ConcurrentJSLockerBase : public AbstractLocker {
46 WTF_MAKE_NONCOPYABLE(ConcurrentJSLockerBase);
47public:
48 explicit ConcurrentJSLockerBase(ConcurrentJSLock& lockable)
49 : m_locker(&lockable)
50 {
51 }
52 explicit ConcurrentJSLockerBase(ConcurrentJSLock* lockable)
53 : m_locker(lockable)
54 {
55 }
56
57 explicit ConcurrentJSLockerBase(NoLockingNecessaryTag)
58 : m_locker(NoLockingNecessary)
59 {
60 }
61
62 ~ConcurrentJSLockerBase()
63 {
64 }
65
66 void unlockEarly()
67 {
68 m_locker.unlockEarly();
69 }
70
71private:
72 ConcurrentJSLockerImpl m_locker;
73};
74
75class GCSafeConcurrentJSLocker : public ConcurrentJSLockerBase {
76public:
77 GCSafeConcurrentJSLocker(ConcurrentJSLock& lockable, Heap& heap)
78 : ConcurrentJSLockerBase(lockable)
79 , m_deferGC(heap)
80 {
81 }
82
83 GCSafeConcurrentJSLocker(ConcurrentJSLock* lockable, Heap& heap)
84 : ConcurrentJSLockerBase(lockable)
85 , m_deferGC(heap)
86 {
87 }
88
89 ~GCSafeConcurrentJSLocker()
90 {
91 // We have to unlock early due to the destruction order of base
92 // vs. derived classes. If we didn't, then we would destroy the
93 // DeferGC object before unlocking the lock which could cause a GC
94 // and resulting deadlock.
95 unlockEarly();
96 }
97
98private:
99 DeferGC m_deferGC;
100};
101
102class ConcurrentJSLocker : public ConcurrentJSLockerBase {
103public:
104 ConcurrentJSLocker(ConcurrentJSLock& lockable)
105 : ConcurrentJSLockerBase(lockable)
106#if ENABLE(CONCURRENT_JS) && !defined(NDEBUG)
107 , m_disallowGC(std::in_place)
108#endif
109 {
110 }
111
112 ConcurrentJSLocker(ConcurrentJSLock* lockable)
113 : ConcurrentJSLockerBase(lockable)
114#if ENABLE(CONCURRENT_JS) && !defined(NDEBUG)
115 , m_disallowGC(std::in_place)
116#endif
117 {
118 }
119
120 ConcurrentJSLocker(NoLockingNecessaryTag)
121 : ConcurrentJSLockerBase(NoLockingNecessary)
122#if ENABLE(CONCURRENT_JS) && !defined(NDEBUG)
123 , m_disallowGC(WTF::nullopt)
124#endif
125 {
126 }
127
128 ConcurrentJSLocker(int) = delete;
129
130#if ENABLE(CONCURRENT_JS) && !defined(NDEBUG)
131private:
132 Optional<DisallowGC> m_disallowGC;
133#endif
134};
135
136} // namespace JSC
137