[comp.unix.wizards] sleeping while at spltty?

chris@mimsy.UUCP (Chris Torek) (04/16/89)

In article <824@twwells.uucp> bill@twwells.uucp (T. William Wells) writes:
>	splsave = spltty();
>	while (!carrier_detect || already_opened) {
>		sleep(&line_flags, 29);
>	}
>	splx(splsave);
>... could someone clue me in on what is really happening?

Sleep() puts the current process to sleep, saving its state *including*
IPL.  A later wakeup() puts it back on the run queue, and when it is
subsequently resumed the IPL is restored to whatever its value was when
the code called sleep().  The carrier detect flag is thus set while the
process is sleeping.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

steve@nuchat.UUCP (Steve Nuchia) (04/17/89)

In article <824@twwells.uucp> bill@twwells.UUCP (T. William Wells) writes:
>	splsave = spltty();
>	while (!carrier_detect || already_opened) {
>		sleep(&line_flags, 29);
>	}
>	splx(splsave);
>
>My understanding of the spl# calls is that they disable interrupts;
>
>So, could someone clue me in on what is really happening?

The missing piece is that the sleep call "does the right thing".
In particular, it remembers the SPL level in effect, suspends
the current process, and reactivates (through part of the
diffuse "scheduler algorithm") some other process, at the
SPL it sent to sleep at.

If you couldn't call sleep from an elevated SPL there would
be unavoidable race conditions in your code, watch:

	x = spltty();
	while ( !flag )
	{
		splx(x);
		sleep(&flag, TTYIN);
		x = spltty();
	}
	splx(x);

If the interrupt occurs after the first spltty it will be serviced
between the inner splx and the sleep call.  The wakup(&flag) in
the interrupt routine is nor prescient, so it has no effect on
the sleep that happens on return from the interrupt.  Now nothing
will wake up your process.  Depending on the priority you've
chosen you may have just created one of the famous unkillable
processes.

-- 
Steve Nuchia	      South Coast Computing Services
uunet!nuchat!steve    POB 890952  Houston, Texas  77289
(713) 964 2462	      Consultation & Systems, Support for PD Software.