Bug 1865597 - Add error checking when initializing parallel marking and disable on...
[gecko.git] / js / src / vm / StencilCache.cpp
blob1bf138c63ee60da9aded676f5f59ae6ca10d7d86
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/. */
7 #include "vm/StencilCache.h"
9 #include "frontend/CompilationStencil.h"
10 #include "js/experimental/JSStencil.h"
11 #include "vm/MutexIDs.h"
13 js::StencilCache::StencilCache()
14 : cache(js::mutexid::StencilCache), enabled(false) {}
16 js::StencilCache::AccessKey js::StencilCache::isSourceCached(
17 ScriptSource* src) {
18 if (!enabled) {
19 return cache.noAccess();
22 AccessKey lock(cache.lock());
23 if (!enabled) {
24 // As we checked the flag before taking the lock, we have to check again to
25 // avoid races on the cache manipulation.
26 return cache.noAccess();
28 if (!lock->watched.has(src)) {
29 // If the source does not have any cached function, and we do not expect to
30 // cache any delazification in the future, then skip any cache handling.
31 return cache.noAccess();
33 return lock;
36 bool js::StencilCache::startCaching(RefPtr<ScriptSource>&& src) {
37 auto guard = cache.lock();
38 if (!guard->watched.putNew(std::move(src))) {
39 return false;
41 enabled = true;
42 return true;
45 js::frontend::CompilationStencil* js::StencilCache::lookup(
46 AccessKey& guard, const StencilContext& key) {
47 auto ptr = guard->functions.lookup(key);
48 if (!ptr) {
49 return nullptr;
52 return ptr->value().get();
55 bool js::StencilCache::putNew(AccessKey& guard, const StencilContext& key,
56 js::frontend::CompilationStencil* value) {
57 return guard->functions.putNew(key, value);
60 // Important: This function should not be called within a scope checking for
61 // isSourceCached, as this would cause a dead-lock.
62 void js::StencilCache::clearAndDisable() {
63 auto guard = cache.lock();
64 guard->functions.clearAndCompact();
65 guard->watched.clearAndCompact();
66 enabled = false;
69 /* static */ js::DelazificationCache js::DelazificationCache::singleton;