Skip to content

Commit 4c5028d

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm/concurrency] Make an IsolateGroup object which will contain group-specific resources
The isolate group object will keep a doubly-linked list of all isolates within it's group. This will allow us to later on iterate all isolates within a group for e.g. GC purposes. The isolate group object will eventually hold the shared heap, thread registry and other resources. This CL is only the start. Issue #36097 Change-Id: I32103bbb4830cd4f16665c4032d47dabdf3594dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108407 Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Alexander Aprelev <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent 24725a8 commit 4c5028d

File tree

9 files changed

+201
-142
lines changed

9 files changed

+201
-142
lines changed

runtime/PRESUBMIT.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ def CheckMemcpy(filename):
2020
content = fh.read()
2121
match = re.search('\\bmemcpy\\b', content)
2222
if match:
23-
line_number = content[0:match.start()].count('\n') + 1
24-
print("%s:%d: use of memcpy is forbidden" % (filename, line_number))
25-
return 1
23+
offset = match.start()
24+
end_of_line = content.index('\n', offset)
25+
# We allow explicit use of memcpy with an opt-in via NOLINT
26+
if 'NOLINT' not in content[offset:end_of_line]:
27+
line_number = content[0:match.start()].count('\n') + 1
28+
print("%s:%d: use of memcpy is forbidden" % (filename, line_number))
29+
return 1
2630
return 0
2731

2832

runtime/lib/isolate.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
129129
}
130130

131131
void Run() override {
132-
IsolateGroupSource* source = state_->source();
132+
auto group = state_->isolate_group();
133133

134134
// The create isolate group call back is mandatory. If not provided we
135135
// cannot spawn isolates.
@@ -152,7 +152,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
152152
// Create a new isolate.
153153
char* error = nullptr;
154154
Isolate* isolate = nullptr;
155-
if (source == nullptr || initialize_callback == nullptr) {
155+
if (group == nullptr || initialize_callback == nullptr) {
156156
// Make a copy of the state's isolate flags and hand it to the callback.
157157
Dart_IsolateFlags api_flags = *(state_->isolate_flags());
158158
isolate = reinterpret_cast<Isolate*>((create_group_callback)(
@@ -166,7 +166,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
166166
return;
167167
}
168168

169-
isolate = CreateIsolateFromExistingSource(source, name, &error);
169+
isolate = CreateWithinExistingIsolateGroup(group, name, &error);
170170
parent_isolate_->DecrementSpawnCount();
171171
parent_isolate_ = nullptr;
172172
if (isolate == nullptr) {
@@ -285,7 +285,7 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 0, 11) {
285285
std::unique_ptr<IsolateSpawnState> state(new IsolateSpawnState(
286286
port.Id(), isolate->origin_id(), String2UTF8(script_uri), func,
287287
&message_buffer, utf8_package_config, paused.value(), fatal_errors,
288-
on_exit_port, on_error_port, utf8_debug_name, isolate->source()));
288+
on_exit_port, on_error_port, utf8_debug_name, isolate->group()));
289289

290290
// Since this is a call to Isolate.spawn, copy the parent isolate's code.
291291
state->isolate_flags()->copy_parent_code = true;
@@ -396,12 +396,10 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 0, 13) {
396396
const char* utf8_debug_name =
397397
debugName.IsNull() ? NULL : String2UTF8(debugName);
398398

399-
IsolateGroupSource* null_source = nullptr;
400-
401399
std::unique_ptr<IsolateSpawnState> state(new IsolateSpawnState(
402400
port.Id(), canonical_uri, utf8_package_config, &arguments_buffer,
403401
&message_buffer, paused.value(), fatal_errors, on_exit_port,
404-
on_error_port, utf8_debug_name, null_source));
402+
on_error_port, utf8_debug_name, /*group=*/nullptr));
405403

406404
// If we were passed a value then override the default flags state for
407405
// checked mode.
@@ -470,7 +468,7 @@ static void ExternalTypedDataFinalizer(void* isolate_callback_data,
470468
free(peer);
471469
}
472470

473-
static intptr_t GetUint8SizeOrThrow(const Instance& instance) {
471+
static intptr_t GetTypedDataSizeOrThrow(const Instance& instance) {
474472
// From the Dart side we are guaranteed that the type of [instance] is a
475473
// subtype of TypedData.
476474
if (instance.IsTypedDataBase()) {
@@ -503,19 +501,18 @@ DEFINE_NATIVE_ENTRY(TransferableTypedData_factory, 0, 2) {
503501
UNREACHABLE();
504502
}
505503
Instance& instance = Instance::Handle();
506-
unsigned long long total_bytes = 0;
507-
const unsigned long kMaxBytes =
508-
TypedData::MaxElements(kTypedDataUint8ArrayCid);
504+
uint64_t total_bytes = 0;
505+
const uint64_t kMaxBytes = TypedData::MaxElements(kTypedDataUint8ArrayCid);
509506
for (intptr_t i = 0; i < array_length; i++) {
510507
instance ^= array.At(i);
511-
total_bytes += GetUint8SizeOrThrow(instance);
508+
total_bytes += static_cast<uintptr_t>(GetTypedDataSizeOrThrow(instance));
512509
if (total_bytes > kMaxBytes) {
513510
const Array& error_args = Array::Handle(Array::New(3));
514511
error_args.SetAt(0, array);
515512
error_args.SetAt(1, String::Handle(String::New("data")));
516-
error_args.SetAt(2,
517-
String::Handle(String::NewFormatted(
518-
"Aggregated list exceeds max size %ld", kMaxBytes)));
513+
error_args.SetAt(
514+
2, String::Handle(String::NewFormatted(
515+
"Aggregated list exceeds max size %" Pu64 "", kMaxBytes)));
519516
Exceptions::ThrowByType(Exceptions::kArgumentValue, error_args);
520517
UNREACHABLE();
521518
}
@@ -539,11 +536,11 @@ DEFINE_NATIVE_ENTRY(TransferableTypedData_factory, 0, 2) {
539536

540537
void* source = typed_data.DataAddr(0);
541538
// The memory does not overlap.
542-
memcpy(data + offset, source, length_in_bytes);
539+
memcpy(data + offset, source, length_in_bytes); // NOLINT
543540
offset += length_in_bytes;
544541
}
545542
}
546-
ASSERT(static_cast<unsigned long>(offset) == total_bytes);
543+
ASSERT(static_cast<uintptr_t>(offset) == total_bytes);
547544
return TransferableTypedData::New(data, total_bytes);
548545
}
549546

runtime/vm/compiler/runtime_offsets_extracted.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ static constexpr dart::compiler::target::word ICData_owner_offset = 20;
138138
static constexpr dart::compiler::target::word ICData_state_bits_offset = 28;
139139
static constexpr dart::compiler::target::word
140140
ICData_receivers_static_type_offset = 16;
141-
static constexpr dart::compiler::target::word Isolate_class_table_offset = 24;
142-
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 8;
143-
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 12;
144-
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 16;
145-
static constexpr dart::compiler::target::word Isolate_object_store_offset = 20;
146-
static constexpr dart::compiler::target::word Isolate_single_step_offset = 44;
147-
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 4;
141+
static constexpr dart::compiler::target::word Isolate_class_table_offset = 32;
142+
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 16;
143+
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 20;
144+
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 24;
145+
static constexpr dart::compiler::target::word Isolate_object_store_offset = 28;
146+
static constexpr dart::compiler::target::word Isolate_single_step_offset = 52;
147+
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 12;
148148
static constexpr dart::compiler::target::word LinkedHashMap_data_offset = 16;
149149
static constexpr dart::compiler::target::word
150150
LinkedHashMap_deleted_keys_offset = 24;
@@ -491,13 +491,13 @@ static constexpr dart::compiler::target::word ICData_owner_offset = 40;
491491
static constexpr dart::compiler::target::word ICData_state_bits_offset = 52;
492492
static constexpr dart::compiler::target::word
493493
ICData_receivers_static_type_offset = 32;
494-
static constexpr dart::compiler::target::word Isolate_class_table_offset = 48;
495-
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 16;
496-
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 24;
497-
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 32;
498-
static constexpr dart::compiler::target::word Isolate_object_store_offset = 40;
499-
static constexpr dart::compiler::target::word Isolate_single_step_offset = 88;
500-
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 8;
494+
static constexpr dart::compiler::target::word Isolate_class_table_offset = 64;
495+
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 32;
496+
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 40;
497+
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 48;
498+
static constexpr dart::compiler::target::word Isolate_object_store_offset = 56;
499+
static constexpr dart::compiler::target::word Isolate_single_step_offset = 104;
500+
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 24;
501501
static constexpr dart::compiler::target::word LinkedHashMap_data_offset = 32;
502502
static constexpr dart::compiler::target::word
503503
LinkedHashMap_deleted_keys_offset = 48;
@@ -845,13 +845,13 @@ static constexpr dart::compiler::target::word ICData_owner_offset = 20;
845845
static constexpr dart::compiler::target::word ICData_state_bits_offset = 28;
846846
static constexpr dart::compiler::target::word
847847
ICData_receivers_static_type_offset = 16;
848-
static constexpr dart::compiler::target::word Isolate_class_table_offset = 24;
849-
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 8;
850-
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 12;
851-
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 16;
852-
static constexpr dart::compiler::target::word Isolate_object_store_offset = 20;
853-
static constexpr dart::compiler::target::word Isolate_single_step_offset = 44;
854-
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 4;
848+
static constexpr dart::compiler::target::word Isolate_class_table_offset = 32;
849+
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 16;
850+
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 20;
851+
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 24;
852+
static constexpr dart::compiler::target::word Isolate_object_store_offset = 28;
853+
static constexpr dart::compiler::target::word Isolate_single_step_offset = 52;
854+
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 12;
855855
static constexpr dart::compiler::target::word LinkedHashMap_data_offset = 16;
856856
static constexpr dart::compiler::target::word
857857
LinkedHashMap_deleted_keys_offset = 24;
@@ -1194,13 +1194,13 @@ static constexpr dart::compiler::target::word ICData_owner_offset = 40;
11941194
static constexpr dart::compiler::target::word ICData_state_bits_offset = 52;
11951195
static constexpr dart::compiler::target::word
11961196
ICData_receivers_static_type_offset = 32;
1197-
static constexpr dart::compiler::target::word Isolate_class_table_offset = 48;
1198-
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 16;
1199-
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 24;
1200-
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 32;
1201-
static constexpr dart::compiler::target::word Isolate_object_store_offset = 40;
1202-
static constexpr dart::compiler::target::word Isolate_single_step_offset = 88;
1203-
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 8;
1197+
static constexpr dart::compiler::target::word Isolate_class_table_offset = 64;
1198+
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 32;
1199+
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 40;
1200+
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 48;
1201+
static constexpr dart::compiler::target::word Isolate_object_store_offset = 56;
1202+
static constexpr dart::compiler::target::word Isolate_single_step_offset = 104;
1203+
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 24;
12041204
static constexpr dart::compiler::target::word LinkedHashMap_data_offset = 32;
12051205
static constexpr dart::compiler::target::word
12061206
LinkedHashMap_deleted_keys_offset = 48;
@@ -1551,13 +1551,13 @@ static constexpr dart::compiler::target::word ICData_owner_offset = 40;
15511551
static constexpr dart::compiler::target::word ICData_state_bits_offset = 52;
15521552
static constexpr dart::compiler::target::word
15531553
ICData_receivers_static_type_offset = 32;
1554-
static constexpr dart::compiler::target::word Isolate_class_table_offset = 48;
1555-
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 16;
1556-
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 24;
1557-
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 32;
1558-
static constexpr dart::compiler::target::word Isolate_object_store_offset = 40;
1559-
static constexpr dart::compiler::target::word Isolate_single_step_offset = 88;
1560-
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 8;
1554+
static constexpr dart::compiler::target::word Isolate_class_table_offset = 64;
1555+
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 32;
1556+
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 40;
1557+
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 48;
1558+
static constexpr dart::compiler::target::word Isolate_object_store_offset = 56;
1559+
static constexpr dart::compiler::target::word Isolate_single_step_offset = 104;
1560+
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 24;
15611561
static constexpr dart::compiler::target::word LinkedHashMap_data_offset = 32;
15621562
static constexpr dart::compiler::target::word
15631563
LinkedHashMap_deleted_keys_offset = 48;
@@ -1835,13 +1835,13 @@ static constexpr dart::compiler::target::word ICData_owner_offset = 20;
18351835
static constexpr dart::compiler::target::word ICData_state_bits_offset = 28;
18361836
static constexpr dart::compiler::target::word
18371837
ICData_receivers_static_type_offset = 16;
1838-
static constexpr dart::compiler::target::word Isolate_class_table_offset = 24;
1839-
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 8;
1840-
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 12;
1841-
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 16;
1842-
static constexpr dart::compiler::target::word Isolate_object_store_offset = 20;
1843-
static constexpr dart::compiler::target::word Isolate_single_step_offset = 44;
1844-
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 4;
1838+
static constexpr dart::compiler::target::word Isolate_class_table_offset = 32;
1839+
static constexpr dart::compiler::target::word Isolate_current_tag_offset = 16;
1840+
static constexpr dart::compiler::target::word Isolate_default_tag_offset = 20;
1841+
static constexpr dart::compiler::target::word Isolate_ic_miss_code_offset = 24;
1842+
static constexpr dart::compiler::target::word Isolate_object_store_offset = 28;
1843+
static constexpr dart::compiler::target::word Isolate_single_step_offset = 52;
1844+
static constexpr dart::compiler::target::word Isolate_user_tag_offset = 12;
18451845
static constexpr dart::compiler::target::word LinkedHashMap_data_offset = 16;
18461846
static constexpr dart::compiler::target::word
18471847
LinkedHashMap_deleted_keys_offset = 24;

runtime/vm/dart.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
#include <memory>
6+
#include <utility>
7+
58
#include "vm/dart.h"
69

710
#include "vm/clustered_snapshot.h"
@@ -234,7 +237,18 @@ char* Dart::Init(const uint8_t* vm_isolate_snapshot,
234237
// Setup default flags for the VM isolate.
235238
Dart_IsolateFlags api_flags;
236239
Isolate::FlagsInitialize(&api_flags);
237-
vm_isolate_ = Isolate::InitIsolate("vm-isolate", api_flags, is_vm_isolate);
240+
241+
// We make a fake [IsolateGroupSource] here, since the "vm-isolate" is not
242+
// really an isolate itself - it acts more as a container for VM-global
243+
// objects.
244+
std::unique_ptr<IsolateGroupSource> source(
245+
new IsolateGroupSource(nullptr, "vm-isolate", nullptr, nullptr, nullptr,
246+
nullptr, nullptr, -1, api_flags));
247+
auto group = new IsolateGroup(std::move(source), /*embedder_data=*/nullptr);
248+
vm_isolate_ =
249+
Isolate::InitIsolate("vm-isolate", group, api_flags, is_vm_isolate);
250+
group->set_initial_spawn_successful();
251+
238252
// Verify assumptions about executing in the VM isolate.
239253
ASSERT(vm_isolate_ == Isolate::Current());
240254
ASSERT(vm_isolate_ == Thread::Current()->isolate());
@@ -604,9 +618,11 @@ char* Dart::Cleanup() {
604618
}
605619

606620
Isolate* Dart::CreateIsolate(const char* name_prefix,
607-
const Dart_IsolateFlags& api_flags) {
621+
const Dart_IsolateFlags& api_flags,
622+
IsolateGroup* isolate_group) {
608623
// Create a new isolate.
609-
Isolate* isolate = Isolate::InitIsolate(name_prefix, api_flags);
624+
Isolate* isolate =
625+
Isolate::InitIsolate(name_prefix, isolate_group, api_flags);
610626
return isolate;
611627
}
612628

@@ -854,7 +870,7 @@ void Dart::RunShutdownCallback() {
854870
Thread* thread = Thread::Current();
855871
ASSERT(thread->execution_state() == Thread::kThreadInVM);
856872
Isolate* isolate = thread->isolate();
857-
void* isolate_group_data = isolate->source()->callback_data;
873+
void* isolate_group_data = isolate->group()->embedder_data();
858874
void* isolate_data = isolate->init_callback_data();
859875
Dart_IsolateShutdownCallback callback = Isolate::ShutdownCallback();
860876
if (callback != NULL) {

runtime/vm/dart.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class Dart : public AllStatic {
4848
static char* Cleanup();
4949

5050
static Isolate* CreateIsolate(const char* name_prefix,
51-
const Dart_IsolateFlags& api_flags);
51+
const Dart_IsolateFlags& api_flags,
52+
IsolateGroup* isolate_group);
5253

5354
// Initialize an isolate, either from a snapshot, from a Kernel binary, or
5455
// from SDK library sources. If the snapshot_buffer is non-NULL,

0 commit comments

Comments
 (0)