From 8593eada03e352fca3e9335c009b83b28986e397 Mon Sep 17 00:00:00 2001 From: Parker TenBroeck <51721964+ParkerTenBroeck@users.noreply.github.com> Date: Thu, 1 May 2025 09:55:56 -0400 Subject: [PATCH] fixed bug in saving to local for await, fixed async runtime (again) --- src/Examples.java | 24 ++++++++++----- src/async_example/Jokio.java | 29 +++++++++---------- src/async_example/net/ServerSocket.java | 6 ---- .../runtime/future/FutureSMBuilder.java | 2 +- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/Examples.java b/src/Examples.java index 77d0ed3..cef80f5 100644 --- a/src/Examples.java +++ b/src/Examples.java @@ -15,20 +15,28 @@ public class Examples { static long sent = 0; static long received = 0; public static Future test(){ - Jokio.runtime(Waker.waker()).spawn(server()); + Jokio.runtime().await().spawn(server()); - for(int i = 0; i < 10000; i ++){ + + for(int i = 0; i < 100; i ++){ var builder = new StringBuilder(); - for(int c = 0; c < 4096*2; c ++) + for(int c = 0; c < 4096*16*3; c ++) builder.append((char)((Math.random()*('z'-'a')+'a'))); - Jokio.runtime(Waker.waker()).spawn(echoForever(builder.toString())); + Jokio.runtime().await().spawn(echoForever(builder.toString())); } + var start = System.currentTimeMillis(); while(true){ - System.out.println(sent + " " + received + " " + Jokio.polled); Delay.delay(100).await(); + var now = System.currentTimeMillis(); + System.out.println(sent + " " + received + " " + (now-start)); + start = now; } } + public static Future number(){ + return Future.ret(12); + } + public static Future server(){ try(var ss = ServerSocket.bind(new InetSocketAddress("0.0.0.0", 42069))){ @@ -44,7 +52,7 @@ public class Examples { public static Future echo(Socket socket){ try(socket){ - var buffer = ByteBuffer.allocate(4096); + var buffer = ByteBuffer.allocate(4096*16*3); while(true){ var read = socket.read(buffer).await(); buffer.clear().limit(read); @@ -66,8 +74,8 @@ public class Examples { sent++; buffer.clear().limit(wrote); socket.read_all(buffer).await(); - if(!buffer.position(0).equals(ByteBuffer.wrap(msg_bytes))) - throw new RuntimeException(); +// if(!buffer.position(0).equals(ByteBuffer.wrap(msg_bytes))) +// throw new RuntimeException(); received++; buffer.clear(); } diff --git a/src/async_example/Jokio.java b/src/async_example/Jokio.java index c6cf18c..2ddd19c 100644 --- a/src/async_example/Jokio.java +++ b/src/async_example/Jokio.java @@ -7,8 +7,6 @@ import java.util.ArrayDeque; import java.util.HashSet; public class Jokio implements Runnable{ - - public static long polled = 0; private class Task implements Waker{ public final Future future; @@ -19,7 +17,7 @@ public class Jokio implements Runnable{ @Override public void wake() { synchronized (Jokio.this){ - if(wokeSet.add(this)) + if(currentSet.contains(this)&&wokeSet.add(this)) wokeQueue.add(this); Jokio.this.notifyAll(); } @@ -43,9 +41,9 @@ public class Jokio implements Runnable{ return ((Task)waker).runtime(); } - private volatile long current = 0; 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(); @@ -54,7 +52,7 @@ public class Jokio implements Runnable{ public Jokio spawn(Future future){ var task = new Task<>(future); synchronized (this){ - current++; + currentSet.add(task); wokeQueue.add(task); wokeSet.add(task); } @@ -63,8 +61,9 @@ public class Jokio implements Runnable{ @Override public void run(){ - while(current > 0) { + while(true) { synchronized (this) { + if(currentSet.isEmpty())break; while (wokeQueue.isEmpty()) { try { this.wait(); @@ -77,26 +76,24 @@ public class Jokio implements Runnable{ synchronized (this){ task = wokeQueue.poll(); wokeSet.remove(task); + if(!currentSet.contains(task))continue; } Object result; try{ result = task.future.poll(task); }catch (Throwable t){ - throw new RuntimeException(t); -//// System.out.println("Future " + task.future + " Threw Exception"); -//// t.printStackTrace(); -// synchronized (this){ -// current--; -// polled++; -// } -// continue; + System.out.println("Future " + task.future + " Threw Exception"); + t.printStackTrace(); + synchronized (this){ + currentSet.remove(task); + } + continue; } synchronized (this){ if(result!=Future.Pending.INSTANCE) { - current--; + currentSet.remove(task); System.out.println(result); } - polled++; } } diff --git a/src/async_example/net/ServerSocket.java b/src/async_example/net/ServerSocket.java index b19dd73..968a2a6 100644 --- a/src/async_example/net/ServerSocket.java +++ b/src/async_example/net/ServerSocket.java @@ -46,12 +46,6 @@ public class ServerSocket implements AutoCloseable{ }else if(key.isAcceptable()){ w.wake(); } -// else if(key.isConnectable()){ -// }else if(key.isReadable()){ -// w.wake(); -// }else if(key.isWritable()){ -// w.wake(); -// } } }catch (Exception e){ e.printStackTrace(); diff --git a/src/generator/runtime/future/FutureSMBuilder.java b/src/generator/runtime/future/FutureSMBuilder.java index fe42041..78cdd44 100644 --- a/src/generator/runtime/future/FutureSMBuilder.java +++ b/src/generator/runtime/future/FutureSMBuilder.java @@ -47,8 +47,8 @@ public class FutureSMBuilder extends StateMachineBuilder { var sst = new SavedStateTracker(); - bcb.storeLocal(TypeKind.REFERENCE, frame.locals().length+2); frame.save_locals(smb, cob, sst,2); + bcb.storeLocal(TypeKind.REFERENCE, frame.locals().length+2); frame.save_stack(smb, cob, sst,1); bcb.loadLocal(TypeKind.REFERENCE, frame.locals().length+2); bcb.areturn().labelBinding(restore_label);