reference-barrierprocedure due to the non-trivial work to implement. Now, there's a post which shows how to implement portably, like this:
(define last-reference 0) (define (reference-barrier x) (let ((y last-reference)) (set! last-reference x) y))It seems ok, but is it? The SRFI says like this:
This procedure ensures that the garbage collector does not break an ephemeron containing an unreferenced key before a certain point in a program. The program can invoke a reference barrier on the key by calling this procedure, which guarantees that even if the program does not use the key, it will be considered strongly reachable until afterDue to the lack of knowledge and imagination, I can't imagine how it should work precisely on like this situation:
(let loop () (when (some-condition) (reference-barrier key1) (reference-barrier key2) (do-something-with-ephemerons) (loop)))Should key1 and key2 be guaranteed not to be garbage collected or only the last one (key2 in this case)? If the first case should be applied, then the proposed implementation doesn't work since it can only hold one reference. To me, it's rather rational to hold multiple reference since it's not always only one key needs to be preserved. However if you extend to hold multiple values, then how do you release the references without calling explicit release procedure? If the references would not be released, then ephemerons would also not release its keys. Thus calling
reference-barriercauses eternal preservation.
Suppose this assumption is correct, then
reference-barriershould work like
allocawith automated push so that the pushed reference is on the location where garbage collector can see them as live objects. In this sense, allocated references are released automatically once caller of
reference-barrieris returned and multiple references can be pushed. Though, it's indeed non-trivial task to implement. I hope there's no errata which says
reference-barrieris *not* optional, otherwise it would be either a huge challenge or dropping the support...