Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FlowSched for task scheduling #256

Merged
merged 7 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- uses: actions/checkout@v2
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 a0c7df
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id "me.champeau.jmh" version "0.7.1"
id 'com.modrinth.minotaur' version '2.+' apply false
id 'com.matthewprenger.cursegradle' version '1.4.0' apply false
id 'com.github.johnrengelman.shadow' version '8.1.1' apply false
}

@SuppressWarnings('unused')
Expand Down Expand Up @@ -36,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 @@ -118,6 +121,7 @@ configure (allprojects - project(":tests")) {
implementation "com.electronwill.night-config:toml:${night_config_version}"
implementation "org.threadly:threadly:${threadly_version}"
implementation "net.objecthunter:exp4j:${exp4j_version}"
implementation "com.ishland.flowsched:flowsched"

}
}
Expand Down Expand Up @@ -146,6 +150,7 @@ dependencies {
include implementation("com.electronwill.night-config:core:${night_config_version}")
include implementation("org.threadly:threadly:${threadly_version}")
include implementation("net.objecthunter:exp4j:${exp4j_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 Expand Up @@ -196,8 +201,9 @@ dependencies {
(subprojects - project(":tests")).forEach {
if (it.parent != project(":")) return

api project(path: ":${it.name}", configuration: "namedElements")
include project("${it.name}:")
def projectName = it.name
api project(path: ":${projectName}", configuration: "namedElements")
include project("${projectName}:")
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions c2me-base/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: 'com.github.johnrengelman.shadow'

configurations {
shadowInclude
}

dependencies {
shadowInclude("com.ishland.flowsched:flowsched") {
transitive false
}
}

shadowJar {
archiveClassifier = "all-dev"
configurations = [ project.configurations.shadowInclude ]
}

remapJar {
input = shadowJar.archiveFile
archiveFileName = shadowJar.archiveFileName.get().replaceAll("-dev\\.jar\$", ".jar")
addNestedDependencies = true
dependsOn shadowJar
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,23 @@

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 {

// private static final C2MEForkJoinWorkerThreadFactory factory = new C2MEForkJoinWorkerThreadFactory("c2me", "C2ME worker #%d", Thread.NORM_PRIORITY - 1);
private static final C2MENormalWorkerThreadFactory factory = new C2MENormalWorkerThreadFactory("c2me", "C2ME worker #%d", Thread.NORM_PRIORITY - 1);
public static final int GLOBAL_EXECUTOR_PARALLELISM = (int) ModuleEntryPoint.globalExecutorParallelism;
// public static final ForkJoinPool executor = new ForkJoinPool(
// GLOBAL_EXECUTOR_PARALLELISM,
// factory,
// null,
// true
// );
public static final ExecutorService executor = Executors.newFixedThreadPool(GLOBAL_EXECUTOR_PARALLELISM, factory);
public static final Executor invokingExecutor = r -> {
if (Thread.currentThread().getThreadGroup() == factory.getThreadGroup()) {
r.run();
} else {
executor.execute(r);
}
};
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 ExecutorService asyncScheduler = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ishland.c2me.base.common.scheduler;

import com.ishland.flowsched.executor.Task;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;

import java.util.Objects;

public abstract class AbstractPosAwarePrioritizedTask implements Task {

protected final ReferenceArrayList<Runnable> postExec = new ReferenceArrayList<>(4);
private final long pos;
private int priority = Integer.MAX_VALUE;

public AbstractPosAwarePrioritizedTask(long pos) {
this.pos = pos;
}

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

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

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

public void addPostExec(Runnable runnable) {
synchronized (this.postExec) {
postExec.add(Objects.requireNonNull(runnable));
}
}
}
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.

Loading
Loading