17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright 1996 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* 77c478bd9Sstevel@tonic-gate * Copyright (c) 1985 Regents of the University of California. 87c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 97c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 107c478bd9Sstevel@tonic-gate */ 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #include <unistd.h> 137c478bd9Sstevel@tonic-gate #include <sys/time.h> 147c478bd9Sstevel@tonic-gate #include <signal.h> 157c478bd9Sstevel@tonic-gate 167c478bd9Sstevel@tonic-gate #define USPS 1000000 /* number of microseconds in a second */ 177c478bd9Sstevel@tonic-gate #define TICK (USPS / _sysconf(_SC_CLK_TCK)) 187c478bd9Sstevel@tonic-gate 197c478bd9Sstevel@tonic-gate #define setvec(vec, a) \ 207c478bd9Sstevel@tonic-gate vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate static int ringring; 237c478bd9Sstevel@tonic-gate 24*8682d1efSRichard Lowe static void 25*8682d1efSRichard Lowe sleepx(void) 26*8682d1efSRichard Lowe { 27*8682d1efSRichard Lowe 28*8682d1efSRichard Lowe ringring = 1; 29*8682d1efSRichard Lowe } 30*8682d1efSRichard Lowe 315d54f3d8Smuffin void 325d54f3d8Smuffin usleep(unsigned n) 337c478bd9Sstevel@tonic-gate { 347c478bd9Sstevel@tonic-gate int omask; 357c478bd9Sstevel@tonic-gate struct itimerval itv, oitv; 365d54f3d8Smuffin struct itimerval *itp = &itv; 377c478bd9Sstevel@tonic-gate struct sigvec vec, ovec; 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate if (n == 0) 407c478bd9Sstevel@tonic-gate return; 417c478bd9Sstevel@tonic-gate timerclear(&itp->it_interval); 427c478bd9Sstevel@tonic-gate timerclear(&itp->it_value); 437c478bd9Sstevel@tonic-gate if (setitimer(ITIMER_REAL, itp, &oitv) < 0) 447c478bd9Sstevel@tonic-gate return; 457c478bd9Sstevel@tonic-gate itp->it_value.tv_sec = n / USPS; 467c478bd9Sstevel@tonic-gate itp->it_value.tv_usec = n % USPS; 477c478bd9Sstevel@tonic-gate if (timerisset(&oitv.it_value)) { 487c478bd9Sstevel@tonic-gate if (timercmp(&oitv.it_value, &itp->it_value, >)) { 497c478bd9Sstevel@tonic-gate oitv.it_value.tv_sec -= itp->it_value.tv_sec; 507c478bd9Sstevel@tonic-gate oitv.it_value.tv_usec -= itp->it_value.tv_usec; 517c478bd9Sstevel@tonic-gate if (oitv.it_value.tv_usec < 0) { 527c478bd9Sstevel@tonic-gate oitv.it_value.tv_usec += USPS; 537c478bd9Sstevel@tonic-gate oitv.it_value.tv_sec--; 547c478bd9Sstevel@tonic-gate } 557c478bd9Sstevel@tonic-gate } else { 567c478bd9Sstevel@tonic-gate itp->it_value = oitv.it_value; 577c478bd9Sstevel@tonic-gate oitv.it_value.tv_sec = 0; 587c478bd9Sstevel@tonic-gate oitv.it_value.tv_usec = 2 * TICK; 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate setvec(vec, sleepx); 627c478bd9Sstevel@tonic-gate (void) sigvec(SIGALRM, &vec, &ovec); 637c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGALRM)); 647c478bd9Sstevel@tonic-gate ringring = 0; 657c478bd9Sstevel@tonic-gate (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0); 667c478bd9Sstevel@tonic-gate while (!ringring) 677c478bd9Sstevel@tonic-gate sigpause(omask &~ sigmask(SIGALRM)); 687c478bd9Sstevel@tonic-gate (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0); 697c478bd9Sstevel@tonic-gate (void) sigsetmask(omask); 707c478bd9Sstevel@tonic-gate (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0); 717c478bd9Sstevel@tonic-gate } 72