Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test-app/runtime/src/main/cpp/CallbackHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ namespace {
* app has one, lacks the field and keeps its own behaviour. The class cache hands out one
* global reference per class name for the life of the process, so it is a stable key.
*/
void SetDefaultOverrides(JNIEnv* env, jobject instance, jclass clazz, uint64_t defaultOverrides) {
thread_local robin_hood::unordered_map<jclass, jfieldID> fields;
void SetDefaultOverrides(Runtime* runtime, JNIEnv* env, jobject instance, jclass clazz, uint64_t defaultOverrides) {
auto& fields = runtime->GetDefaultOverridesFields();

jfieldID field;
auto found = fields.find(clazz);
Expand Down Expand Up @@ -263,7 +263,7 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local<Object> &j
// Before any Java code but the constructor's own can see the instance.
// An interface proxy's handler received the mask with the instance.
if (defaultOverrides != 0 && instance != nullptr && !mi.isInterfaceProxy) {
SetDefaultOverrides(env, instance, generatedJavaClass, defaultOverrides);
SetDefaultOverrides(runtime, env, instance, generatedJavaClass, defaultOverrides);
}

jlong usedJavaMemory = env.CallLongMethod(runtime->GetJavaRuntime(), REGISTER_CONSTRUCTED_INSTANCE_ID,
Expand Down
15 changes: 5 additions & 10 deletions test-app/runtime/src/main/cpp/EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,18 +792,13 @@ EventLoop::PumpResult EventLoop::PumpUntil(double deadlineSeconds,
}

namespace {
// Depth, not a flag: an fd callback can dispatch JS that lands back in
// another callback through a nested drain.
thread_local int t_looperCallbackDepth = 0;

struct LooperCallbackScope {
LooperCallbackScope() { ++t_looperCallbackDepth; }
~LooperCallbackScope() { --t_looperCallbackDepth; }
explicit LooperCallbackScope(int& depth) : depth_(depth) { ++depth_; }
~LooperCallbackScope() { --depth_; }
int& depth_;
};
} // namespace

bool EventLoop::IsInLooperCallback() { return t_looperCallbackDepth > 0; }

void EventLoop::WaitForInternalWork(int timeoutMs, bool pumpDeliverable) {
const DrainFilter filter =
pumpDeliverable ? DrainFilter::kPumpDeliverable : DrainFilter::kNestableV8;
Expand Down Expand Up @@ -853,7 +848,7 @@ void EventLoop::WaitForInternalWork(int timeoutMs, bool pumpDeliverable) {
}

int EventLoop::EventFdCallback(int fd, int events, void* data) {
LooperCallbackScope callbackScope;
LooperCallbackScope callbackScope(static_cast<EventLoop*>(data)->looperCallbackDepth_);
uint64_t value;
// EFD_SEMAPHORE: consumes exactly one unit; while more remain the fd stays
// readable and ALooper calls back next poll, interleaving with Java
Expand All @@ -867,7 +862,7 @@ int EventLoop::EventFdCallback(int fd, int events, void* data) {
}

int EventLoop::TimerFdCallback(int fd, int events, void* data) {
LooperCallbackScope callbackScope;
LooperCallbackScope callbackScope(static_cast<EventLoop*>(data)->looperCallbackDepth_);
uint64_t expirations;
if (read(fd, &expirations, sizeof(expirations)) != sizeof(expirations)) {
return 1;
Expand Down
16 changes: 10 additions & 6 deletions test-app/runtime/src/main/cpp/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ class EventLoop {
void RunNestableV8Tasks();

/**
* True while the calling thread is inside one of this process's ALooper
* fd callbacks. Android's Looper::pollInner holds a Response& into its
* response vector across each callback; a nested ALooper_pollOnce on the
* same looper clears and reallocates that vector, so the outer poll
* resumes over freed memory. Any code that pumps the looper (module
* True while this loop's home thread is inside one of the loop's ALooper
* fd callbacks; ask it only from the home thread. Android's
* Looper::pollInner holds a Response& into its response vector across
* each callback; a nested ALooper_pollOnce on the same looper clears and
* reallocates that vector, so the outer poll resumes over freed memory. Any code that pumps the looper (module
* evaluation, the boot backstop, the fetch yield) must consult this and
* drain queues directly instead of polling when it is set.
*/
static bool IsInLooperCallback();
bool IsInLooperCallback() const { return looperCallbackDepth_ > 0; }

/**
* Marks this loop's isolate as termination-requested. Callable from any
Expand Down Expand Up @@ -419,6 +419,10 @@ class EventLoop {
std::atomic<uint64_t> claimCells_[kClaimCells] = {};
// Set by NoteTerminationRequested (any thread), read by PumpUntil.
std::atomic_bool terminationRequested_{false};
// Depth, not a flag: an fd callback can dispatch JS that lands back in
// another callback through a nested drain. Home thread only, like the fd
// callbacks that maintain it.
int looperCallbackDepth_ = 0;
// ordered-lane source with its own bookkeeping (Timers); home-thread only
OrderedTaskSource* timerSource_ = nullptr;
// bare ordered tokens posted before the bind; flushed by Bind
Expand Down
18 changes: 9 additions & 9 deletions test-app/runtime/src/main/cpp/MethodCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@ void MethodCache::Init() {
NS_CHECK(RESOLVE_CONSTRUCTOR_SIGNATURE_ID != nullptr);
}

// Reused for every lookup so a cache hit allocates nothing.
static thread_local std::string s_signatureKey;
static const MethodCache::CacheMethodInfo s_unresolved;

const MethodCache::CacheMethodInfo& MethodCache::ResolveMethodSignature(const string& className, const string& methodName, const FunctionCallbackInfo<Value>& args, bool isStatic) {
CacheMethodInfo method_info;

EncodeSignature(s_signatureKey, className, methodName, args, isStatic);
auto& signatureKey = Runtime::GetRuntime(args.GetIsolate())->GetSignatureKeyScratch();
EncodeSignature(signatureKey, className, methodName, args, isStatic);
{
std::shared_lock<std::shared_mutex> lock(signatureCacheMutex);
auto it = s_mthod_ctor_signature_cache.find(s_signatureKey);
auto it = s_mthod_ctor_signature_cache.find(signatureKey);
if (it != s_mthod_ctor_signature_cache.end()) {
return it->second;
}
}
// Resolving calls into Java, which may call back into JS and reuse s_signatureKey.
string encoded_method_signature = s_signatureKey;
// Resolving calls into Java, which may call back into JS and reuse the scratch key.
string encoded_method_signature = signatureKey;

auto signature = ResolveJavaMethod(args, className, methodName);

Expand Down Expand Up @@ -89,15 +88,16 @@ const MethodCache::CacheMethodInfo& MethodCache::ResolveConstructorSignature(con

auto& args = argWrapper.args;
static const string kConstructorName = "<init>";
EncodeSignature(s_signatureKey, fullClassName, kConstructorName, args, false);
auto& signatureKey = Runtime::GetRuntime(args.GetIsolate())->GetSignatureKeyScratch();
EncodeSignature(signatureKey, fullClassName, kConstructorName, args, false);
{
std::shared_lock<std::shared_mutex> lock(signatureCacheMutex);
auto it = s_mthod_ctor_signature_cache.find(s_signatureKey);
auto it = s_mthod_ctor_signature_cache.find(signatureKey);
if (it != s_mthod_ctor_signature_cache.end()) {
return it->second;
}
}
string encoded_ctor_signature = s_signatureKey;
string encoded_ctor_signature = signatureKey;

auto signature = ResolveConstructor(args, javaClass, isInterface);

Expand Down
19 changes: 19 additions & 0 deletions test-app/runtime/src/main/cpp/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ class Runtime {

ObjectManager* GetObjectManager() const;

/*
* Scratch for MethodCache's signature keys, so a cache hit allocates
* nothing. Only for the thread holding the isolate, and not across a
* call that can re-enter JS.
*/
std::string& GetSignatureKeyScratch() {
return m_signatureKeyScratch;
}

// Each default-overriding class's defaultOverrides__ns field, or null
// when the class has none. Only for the thread holding the isolate.
robin_hood::unordered_map<jclass, jfieldID>& GetDefaultOverridesFields() {
return m_defaultOverridesFields;
}

AdmissionGate& GetAdmissionGate() {
return m_admissionGate;
}
Expand Down Expand Up @@ -395,6 +410,10 @@ class Runtime {
// Decided by ElectMainRuntime, before anything can read it.
bool m_isMainThread = false;

std::string m_signatureKeyScratch;

robin_hood::unordered_map<jclass, jfieldID> m_defaultOverridesFields;

// This isolate's performance time origin, captured at isolate
// creation: the monotonic clock reading now() is relative to, and the
// wall-clock milliseconds that reading corresponds to.
Expand Down