Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

81
LINES

< > BotCompany Repo | #1015711 // Thread Hibernation Spike v2 [dev.]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7614L/51K).

!7

p-experiment {
  catchHibernationException(r {
    callHibernatable(f serveLater, fg(f print, f reversedString));
  });
  catchHibernationException(r {
    callHibernatable(f serveNow, fg(f print, f reversedString));
  });
}

svoid catchHibernationException(Runnable r) {
  try {
    callF(r);
  } catch (RuntimeException e) {
    Throwable e2 = innerException(e);
    if (!e2 instanceof HibernatingTheThread) throw rethrow(e);
  }
}

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;
    
    final new HibernationFuture future2;
    f.setContinuation(r {
      future2.setValue(callF(f2, f.getValue()))
    });
    throw HibernatingTheThread(future2);
  }
  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);
}

sclass HibernatingTheThread extends QuickException {
  HibernationFuture future;

  *(HibernationFuture *future) {}
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015711
Snippet name: Thread Hibernation Spike v2 [dev.]
Eternal ID of this version: #1015711/11
Text MD5: 25dd09cd8862113a6c0c1e0e0625cee2
Transpilation MD5: 6f898c9dea2902f75ece044e64ccdbde
Author: stefan
Category:
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-24 17:32:44
Source code size: 1739 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 296 / 782
Version history: 10 change(s)
Referenced in: [show references]