package com.bellsw.yield; import static java.util.concurrent.TimeUnit.*; import static org.openjdk.jmh.annotations.Mode.*; import static org.openjdk.jmh.annotations.Scope.*; import static org.openjdk.jmh.annotations.Level.*; import org.openjdk.jmh.infra.Control; import org.openjdk.jmh.annotations.*; @BenchmarkMode(Throughput) @Warmup(iterations = 5, time = 1, timeUnit = SECONDS) @Measurement(iterations = 5, time = 1, timeUnit = MILLISECONDS, batchSize = 1000) @OutputTimeUnit(MILLISECONDS) @Fork(5) @State(Group) public class SpinWaitNoAuxBench { public static volatile long spinData; // even: ready to produce; odd: ready to consume; -3: terminate @Benchmark @Group("pong") @GroupThreads(1) public void produce(Control cnt) { int spin = 0; while (((spinData & 0x1) == 1) && ((spin & 0b1000000) == 0)) { // <64 spins // busy spin until ready to produce java.lang.Thread.onSpinWait(); spin++; } spinData++; // produce } @Benchmark @Group("pong") @GroupThreads(1) public void consume(Control cnt) { int spin = 0; while (((spinData & 0x1) == 0) && ((spin & 0b1000000) == 0)) { //<64 spins // busy spin until ready to consume java.lang.Thread.onSpinWait(); spin++; } spinData++; // consume } }