[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

conditional monitor locking



A simple extension to the Monitor module on pages 4-56 to 4-57
of the docs gives a timed wait:

	Monitor.timedwait(m: self Monitor, waitms: int) : int
	{
		tchan := chan of int;
		spawn timer(tchan, waitms);
		tpid := <- tchan;

		alt{
		m.ch <- = 0 =>
			killpid(tpid);	#could be optional
			return 1;
		<- tchan =>
			return 0; 
		}
	}

but the overhead to spawn and kill the timer is neither small nor
constant.  On the other hand, doing

	Monitor.trylock(m: self Monitor) : int
	{
		alt{
		m.ch <- = 0 =>
			return 1;
		* =>
			return 0;
		}
	}

always returns 0 if called immediately after Monitor.unlock().  A
nop such as this is needed:

	sys->sleep(0);
	alt{
	m.ch <- = 0 =>
	...

Spin locking has similar issues with channel latency.

Timings under emu are bound to be approximate and channels are subject
to scheduling, but I'm wondering if including a set of monitor
primitives in the compiled-in library would be helpful.  Or maybe
there's another way to do a conditional mutex lock in Limbo.  Anyway,
an atomic channel ready test like Alef's "?" would be great.

Steve