[net.sources] Pseudo-device for nap

sean@garfield.UUCP (Sean Byrne) (06/21/83)

	Here is a driver for a less than 1 second
sleep.  In 1/60th's of a second.  It uses the system's
routines timeout() and sleep().  Have fun implementing
this.  I did it on a VAX under 4.1 using the autoconf
stuff, so you other guys will have to install in without
the wonderful aid of autoconf.

Here is the driver: it goes in your sys/dev directory.

#include "nap.h"
#if NNAP > 0
#include "../h/param.h"
#include "../h/systm.h"
#include "../h/tty.h"
#include "../h/dir.h"
#include "../h/user.h"
#include "../h/proc.h"

#define N_NAP	32
#define NAPPRI	31

int ttnap[N_NAP];

napioctl(dev, cmd, addr, flags)
dev_t	dev;
caddr_t addr;
{
	int wakeup();
	int *ptr;

	for(ptr=ttnap; *ptr && (ptr <= &ttnap[N_NAP]); ptr++);
	if(ptr == &ttnap[N_NAP]) {
		u.u_error = ENXIO;
		return;
	}

	*ptr = u.u_procp->p_pid;
	timeout(wakeup, ptr, cmd);
	sleep(ptr, NAPPRI);
	*ptr = 0;
}

napclose(dev, flags)
	dev_t	dev;
{
	int *ptr;
	for(ptr=ttnap; (*ptr != u.u_procp->p_pid) && (ptr <= &ttnap[N_NAP]); ptr++);
	if(*ptr == u.u_procp->p_pid)
		*ptr = 0;
}
#endif

Here is the nap.h that goes in your SYS_NAME directory, those
of you with autoconf, forget it.

#define NNAP 1

Here is the addition to conf/files:

dev/nap.c		optional nap device-driver

Here is the addition to dev/conf.c:

/*
 *	Added by garfield!sean to do 1/60 sec sleeps
 *	Date: June 6, 1983
 */

#include "nap.h"
#if NNAP > 0
int	napioctl(), napclose();			/* for nap() */
#else
#define	napioctl	nodev
#define	napclose	nodev
#endif

Also add this to the struct cdevsw[].
I used 26, you can use whatever is free.
	nulldev,	napclose,	nulldev,	nulldev,	/*26*/
	napioctl,	nodev,		nodev,		0,

Those of you with autoconf, make the changes in sys/conf,
run config and then, in your SYS_NAME directory do
make depend and make vmunix (or whatever).
Then mknod for /dev/nap.

To call this you must:

	fd = open("/dev/nap", 0);
	
	ioctl(fd, NUMBER_OF_HERTZ_DELAY, 0);

Voila less than 1 sec sleep.  Have fun and remember, this
is just one way to do it.  I'm sure there are others,
probably better, but this is the way I chose to do it.
Feel free to experiment and devise.

				Sean Byrne
				{allegra,utcsrgv}!garfield!sean
				Memorial U of NF