MODULE Example6;
TYPE
ReadersWritersScheduler* = OBJECT
VAR busy: BOOLEAN; readers, writers: LONGINT;
PROCEDURE StartRead*;
BEGIN {EXCLUSIVE}
AWAIT(~busy & (writers = 0));
INC(readers)
END StartRead;
PROCEDURE EndRead*;
BEGIN {EXCLUSIVE}
DEC(readers)
END EndRead;
PROCEDURE StartWrite*;
BEGIN {EXCLUSIVE}
INC(writers);
AWAIT(~busy & (readers = 0));
busy := TRUE
END StartWrite;
PROCEDURE EndWrite*;
BEGIN {EXCLUSIVE}
DEC(writers); busy := FALSE
END EndWrite;
PROCEDURE &Init*;
BEGIN
busy := FALSE; readers := 0; writers := 0
END Init;
END ReadersWritersScheduler;
END Example6.