Await counter
Since the Perl implementation is single-threaded, there is no need for await counters.
An await counter keeps track of unfinished (asynchronous) tasks, and executes a handler once all tasks have completed:
Each await call must be released by a done call. The then handler is executed immediately after the last task is done.
The await counter must always be used from the same thread, even if tasks are executed on different threads.
Await counters are typically used to wait for the completion of multiple asynchronous tasks:
class implements AwaitCounter.Done { ... void processItems() { AwaitCounter awaitCounter = new AwaitCounter(); // Process all items for (Item item: ...) new ProcessItem(item, awaitCounter); // Then call the done handler awaitCounter.then(this); } void onAwaitCounterDone() { // This is executed once all items have been processed } } class ProcessItem implements BackgroundTask { final Item item; SomeTask(Item item, AwaitCounter awaitCounter) { if (item.isEmpty()) return; awaitCounter.await(); someExecutor.execute(this); } void background() { ... } void after() { awaitCounter.done(); } }
function processItems() { var awaitCounter = new cds.AwaitCounter(); // Process all items for (var i = 0; i < items.length; i++) processItem(item); function processItem(item) { if (item.isEmpty()) return; awaitCounter.await(); ... // potentially asynchronous awaitCounter.done(); } // Then call the done handler awaitCounter.then(onAwaitCounterDone); function onAwaitCounterDone() { // This is executed once all items have been processed } }
Functions
AwaitCounter awaitCounter = new AwaitCounter();
var awaitCounter = new AwaitCounter();
Creates an await counter.
awaitCounter.await()
awaitCounter.await();
Increments the await counter. Each await call must be released by a done call. Calling await is only allowed if the await counter has not completed yet, i.e.
- if then has not been called yet,
- if you are still holding at least one await.
awaitCounter.done()
awaitCounter.done();
Decrements the await counter. If the counter falls back to 0, and then has been called, the await counter has completed and the then handler is called.
awaitCounter.then(handler)
awaitCounter.then(handler);
Registers the then handler. If the await counter is 0, the handler is executed immediately.