14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*8462SApril.Chin@Sun.COM * Copyright (c) 1985-2008 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 7*8462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * Glenn Fowler <gsf@research.att.com> * 184887Schin * David Korn <dgk@research.att.com> * 194887Schin * Phong Vo <kpv@research.att.com> * 204887Schin * * 214887Schin ***********************************************************************/ 224887Schin #pragma prototyped 234887Schin 244887Schin #include <tv.h> 254887Schin #include <tm.h> 264887Schin 274887Schin #include "FEATURE/tvlib" 284887Schin 294887Schin #if !_lib_nanosleep 304887Schin # if _lib_select 314887Schin # if _sys_select 324887Schin # include <sys/select.h> 334887Schin # else 344887Schin # include <sys/socket.h> 354887Schin # endif 364887Schin # else 374887Schin # if !_lib_usleep 384887Schin # if _lib_poll_notimer 394887Schin # undef _lib_poll 404887Schin # endif 414887Schin # if _lib_poll 424887Schin # include <poll.h> 434887Schin # endif 444887Schin # endif 454887Schin # endif 464887Schin #endif 474887Schin 484887Schin /* 494887Schin * sleep for tv 504887Schin * non-zero exit if sleep did not complete 514887Schin * with remaining time in rv 524887Schin */ 534887Schin 544887Schin int 554887Schin tvsleep(register const Tv_t* tv, register Tv_t* rv) 564887Schin { 574887Schin 584887Schin #if _lib_nanosleep 594887Schin 604887Schin struct timespec stv; 614887Schin struct timespec srv; 624887Schin int r; 634887Schin 644887Schin stv.tv_sec = tv->tv_sec; 654887Schin stv.tv_nsec = tv->tv_nsec; 664887Schin if ((r = nanosleep(&stv, &srv)) && rv) 674887Schin { 684887Schin rv->tv_sec = srv.tv_sec; 694887Schin rv->tv_nsec = srv.tv_nsec; 704887Schin } 714887Schin return r; 724887Schin 734887Schin #else 744887Schin 754887Schin #if _lib_select 764887Schin 774887Schin struct timeval stv; 784887Schin 794887Schin stv.tv_sec = tv->tv_sec; 804887Schin stv.tv_usec = tv->tv_nsec / 1000; 814887Schin if (select(0, NiL, NiL, NiL, &stv) < 0) 824887Schin { 834887Schin if (rv) 844887Schin *rv = *tv; 854887Schin return -1; 864887Schin } 874887Schin if (rv) 884887Schin { 894887Schin rv->tv_sec = stv.tv_sec; 904887Schin rv->tv_nsec = stv.tv_usec * 1000; 914887Schin } 924887Schin return 0; 934887Schin 944887Schin #else 954887Schin 964887Schin unsigned int s = tv->tv_sec; 974887Schin uint32_t n = tv->tv_nsec; 984887Schin 994887Schin #if _lib_usleep 1004887Schin 1014887Schin 1024887Schin unsigned long t; 1034887Schin 1044887Schin if (t = (n + 999L) / 1000L) 1054887Schin { 1064887Schin usleep(t); 1074887Schin s -= t / 1000000L; 1084887Schin n = 0; 1094887Schin } 1104887Schin 1114887Schin #else 1124887Schin 1134887Schin #if _lib_poll 1144887Schin 1154887Schin struct pollfd pfd; 1164887Schin int t; 1174887Schin 1184887Schin if ((t = (n + 999999L) / 1000000L) > 0) 1194887Schin { 1204887Schin poll(&pfd, 0, t); 1214887Schin s -= t / 1000L; 1224887Schin n = 0; 1234887Schin } 1244887Schin 1254887Schin #endif 1264887Schin 1274887Schin #endif 1284887Schin 1294887Schin if ((s += (n + 999999999L) / 1000000000L) && (s = sleep(s))) 1304887Schin { 1314887Schin if (rv) 1324887Schin { 1334887Schin rv->tv_sec = s; 1344887Schin rv->tv_nsec = 0; 1354887Schin } 1364887Schin return -1; 1374887Schin } 1384887Schin return 0; 1394887Schin 1404887Schin #endif 1414887Schin 1424887Schin #endif 1434887Schin 1444887Schin } 145