Skip to content

Commit 8149625

Browse files
authored
don't use unsafe to update nextName (#1892)
* don't use unsafe to update nextName * Update Children.scala * Update Children.scala * use varhandle * Update AbstractActorCell.java * review comment * Update Children.scala
1 parent 5aad47e commit 8149625

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

actor/src/main/java/org/apache/pekko/actor/dungeon/AbstractActorCell.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313

1414
package org.apache.pekko.actor.dungeon;
1515

16+
import java.lang.invoke.MethodHandles;
17+
import java.lang.invoke.VarHandle;
18+
1619
import org.apache.pekko.actor.ActorCell;
1720
import org.apache.pekko.util.Unsafe;
1821

1922
final class AbstractActorCell {
2023
static final long mailboxOffset;
2124
static final long childrenOffset;
22-
static final long nextNameOffset;
25+
static final VarHandle nextNameHandle;
2326
static final long functionRefsOffset;
2427

2528
static {
@@ -32,14 +35,17 @@ final class AbstractActorCell {
3235
Unsafe.instance.objectFieldOffset(
3336
ActorCell.class.getDeclaredField(
3437
"org$apache$pekko$actor$dungeon$Children$$_childrenRefsDoNotCallMeDirectly"));
35-
nextNameOffset =
36-
Unsafe.instance.objectFieldOffset(
37-
ActorCell.class.getDeclaredField(
38-
"org$apache$pekko$actor$dungeon$Children$$_nextNameDoNotCallMeDirectly"));
3938
functionRefsOffset =
4039
Unsafe.instance.objectFieldOffset(
4140
ActorCell.class.getDeclaredField(
4241
"org$apache$pekko$actor$dungeon$Children$$_functionRefsDoNotCallMeDirectly"));
42+
MethodHandles.Lookup lookup =
43+
MethodHandles.privateLookupIn(ActorCell.class, MethodHandles.lookup());
44+
nextNameHandle =
45+
lookup.findVarHandle(
46+
ActorCell.class,
47+
"org$apache$pekko$actor$dungeon$Children$$_nextNameDoNotCallMeDirectly",
48+
long.class);
4349
} catch (Throwable t) {
4450
throw new ExceptionInInitializerError(t);
4551
}

actor/src/main/scala/org/apache/pekko/actor/dungeon/Children.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ private[pekko] trait Children { this: ActorCell =>
122122

123123
@nowarn @volatile private var _nextNameDoNotCallMeDirectly = 0L
124124
final protected def randomName(sb: java.lang.StringBuilder): String = {
125-
val num = Unsafe.instance.getAndAddLong(this, AbstractActorCell.nextNameOffset, 1): @nowarn("cat=deprecation")
125+
val num: Long = AbstractActorCell.nextNameHandle.getAndAdd(this, 1L)
126126
Helpers.base64(num, sb)
127127
}
128128
final protected def randomName(): String = {
129-
val num = Unsafe.instance.getAndAddLong(this, AbstractActorCell.nextNameOffset, 1): @nowarn("cat=deprecation")
129+
val num: Long = AbstractActorCell.nextNameHandle.getAndAdd(this, 1L)
130130
Helpers.base64(num)
131131
}
132132

0 commit comments

Comments
 (0)