Princeton University |
Computer Science 510 |
|
This program implements the concurrent-read-exclusive-write (CRXW) lock covered in lecture. To run this program, make a directory containing the two file
Run "sml" version 110.0.7 (i.e., /usr/local/sml/bin/sml), type CM.make(); at the first prompt, then Test.go(); at the next prompt.It will run indefinitely, though you may stop it with an interrupt (control-C).
The sml file contains two separate implementations of the CRXW lock; one in which the locking operations are functions and one in which they are events. The first one is a bit simpler to understand; the second one gives the client a bit more choice about blocking.
Notes on CML programming:
local val randCh : int chan = channel() fun loop s = (send(randCh, s mod 10); loop((s+37) mod 61)) val _ = spawnc loop 0 in fun random() = recv randCh endis not at top level in the structure, but is put inside the main function. Reppy's book does not really explain this, you can see the CML FAQ.
Therefore, the last line of Test.main, which reads
app (sync o joinEvt) threadsis not really necessary. But I left it in to illustrate the management of thread id's.