[sdk/vm] Use FinalThreadLocal to store _Random singleton object.
This allows use of Random in isolategroup-bound callbacks. BUG=https://github.com/dart-lang/sdk/issues/61541 TEST=run_isolate_group_run_test Change-Id: Id847e900be05f2866386c025b1796b3f34c1e8df Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/461940 Reviewed-by: Slava Egorov <vegorov@google.com> Commit-Queue: Alexander Aprelev <aam@google.com>
This commit is contained in:
committed by
Commit Queue
parent
26ede3886b
commit
c7fc60c78b
@@ -10,6 +10,7 @@
|
||||
import "dart:_internal" show patch;
|
||||
|
||||
import "dart:typed_data" show Uint32List;
|
||||
import "dart:_vm" show FinalThreadLocal;
|
||||
|
||||
/// There are no parts of this patch library.
|
||||
|
||||
@@ -245,7 +246,10 @@ class _Random implements Random {
|
||||
static const _POW2_27_D = 1.0 * (1 << 27);
|
||||
|
||||
// Use a singleton Random object to get a new seed if no seed was passed.
|
||||
static final _prng = _Random._withState(_initialSeed());
|
||||
@pragma('vm:shared')
|
||||
static final _prng = FinalThreadLocal<_Random>(
|
||||
() => _Random._withState(_initialSeed()),
|
||||
);
|
||||
|
||||
// Thomas Wang 64-bit mix.
|
||||
// http://www.concentric.net/~Ttwang/tech/inthash.htm
|
||||
@@ -270,8 +274,8 @@ class _Random implements Random {
|
||||
|
||||
static int _nextSeed() {
|
||||
// Trigger the PRNG once to change the internal state.
|
||||
_prng._nextState();
|
||||
return _prng._state & 0xFFFFFFFF;
|
||||
_prng.value._nextState();
|
||||
return _prng.value._state & 0xFFFFFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import 'package:dart_internal/isolate_group.dart' show IsolateGroup;
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
import 'dart:isolate';
|
||||
import 'dart:math';
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
@@ -267,5 +268,9 @@ main(List<String> args) {
|
||||
}),
|
||||
);
|
||||
|
||||
IsolateGroup.runSync(() {
|
||||
Random().nextInt(10);
|
||||
});
|
||||
|
||||
print("All tests completed :)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user