-- FIFOGuardianImpl.mesa
-- created by Suzuki:   
-- last edited by Suzuki: June 19, 1981  1:58 PM

DIRECTORY
	FIFOConnectorBool,
	FIFOGuardian;

FIFOGuardianImpl: MONITOR 
	IMPORTS
		ReadReq: FIFOConnectorBool,
		WriteReq: FIFOConnectorBool,
		DataAv: FIFOConnectorBool,
		SpaceAv: FIFOConnectorBool,
		Init: FIFOConnectorBool
	EXPORTS FIFOGuardian = {

NoTimeToInit: PUBLIC SIGNAL = CODE;
NoDataAv: PUBLIC SIGNAL = CODE;
NoSpaceAv: PUBLIC SIGNAL = CODE;

Main: PUBLIC PROC = {
	p,q,r: PROCESS;
	Initialized ← FALSE;

	p ← FORK InitProc[];
	q ← FORK Read[];
	r ← FORK Write[];
};

Initialized: BOOLEAN;

InitProc: PROC = {
	DO
		Init.GetNew[TRUE];
		IF ReadReq.Get[] OR 
				WriteReq.Get[] THEN 
			SIGNAL NoTimeToInit;
		Initialize[];
	ENDLOOP};

Read: PROC = {
	DO
		ReadReq.GetNew[TRUE];
		IF NotInitialized[] OR Init.Get[] OR 
				NOT DataAv.Get[] THEN
			SIGNAL NoDataAv;
	ENDLOOP};

Write: PROC = {
	DO
		WriteReq.GetNew[TRUE];
		IF NotInitialized[] OR Init.Get[] OR 
				NOT SpaceAv.Get[] THEN
			SIGNAL NoSpaceAv;
	ENDLOOP};

Initialize: ENTRY PROC = {
	Initialized ← TRUE};

NotInitialized: ENTRY PROC RETURNS [BOOLEAN] = {
	RETURN[~Initialized]};

}.