Skip to content

Commit c7fc60c

Browse files
aamCommit Queue
authored andcommitted
[sdk/vm] Use FinalThreadLocal to store _Random singleton object.
This allows use of Random in isolategroup-bound callbacks. BUG=#61541 TEST=run_isolate_group_run_test Change-Id: Id847e900be05f2866386c025b1796b3f34c1e8df Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/461940 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Aprelev <[email protected]>
1 parent 26ede38 commit c7fc60c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

sdk/lib/_internal/vm/lib/math_patch.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import "dart:_internal" show patch;
1111

1212
import "dart:typed_data" show Uint32List;
13+
import "dart:_vm" show FinalThreadLocal;
1314

1415
/// There are no parts of this patch library.
1516
@@ -245,7 +246,10 @@ class _Random implements Random {
245246
static const _POW2_27_D = 1.0 * (1 << 27);
246247

247248
// Use a singleton Random object to get a new seed if no seed was passed.
248-
static final _prng = _Random._withState(_initialSeed());
249+
@pragma('vm:shared')
250+
static final _prng = FinalThreadLocal<_Random>(
251+
() => _Random._withState(_initialSeed()),
252+
);
249253

250254
// Thomas Wang 64-bit mix.
251255
// http://www.concentric.net/~Ttwang/tech/inthash.htm
@@ -270,8 +274,8 @@ class _Random implements Random {
270274

271275
static int _nextSeed() {
272276
// Trigger the PRNG once to change the internal state.
273-
_prng._nextState();
274-
return _prng._state & 0xFFFFFFFF;
277+
_prng.value._nextState();
278+
return _prng.value._state & 0xFFFFFFFF;
275279
}
276280
}
277281

tests/ffi/run_isolate_group_run_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:dart_internal/isolate_group.dart' show IsolateGroup;
1616
import 'dart:convert';
1717
import 'dart:developer';
1818
import 'dart:isolate';
19+
import 'dart:math';
1920

2021
import "package:expect/expect.dart";
2122

@@ -267,5 +268,9 @@ main(List<String> args) {
267268
}),
268269
);
269270

271+
IsolateGroup.runSync(() {
272+
Random().nextInt(10);
273+
});
274+
270275
print("All tests completed :)");
271276
}

0 commit comments

Comments
 (0)