Debian Codegraph @

js/src/threading/ProtectedData.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "threading/ProtectedData.h"
8
9 #include "threading/Mutex.h"
10 #include "vm/HelperThreads.h"
11 #include "vm/JSContext.h"
12
13 namespace js {
14
15 #ifdef JS_HAS_PROTECTED_DATA_CHECKS
16
17 /* static */ mozilla::Atomic<size_t, mozilla::SequentiallyConsistent>
18 AutoNoteSingleThreadedRegion::count(0);
19
20 template <AllowedHelperThread Helper>
21 static inline bool OnHelperThread() {
22 if (Helper == AllowedHelperThread::IonCompile ||
23 Helper == AllowedHelperThread::GCTaskOrIonCompile) {
24 if (CurrentThreadIsOffThreadCompiling()) {
25 return true;
26 }
27 }
28
29 if (Helper == AllowedHelperThread::GCTask ||
30 Helper == AllowedHelperThread::GCTaskOrIonCompile) {
31 if (CurrentThreadIsPerformingGC()) {
32 return true;
33 }
34 }
35
36 return false;
37 }
38
39 void CheckThreadLocal::check() const {
40 JSContext* cx = TlsContext.get();
41 MOZ_ASSERT(cx);
42 MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
43 MOZ_ASSERT(id == ThreadId::ThisThreadId());
44 }
45
46 void CheckContextLocal::check() const {
47 if (!cx_->isInitialized()) {
48 return;
49 }
50
51 JSContext* cx = TlsContext.get();
52 MOZ_ASSERT(cx);
53 MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
54 MOZ_ASSERT(cx_ == cx);
55 }
56
57 void CheckMutexHeld::check() const { mutex_.assertOwnedByCurrentThread(); }
58
59 template <AllowedHelperThread Helper>
60 void CheckMainThread<Helper>::check() const {
61 if (OnHelperThread<Helper>()) {
62 return;
63 }
64
65 JSContext* cx = TlsContext.get();
66 MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
67 }
68
69 template class CheckMainThread<AllowedHelperThread::None>;
70 template class CheckMainThread<AllowedHelperThread::GCTask>;
71 template class CheckMainThread<AllowedHelperThread::IonCompile>;
72 template class CheckMainThread<AllowedHelperThread::GCTaskOrIonCompile>;
73
74 template <GlobalLock Lock, AllowedHelperThread Helper>
75 void CheckGlobalLock<Lock, Helper>::check() const {
76 if (OnHelperThread<Helper>()) {
77 return;
78 }
79
80 switch (Lock) {
81 case GlobalLock::GCLock:
82 TlsGCContext.get()
83 ->runtimeFromAnyThread()
84 ->gc.assertCurrentThreadHasLockedGC();
85 break;
86 case GlobalLock::HelperThreadLock:
87 gHelperThreadLock.assertOwnedByCurrentThread();
88 break;
89 }
90 }
91
92 template class CheckGlobalLock<GlobalLock::GCLock, AllowedHelperThread::None>;
93 template class CheckGlobalLock<GlobalLock::HelperThreadLock,
94 AllowedHelperThread::None>;
95
96 #endif // JS_HAS_PROTECTED_DATA_CHECKS
97
98 } // namespace js