This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Pseudo-weak?


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>)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]