Skip to content
Open
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: 3 additions & 0 deletions libraries/executors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Executors API

The Executors API provides an interface for main thread and background thread executors.
5 changes: 5 additions & 0 deletions libraries/executors/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
setUpLibrary(project)

dependencies {
implementation 'com.google.code.gson:gson:2.8.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
min_mc_version = 1.7.6-pre1
max_mc_version = 1.7.10
minecraft_dependency = >=1.7.6-rc.1 <=1.7.10

minecraft_version = 1.7.10
nests_build = 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.ornithemc.osl.executors.impl.mixin.client;

import java.util.concurrent.Executor;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.google.common.util.concurrent.ListenableFuture;

import net.minecraft.client.Minecraft;

import net.ornithemc.osl.executors.impl.Executors;

@Mixin(Minecraft.class)
public class MinecraftMixin implements Executor {

@Shadow
private ListenableFuture<?> executeTask(Runnable task) { return null; }

@Override
public void execute(Runnable command) {
this.executeTask(command);
}

@Inject(
method = "shutdown",
at = @At(
value = "TAIL"
)
)
private void osl$executors$shutdownBackgroundExecutor(CallbackInfo ci) {
Executors.shutdownBackgroundExecutor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package net.ornithemc.osl.executors.impl.mixin.common;

import java.util.ArrayDeque;
import java.util.Queue;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.server.MinecraftServer;
import net.minecraft.util.profiler.Profiler;

import net.ornithemc.osl.executors.api.MainThreadExecutor;
import net.ornithemc.osl.executors.impl.Executors;

@Mixin(MinecraftServer.class)
public class MinecraftServerMixin implements MainThreadExecutor {

@Shadow @Final
private Profiler profiler;

@Unique
private final Queue<Runnable> pendingTasks = new ArrayDeque<>();

@Unique
private Thread thread;

@Inject(
method = "run",
at = @At(
value = "HEAD"
)
)
private void osl$executors$setThread(CallbackInfo ci) {
this.thread = Thread.currentThread();
}

@Override
public void execute(Runnable command) {
if (this.isOnSameThread()) {
command.run();
} else {
synchronized (this.pendingTasks) {
this.pendingTasks.add(command);
}
}
}

@Override
public boolean isOnSameThread() {
return Thread.currentThread() == this.thread;
}

@Inject(
method = "tickWorlds",
at = @At(
value = "HEAD"
)
)
private void osl$executors$runPendingTasks(CallbackInfo ci) {
this.profiler.push("scheduledExecutables");
this.runPendingTasks();
this.profiler.pop();
}

@Unique
private void runPendingTasks() {
synchronized (this.pendingTasks) {
while (!this.pendingTasks.isEmpty()) {
this.runTask(this.pendingTasks.poll());
}
}
}

@Unique
private void runTask(Runnable task) {
try {
task.run();
} catch (Throwable t) {
Executors.LOGGER.fatal("Error running task", t);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.ornithemc.osl.executors.impl.mixin.server;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.server.MinecraftServer;

import net.ornithemc.osl.executors.impl.Executors;

@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {

@Inject(
method = "shutdown",
at = @At(
value = "TAIL"
)
)
private void osl$executors$shutdownBackgroundExecutor(CallbackInfo ci) {
Executors.shutdownBackgroundExecutor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"schemaVersion": 1,
"id": "osl-executors",
"version": "0.1.0-alpha.1+mc1.7.6-pre1-mc1.7.10",
"environment": "*",
"mixins": [
"osl.executors.mixins.json"
],
"accessWidener": "osl.executors.classtweaker",
"depends": {
"fabricloader": "\u003e\u003d0.17.3",
"minecraft": "\u003e\u003d1.7.6-rc.1 \u003c\u003d1.7.10",
"osl-core": "\u003e\u003d0.7.0"
},
"name": "OSL Executors",
"description": "Interface for main thread and background threads executors.",
"authors": [
"OrnitheMC"
],
"contact": {
"homepage": "https://ornithemc.net/",
"issues": "https://github.com/OrnitheMC/ornithe-standard-libraries/issues",
"sources": "https://github.com/OrnitheMC/ornithe-standard-libraries"
},
"license": "Apache-2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
classTweaker v1 named

transitive-inject-interface net/minecraft/client/Minecraft java/util/concurrent/Executor
transitive-inject-interface net/minecraft/server/MinecraftServer net/ornithemc/osl/executors/api/MainThreadExecutor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"required": true,
"minVersion": "0.8",
"package": "net.ornithemc.osl.executors.impl.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"common.MinecraftServerMixin"
],
"client": [
"client.MinecraftMixin"
],
"server": [
"server.MinecraftServerMixin"
],
"injectors": {
"defaultRequire": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
min_mc_version = 12w18a
max_mc_version = 1.7.5
minecraft_dependency = >=1.3-alpha.12.18.a <=1.7.5

minecraft_version = 1.7.5
nests_build = 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package net.ornithemc.osl.executors.impl.mixin.client;

import java.util.ArrayDeque;
import java.util.Queue;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.Minecraft;

import net.ornithemc.osl.executors.api.MainThreadExecutor;
import net.ornithemc.osl.executors.impl.Executors;

@Mixin(Minecraft.class)
public class MinecraftMixin implements MainThreadExecutor {

@Unique
private final Queue<Runnable> pendingTasks = new ArrayDeque<>();

@Unique
private Thread thread;

@Inject(
method = "init",
at = @At(
value = "HEAD"
)
)
private void osl$executors$setThread(CallbackInfo ci) {
this.thread = Thread.currentThread();
}

@Override
public void execute(Runnable command) {
if (this.isOnSameThread()) {
command.run();
} else {
synchronized (this.pendingTasks) {
this.pendingTasks.add(command);
}
}
}

@Override
public boolean isOnSameThread() {
return Thread.currentThread() == this.thread;
}

@Inject(
method = "runGame",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/TickTimer;advance()V",
shift = Shift.AFTER
)
)
private void osl$executors$runPendingTasks(CallbackInfo ci) {
this.runPendingTasks();
}

@Unique
private void runPendingTasks() {
synchronized (this.pendingTasks) {
while (!this.pendingTasks.isEmpty()) {
this.runTask(this.pendingTasks.poll());
}
}
}

@Unique
private void runTask(Runnable task) {
try {
task.run();
} catch (Throwable t) {
Executors.LOGGER.fatal("Error running task", t);
}
}

@Inject(
method = "shutdown",
at = @At(
value = "TAIL"
)
)
private void osl$executors$shutdownBackgroundExecutor(CallbackInfo ci) {
Executors.shutdownBackgroundExecutor();
}
}
Loading
Loading