xref: /titanic_50/usr/src/lib/libast/common/tm/tvsleep.c (revision 2777f608ce96fd0d3b91accea29d21d6aacbe7e5)
1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin  *                                                                      *
3da2e3ebdSchin  *               This software is part of the ast package               *
4*2777f608SMarcel Telka  *          Copyright (c) 1985-2013 AT&T Intellectual Property          *
5da2e3ebdSchin  *                      and is licensed under the                       *
6*2777f608SMarcel Telka  *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin  *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin  *                                                                      *
9da2e3ebdSchin  *                A copy of the License is available at                 *
10*2777f608SMarcel Telka  *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*2777f608SMarcel Telka  *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12da2e3ebdSchin  *                                                                      *
13da2e3ebdSchin  *              Information and Software Systems Research               *
14da2e3ebdSchin  *                            AT&T Research                             *
15da2e3ebdSchin  *                           Florham Park NJ                            *
16da2e3ebdSchin  *                                                                      *
17*2777f608SMarcel Telka  *               Glenn Fowler <glenn.s.fowler@gmail.com>                *
18*2777f608SMarcel Telka  *                    David Korn <dgkorn@gmail.com>                     *
19*2777f608SMarcel Telka  *                     Phong Vo <phongvo@gmail.com>                     *
20da2e3ebdSchin  *                                                                      *
21da2e3ebdSchin  ***********************************************************************/
22da2e3ebdSchin 
23*2777f608SMarcel Telka #include <errno.h>
24*2777f608SMarcel Telka #include <time.h>
25da2e3ebdSchin 
26*2777f608SMarcel Telka #include "tv.h"
27da2e3ebdSchin 
28da2e3ebdSchin /*
29da2e3ebdSchin  * sleep for tv
30da2e3ebdSchin  * non-zero exit if sleep did not complete
31da2e3ebdSchin  * with remaining time in rv
32*2777f608SMarcel Telka  *
33*2777f608SMarcel Telka  * NOTE: some systems hide nanosleep() ouside of -lc -- puleeze
34da2e3ebdSchin  */
35da2e3ebdSchin 
tvsleep(const Tv_t * tv,Tv_t * rv)36*2777f608SMarcel Telka int tvsleep(const Tv_t *tv, Tv_t *rv) {
37da2e3ebdSchin     struct timespec stv;
38da2e3ebdSchin     struct timespec srv;
39da2e3ebdSchin     int r;
40da2e3ebdSchin 
41da2e3ebdSchin     stv.tv_sec = tv->tv_sec;
42da2e3ebdSchin     stv.tv_nsec = tv->tv_nsec;
43*2777f608SMarcel Telka     if ((r = nanosleep(&stv, &srv)) && errno == EINTR && rv) {
44da2e3ebdSchin         rv->tv_sec = srv.tv_sec;
45da2e3ebdSchin         rv->tv_nsec = srv.tv_nsec;
46da2e3ebdSchin     }
47da2e3ebdSchin     return r;
48da2e3ebdSchin }
49