Skip to content

Commit fddb38e

Browse files
aamCommit Queue
authored andcommitted
[vm/shared] Introduce IsolateGroup.runSync()
Allow dart code execution on mutator thread, do not require an isolate. It moves some states that was kept on an isolate to thread or isolate group. Bug: #54530 Bug: #56841 TEST=run_isolate_group_run_test CoreLibraryReviewExempt: only internal library is being updated Change-Id: I99df09e23954755387ea6230bfd166493d78e989 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/418503 Reviewed-by: Slava Egorov <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Aprelev <[email protected]>
1 parent 3b444c5 commit fddb38e

34 files changed

+526
-348
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// ignore: export_internal_library
6+
export 'dart:_internal' show IsolateGroup;

runtime/lib/concurrent.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,45 @@ DEFINE_FFI_NATIVE_ENTRY(ConditionVariable_NotifyAll,
110110
condvar->NotifyAll();
111111
}
112112

113+
DEFINE_FFI_NATIVE_ENTRY(IsolateGroup_runSync,
114+
Dart_Handle,
115+
(Dart_Handle closure)) {
116+
Thread* current_thread = Thread::Current();
117+
ASSERT(current_thread->execution_state() == Thread::kThreadInNative);
118+
Isolate* saved_isolate = current_thread->isolate();
119+
current_thread->ExitSafepointFromNative();
120+
current_thread->set_execution_state(Thread::kThreadInVM);
121+
Thread::ExitIsolate(/*isolate_shutdown=*/false);
122+
123+
Thread::EnterIsolateGroupAsMutator(current_thread->isolate_group(),
124+
/*bypass_safepoint=*/false);
125+
126+
auto mutator_thread = Thread::Current();
127+
128+
ApiState* state = mutator_thread->isolate_group()->api_state();
129+
ASSERT(state != nullptr);
130+
mutator_thread->EnterApiScope();
131+
ASSERT(mutator_thread->execution_state() == Thread::kThreadInVM);
132+
133+
Dart_PersistentHandle persistent_result;
134+
{
135+
TransitionVMToNative transition(mutator_thread);
136+
Dart_Handle result = Dart_InvokeClosure(closure, 0, nullptr);
137+
persistent_result = Dart_NewPersistentHandle(result);
138+
}
139+
140+
mutator_thread->ExitApiScope();
141+
142+
Thread::ExitIsolateGroupAsMutator(/*bypass_safepoint=*/false);
143+
Thread::EnterIsolate(saved_isolate);
144+
145+
Thread* T = Thread::Current();
146+
T->EnterSafepointToNative();
147+
T->set_execution_state(Thread::kThreadInNative);
148+
149+
Dart_Handle local_handle = Dart_HandleFromPersistent(persistent_result);
150+
Dart_DeletePersistentHandle(persistent_result);
151+
return local_handle;
152+
}
153+
113154
} // namespace dart

runtime/lib/isolate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ DEFINE_NATIVE_ENTRY(SendPort_sendInternal_, 0, 2) {
117117
#if defined(DEBUG)
118118
if (same_group) {
119119
ASSERT(PortMap::IsReceiverInThisIsolateGroupOrClosed(destination_port_id,
120-
isolate->group()));
120+
group));
121121
}
122122
#endif
123123

runtime/vm/bootstrap_natives.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ namespace dart {
375375
V(ConditionVariable_NotifyAll, void, (Dart_Handle)) \
376376
V(ConditionVariable_Wait, void, (Dart_Handle, Dart_Handle)) \
377377
V(FinalizerEntry_SetExternalSize, void, (Dart_Handle, intptr_t)) \
378+
V(IsolateGroup_runSync, Dart_Handle, (Dart_Handle)) \
378379
V(Mutex_Initialize, void, (Dart_Handle)) \
379380
V(Mutex_RunLocked, Dart_Handle, (Dart_Handle, Dart_Handle)) \
380381
V(Pointer_asTypedListFinalizerAllocateData, void*, ()) \

runtime/vm/compiler/backend/il.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7320,19 +7320,6 @@ void NativeCallInstr::SetupNative() {
73207320
Thread* thread = Thread::Current();
73217321
Zone* zone = thread->zone();
73227322

7323-
// Currently we perform unoptimized compilations only on mutator threads. If
7324-
// the compiler has to resolve a native to a function pointer it calls out to
7325-
// the embedder to do so.
7326-
//
7327-
// Unfortunately that embedder API was designed by giving it a handle to a
7328-
// string. So the embedder will have to call back into the VM to convert it to
7329-
// a C string - which requires an active isolate.
7330-
//
7331-
// => To allow this `dart-->jit-compiler-->embedder-->dart api` we set the
7332-
// active isolate again.
7333-
//
7334-
ActiveIsolateScope active_isolate(thread);
7335-
73367323
const Class& cls = Class::Handle(zone, function().Owner());
73377324
const Library& library = Library::Handle(zone, cls.library());
73387325

runtime/vm/compiler/runtime_api.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,10 @@ class Thread : public AllStatic {
12961296

12971297
static word suspend_state_handle_exception_entry_point_offset();
12981298

1299+
#if !defined(PRODUCT)
1300+
static word single_step_offset();
1301+
#endif // !defined(PRODUCT)
1302+
12991303
static word OffsetFromThread(const dart::Object& object);
13001304
static intptr_t OffsetFromThread(const dart::RuntimeEntry* runtime_entry);
13011305
};
@@ -1344,7 +1348,6 @@ class Isolate : public AllStatic {
13441348
static word user_tag_offset();
13451349
static word finalizers_offset();
13461350
#if !defined(PRODUCT)
1347-
static word single_step_offset();
13481351
static word has_resumption_breakpoints_offset();
13491352
#endif // !defined(PRODUCT)
13501353
};

runtime/vm/compiler/runtime_offsets_extracted.h

Lines changed: 97 additions & 97 deletions
Large diffs are not rendered by default.

runtime/vm/compiler/runtime_offsets_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@
174174
FIELD(IsolateGroup, object_store_offset) \
175175
FIELD(IsolateGroup, class_table_offset) \
176176
FIELD(IsolateGroup, cached_class_table_table_offset) \
177-
NOT_IN_PRODUCT(FIELD(Isolate, single_step_offset)) \
178177
FIELD(Isolate, user_tag_offset) \
179178
FIELD(LinkedHashBase, data_offset) \
180179
FIELD(ImmutableLinkedHashBase, data_offset) \
@@ -319,6 +318,7 @@
319318
FIELD(Thread, saved_shadow_call_stack_offset) \
320319
FIELD(Thread, safepoint_state_offset) \
321320
FIELD(Thread, shared_field_table_values_offset) \
321+
NOT_IN_PRODUCT(FIELD(Thread, single_step_offset)) \
322322
FIELD(Thread, slow_type_test_stub_offset) \
323323
FIELD(Thread, slow_type_test_entry_point_offset) \
324324
FIELD(Thread, stack_limit_offset) \

runtime/vm/compiler/stub_code_compiler_arm.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,8 +2328,7 @@ void StubCodeCompiler::GenerateNArgsCheckInlineCacheStub(
23282328
Label stepping, done_stepping;
23292329
if (optimized == kUnoptimized) {
23302330
__ Comment("Check single stepping");
2331-
__ LoadIsolate(R8);
2332-
__ ldrb(R8, Address(R8, target::Isolate::single_step_offset()));
2331+
__ ldrb(R8, Address(THR, target::Thread::single_step_offset()));
23332332
__ CompareImmediate(R8, 0);
23342333
__ b(&stepping, NE);
23352334
__ Bind(&done_stepping);
@@ -2681,8 +2680,7 @@ void StubCodeCompiler::GenerateZeroArgsUnoptimizedStaticCallStub() {
26812680
#if !defined(PRODUCT)
26822681
// Check single stepping.
26832682
Label stepping, done_stepping;
2684-
__ LoadIsolate(R8);
2685-
__ ldrb(R8, Address(R8, target::Isolate::single_step_offset()));
2683+
__ ldrb(R8, Address(THR, target::Thread::single_step_offset()));
26862684
__ CompareImmediate(R8, 0);
26872685
__ b(&stepping, NE);
26882686
__ Bind(&done_stepping);
@@ -2899,8 +2897,7 @@ void StubCodeCompiler::GenerateDebugStepCheckStub() {
28992897
#else
29002898
// Check single stepping.
29012899
Label stepping, done_stepping;
2902-
__ LoadIsolate(R1);
2903-
__ ldrb(R1, Address(R1, target::Isolate::single_step_offset()));
2900+
__ ldrb(R1, Address(THR, target::Thread::single_step_offset()));
29042901
__ CompareImmediate(R1, 0);
29052902
__ b(&stepping, NE);
29062903
__ Bind(&done_stepping);
@@ -3220,8 +3217,7 @@ void StubCodeCompiler::GenerateUnoptimizedIdenticalWithNumberCheckStub() {
32203217
#if !defined(PRODUCT)
32213218
// Check single stepping.
32223219
Label stepping, done_stepping;
3223-
__ LoadIsolate(R1);
3224-
__ ldrb(R1, Address(R1, target::Isolate::single_step_offset()));
3220+
__ ldrb(R1, Address(THR, target::Thread::single_step_offset()));
32253221
__ CompareImmediate(R1, 0);
32263222
__ b(&stepping, NE);
32273223
__ Bind(&done_stepping);

runtime/vm/compiler/stub_code_compiler_arm64.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,8 +2723,7 @@ void StubCodeCompiler::GenerateNArgsCheckInlineCacheStub(
27232723
Label stepping, done_stepping;
27242724
if (optimized == kUnoptimized) {
27252725
__ Comment("Check single stepping");
2726-
__ LoadIsolate(R6);
2727-
__ LoadFromOffset(R6, R6, target::Isolate::single_step_offset(),
2726+
__ LoadFromOffset(R6, THR, target::Thread::single_step_offset(),
27282727
kUnsignedByte);
27292728
__ CompareRegisters(R6, ZR);
27302729
__ b(&stepping, NE);
@@ -3095,8 +3094,7 @@ void StubCodeCompiler::GenerateZeroArgsUnoptimizedStaticCallStub() {
30953094
// Check single stepping.
30963095
#if !defined(PRODUCT)
30973096
Label stepping, done_stepping;
3098-
__ LoadIsolate(R6);
3099-
__ LoadFromOffset(R6, R6, target::Isolate::single_step_offset(),
3097+
__ LoadFromOffset(R6, THR, target::Thread::single_step_offset(),
31003098
kUnsignedByte);
31013099
__ CompareImmediate(R6, 0);
31023100
__ b(&stepping, NE);
@@ -3337,8 +3335,7 @@ void StubCodeCompiler::GenerateDebugStepCheckStub() {
33373335
#else
33383336
// Check single stepping.
33393337
Label stepping, done_stepping;
3340-
__ LoadIsolate(R1);
3341-
__ LoadFromOffset(R1, R1, target::Isolate::single_step_offset(),
3338+
__ LoadFromOffset(R1, THR, target::Thread::single_step_offset(),
33423339
kUnsignedByte);
33433340
__ CompareImmediate(R1, 0);
33443341
__ b(&stepping, NE);
@@ -3597,8 +3594,7 @@ void StubCodeCompiler::GenerateUnoptimizedIdenticalWithNumberCheckStub() {
35973594
#if !defined(PRODUCT)
35983595
// Check single stepping.
35993596
Label stepping, done_stepping;
3600-
__ LoadIsolate(R1);
3601-
__ LoadFromOffset(R1, R1, target::Isolate::single_step_offset(),
3597+
__ LoadFromOffset(R1, THR, target::Thread::single_step_offset(),
36023598
kUnsignedByte);
36033599
__ CompareImmediate(R1, 0);
36043600
__ b(&stepping, NE);

0 commit comments

Comments
 (0)