[DGD] Continuations
Felix A. Croes
felix at dworkin.nl
Thu Apr 18 19:06:27 CEST 2019
Within the cloud library, continuations from different objects can now
be used together. This enables code like:
c = queue->dequeueCont() >> new ChainedContinuation("process");
c->runNext();
I.e. queue->dequeueCont() returns a continuation that can be used to
dequeue an item, when one is available. If the queue is empty, the
continuation can be suspended until a new item is enqueued; at that
point, the item will be forwarded to the next step to process it. I
include the code of the queue below, as an example of the use of
suspension and reawakening of continuations:
private object *queue, *suspended;
static void create()
{
queue = ({ });
suspended = ({ });
}
Continuation enqueueCont(object obj)
{
return new Continuation("enqueue", obj);
}
static void enqueue(object obj)
{
object token;
if (sizeof(suspended) != 0) {
token = suspended[0];
suspended = suspended[1 ..];
token->wakeContinuation(obj);
} else {
queue += ({ obj });
}
}
Continuation dequeueCont()
{
return new Continuation("dequeue");
}
static object dequeue()
{
object obj;
if (sizeof(queue) != 0) {
obj = queue[0];
queue = queue[1 ..];
return obj;
} else {
suspended += ({ suspendContinuation() });
returh nil;
}
}
Regards,
Felix Croes
More information about the DGD
mailing list