os/ossrv/genericopenlibs/openenvcore/libc/src/nanosleep.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
     3  *
     4  * Permission to use, copy, modify, and distribute this software for any
     5  * purpose with or without fee is hereby granted, provided that the above
     6  * copyright notice and this permission notice appear in all copies.
     7  *
     8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15  * * Portions Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
    16  */
    17 
    18 
    19 
    20 
    21 #include <sys/time.h>
    22 #include <sys/timespec.h>
    23 #include <sys/select.h>
    24 #include <errno.h>
    25 
    26 //#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
    27 
    28 EXPORT_C 
    29 int nanosleep(const struct timespec *req, struct timespec *rem)
    30 {
    31         int rc, saverrno;
    32         extern int errno;
    33         struct timeval tstart, tstop, tremain, time2wait;
    34 
    35         if(!req)
    36         	{ 
    37         		return 0 ;
    38         	} 	 
    39         TIMESPEC_TO_TIMEVAL(&time2wait, req);
    40         (void) gettimeofday(&tstart, NULL);
    41         rc = select(0, NULL, NULL, NULL, &time2wait);
    42         if (rc == -1) {
    43                 saverrno = errno;
    44                 (void) gettimeofday (&tstop, NULL);
    45                 errno = saverrno;
    46                 tremain.tv_sec = time2wait.tv_sec -
    47                         (tstop.tv_sec - tstart.tv_sec);
    48                 tremain.tv_usec = time2wait.tv_usec -
    49                         (tstop.tv_usec - tstart.tv_usec);
    50                 tremain.tv_sec += tremain.tv_usec / 1000000L;
    51                 tremain.tv_usec %= 1000000L;
    52         } else {
    53                 tremain.tv_sec = 0;
    54                 tremain.tv_usec = 0;
    55         }
    56         if(rem)
    57         	{
    58         	TIMEVAL_TO_TIMESPEC(&tremain, rem);
    59         	}
    60 
    61         return(rc);
    62 }
    63 //#endif