Child control with the Notifier

Mark_Weiser.PARC at xerox.com Mark_Weiser.PARC at xerox.com
Thu Nov 1 11:23:23 AEST 1990


The problem is that you have to give control back to the notifier so that
it can do the wait for you and reap the children.  As long as you are in
your loop the notifier cannot do anything.

This is a very common problem, and the solution I use is to, rather than
loop, schedule myself to run again ASAP and return to the notifier,
keeping track of the iterations with a global variable.  This problem came
up so often that I wrote the following  simple routines to make it easier
to schedule an arbitrary procedure call in the future:

/*
 * Call procedure f in a little while.
 */

struct call_wrapper {
	/* Dynamically allocating a wrapper ensures unique notifier id's. */
	void (*f)();
}

do_with_delay(f, secs, usecs)
void (*f)();
int secs, usecs;
{
	Notify_value do_the_call();
	struct call_wrapper *w;
	struct itimerval timer;

	/* Sigh, so much work just to wait a bit before starting up. */
	timer.it_interval.tv_usec = 0;
	timer.it_interval.tv_sec = 0;
	timer.it_value.tv_usec = usecs;
	timer.it_value.tv_sec = secs;
 	w = (struct call_wrapper *)calloc(sizeof(struct call_wrapper), 1);
	w->f = f;
	notify_set_itimer_func(w, do_the_call,
		ITIMER_REAL, &timer, NULL);
}

/*
 * Wrapper to make sure procedures from do_with_delay return good values
 * to the notifier.
 */
Notify_value
do_the_call(w, which)
struct call_wrapper *w;
{
	(*(w->f))();
	free(w);
	return NOTIFY_DONE;
}



More information about the Comp.sys.sun mailing list