Re: coop - a concurrent ml-like wanna be (pre-SRFI)
Amirouche 06 May 2021 16:28 UTC
On 2021-05-06 17:41, Marc Nieper-Wißkirchen wrote:
> This needs delimited continuations to prevent space leaks, doesn't it?
What does "space leaks" means:
A) Memory is lost and can never be retrieved
Or
B) Use a lot of memory.
Here is the code I use:
;;
;; XXX: Inside call/pause, escapade-singleton allows to tell how
;; call/pause thunk continuation is called:
;;
;; - nominal case: thunk returned.
;;
;; - thunk called untangle-escapade, in that case escapade-singleton
;; is the first argument of the continuation of thunk inside
;; call/pause.
;;
;; escapade-singleton is a singleton, user code can not create it.
;;
(define escapade-singleton '(escapade-singleton))
;; TODO: probably replace untangle with a box that contains the
;; escapade.
(define (call/pause untangle thunk)
(call-with-values (lambda ()
(call/1cc
(lambda (escape)
(assume (not (untangle-escapade untangle)))
(untangle-escapade! untangle escape)
(thunk))))
(lambda args
;; TODO: use chibi's match
;; args may be the empty list if THUNK returns nothing.
(if (and (pair? args) (eq? (car args) escapade-singleton))
;; XXX: The following code is the escapade handler! That
;; is always a proc and a continuation, because of how
;; pause is implemented. Racket call/ec has an optional
;; argument called handler that allows to change that
;; behavior.
(let ((proc (cadr args))
(k (caddr args)))
;; call the procedure proc passed to untangle-escapade
;; with its continuation called k. That is, k, is what
;; follow escapade call inside THUNK. K will allow to
;; resume THUNK.
(proc k))
(apply values args)))))
(define (pause untangle proc)
(assume? untangle)
;; XXX: Capture the continuation and call it later, that is why it
;; use call/cc instead of call/1cc.
(call/cc
(lambda (k)
;; save escapade
(define escapade (untangle-escapade untangle))
;; The escapade is a call/1cc continuation, no need to keep it
;; around, and it might lead to strange bugs.
(untangle-escapade! untangle #f)
;; XXX: escapade is the continuation of a thunk inside
;; call/pause whereas k is the continuation of the caller of
;; pause inside thunk.
;; XXX: Continue with thunk's continuation inside call/pause as
;; known as escapade. Inside call/pause, proc and k are used to
;; build the pause handler.
(escapade escapade-singleton proc k))))
> Unfortunately, these are not yet standardized.
Does it mean it is useless to work on coop?