!7 p-experiment { callHibernatable(callHibernatable(f serveLater, f reversedString), f print); callHibernatable(callHibernatable(f serveNow, f reversedString), f print); } sclass HibernationFuture { new Flag done; O value; Runnable continuation; void setContinuation(Runnable r) { synchronized(done) { if (!done.isUp()) ret with continuation = r; } callF(r); } void setValue(O value) { synchronized(done) { if (done.isUp()) fail("setValue called twice"); this.value = value; done.raise(); } callF(continuation); } O getValue() { assertTrue(done.isUp()); ret value; } } // f1: func -> A, f2 : func(A) -> B, returns: B static O callHibernatable(O f1, fO f2) { O result; try { result = callF(f1); } catch (RuntimeException e) { Throwable e2 = innerException(e); if (!e2 instanceof HibernatingTheThread) throw rethrow(e); final HibernationFuture f = e2/HibernatingTheThread.future; f.setContinuation(r { callF(f2, f.getValue()) }); } ret callF(f2, result); } sS serveNow() { ret "Served now"; } sS serveLater() { final new HibernationFuture f; doLater(1000, r { f.setValue("Served late") }); throw HibernatingTheThread(f); } sS aFunction(bool hibernate) { if (hibernate) { print("Hibernating for 1s..."); HibernatingTheThread e = new(func -> S { ret "finished"; }); doLater(1000, e); throw e; } else ret "hello"; } sclass HibernatingTheThread extends QuickException { HibernationFuture future; *(HibernationFuture *future) {} }