Subject: lib/19444: usleep(0); seems to microsleep(1);
To: None <gnats-bugs@gnats.netbsd.org>
From: None <takkun@mma.club.uec.ac.jp>
List: netbsd-bugs
Date: 12/18/2002 12:59:45
>Number:         19444
>Category:       lib
>Synopsis:       usleep(0); seems to microsleep(1);
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Dec 18 13:00:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     IIMURA Takuji
>Release:        1.6
>Organization:
University of Electro Communications
>Environment:
NetBSD polar.iimura 1.6C NetBSD 1.6C (KUMA) #1: Thu Jul 25 18:52:26 JST 2002     uirou@polar.iimura:/ein/tmp/polar/usr/src/sys/arch/i386/compile/KUMA i386

>Description:
usleep(3) manual say: "If the value of microseconds is 0, then the call has no effect."
But this function is call nanosleep(2).
I don't know more infomation to nanosleep(2).
Howeber, If I call usleep(0), It's looks like usleep(1000).
I think, nanosleep(2) sleep 1 tick time.

I think manual is true. usleep(3) is need if(useconds == 0) return;.

>How-To-Repeat:
try this code.
usleep(0)*1000 -> 10sec
not_effect(0)*1000 -> 0sec
-------
#include <stdio.h>

void not_effect(int i){
    /* */
}

int main(int argc, char *argv[]){
    int i;
    
    printf("time: %d\n", time(NULL));
    for(i = 0; i < 1000; i++){
	usleep(0);
    }
    printf("time: %d\n", time(NULL));
    for(i = 0; i < 1000; i++){
	not_effect(0);
    }
    printf("time: %d\n", time(NULL));
    return 0;
}

>Fix:
*** usleep.c.orig	Thu Dec 19 05:57:40 2002
--- usleep.c	Thu Dec 19 05:59:19 2002
***************
*** 56,61 ****
--- 56,64 ----
  {
  	struct timespec ts;
  
+ 	if (useconds == 0)
+ 		return (0);
+ 
  	if (useconds >= 1000000) {
  		errno = EINVAL;
  		return (-1);

>Release-Note:
>Audit-Trail:
>Unformatted: