[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making guardians a module?

From: Dirk Herrmann
Subject: Re: Making guardians a module?
Date: Wed, 6 Dec 2000 14:42:29 +0100 (MET)

On Wed, 6 Dec 2000, Dirk Herrmann wrote:

> With respect to guardians one should know, that guardians are not a very
> well thought out thing at all (IMO).  The interface is nice, but the
> semantics are quite strange.  Assume for example, that every port that is
> created is placed into a guardian, to allow for a close operation if the
> file object gets lost.  Further, assume that the user puts a pair
> consisting of a string and a port into a guardian, with the intention to
> print out the string to the port as soon as the pair gets lost.  Now
> assume that the pair actually gets lost and with it the port.  Then, the
> pair can be fetched from the pair's guardian, and the port object can be
> fetched from the guardian that stores all ports on creation.  
> Unfortunately, there is no protection agains the case that the port is
> fetched first by some code, that then performs finalization (i. e. closes
> the port) and only later the pair is fetched from the other guardian.  
> The attempt to print out the pair's string on the port will fail, because
> the port is already closed.

To see this effect, here is an example program:

(define port-guardian (make-guardian))
(define pair-guardian (make-guardian))
(define port (open-output-file "tmp"))
(port-guardian port)
(pair-guardian (cons "Collected." port))
(undefine port)


(let* ((port (port-guardian)))
  (if port (close-output-port port)))
(let* ((pair (pair-guardian)))
  (if pair (display (car pair) (cdr pair))))


0* (let ((pair (pair-guardian))) (if pair (display (car pair) (cdr pair))))
1  (if pair (display (car pair) (cdr pair)))
2  [display "Collected." #<closed: file 0>]

ERROR: In procedure display in expression (display (car pair) (cdr pair)):
ERROR: Wrong type argument in position 2: #<closed: file 0>
ABORT: (wrong-type-arg)

If, however, you swap the last two statements such that you first access
the pair-guardian and then the port-guardian, everything works fine.  Now
imagine that the two guardians belong to two different modules, and that
for a developer it is not obvious, at which moment the different guardians
would be accessed...

This property of guardians makes it difficult to use them in modular code,
since whether or not an object is protected by a guardian should be an
implementation detail, and other code should not depend on that fact.

Best regards

reply via email to

[Prev in Thread] Current Thread [Next in Thread]