Skip to content

Commit

Permalink
Check exchanger algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Jan 9, 2024
1 parent b162712 commit 2729f9a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core/src/main/java/com/softwaremill/jox/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ final class Continuation {
* The number of busy-looping iterations before yielding, during {@link Continuation#await(Segment, int)}.
* {@code 0}, if there's a single CPU.
*/
static final int SPINS = Runtime.getRuntime().availableProcessors() == 1 ? 0 : 1000;
static final int SPINS = Runtime.getRuntime().availableProcessors() == 1 ? 0 : (1 << 10);

private final Thread creatingThread;
private volatile Object data; // set using DATA var handle
Expand Down Expand Up @@ -1118,10 +1118,20 @@ boolean tryResume(Object value) {
*/
Object await(Segment segment, int cellIndex) throws InterruptedException {
var spinIterations = SPINS;
var h = 0; // TODO
while (data == null) {
if (spinIterations > 0) {
Thread.onSpinWait();
spinIterations -= 1;
h ^= h << 1;
h ^= h >>> 3;
h ^= h << 10; // xorshift
if (h == 0) { // initialize hash
h = SPINS | (int) creatingThread.threadId();
} else if (h < 0 && // approx 50% true
(--spinIterations & ((SPINS >>> 1) - 1)) == 0) {
Thread.yield(); // two yields per wait
} else {
Thread.onSpinWait();
}
} else {
LockSupport.park();

Expand Down

0 comments on commit 2729f9a

Please sign in to comment.