<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version 6.0.6249.1">
<TITLE>Re: [plt-scheme] RE: Shared resources for servlets</TITLE>
</HEAD>
<BODY dir=ltr>
<DIV>Well it's not perfect because it's just an abstraction. It doesn't deal
with exceptions, for one.</DIV>
<DIV>I have one questions though:</DIV>
<DIV>Do the threads not wait to their turn and only change at (sleep)s and
blocks?</DIV>
<DIV>If so, all the race conditions would not happen</DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV><FONT size=2>-----Original Message----- <BR><B>From:</B> Joe Marshall
[mailto:jrm@ccs.neu.edu] <BR><B>Sent:</B> ד 10/09/2003 20:21 <BR><B>To:</B>
Dor Kleiman; Keith Frost <BR><B>Cc:</B> plt-scheme@list.cs.brown.edu
<BR><B>Subject:</B> Re: [plt-scheme] RE: Shared resources for
servlets<BR><BR></FONT></DIV>
<P><FONT size=2>Re: Shared resources for servletsFrom: Dor Kleiman
<dor@ntr.co.il>;<BR>To: Keith Frost
<keithf@amnis.com><BR><BR><BR>> (module lock
mzscheme<BR>> (define
read/write-object<BR>>
(class<BR>> (define (read)
'todo)<BR>> (define (write to-write)
'todo)))<BR>><BR>> (define
lockable-object<BR>>
(class<BR>> (define reading
0)<BR>> (define writing?
#f)<BR>><BR>> (init-field
obj)<BR>><BR>> (define
(open-read)<BR>> (if
writing?<BR>>
(begin (sleep)
(open-read))<BR>>
(set! reading (+ reading 1))))<BR><BR>Race condition here, writing? could be
#F when<BR>conditional is tested, but changed to #T before<BR>reading is
incremented.<BR><BR>Additionally, reading is not atomically incremented,<BR>so
an increment could be lost.<BR><BR>>
(define (close-read)<BR>>
(set! reading (- reading 1)))<BR><BR>Could lose the decrement on a race
condition.<BR><BR>> (define
(open-write)<BR>> (if (or
writing? (> reading
0))<BR>>
(begin (sleep)
(open-write))<BR>>
(set! writing? #t)))<BR><BR>Again, race condition in the conditional, two
processes<BR>could both detect no writing? condition and each think<BR>that it
can write.<BR><BR>> (define
(close-write)<BR>> (set!
writing? #f))<BR>><BR>> (define
(read)<BR>> ; returns
current value.<BR>> ;
Copies it first so it doesn't change when writing later
on<BR>>
(open-read)<BR>> (let ([r
(send obj
read)])<BR>>
(close-read)<BR>>
r)))<BR><BR>Need a dynamic-wind here to ensure that the lock is freed<BR>even
if the reader aborts.<BR><BR>> (define
(write to-write)<BR>>
(open-write)<BR>> (send obj
write to-write)<BR>>
(close-write)))))<BR><BR>Need a dynamic-wind here to ensure that the lock is
freed<BR>even if the writer
aborts.<BR><BR></FONT></P></BLOCKQUOTE>
</BODY>
</HTML>