diff --git a/src/Main.java b/src/Main.java index 11dc7e6..7840f33 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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 test = new Function() { + 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 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> 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 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 async_lambda(Supplier> 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); +// } +// } } \ No newline at end of file diff --git a/src/async_runtime/Jokio.java b/src/async_runtime/Jokio.java index 06aeca6..b169f13 100644 --- a/src/async_runtime/Jokio.java +++ b/src/async_runtime/Jokio.java @@ -7,10 +7,12 @@ import java.util.ArrayDeque; import java.util.HashSet; public class Jokio implements Runnable{ - private class Task implements Waker{ - public final Future future; + public class TaskHandle implements Waker, Future{ + private final Future future; + private Object result = Pending.INSTANCE; + private Throwable err; - private Task(Future future) { + private TaskHandle(Future 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 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> wokeQueue = new ArrayDeque<>(); - private final HashSet> wokeSet = new HashSet<>(); - private final HashSet> currentSet = new HashSet<>(); + private final ArrayDeque> wokeQueue = new ArrayDeque<>(); + private final HashSet> wokeSet = new HashSet<>(); + private final HashSet> currentSet = new HashSet<>(); - public void blocking(Future fut){ - spawn(fut).run(); + public T blocking(Future fut) throws E { + var result = spawn(fut); + run(); + return result.blocking(); } - public Jokio spawn(Future future){ - var task = new Task<>(future); + public TaskHandle spawn(Future 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); }