[plt-scheme] RE: Shared resources for servlets

From: Joe Marshall (jrm at ccs.neu.edu)
Date: Wed Sep 10 15:47:46 EDT 2003

Re: [plt-scheme] RE: Shared resources for servletsThe threads in PLT Scheme
are free running and can be
swapped at any time.

----- Original Message ----- 
From: Dor Kleiman
To: Joe Marshall ; Keith Frost
Cc: plt-scheme at list.cs.brown.edu
Sent: Wednesday, September 10, 2003 16:45
Subject: RE: [plt-scheme] RE: Shared resources for servlets


Well it's not perfect because it's just an abstraction. It doesn't deal with
exceptions, for one.
I have one questions though:
Do the threads not wait to their turn and only change at (sleep)s and
blocks?
If so, all the race conditions would not happen
-----Original Message----- 
From: Joe Marshall [mailto:jrm at ccs.neu.edu]
Sent: ד 10/09/2003 20:21
To: Dor Kleiman; Keith Frost
Cc: plt-scheme at list.cs.brown.edu
Subject: Re: [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.



Posted on the users mailing list.