Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f30675a

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm/concurrency] Ensure static field registration is done under programlock
Also prepare for being able to grow field table of other isolates when registering a new static field. Issue dart-lang/sdk#36097 TEST=Tests using --enable-isolate-groups. Change-Id: If1bc272e71da5cc89f962cfcac04d72c9314cf51 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175066 Reviewed-by: Alexander Aprelev <aam@google.com> Commit-Queue: Martin Kustermann <kustermann@google.com>
1 parent 073acc7 commit f30675a

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

runtime/vm/compiler/backend/type_propagator_test.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ ISOLATE_UNIT_TEST_CASE(TypePropagator_Refinement) {
187187
/*is_reflectable=*/true,
188188
/*is_late=*/false, object_class, Object::dynamic_type(),
189189
TokenPosition::kNoSource, TokenPosition::kNoSource));
190-
thread->isolate_group()->RegisterStaticField(field, Instance::Handle());
190+
{
191+
SafepointWriteRwLocker locker(thread,
192+
thread->isolate_group()->program_lock());
193+
thread->isolate_group()->RegisterStaticField(field, Instance::Handle());
194+
}
191195

192196
FlowGraphBuilderHelper H;
193197

runtime/vm/dart.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,7 @@ static bool CloneIntoChildIsolateAOT(Thread* T,
697697
}
698698
I->isolate_object_store()->Init();
699699
I->isolate_object_store()->PreallocateObjects();
700-
I->set_field_table(T, source_isolate_group->initial_field_table()->Clone(
701-
/*is_isolate_field_table=*/true));
700+
I->set_field_table(T, source_isolate_group->initial_field_table()->Clone(I));
702701

703702
return true;
704703
}
@@ -743,8 +742,7 @@ ErrorPtr Dart::InitIsolateFromSnapshot(Thread* T,
743742
return error.raw();
744743
}
745744

746-
I->set_field_table(T, I->group()->initial_field_table()->Clone(
747-
/*is_isolate_field_table=*/true));
745+
I->set_field_table(T, I->group()->initial_field_table()->Clone(I));
748746

749747
#if defined(SUPPORT_TIMELINE)
750748
if (tbes.enabled()) {

runtime/vm/field_table.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ void FieldTable::Grow(intptr_t new_capacity) {
9898
// via store to table_.
9999
std::atomic_thread_fence(std::memory_order_release);
100100
table_ = new_table;
101-
if (is_isolate_field_table_) {
102-
Thread::Current()->field_table_values_ = table_;
101+
if (isolate_ != nullptr) {
102+
isolate_->mutator_thread()->field_table_values_ = table_;
103103
}
104104
}
105105

106-
FieldTable* FieldTable::Clone(bool is_isolate_field_table) {
107-
FieldTable* clone = new FieldTable(is_isolate_field_table);
106+
FieldTable* FieldTable::Clone(Isolate* for_isolate) {
107+
FieldTable* clone = new FieldTable(for_isolate);
108108
auto new_table = static_cast<InstancePtr*>(
109109
malloc(capacity_ * sizeof(InstancePtr))); // NOLINT
110110
memmove(new_table, table_, capacity_ * sizeof(InstancePtr));

runtime/vm/field_table.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
namespace dart {
1818

19+
class Isolate;
1920
class Field;
2021

2122
class FieldTable {
2223
public:
23-
explicit FieldTable(bool is_isolate_field_table)
24+
explicit FieldTable(Isolate* isolate)
2425
: top_(0),
2526
capacity_(0),
2627
free_head_(-1),
2728
table_(nullptr),
2829
old_tables_(new MallocGrowableArray<InstancePtr*>()),
29-
is_isolate_field_table_(is_isolate_field_table) {}
30+
isolate_(isolate) {}
3031

3132
~FieldTable();
3233

@@ -56,7 +57,7 @@ class FieldTable {
5657
}
5758
void SetAt(intptr_t index, InstancePtr raw_instance);
5859

59-
FieldTable* Clone(bool is_isolate_field_table);
60+
FieldTable* Clone(Isolate* for_isolate);
6061

6162
void VisitObjectPointers(ObjectPointerVisitor* visitor);
6263

@@ -83,10 +84,10 @@ class FieldTable {
8384
// so it will get freed when its are no longer in use.
8485
MallocGrowableArray<InstancePtr*>* old_tables_;
8586

86-
// Whether this table is used as a isolate-specific table of it's global field
87-
// values and therefore needs to keep the cached field table in the `Thread`
88-
// object up-to-date.
89-
bool is_isolate_field_table_;
87+
// If non-NULL, it will specify the isolate this field table belongs to.
88+
// Growing the field table will keep the cached field table on the isolate's
89+
// mutator thread up-to-date.
90+
Isolate* isolate_;
9091

9192
DISALLOW_COPY_AND_ASSIGN(FieldTable);
9293
};

runtime/vm/isolate.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ IsolateGroup::IsolateGroup(std::shared_ptr<IsolateGroupSource> source,
353353
store_buffer_(new StoreBuffer()),
354354
heap_(nullptr),
355355
saved_unlinked_calls_(Array::null()),
356-
initial_field_table_(new FieldTable(/*is_isolate_field_table=*/false)),
356+
initial_field_table_(new FieldTable(/*isolate=*/nullptr)),
357357
symbols_lock_(new SafepointRwLock()),
358358
type_canonicalization_mutex_(
359359
NOT_IN_PRODUCT("IsolateGroup::type_canonicalization_mutex_")),
@@ -907,6 +907,8 @@ void Isolate::ValidateClassTable() {
907907

908908
void IsolateGroup::RegisterStaticField(const Field& field,
909909
const Instance& initial_value) {
910+
ASSERT(program_lock()->IsCurrentThreadWriter());
911+
910912
ASSERT(field.is_static());
911913
initial_field_table()->Register(field);
912914
initial_field_table()->SetAt(field.field_id(), initial_value.raw());
@@ -1631,7 +1633,7 @@ Isolate::Isolate(IsolateGroup* isolate_group,
16311633
default_tag_(UserTag::null()),
16321634
ic_miss_code_(Code::null()),
16331635
shared_class_table_(isolate_group->shared_class_table()),
1634-
field_table_(new FieldTable(/*is_isolate_field_table=*/true)),
1636+
field_table_(new FieldTable(/*isolate=*/this)),
16351637
isolate_group_(isolate_group),
16361638
isolate_object_store_(
16371639
new IsolateObjectStore(isolate_group->object_store())),

runtime/vm/object_test.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,11 @@ static FieldPtr CreateTestField(const char* name) {
30653065
const Field& field = Field::Handle(Field::New(
30663066
field_name, true, false, false, true, false, cls, Object::dynamic_type(),
30673067
TokenPosition::kMinSource, TokenPosition::kMinSource));
3068-
thread->isolate_group()->RegisterStaticField(field, Instance::sentinel());
3068+
{
3069+
SafepointWriteRwLocker locker(thread,
3070+
thread->isolate_group()->program_lock());
3071+
thread->isolate_group()->RegisterStaticField(field, Instance::sentinel());
3072+
}
30693073
return field.raw();
30703074
}
30713075

0 commit comments

Comments
 (0)