-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ver/1.19.4' into feature/fast-chunk-serializer
- Loading branch information
Showing
28 changed files
with
330 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
c2me-base/src/main/java/com/ishland/c2me/base/common/scheduler/NeighborLockingManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.ishland.c2me.base.common.scheduler; | ||
|
||
import com.ishland.c2me.base.common.structs.SimpleObjectPool; | ||
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.ReferenceArraySet; | ||
import net.minecraft.util.math.ChunkPos; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
/** | ||
* Not thread safe. | ||
*/ | ||
public class NeighborLockingManager { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger("NeighborLockingManager"); | ||
|
||
private final SimpleObjectPool<ReferenceArraySet<Runnable>> pool = new SimpleObjectPool<>( | ||
pool -> new ReferenceArraySet<>(16), | ||
ReferenceArraySet::clear, | ||
1024 | ||
); | ||
private final Long2ReferenceOpenHashMap<ReferenceArraySet<Runnable>> activeLocks = new Long2ReferenceOpenHashMap<>(); | ||
|
||
public boolean isLocked(long pos) { | ||
return activeLocks.containsKey(pos); | ||
} | ||
|
||
public void acquireLock(long pos) { | ||
if (isLocked(pos)) throw new IllegalStateException("Already locked"); | ||
activeLocks.put(pos, pool.alloc()); | ||
} | ||
|
||
public void releaseLock(long pos) { | ||
if (!isLocked(pos)) throw new IllegalStateException("Not locked"); | ||
final ReferenceArraySet<Runnable> runnables = activeLocks.remove(pos); | ||
for (Runnable runnable : runnables) { | ||
try { | ||
runnable.run(); | ||
} catch (Throwable t) { | ||
LOGGER.error("Failed to notify lock release at chunk %s".formatted(new ChunkPos(pos)), t); | ||
} | ||
} | ||
runnables.clear(); | ||
pool.release(runnables); | ||
} | ||
|
||
public void addReleaseListener(long pos, Runnable runnable) { | ||
if (!isLocked(pos)) throw new IllegalStateException("Not locked"); | ||
activeLocks.get(pos).add(runnable); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
c2me-base/src/main/java/com/ishland/c2me/base/common/scheduler/NeighborLockingTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.ishland.c2me.base.common.scheduler; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.ishland.c2me.base.common.GlobalExecutors; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.BooleanSupplier; | ||
import java.util.function.Supplier; | ||
|
||
public class NeighborLockingTask<T> implements ScheduledTask { | ||
|
||
private final SchedulingManager schedulingManager; | ||
private final long center; | ||
private final long[] names; | ||
private final BooleanSupplier isCancelled; | ||
private final Supplier<CompletableFuture<T>> action; | ||
private final String desc; | ||
private final boolean async; | ||
private final CompletableFuture<T> future = new CompletableFuture<>(); | ||
private boolean acquired = false; | ||
|
||
public NeighborLockingTask(SchedulingManager schedulingManager, long center, long[] names, BooleanSupplier isCancelled, Supplier<CompletableFuture<T>> action, String desc, boolean async) { | ||
this.schedulingManager = schedulingManager; | ||
this.center = center; | ||
this.names = names; | ||
this.isCancelled = isCancelled; | ||
this.action = action; | ||
this.desc = desc; | ||
this.async = async; | ||
|
||
this.schedulingManager.enqueue(this); | ||
} | ||
|
||
|
||
@Override | ||
public boolean tryPrepare() { | ||
final NeighborLockingManager lockingManager = this.schedulingManager.getNeighborLockingManager(); | ||
for (long l : names) { | ||
if (lockingManager.isLocked(l)) { | ||
lockingManager.addReleaseListener(l, () -> this.schedulingManager.enqueue(this)); | ||
return false; | ||
} | ||
} | ||
for (long l : names) { | ||
lockingManager.acquireLock(l); | ||
} | ||
acquired = true; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void runTask(Runnable postAction) { | ||
Preconditions.checkNotNull(postAction); | ||
if (!acquired) throw new IllegalStateException(); | ||
final CompletableFuture<T> future = this.action.get(); | ||
Preconditions.checkNotNull(future, "future"); | ||
future.handleAsync((result, throwable) -> { | ||
this.schedulingManager.getExecutor().execute(() -> { | ||
final NeighborLockingManager lockingManager = this.schedulingManager.getNeighborLockingManager(); | ||
for (long l : names) { | ||
lockingManager.releaseLock(l); | ||
} | ||
}); | ||
try { | ||
postAction.run(); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
if (throwable != null) this.future.completeExceptionally(throwable); | ||
else this.future.complete(result); | ||
return null; | ||
}, GlobalExecutors.invokingExecutor); | ||
} | ||
|
||
@Override | ||
public long centerPos() { | ||
return center; | ||
} | ||
|
||
@Override | ||
public boolean isAsync() { | ||
return async; | ||
} | ||
|
||
public CompletableFuture<T> getFuture() { | ||
return future; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../opts/allocs/common/SimpleObjectPool.java → ...base/common/structs/SimpleObjectPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
c2me-opts-allocs/src/main/java/com/ishland/c2me/opts/allocs/common/PooledFeatureContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ava/com/ishland/c2me/opts/allocs/mixin/object_pooling_caching/MixinConfiguredFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
...main/java/com/ishland/c2me/opts/scheduling/mixin/task_scheduling/MixinThreadExecutor.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.