From owner-freebsd-hackers Tue Aug 5 01:47:07 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id BAA00363 for hackers-outgoing; Tue, 5 Aug 1997 01:47:07 -0700 (PDT) Received: from counterintelligence.ml.org (mdean.vip.best.com [206.86.94.101]) by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id BAA00346; Tue, 5 Aug 1997 01:47:02 -0700 (PDT) Received: from localhost (jamil@localhost) by counterintelligence.ml.org (8.8.6/8.8.5) with SMTP id BAA00537; Tue, 5 Aug 1997 01:46:33 -0700 (PDT) Date: Tue, 5 Aug 1997 01:46:33 -0700 (PDT) From: "Jamil J. Weatherbee" To: freebsd-hackers@freebsd.org cc: freebsd-realtime@freebsd.org Subject: ?? The right way to do a microsecond resolution wait Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk Is this the best way to do a microsecond resolution wait under freebsd (usleep only appears good to 10,000 microseconds). Doing small waits is an important part of realtime software -- something that it would be interesting to see addressed, particularily making a usleep that actually sleeps microseconds. With the following (unless i am off my rocker) i can get a 4 microsecond wait on a ppro 180, a 25 microsecond wait on a pentium 100, and a 12 microsecond wait on a nexgen-100. What I want to know is this the best way to get a highly accurate wait in a thread of execution, is this even doing what I think it is, and why doesnt usleep do something like this? I guess I could use it to output a square wave on a paralell pin and look at that with an oscilliscope. For the application I use it for the usleeps for the significant portion of the time bring the cpu% down from 99% to about 1.5% pretty significant. #define USLEEP_RESOLUTION 10000 /* Don't use for times > 2000 seconds */ void rsleep (long usecs) { struct timeval begin_time, cur_time; long elapsed, utm; gettimeofday (&begin_time, NULL); /* record the starting time */ if (utm = (usecs / USLEEP_RESOLUTION)*USLEEP_RESOLUTION) usleep (utm); do gettimeofday (&cur_time, NULL); /* get the current time */ while (((cur_time.tv_sec - begin_time.tv_sec) * 1000000 + (cur_time.tv_usec - begin_time.tv_usec)) < usecs); }