[plt-scheme] RE: Shared resources for servlets
Re: Shared resources for servletsFrom: Dor Kleiman <dor at ntr.co.il>;
To: Keith Frost <keithf at amnis.com>
> (module lock mzscheme
> (define read/write-object
> (class
> (define (read) 'todo)
> (define (write to-write) 'todo)))
>
> (define lockable-object
> (class
> (define reading 0)
> (define writing? #f)
>
> (init-field obj)
>
> (define (open-read)
> (if writing?
> (begin (sleep) (open-read))
> (set! reading (+ reading 1))))
Race condition here, writing? could be #F when
conditional is tested, but changed to #T before
reading is incremented.
Additionally, reading is not atomically incremented,
so an increment could be lost.
> (define (close-read)
> (set! reading (- reading 1)))
Could lose the decrement on a race condition.
> (define (open-write)
> (if (or writing? (> reading 0))
> (begin (sleep) (open-write))
> (set! writing? #t)))
Again, race condition in the conditional, two processes
could both detect no writing? condition and each think
that it can write.
> (define (close-write)
> (set! writing? #f))
>
> (define (read)
> ; returns current value.
> ; Copies it first so it doesn't change when writing later on
> (open-read)
> (let ([r (send obj read)])
> (close-read)
> r)))
Need a dynamic-wind here to ensure that the lock is freed
even if the reader aborts.
> (define (write to-write)
> (open-write)
> (send obj write to-write)
> (close-write)))))
Need a dynamic-wind here to ensure that the lock is freed
even if the writer aborts.