On 3 Dec 2023, at 06:15, John Cowan <[email protected]> wrote:

> On Thu, Nov 30, 2023 at 6:34 PM Daphne Preston-Kendal <[email protected]> 
> wrote:
> 
>> I would like to propose an additional procedure, guardian-try-collect. When 
>> called on a guardian, it instructs the garbage collector to do as much 
>> collection as might be required to enqueue one object in the guardian for 
>> finalization.
> 
> I think this is the same as the `guardian-wait` procedure described by Marc 
> Feeley in <https://srfi-email.schemers.org/srfi-246/msg/23507606/>.

Marc’s proposal seems to be to block the thread until the next garbage 
collection happens to run anyway. Mine is to request an immediate garbage 
collection without ‘blocking’ in that sense. I think mine is the better 
solution since it does not depend on threading to work. It’s not clear to me 
how the unwind-protect case could be solved by Marc’s solution.

> I think that your definition of unwind-protect should be added to the SRFI.

If so, use this one: 

(define-syntax unwind-protect
  (syntax-rules ()
    ((_ protected-form finalization-form)
     (let ((g (make-guardian)))
       (dynamic-wind
         (lambda () #f)
         (lambda ()
           (let ((p (cons 'p '()))) ; p will only be alive as long as
                                    ; the continuation of
                                    ; unwind-protect is still alive
             (g p)
             (begin0 protected-form
               (reference-barrier p)))
           (lambda ()
             (guardian-try-collect g) ; try to collect p
             (when (g)
               finalization-form))))))))

which uses the begin0 form to be multiple-value safe for the protected-form. 
Since that isn’t in any SRFI yet (although it is in the R6RS operational 
semantics), you can put this portable implementation in with it if you think it 
necessary:

(define-syntax begin0
  (syntax-rules ()
    ((_ expr_0 expr_1 ...)
     (call-with-values
         (lambda () expr_0)
       (lambda x
         expr_1 ...
         (apply values x))))))


Daphne

Reply via email to