This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: Pseudo-weak?
- To: forcer <forcer at mindless dot com>
- Subject: Re: Pseudo-weak?
- From: Mikael Djurfeldt <mdj at thalamus dot nada dot kth dot se>
- Date: 27 Sep 1999 19:21:54 +0200
- Cc: guile at sourceware dot cygnus dot com
- Cc: djurfeldt at nada dot kth dot se
- References: <199909270209.EAA20228@forcix.roof.lan>
- Reply-to: Mikael Djurfeldt <djurfeldt at nada dot kth dot se>
forcer <forcer@mindless.com> writes:
> Is it possible to designate function to be called when an object
> isn't referenced anymore except for one (some?) "pseudo-weak"
> locations? E.g. the same behavior as with weak references, except
> that the object does not get garbage collected.
>
> I'm trying to implement a cache for database connections. My
> "problem" with that is that it should be transparent, e.g. one
> shouldn't have to free the connection to give it back to the
> cache.
>
> This would be possible on the C-level, e.g. to dynamically build
> smobs of those database connections, and return them to the cache
> upon smob collection, but i'd like to do them on the Scheme
> level.
>
> Any ideas?
Yes. This is exactly the kind of situation which is solved by guardians.
Create a connection guardian
(define connection-guardian (make-guardian))
Put every new connection in the guardian
(connection-guardian <new-connection>)
At some suitable point in the program, for example before inserting a
new entry into the cache, do
(do ((freed-connection (connection-guardian) (connection-guardian)))
((not freed-connection))
<remove freed-connection from cache>)