Skip to content

Commit

Permalink
Address concurrent access to randomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
OreCruncher committed Feb 14, 2024
1 parent 7085d3d commit f73673e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
66 changes: 58 additions & 8 deletions src/main/java/org/orecruncher/dsurround/lib/random/Randomizer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.orecruncher.dsurround.lib.random;

import net.minecraft.util.RandomSource;
import net.minecraft.world.level.levelgen.PositionalRandomFactory;
import org.jetbrains.annotations.NotNull;
import org.orecruncher.dsurround.lib.Library;

import java.util.function.Supplier;
Expand All @@ -11,26 +14,73 @@
* the number of randoms generated per tick, I sweat these details.
*/
@SuppressWarnings("unused")
public class Randomizer {
public final class Randomizer implements IRandomizer {
private static final Supplier<IRandomizer> DEFAULT_RANDOMIZER = Randomizer::getRandomizer;
private static final ThreadLocal<IRandomizer> SHARED = ThreadLocal.withInitial(DEFAULT_RANDOMIZER);

private static final ThreadLocal<IRandomizer> THREAD_LOCAL = ThreadLocal.withInitial(DEFAULT_RANDOMIZER);
/**
* Instantiates a new instance of the default randomizer.
* Reusable instance that wraps a ThreadLocal. Guards against multiple threads trying to utilize the same
* concrete randomizer.
*/
public static IRandomizer create() {
return DEFAULT_RANDOMIZER.get();
}
private static final IRandomizer SHARED = new Randomizer();

/**
* Returns a shared instance of the default randomizer.
*/
public static IRandomizer current() {
return SHARED.get();
return SHARED;
}

private Randomizer() {
}

@Override
public @NotNull RandomSource fork() {
return THREAD_LOCAL.get().fork();
}

@Override
public @NotNull PositionalRandomFactory forkPositional() {
return THREAD_LOCAL.get().forkPositional();
}

@Override
public void setSeed(long l) {
THREAD_LOCAL.get().setSeed(l);
}

@Override
public int nextInt() {
return THREAD_LOCAL.get().nextInt();
}

@Override
public int nextInt(int i) {
return THREAD_LOCAL.get().nextInt(i);
}

@Override
public long nextLong() {
return THREAD_LOCAL.get().nextLong();
}

@Override
public boolean nextBoolean() {
return THREAD_LOCAL.get().nextBoolean();
}

@Override
public float nextFloat() {
return THREAD_LOCAL.get().nextFloat();
}

@Override
public double nextDouble() {
return THREAD_LOCAL.get().nextDouble();
}

@Override
public double nextGaussian() {
return THREAD_LOCAL.get().nextGaussian();
}

private static IRandomizer getRandomizer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.openal.EXTEfx;
import org.orecruncher.dsurround.lib.random.IRandomizer;
import org.orecruncher.dsurround.lib.random.Randomizer;
import org.orecruncher.dsurround.runtime.audio.effects.Effects;
import org.orecruncher.dsurround.runtime.audio.effects.LowPassData;
Expand All @@ -16,9 +15,6 @@

public final class SourceContext implements Callable<Void> {

// Randomizer used to distribute updates across an interval
private static final IRandomizer RANDOM = Randomizer.create();

// Frequency of sound effect updates in thread schedule ticks. Works out to be 3 times a second.
private static final int UPDATE_FEQUENCY_TICKS = 7;

Expand Down Expand Up @@ -156,7 +152,7 @@ public Void call() {
public void exec() {
this.captureState();
this.updateImpl();
this.updateCount = RANDOM.nextInt(UPDATE_FEQUENCY_TICKS);
this.updateCount = Randomizer.current().nextInt(UPDATE_FEQUENCY_TICKS);
this.tick();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import org.jetbrains.annotations.NotNull;
import org.orecruncher.dsurround.lib.random.Randomizer;

public class BackgroundSoundLoop extends AbstractTickableSoundInstance {

Expand All @@ -19,7 +19,7 @@ public class BackgroundSoundLoop extends AbstractTickableSoundInstance {
private boolean isFading;

public BackgroundSoundLoop(SoundEvent soundEvent) {
super(soundEvent, SoundSource.AMBIENT, RandomSource.create());
super(soundEvent, SoundSource.AMBIENT, Randomizer.current());
this.scale = INITIAL_SCALE;
this.target = 1F;
this.isFading = false;
Expand All @@ -30,7 +30,7 @@ public BackgroundSoundLoop(SoundEvent soundEvent) {
}

public BackgroundSoundLoop(SoundEvent soundEvent, BlockPos pos) {
super(soundEvent, SoundSource.AMBIENT, RandomSource.create());
super(soundEvent, SoundSource.AMBIENT, Randomizer.current());
this.scale = INITIAL_SCALE;
this.target = 1F;
this.isFading = false;
Expand Down

0 comments on commit f73673e

Please sign in to comment.