static class Q implements AutoCloseable { S name = "Unnamed Queue"; L q = synchroLinkedList(); ReliableSingleThread rst = new(r { _run() }); gettable volatile bool retired; gettable volatile Runnable currentJob; new AtomicLong jobsDone; *() {} *(S *name) {} void add(Runnable r) { assertNotRetired(); q.add(r); _trigger(); } void addInFront(Runnable r) { assertNotRetired(); q.add(0, r); _trigger(); } void _trigger() { rst.name = name; rst.go(); } void add(O r) { add(toRunnable(r)); } void _run() { Runnable r; while (licensed() && !retired && (r = syncPopFirst(q)) != null) { currentJob = r; inc(jobsDone); pcall { r.run(); } currentJob = null; } onIdle(); } public void close() { retired = true; } // TODO: interrupt thread void done() {} // legacy function bool isEmpty() { ret q.isEmpty(); } int size() { ret q.size(); } O mutex() { ret q; } // clients can synchronize on this L snapshot() { ret cloneList(q); } // override this void onIdle {} bool busy() { ret currentJob != null; } void assertNotRetired() { assertFalse("Queue is retired", retired()); } bool hasThread() { ret rst.hasThread(); } long nJobsDone() { ret jobsDone!; } toString { ret (retired ? "Retired " : "") + "Q " + systemHashCodeHex(this) + " (" + (isEmpty() ? "empty" : nEntries(size()) + ", current job: " + currentJob) + ")"; } }