with Java 17 min - we can call Thread.onSpinWait directly (#2021)

This commit is contained in:
PJ Fanning 2025-08-08 08:57:52 +01:00 committed by GitHub
parent c11434abb4
commit 5bfbfc5c89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 1 additions and 46 deletions

View file

@ -13,7 +13,6 @@ ignored-files = [
"FlightAppModels.java",
//pekko-actor
"OnSpinWait.java",
"AbstractBoundedNodeQueue.java",
"AbstractMessageDispatcher.java",
"AbstractNodeQueue.java",

View file

@ -1,44 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* This file is part of the Apache Pekko project, which was derived from Akka.
*/
/*
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
*/
package org.apache.pekko.dispatch.affinity;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import org.apache.pekko.annotation.InternalApi;
import static java.lang.invoke.MethodType.methodType;
/**
* INTERNAL API
*/
@InternalApi
final class OnSpinWait {
private final static MethodHandle handle;
public final static void spinWait() throws Throwable {
handle.invoke(); // Will be inlined as an invokeExact since the callsite matches the MH definition of () -> void
}
static {
final MethodHandle noop = MethodHandles.constant(Object.class, null).asType(methodType(Void.TYPE));
MethodHandle impl;
try {
impl = MethodHandles.lookup().findStatic(Thread.class, "onSpinWait", methodType(Void.TYPE));
} catch (NoSuchMethodException nsme) {
impl = noop;
} catch (IllegalAccessException iae) {
impl = noop;
}
handle = impl;
};
}

View file

@ -85,7 +85,7 @@ private[affinity] object AffinityPool {
idling = true
transitionTo(Spinning)
case Spinning =>
OnSpinWait.spinWait()
Thread.onSpinWait()
turns += 1
if (turns > maxSpins)
transitionTo(Yielding)