-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathisolate.h
1892 lines (1568 loc) · 63.5 KB
/
isolate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_ISOLATE_H_
#define RUNTIME_VM_ISOLATE_H_
#if defined(SHOULD_NOT_INCLUDE_RUNTIME)
#error "Should not include runtime"
#endif
#include <functional>
#include <memory>
#include <utility>
#include "include/dart_api.h"
#include "platform/assert.h"
#include "platform/atomic.h"
#include "vm/base_isolate.h"
#include "vm/class_table.h"
#include "vm/dispatch_table.h"
#include "vm/exceptions.h"
#include "vm/field_table.h"
#include "vm/fixed_cache.h"
#include "vm/growable_array.h"
#include "vm/handles.h"
#include "vm/heap/verifier.h"
#include "vm/intrusive_dlist.h"
#include "vm/megamorphic_cache_table.h"
#include "vm/metrics.h"
#include "vm/os_thread.h"
#include "vm/random.h"
#include "vm/tags.h"
#include "vm/thread.h"
#include "vm/thread_pool.h"
#include "vm/thread_stack_resource.h"
#include "vm/token_position.h"
#include "vm/virtual_memory.h"
#if !defined(DART_PRECOMPILED_RUNTIME)
#include "vm/ffi_callback_trampolines.h"
#endif // !defined(DART_PRECOMPILED_RUNTIME)
namespace dart {
// Forward declarations.
class ApiState;
class BackgroundCompiler;
class Become;
class Capability;
class CodeIndexTable;
class Debugger;
class DeoptContext;
class ExternalTypedData;
class GroupDebugger;
class HandleScope;
class HandleVisitor;
class Heap;
class ICData;
class IsolateGroupReloadContext;
class IsolateObjectStore;
class IsolateProfilerData;
class ProgramReloadContext;
class ReloadHandler;
class Log;
class Message;
class MessageHandler;
class MonitorLocker;
class Mutex;
class Object;
class ObjectIdRing;
class ObjectPointerVisitor;
class ObjectStore;
class PersistentHandle;
class RwLock;
class SafepointRwLock;
class SafepointHandler;
class SampleBuffer;
class SampleBlock;
class SampleBlockBuffer;
class SendPort;
class SerializedObjectBuffer;
class ServiceIdZone;
class Simulator;
class StackResource;
class StackZone;
class StoreBuffer;
class StubCode;
class ThreadRegistry;
class UserTag;
class WeakTable;
class IsolateVisitor {
public:
IsolateVisitor() {}
virtual ~IsolateVisitor() {}
virtual void VisitIsolate(Isolate* isolate) = 0;
protected:
// Returns true if |isolate| is the VM or service isolate.
bool IsSystemIsolate(Isolate* isolate) const;
private:
DISALLOW_COPY_AND_ASSIGN(IsolateVisitor);
};
class Callable : public ValueObject {
public:
Callable() {}
virtual ~Callable() {}
virtual void Call() = 0;
private:
DISALLOW_COPY_AND_ASSIGN(Callable);
};
template <typename T>
class LambdaCallable : public Callable {
public:
explicit LambdaCallable(T& lambda) : lambda_(lambda) {}
void Call() { lambda_(); }
private:
T& lambda_;
DISALLOW_COPY_AND_ASSIGN(LambdaCallable);
};
// Fixed cache for exception handler lookup.
typedef FixedCache<intptr_t, ExceptionHandlerInfo, 16> HandlerInfoCache;
// Fixed cache for catch entry state lookup.
typedef FixedCache<intptr_t, CatchEntryMovesRefPtr, 16> CatchEntryMovesCache;
// List of Isolate flags with corresponding members of Dart_IsolateFlags and
// corresponding global command line flags.
#define BOOL_ISOLATE_FLAG_LIST(V) BOOL_ISOLATE_FLAG_LIST_DEFAULT_GETTER(V)
#define BOOL_ISOLATE_GROUP_FLAG_LIST(V) \
BOOL_ISOLATE_GROUP_FLAG_LIST_DEFAULT_GETTER(V) \
BOOL_ISOLATE_GROUP_FLAG_LIST_CUSTOM_GETTER(V)
// List of Isolate flags with default getters.
//
// V(when, name, bit-name, Dart_IsolateFlags-name, command-line-flag-name)
//
#define BOOL_ISOLATE_GROUP_FLAG_LIST_DEFAULT_GETTER(V) \
V(PRECOMPILER, obfuscate, Obfuscate, obfuscate, false) \
V(NONPRODUCT, asserts, EnableAsserts, enable_asserts, FLAG_enable_asserts) \
V(NONPRODUCT, use_field_guards, UseFieldGuards, use_field_guards, \
FLAG_use_field_guards) \
V(PRODUCT, should_load_vmservice_library, ShouldLoadVmService, \
load_vmservice_library, false) \
V(NONPRODUCT, use_osr, UseOsr, use_osr, FLAG_use_osr) \
V(NONPRODUCT, snapshot_is_dontneed_safe, SnapshotIsDontNeedSafe, \
snapshot_is_dontneed_safe, false) \
V(NONPRODUCT, branch_coverage, BranchCoverage, branch_coverage, \
FLAG_branch_coverage)
#define BOOL_ISOLATE_FLAG_LIST_DEFAULT_GETTER(V) \
V(PRODUCT, copy_parent_code, CopyParentCode, copy_parent_code, false) \
V(PRODUCT, is_system_isolate, IsSystemIsolate, is_system_isolate, false)
// List of Isolate flags with custom getters named #name().
//
// V(when, name, bit-name, Dart_IsolateFlags-name, default_value)
//
#define BOOL_ISOLATE_GROUP_FLAG_LIST_CUSTOM_GETTER(V) \
V(PRODUCT, null_safety, NullSafety, null_safety, false)
// Represents the information used for spawning the first isolate within an
// isolate group. All isolates within a group will refer to this
// [IsolateGroupSource].
class IsolateGroupSource {
public:
IsolateGroupSource(const char* script_uri,
const char* name,
const uint8_t* snapshot_data,
const uint8_t* snapshot_instructions,
const uint8_t* kernel_buffer,
intptr_t kernel_buffer_size,
Dart_IsolateFlags flags)
: script_uri(script_uri == nullptr ? nullptr : Utils::StrDup(script_uri)),
name(Utils::StrDup(name)),
snapshot_data(snapshot_data),
snapshot_instructions(snapshot_instructions),
kernel_buffer(kernel_buffer),
kernel_buffer_size(kernel_buffer_size),
flags(flags),
script_kernel_buffer(nullptr),
script_kernel_size(-1),
loaded_blobs_(nullptr),
num_blob_loads_(0) {}
~IsolateGroupSource() {
free(script_uri);
free(name);
}
void add_loaded_blob(Zone* zone_,
const ExternalTypedData& external_typed_data);
// The arguments used for spawning in
// `Dart_CreateIsolateGroupFromKernel` / `Dart_CreateIsolate`.
char* script_uri;
char* name;
const uint8_t* snapshot_data;
const uint8_t* snapshot_instructions;
const uint8_t* kernel_buffer;
const intptr_t kernel_buffer_size;
Dart_IsolateFlags flags;
// The kernel buffer used in `Dart_LoadScriptFromKernel`.
const uint8_t* script_kernel_buffer;
intptr_t script_kernel_size;
// List of weak pointers to external typed data for loaded blobs.
ArrayPtr loaded_blobs_;
intptr_t num_blob_loads_;
};
// Tracks idle time and notifies heap when idle time expired.
class IdleTimeHandler : public ValueObject {
public:
IdleTimeHandler() {}
// Initializes the idle time handler with the given [heap], to which
// idle notifications will be sent.
void InitializeWithHeap(Heap* heap);
// Returns whether the caller should check for idle timeouts.
bool ShouldCheckForIdle();
// Declares that the idle time should be reset to now.
void UpdateStartIdleTime();
// Returns whether idle time expired and [NotifyIdle] should be called.
bool ShouldNotifyIdle(int64_t* expiry);
// Notifies the heap that now is a good time to do compactions and indicates
// we have time for the GC until [deadline].
void NotifyIdle(int64_t deadline);
// Calls [NotifyIdle] with the default deadline.
void NotifyIdleUsingDefaultDeadline();
private:
friend class DisableIdleTimerScope;
Mutex mutex_;
Heap* heap_ = nullptr;
intptr_t disabled_counter_ = 0;
int64_t idle_start_time_ = 0;
};
// Disables firing of the idle timer while this object is alive.
class DisableIdleTimerScope : public ValueObject {
public:
explicit DisableIdleTimerScope(IdleTimeHandler* handler);
~DisableIdleTimerScope();
private:
IdleTimeHandler* handler_;
};
class MutatorThreadPool : public ThreadPool {
public:
MutatorThreadPool(IsolateGroup* isolate_group, intptr_t max_pool_size)
: ThreadPool(max_pool_size), isolate_group_(isolate_group) {}
virtual ~MutatorThreadPool() {}
protected:
virtual void OnEnterIdleLocked(MonitorLocker* ml);
private:
void NotifyIdle();
IsolateGroup* isolate_group_ = nullptr;
};
// Represents an isolate group and is shared among all isolates within a group.
class IsolateGroup : public IntrusiveDListEntry<IsolateGroup> {
public:
IsolateGroup(std::shared_ptr<IsolateGroupSource> source,
void* embedder_data,
ObjectStore* object_store,
Dart_IsolateFlags api_flags);
IsolateGroup(std::shared_ptr<IsolateGroupSource> source,
void* embedder_data,
Dart_IsolateFlags api_flags);
~IsolateGroup();
void RehashConstants();
#if defined(DEBUG)
void ValidateConstants();
void ValidateClassTable();
#endif
IsolateGroupSource* source() const { return source_.get(); }
std::shared_ptr<IsolateGroupSource> shareable_source() const {
return source_;
}
void* embedder_data() const { return embedder_data_; }
bool initial_spawn_successful() { return initial_spawn_successful_; }
void set_initial_spawn_successful() { initial_spawn_successful_ = true; }
Heap* heap() const { return heap_.get(); }
BackgroundCompiler* background_compiler() const {
#if defined(DART_PRECOMPILED_RUNTIME)
return nullptr;
#else
return background_compiler_.get();
#endif
}
#if !defined(DART_PRECOMPILED_RUNTIME)
intptr_t optimization_counter_threshold() const {
if (IsSystemIsolateGroup(this)) {
return kDefaultOptimizationCounterThreshold;
}
return FLAG_optimization_counter_threshold;
}
#endif
#if !defined(PRODUCT)
GroupDebugger* debugger() const { return debugger_; }
#endif
IdleTimeHandler* idle_time_handler() { return &idle_time_handler_; }
// Returns true if this is the first isolate registered.
void RegisterIsolate(Isolate* isolate);
void UnregisterIsolate(Isolate* isolate);
// Returns `true` if this was the last isolate and the caller is responsible
// for deleting the isolate group.
bool UnregisterIsolateDecrementCount();
bool ContainsOnlyOneIsolate();
void RunWithLockedGroup(std::function<void()> fun);
Monitor* threads_lock() const;
ThreadRegistry* thread_registry() const { return thread_registry_.get(); }
SafepointHandler* safepoint_handler() { return safepoint_handler_.get(); }
#if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
ReloadHandler* reload_handler() { return reload_handler_.get(); }
#endif
void CreateHeap(bool is_vm_isolate, bool is_service_or_kernel_isolate);
void SetupImagePage(const uint8_t* snapshot_buffer, bool is_executable);
void Shutdown();
#define ISOLATE_METRIC_ACCESSOR(type, variable, name, unit) \
type* Get##variable##Metric() { return &metric_##variable##_; }
ISOLATE_GROUP_METRIC_LIST(ISOLATE_METRIC_ACCESSOR);
#undef ISOLATE_METRIC_ACCESSOR
#if !defined(PRODUCT)
void UpdateLastAllocationProfileAccumulatorResetTimestamp() {
last_allocationprofile_accumulator_reset_timestamp_ =
OS::GetCurrentTimeMillis();
}
int64_t last_allocationprofile_accumulator_reset_timestamp() const {
return last_allocationprofile_accumulator_reset_timestamp_;
}
void UpdateLastAllocationProfileGCTimestamp() {
last_allocationprofile_gc_timestamp_ = OS::GetCurrentTimeMillis();
}
int64_t last_allocationprofile_gc_timestamp() const {
return last_allocationprofile_gc_timestamp_;
}
#endif // !defined(PRODUCT)
DispatchTable* dispatch_table() const { return dispatch_table_.get(); }
void set_dispatch_table(DispatchTable* table) {
dispatch_table_.reset(table);
}
const uint8_t* dispatch_table_snapshot() const {
return dispatch_table_snapshot_;
}
void set_dispatch_table_snapshot(const uint8_t* snapshot) {
dispatch_table_snapshot_ = snapshot;
}
intptr_t dispatch_table_snapshot_size() const {
return dispatch_table_snapshot_size_;
}
void set_dispatch_table_snapshot_size(intptr_t size) {
dispatch_table_snapshot_size_ = size;
}
ClassTableAllocator* class_table_allocator() {
return &class_table_allocator_;
}
static intptr_t class_table_offset() {
COMPILE_ASSERT(sizeof(IsolateGroup::class_table_) == kWordSize);
return OFFSET_OF(IsolateGroup, class_table_);
}
ClassPtr* cached_class_table_table() {
return cached_class_table_table_.load();
}
void set_cached_class_table_table(ClassPtr* cached_class_table_table) {
cached_class_table_table_.store(cached_class_table_table);
}
static intptr_t cached_class_table_table_offset() {
COMPILE_ASSERT(sizeof(IsolateGroup::cached_class_table_table_) ==
kWordSize);
return OFFSET_OF(IsolateGroup, cached_class_table_table_);
}
void set_object_store(ObjectStore* object_store);
static intptr_t object_store_offset() {
COMPILE_ASSERT(sizeof(IsolateGroup::object_store_) == kWordSize);
return OFFSET_OF(IsolateGroup, object_store_);
}
void set_obfuscation_map(const char** map) { obfuscation_map_ = map; }
const char** obfuscation_map() const { return obfuscation_map_; }
Random* random() { return &random_; }
bool is_system_isolate_group() const { return is_system_isolate_group_; }
// IsolateGroup-specific flag handling.
static void FlagsInitialize(Dart_IsolateFlags* api_flags);
void FlagsCopyTo(Dart_IsolateFlags* api_flags);
void FlagsCopyFrom(const Dart_IsolateFlags& api_flags);
#if defined(DART_PRECOMPILER)
#define FLAG_FOR_PRECOMPILER(from_field, from_flag) (from_field)
#else
#define FLAG_FOR_PRECOMPILER(from_field, from_flag) (from_flag)
#endif
#if !defined(PRODUCT)
#define FLAG_FOR_NONPRODUCT(from_field, from_flag) (from_field)
#else
#define FLAG_FOR_NONPRODUCT(from_field, from_flag) (from_flag)
#endif
#define FLAG_FOR_PRODUCT(from_field, from_flag) (from_field)
#define DECLARE_GETTER(when, name, bitname, isolate_flag_name, flag_name) \
bool name() const { \
return FLAG_FOR_##when(bitname##Bit::decode(isolate_group_flags_), \
flag_name); \
}
BOOL_ISOLATE_GROUP_FLAG_LIST_DEFAULT_GETTER(DECLARE_GETTER)
#undef FLAG_FOR_NONPRODUCT
#undef FLAG_FOR_PRECOMPILER
#undef FLAG_FOR_PRODUCT
#undef DECLARE_GETTER
bool null_safety_not_set() const {
return !NullSafetySetBit::decode(isolate_group_flags_);
}
bool null_safety() const {
ASSERT(!null_safety_not_set());
return NullSafetyBit::decode(isolate_group_flags_);
}
void set_null_safety(bool null_safety) {
isolate_group_flags_ = NullSafetySetBit::update(true, isolate_group_flags_);
isolate_group_flags_ =
NullSafetyBit::update(null_safety, isolate_group_flags_);
}
bool use_strict_null_safety_checks() const {
return null_safety() || FLAG_strict_null_safety_checks;
}
bool should_load_vmservice() const {
return ShouldLoadVmServiceBit::decode(isolate_group_flags_);
}
void set_should_load_vmservice(bool value) {
isolate_group_flags_ =
ShouldLoadVmServiceBit::update(value, isolate_group_flags_);
}
void set_asserts(bool value) {
isolate_group_flags_ =
EnableAssertsBit::update(value, isolate_group_flags_);
}
void set_branch_coverage(bool value) {
isolate_group_flags_ =
BranchCoverageBit::update(value, isolate_group_flags_);
}
#if !defined(PRODUCT)
#if !defined(DART_PRECOMPILED_RUNTIME)
bool HasAttemptedReload() const {
return HasAttemptedReloadBit::decode(isolate_group_flags_);
}
void SetHasAttemptedReload(bool value) {
isolate_group_flags_ =
HasAttemptedReloadBit::update(value, isolate_group_flags_);
}
void MaybeIncreaseReloadEveryNStackOverflowChecks();
intptr_t reload_every_n_stack_overflow_checks() const {
return reload_every_n_stack_overflow_checks_;
}
#else
bool HasAttemptedReload() const { return false; }
#endif // !defined(DART_PRECOMPILED_RUNTIME)
#endif // !defined(PRODUCT)
#if defined(PRODUCT)
void set_use_osr(bool use_osr) { ASSERT(!use_osr); }
#else // defined(PRODUCT)
void set_use_osr(bool use_osr) {
isolate_group_flags_ = UseOsrBit::update(use_osr, isolate_group_flags_);
}
#endif // defined(PRODUCT)
// Class table for the program loaded into this isolate group.
//
// This table is modified by kernel loading.
ClassTable* class_table() const {
return class_table_;
}
// Class table used for heap walks by GC visitors. Usually it
// is the same table as one in |class_table_|, except when in the
// middle of the reload.
//
// See comment for |ClassTable| class for more details.
ClassTable* heap_walk_class_table() const {
return heap_walk_class_table_;
}
void CloneClassTableForReload();
void RestoreOriginalClassTable();
void DropOriginalClassTable();
StoreBuffer* store_buffer() const { return store_buffer_.get(); }
ObjectStore* object_store() const { return object_store_.get(); }
Mutex* symbols_mutex() { return &symbols_mutex_; }
Mutex* type_canonicalization_mutex() { return &type_canonicalization_mutex_; }
Mutex* type_arguments_canonicalization_mutex() {
return &type_arguments_canonicalization_mutex_;
}
Mutex* subtype_test_cache_mutex() { return &subtype_test_cache_mutex_; }
Mutex* megamorphic_table_mutex() { return &megamorphic_table_mutex_; }
Mutex* type_feedback_mutex() { return &type_feedback_mutex_; }
Mutex* patchable_call_mutex() { return &patchable_call_mutex_; }
Mutex* constant_canonicalization_mutex() {
return &constant_canonicalization_mutex_;
}
Mutex* kernel_data_lib_cache_mutex() { return &kernel_data_lib_cache_mutex_; }
Mutex* kernel_data_class_cache_mutex() {
return &kernel_data_class_cache_mutex_;
}
Mutex* kernel_constants_mutex() { return &kernel_constants_mutex_; }
#if defined(DART_PRECOMPILED_RUNTIME)
Mutex* unlinked_call_map_mutex() { return &unlinked_call_map_mutex_; }
#endif
#if !defined(DART_PRECOMPILED_RUNTIME)
Mutex* initializer_functions_mutex() { return &initializer_functions_mutex_; }
#endif // !defined(DART_PRECOMPILED_RUNTIME)
SafepointRwLock* program_lock() { return program_lock_.get(); }
static inline IsolateGroup* Current() {
Thread* thread = Thread::Current();
return thread == nullptr ? nullptr : thread->isolate_group();
}
Thread* ScheduleThreadLocked(MonitorLocker* ml,
Thread* existing_mutator_thread,
bool is_vm_isolate,
bool is_mutator,
bool bypass_safepoint = false);
void UnscheduleThreadLocked(MonitorLocker* ml,
Thread* thread,
bool is_mutator,
bool bypass_safepoint = false);
Thread* ScheduleThread(bool bypass_safepoint = false);
void UnscheduleThread(Thread* thread,
bool is_mutator,
bool bypass_safepoint = false);
void IncreaseMutatorCount(Isolate* mutator, bool is_nested_reenter);
void DecreaseMutatorCount(Isolate* mutator, bool is_nested_exit);
intptr_t MutatorCount() const {
MonitorLocker ml(active_mutators_monitor_.get());
return active_mutators_;
}
bool HasTagHandler() const { return library_tag_handler() != nullptr; }
ObjectPtr CallTagHandler(Dart_LibraryTag tag,
const Object& arg1,
const Object& arg2);
Dart_LibraryTagHandler library_tag_handler() const {
return library_tag_handler_;
}
void set_library_tag_handler(Dart_LibraryTagHandler handler) {
library_tag_handler_ = handler;
}
Dart_DeferredLoadHandler deferred_load_handler() const {
return deferred_load_handler_;
}
void set_deferred_load_handler(Dart_DeferredLoadHandler handler) {
deferred_load_handler_ = handler;
}
// Prepares all threads in an isolate for Garbage Collection.
void ReleaseStoreBuffers();
void EnableIncrementalBarrier(MarkingStack* marking_stack,
MarkingStack* deferred_marking_stack);
void DisableIncrementalBarrier();
MarkingStack* marking_stack() const { return marking_stack_; }
MarkingStack* deferred_marking_stack() const {
return deferred_marking_stack_;
}
// Runs the given [function] on every isolate in the isolate group.
//
// During the duration of this function, no new isolates can be added or
// removed.
//
// If [at_safepoint] is `true`, then the entire isolate group must be in a
// safepoint. There is therefore no reason to guard against other threads
// adding/removing isolates, so no locks will be held.
void ForEachIsolate(std::function<void(Isolate* isolate)> function,
bool at_safepoint = false);
Isolate* FirstIsolate() const;
Isolate* FirstIsolateLocked() const;
// Ensures mutators are stopped during execution of the provided function.
//
// If the current thread is the only mutator in the isolate group,
// [single_current_mutator] will be called. Otherwise [otherwise] will be
// called inside a [SafepointOperationsScope] (or
// [ForceGrowthSafepointOperationScope] if [use_force_growth_in_otherwise]
// is set).
//
// During the duration of this function, no new isolates can be added to the
// isolate group.
void RunWithStoppedMutatorsCallable(
Callable* single_current_mutator,
Callable* otherwise,
bool use_force_growth_in_otherwise = false);
template <typename T, typename S>
void RunWithStoppedMutators(T single_current_mutator,
S otherwise,
bool use_force_growth_in_otherwise = false) {
LambdaCallable<T> single_callable(single_current_mutator);
LambdaCallable<S> otherwise_callable(otherwise);
RunWithStoppedMutatorsCallable(&single_callable, &otherwise_callable,
use_force_growth_in_otherwise);
}
template <typename T>
void RunWithStoppedMutators(T function, bool use_force_growth = false) {
LambdaCallable<T> callable(function);
RunWithStoppedMutatorsCallable(&callable, &callable, use_force_growth);
}
#ifndef PRODUCT
void PrintJSON(JSONStream* stream, bool ref = true);
void PrintToJSONObject(JSONObject* jsobj, bool ref);
// Creates an object with the total heap memory usage statistics for this
// isolate group.
void PrintMemoryUsageJSON(JSONStream* stream);
#endif
#if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
// By default the reload context is deleted. This parameter allows
// the caller to delete is separately if it is still needed.
bool ReloadSources(JSONStream* js,
bool force_reload,
const char* root_script_url = nullptr,
const char* packages_url = nullptr,
bool dont_delete_reload_context = false);
// If provided, the VM takes ownership of kernel_buffer.
bool ReloadKernel(JSONStream* js,
bool force_reload,
const uint8_t* kernel_buffer = nullptr,
intptr_t kernel_buffer_size = 0,
bool dont_delete_reload_context = false);
void set_last_reload_timestamp(int64_t value) {
last_reload_timestamp_ = value;
}
int64_t last_reload_timestamp() const { return last_reload_timestamp_; }
IsolateGroupReloadContext* reload_context() {
return group_reload_context_.get();
}
ProgramReloadContext* program_reload_context() {
return program_reload_context_;
}
void DeleteReloadContext();
bool CanReload();
#else
bool CanReload() { return false; }
#endif // !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
bool IsReloading() const {
#if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
return group_reload_context_ != nullptr;
#else
return false;
#endif
}
Become* become() const { return become_; }
void set_become(Become* become) { become_ = become; }
uint64_t id() const { return id_; }
static void Init();
static void Cleanup();
static void ForEach(std::function<void(IsolateGroup*)> action);
static void RunWithIsolateGroup(uint64_t id,
std::function<void(IsolateGroup*)> action,
std::function<void()> not_found);
// Manage list of existing isolate groups.
static void RegisterIsolateGroup(IsolateGroup* isolate_group);
static void UnregisterIsolateGroup(IsolateGroup* isolate_group);
static bool HasApplicationIsolateGroups();
static bool HasOnlyVMIsolateGroup();
static bool IsSystemIsolateGroup(const IsolateGroup* group);
int64_t UptimeMicros() const;
ApiState* api_state() const { return api_state_.get(); }
// Visit all object pointers. Caller must ensure concurrent sweeper is not
// running, and the visitor must not allocate.
void VisitObjectPointers(ObjectPointerVisitor* visitor,
ValidationPolicy validate_frames);
void VisitSharedPointers(ObjectPointerVisitor* visitor);
void VisitStackPointers(ObjectPointerVisitor* visitor,
ValidationPolicy validate_frames);
void VisitObjectIdRingPointers(ObjectPointerVisitor* visitor);
void VisitWeakPersistentHandles(HandleVisitor* visitor);
bool compaction_in_progress() const {
return CompactionInProgressBit::decode(isolate_group_flags_);
}
void set_compaction_in_progress(bool value) {
isolate_group_flags_ =
CompactionInProgressBit::update(value, isolate_group_flags_);
}
// In precompilation we finalize all regular classes before compiling.
bool all_classes_finalized() const {
return AllClassesFinalizedBit::decode(isolate_group_flags_);
}
void set_all_classes_finalized(bool value) {
isolate_group_flags_ =
AllClassesFinalizedBit::update(value, isolate_group_flags_);
}
bool remapping_cids() const {
return RemappingCidsBit::decode(isolate_group_flags_);
}
void set_remapping_cids(bool value) {
isolate_group_flags_ =
RemappingCidsBit::update(value, isolate_group_flags_);
}
void RememberLiveTemporaries();
void DeferredMarkLiveTemporaries();
ArrayPtr saved_unlinked_calls() const { return saved_unlinked_calls_; }
void set_saved_unlinked_calls(const Array& saved_unlinked_calls);
FieldTable* initial_field_table() const { return initial_field_table_.get(); }
std::shared_ptr<FieldTable> initial_field_table_shareable() {
return initial_field_table_;
}
void set_initial_field_table(std::shared_ptr<FieldTable> field_table) {
initial_field_table_ = field_table;
}
MutatorThreadPool* thread_pool() { return thread_pool_.get(); }
void RegisterClass(const Class& cls);
void RegisterStaticField(const Field& field, const Object& initial_value);
void FreeStaticField(const Field& field);
private:
friend class Dart; // For `object_store_ = ` in Dart::Init
friend class Heap;
friend class StackFrame; // For `[isolates_].First()`.
// For `object_store_shared_untag()`, `class_table_shared_untag()`
friend class Isolate;
#define ISOLATE_GROUP_FLAG_BITS(V) \
V(AllClassesFinalized) \
V(CompactionInProgress) \
V(EnableAsserts) \
V(HasAttemptedReload) \
V(NullSafety) \
V(RemappingCids) \
V(ShouldLoadVmService) \
V(NullSafetySet) \
V(Obfuscate) \
V(UseFieldGuards) \
V(UseOsr) \
V(SnapshotIsDontNeedSafe) \
V(BranchCoverage)
// Isolate group specific flags.
enum FlagBits {
#define DECLARE_BIT(Name) k##Name##Bit,
ISOLATE_GROUP_FLAG_BITS(DECLARE_BIT)
#undef DECLARE_BIT
};
#define DECLARE_BITFIELD(Name) \
class Name##Bit : public BitField<uint32_t, bool, k##Name##Bit, 1> {};
ISOLATE_GROUP_FLAG_BITS(DECLARE_BITFIELD)
#undef DECLARE_BITFIELD
void set_heap(std::unique_ptr<Heap> value);
// Accessed from generated code.
ClassTable* class_table_;
AcqRelAtomic<ClassPtr*> cached_class_table_table_;
std::unique_ptr<ObjectStore> object_store_;
// End accessed from generated code.
ClassTableAllocator class_table_allocator_;
ClassTable* heap_walk_class_table_;
const char** obfuscation_map_ = nullptr;
bool is_vm_isolate_heap_ = false;
void* embedder_data_ = nullptr;
IdleTimeHandler idle_time_handler_;
std::unique_ptr<MutatorThreadPool> thread_pool_;
std::unique_ptr<SafepointRwLock> isolates_lock_;
IntrusiveDList<Isolate> isolates_;
intptr_t isolate_count_ = 0;
bool initial_spawn_successful_ = false;
Dart_LibraryTagHandler library_tag_handler_ = nullptr;
Dart_DeferredLoadHandler deferred_load_handler_ = nullptr;
int64_t start_time_micros_;
bool is_system_isolate_group_;
Random random_;
#if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
int64_t last_reload_timestamp_;
std::shared_ptr<IsolateGroupReloadContext> group_reload_context_;
// Per-isolate-group copy of FLAG_reload_every.
RelaxedAtomic<intptr_t> reload_every_n_stack_overflow_checks_;
ProgramReloadContext* program_reload_context_ = nullptr;
#endif
Become* become_ = nullptr;
#define ISOLATE_METRIC_VARIABLE(type, variable, name, unit) \
type metric_##variable##_;
ISOLATE_GROUP_METRIC_LIST(ISOLATE_METRIC_VARIABLE);
#undef ISOLATE_METRIC_VARIABLE
#if !defined(PRODUCT)
// Timestamps of last operation via service.
int64_t last_allocationprofile_accumulator_reset_timestamp_ = 0;
int64_t last_allocationprofile_gc_timestamp_ = 0;
#endif // !defined(PRODUCT)
MarkingStack* marking_stack_ = nullptr;
MarkingStack* deferred_marking_stack_ = nullptr;
std::shared_ptr<IsolateGroupSource> source_;
std::unique_ptr<ApiState> api_state_;
std::unique_ptr<ThreadRegistry> thread_registry_;
std::unique_ptr<SafepointHandler> safepoint_handler_;
NOT_IN_PRODUCT(
NOT_IN_PRECOMPILED(std::unique_ptr<ReloadHandler> reload_handler_));
static RwLock* isolate_groups_rwlock_;
static IntrusiveDList<IsolateGroup>* isolate_groups_;
static Random* isolate_group_random_;
uint64_t id_ = 0;
std::unique_ptr<StoreBuffer> store_buffer_;
std::unique_ptr<Heap> heap_;
std::unique_ptr<DispatchTable> dispatch_table_;
const uint8_t* dispatch_table_snapshot_ = nullptr;
intptr_t dispatch_table_snapshot_size_ = 0;
ArrayPtr saved_unlinked_calls_;
std::shared_ptr<FieldTable> initial_field_table_;
uint32_t isolate_group_flags_ = 0;
NOT_IN_PRECOMPILED(std::unique_ptr<BackgroundCompiler> background_compiler_);
Mutex symbols_mutex_;
Mutex type_canonicalization_mutex_;
Mutex type_arguments_canonicalization_mutex_;
Mutex subtype_test_cache_mutex_;
Mutex megamorphic_table_mutex_;
Mutex type_feedback_mutex_;
Mutex patchable_call_mutex_;
Mutex constant_canonicalization_mutex_;
Mutex kernel_data_lib_cache_mutex_;
Mutex kernel_data_class_cache_mutex_;
Mutex kernel_constants_mutex_;
#if defined(DART_PRECOMPILED_RUNTIME)
Mutex unlinked_call_map_mutex_;
#endif
#if !defined(DART_PRECOMPILED_RUNTIME)
Mutex initializer_functions_mutex_;
#endif // !defined(DART_PRECOMPILED_RUNTIME)
// Protect access to boxed_field_list_.
Mutex field_list_mutex_;
// List of fields that became boxed and that trigger deoptimization.
GrowableObjectArrayPtr boxed_field_list_;
// Ensures synchronized access to classes functions, fields and other
// program structure elements to accommodate concurrent modification done
// by multiple isolates and background compiler.
std::unique_ptr<SafepointRwLock> program_lock_;
// Allow us to ensure the number of active mutators is limited by a maximum.
std::unique_ptr<Monitor> active_mutators_monitor_;
intptr_t active_mutators_ = 0;
intptr_t waiting_mutators_ = 0;
intptr_t max_active_mutators_ = 0;
NOT_IN_PRODUCT(GroupDebugger* debugger_ = nullptr);
};
// When an isolate sends-and-exits this class represent things that it passed
// to the beneficiary.
class Bequest {
public:
Bequest(PersistentHandle* handle, Dart_Port beneficiary)
: handle_(handle), beneficiary_(beneficiary) {}
~Bequest();
PersistentHandle* handle() { return handle_; }
PersistentHandle* TakeHandle() {
auto handle = handle_;
handle_ = nullptr;
return handle;
}
Dart_Port beneficiary() { return beneficiary_; }
private:
PersistentHandle* handle_;
Dart_Port beneficiary_;
};
class Isolate : public BaseIsolate, public IntrusiveDListEntry<Isolate> {
public:
// Keep both these enums in sync with isolate_patch.dart.
// The different Isolate API message types.
enum LibMsgId {
kPauseMsg = 1,
kResumeMsg = 2,
kPingMsg = 3,
kKillMsg = 4,
kAddExitMsg = 5,
kDelExitMsg = 6,
kAddErrorMsg = 7,
kDelErrorMsg = 8,
kErrorFatalMsg = 9,
// Internal message ids.
kInterruptMsg = 10, // Break in the debugger.
kInternalKillMsg = 11, // Like kill, but does not run exit listeners, etc.
kDrainServiceExtensionsMsg = 12, // Invoke pending service extensions
kCheckForReload = 13, // Participate in other isolate group reload.
};
// The different Isolate API message priorities for ping and kill messages.
enum LibMsgPriority {
kImmediateAction = 0,
kBeforeNextEventAction = 1,
kAsEventAction = 2
};
~Isolate();