Skip to content

Commit

Permalink
new: use FlowSched for worldgen scheduling
Browse files Browse the repository at this point in the history
Doesn't build artifacts but devlaunch works
  • Loading branch information
ishland committed Oct 29, 2023
1 parent bfefd17 commit a35fba4
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 469 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true

- name: Set up JDK 17
uses: actions/setup-java@v2
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "FlowSched"]
path = FlowSched
url = https://github.com/RelativityMC/FlowSched.git
1 change: 1 addition & 0 deletions FlowSched
Submodule FlowSched added at 00a98c
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ configure(allprojects) {

archivesBaseName = "${project.archives_base_name}-mc${project.minecraft_version}"

clean.dependsOn gradle.includedBuild('FlowSched').task(':clean')

configure (allprojects - project(":tests")) {
if (project != project(":") && project.parent != project(":")) return

Expand Down Expand Up @@ -122,6 +124,7 @@ configure (allprojects - project(":tests")) {
implementation "org.threadly:threadly:${threadly_version}"
implementation "net.objecthunter:exp4j:${exp4j_version}"
implementation "com.github.LlamaLad7:MixinExtras:${mixinextras_version}"
implementation "com.ishland.flowsched:flowsched"

annotationProcessor "com.github.LlamaLad7:MixinExtras:${mixinextras_version}"
}
Expand Down Expand Up @@ -152,6 +155,7 @@ dependencies {
include implementation("org.threadly:threadly:${threadly_version}")
include implementation("net.objecthunter:exp4j:${exp4j_version}")
include implementation("com.github.LlamaLad7:MixinExtras:${mixinextras_version}")
include implementation("com.ishland.flowsched:flowsched")

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.ishland.c2me.base.ModuleEntryPoint;
import com.ishland.c2me.base.common.util.C2MENormalWorkerThreadFactory;
import com.ishland.flowsched.executor.ExecutorManager;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class GlobalExecutors {

Expand All @@ -23,6 +25,11 @@ public class GlobalExecutors {
// true
// );
public static final ExecutorService executor = Executors.newFixedThreadPool(GLOBAL_EXECUTOR_PARALLELISM, factory);
private static final AtomicInteger prioritizedSchedulerCounter = new AtomicInteger(0);
public static final ExecutorManager prioritizedScheduler = new ExecutorManager(GlobalExecutors.GLOBAL_EXECUTOR_PARALLELISM, thread -> {
thread.setDaemon(true);
thread.setName("c2me-prioritized-%d".formatted(prioritizedSchedulerCounter.getAndIncrement()));
});
public static final Executor invokingExecutor = r -> {
if (Thread.currentThread().getThreadGroup() == factory.getThreadGroup()) {
r.run();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ishland.c2me.base.common.scheduler;

import com.ishland.flowsched.executor.LockToken;

public record LockTokenImpl(int ownerTag, long pos) implements LockToken {
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
package com.ishland.c2me.base.common.scheduler;

public interface ScheduledTask {
import com.ishland.flowsched.executor.LockToken;
import com.ishland.flowsched.executor.Task;

public boolean tryPrepare();
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public void runTask(Runnable postAction);
public class ScheduledTask<T> implements Task {

public long centerPos();
private final long pos;
private final Supplier<CompletableFuture<T>> action;
private final LockToken[] lockTokens;
private final CompletableFuture<T> future = new CompletableFuture<>();
private int priority = Integer.MAX_VALUE;

public boolean isAsync();
public ScheduledTask(long pos, Supplier<CompletableFuture<T>> action, LockToken[] lockTokens) {
this.pos = pos;
this.action = action;
this.lockTokens = lockTokens;
}

@Override
public void run() {
action.get().whenComplete((t, throwable) -> {
if (throwable != null) {
future.completeExceptionally(throwable);
} else {
future.complete(t);
}
});
}

@Override
public void propagateException(Throwable t) {
future.completeExceptionally(t);
}

@Override
public LockToken[] lockTokens() {
return this.lockTokens;
}

@Override
public int priority() {
return this.priority;
}

public void setPriority(int priority) {
this.priority = priority;
}

public long getPos() {
return this.pos;
}

public CompletableFuture<T> getFuture() {
return this.future;
}
}
Loading

0 comments on commit a35fba4

Please sign in to comment.