mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-06 21:00:35 -04:00
added task handles to runtime
This commit is contained in:
parent
0d23093d3b
commit
0dd6fb237d
2 changed files with 100 additions and 54 deletions
|
|
@ -8,6 +8,7 @@ import generators.loadtime.future.Cancellation;
|
|||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class Main implements Runnable {
|
||||
|
|
@ -17,40 +18,50 @@ public class Main implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
int value = 0;
|
||||
String meow = "";
|
||||
Function<Integer, String> test = new Function<Integer, String>() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public String apply(Integer i) {
|
||||
return Main.this + meow + value + i;
|
||||
}
|
||||
};
|
||||
System.out.println(test.apply(12));
|
||||
// lexer();
|
||||
// await();
|
||||
try {
|
||||
new Jokio().blocking(files());
|
||||
} catch (IOException ignore) {}
|
||||
}
|
||||
|
||||
static Future<Void, Throwable> files() throws IOException{
|
||||
try(@Cancellation("close") var file = File.open(Path.of("./src/Main.java"))){
|
||||
var buf = ByteBuffer.allocate((int) file.size());
|
||||
var read = file.read_all(buf).await();
|
||||
System.out.println(new String(buf.array(), 0, read));
|
||||
}
|
||||
return Future.ret();
|
||||
}
|
||||
|
||||
void async_lambda(Supplier<Future<?, ?>> lambda){
|
||||
new Jokio().blocking(lambda.get());
|
||||
}
|
||||
|
||||
|
||||
void await(){
|
||||
try{
|
||||
new Jokio().blocking(AsyncExamples.run());
|
||||
}catch (Exception e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lexer(){
|
||||
var gen = Lexer.parse("f7(x,y,z,w, u,v, othersIg) = v-(x*y+y+ln(z)^2*sin(z*pi/2))/(w*u)+sqrt(othersIg*120e-1)");
|
||||
while(gen.next() instanceof Gen.Yield(var tok)) {
|
||||
System.out.println(tok);
|
||||
}
|
||||
//// await();
|
||||
// try {
|
||||
// System.out.println(new Jokio().blocking(files()));
|
||||
// } catch (IOException ignore) {}
|
||||
}
|
||||
//
|
||||
// static Future<String, IOException> files() throws IOException{
|
||||
// try(@Cancellation("close") var file = File.open(Path.of("./src/Main.java"))){
|
||||
// var buf = ByteBuffer.allocate((int) file.size());
|
||||
// var read = file.read_all(buf).await();
|
||||
// return Future.ret(new String(buf.array(), 0, read));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// <T, E extends Throwable> T async_lambda(Supplier<Future<T, E>> lambda) throws E{
|
||||
// return new Jokio().blocking(lambda.get());
|
||||
// }
|
||||
//
|
||||
//
|
||||
// void await(){
|
||||
// try{
|
||||
// new Jokio().blocking(AsyncExamples.run());
|
||||
// }catch (Exception e){
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// void lexer(){
|
||||
// var gen = Lexer.parse("f7(x,y,z,w, u,v, othersIg) = v-(x*y+y+ln(z)^2*sin(z*pi/2))/(w*u)+sqrt(othersIg*120e-1)");
|
||||
// while(gen.next() instanceof Gen.Yield(var tok)) {
|
||||
// System.out.println(tok);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
@ -7,10 +7,12 @@ import java.util.ArrayDeque;
|
|||
import java.util.HashSet;
|
||||
|
||||
public class Jokio implements Runnable{
|
||||
private class Task<T, E extends Throwable> implements Waker{
|
||||
public final Future<T, E> future;
|
||||
public class TaskHandle<T, E extends Throwable> implements Waker, Future<T, E>{
|
||||
private final Future<T, E> future;
|
||||
private Object result = Pending.INSTANCE;
|
||||
private Throwable err;
|
||||
|
||||
private Task(Future<T, E> future) {
|
||||
private TaskHandle(Future<T, E> future) {
|
||||
this.future = future;
|
||||
}
|
||||
|
||||
|
|
@ -26,37 +28,60 @@ public class Jokio implements Runnable{
|
|||
public Jokio runtime(){
|
||||
return Jokio.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object poll(Waker waker) throws E {
|
||||
if(err!=null)throw (E)err;
|
||||
return result;
|
||||
}
|
||||
|
||||
public T blocking() throws E{
|
||||
while(result == Pending.INSTANCE) {
|
||||
try {
|
||||
this.wait();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if(err!=null)throw (E)err;
|
||||
}
|
||||
return (T) result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void cancel() throws E {
|
||||
synchronized (Jokio.this){
|
||||
currentSet.remove(this);
|
||||
}
|
||||
future.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public static Future<Jokio, RuntimeException> runtime(){
|
||||
return new Future<>() {
|
||||
@Override
|
||||
public Jokio poll(Waker waker) {
|
||||
return ((Task<?, ?>)waker).runtime();
|
||||
}
|
||||
};
|
||||
return Jokio::runtime;
|
||||
}
|
||||
|
||||
public static Jokio runtime(Waker waker){
|
||||
return ((Task<?, ?>)waker).runtime();
|
||||
return ((TaskHandle<?, ?>)waker).runtime();
|
||||
}
|
||||
|
||||
private final ArrayDeque<Task<?, ?>> wokeQueue = new ArrayDeque<>();
|
||||
private final HashSet<Task<?, ?>> wokeSet = new HashSet<>();
|
||||
private final HashSet<Task<?, ?>> currentSet = new HashSet<>();
|
||||
private final ArrayDeque<TaskHandle<?, ?>> wokeQueue = new ArrayDeque<>();
|
||||
private final HashSet<TaskHandle<?, ?>> wokeSet = new HashSet<>();
|
||||
private final HashSet<TaskHandle<?, ?>> currentSet = new HashSet<>();
|
||||
|
||||
public void blocking(Future<?, ?> fut){
|
||||
spawn(fut).run();
|
||||
public <T, E extends Throwable> T blocking(Future<T, E> fut) throws E {
|
||||
var result = spawn(fut);
|
||||
run();
|
||||
return result.blocking();
|
||||
}
|
||||
|
||||
public Jokio spawn(Future<?, ?> future){
|
||||
var task = new Task<>(future);
|
||||
public <T, E extends Throwable> TaskHandle<T, E> spawn(Future<T, E> future){
|
||||
var task = new TaskHandle<>(future);
|
||||
synchronized (this){
|
||||
currentSet.add(task);
|
||||
wokeQueue.add(task);
|
||||
wokeSet.add(task);
|
||||
}
|
||||
return this;
|
||||
return task;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -72,7 +97,7 @@ public class Jokio implements Runnable{
|
|||
}
|
||||
}
|
||||
}
|
||||
Task<?, ?> task;
|
||||
TaskHandle<?, ?> task;
|
||||
synchronized (this){
|
||||
task = wokeQueue.poll();
|
||||
wokeSet.remove(task);
|
||||
|
|
@ -80,8 +105,14 @@ public class Jokio implements Runnable{
|
|||
}
|
||||
Object result;
|
||||
try{
|
||||
result = task.future.poll(task);
|
||||
synchronized (task){
|
||||
result = task.future.poll(task);
|
||||
}
|
||||
}catch (Throwable t){
|
||||
synchronized (task){
|
||||
task.err = t;
|
||||
task.notify();
|
||||
}
|
||||
System.out.println("Future " + task.future + " Threw Exception");
|
||||
t.printStackTrace();
|
||||
synchronized (this){
|
||||
|
|
@ -91,6 +122,10 @@ public class Jokio implements Runnable{
|
|||
}
|
||||
synchronized (this){
|
||||
if(result!=Future.Pending.INSTANCE) {
|
||||
synchronized (task){
|
||||
task.result = result;
|
||||
task.notify();
|
||||
}
|
||||
currentSet.remove(task);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue