[plt-scheme] Re: Shared resources for servlets

From: Keith Frost (keithf at amnis.com)
Date: Tue Sep 9 16:14:20 EDT 2003

Robby Findler wrote:

>At Tue, 09 Sep 2003 09:31:23 -0700, Keith Frost wrote:
>  
>
>>Which brings to mind a question I've had in the back of my mind, and
>>I expect someone on this list could help me with. Consider the common
>>resource model "One writer -- many readers". In other words, if a
>>reader goes to access the resource, it needs only check whether there
>>is a writer accessing it. If so, it blocks until the writer releases
>>it. If not, it may go ahead, so that any number of readers can be
>>simultaneously accessing the resource (a database, or even a file,
>>say). If a writer goes to access the resource, it has to check if
>>there is another writer, or any readers, currently accessing it. If
>>so, it blocks until all readers and any writer have released it.
>>    
>>
>
>  
>
>>What is the simplest, or a recommended, way to achieve this resource
>>pattern using MzScheme's threads and semaphores?
>>    
>>
>
>Whatever you do, don't use sempahores! Use waitables and the operations
>on them. There's some description of them in the mzscheme manual, and
>you can also find research papers on CML that discuss the same
>primitives (althou in the context of a different programming language).
>
>If that's not enough help, perhaps if you can say more about the
>resource and how you plan to write to it and I can be more concrete
>about how to implement it.
>
>hth,
>Robby
>
>  
>

Okay, let me impose on you to help me with a  simple example.
Let us suppose the resource is a single file,
and I want a module which provides four functions:
acquire-input, acquire-output, release-input, and release-output.

Without requiring the implementation to work this way, but just
for the sake of discussion, I now assume that internal to the module,
we have variables
(define readers 0)
(define writer? #f)

(acquire-input) blocks until (not writer?), then increments readers and
returns an input port opened on the file.

(acquire-output) blocks until (and (not writer?) (zero? readers)),
then sets writer? to #t and returns an output port opened on the file 
(with 'replace).

(release-input port) closes the input port and decrements the number of 
readers.

(release-output port) closes the output port and sets writer? to #f.

What is the recommended way to achieve this with waitables?

Thanks,

Keith



Posted on the users mailing list.