home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / library / rcs / kern_time.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  5.5 KB  |  225 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ *  @;
  7.  
  8.  
  9. 1.1
  10. date    92.05.14.19.55.40;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @date related stuff
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  *  This file is part of ixemul.library for the Amiga.
  27.  *  Copyright (C) 1991, 1992  Markus M. Wild
  28.  *
  29.  *  This library is free software; you can redistribute it and/or
  30.  *  modify it under the terms of the GNU Library General Public
  31.  *  License as published by the Free Software Foundation; either
  32.  *  version 2 of the License, or (at your option) any later version.
  33.  *
  34.  *  This library is distributed in the hope that it will be useful,
  35.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  37.  *  Library General Public License for more details.
  38.  *
  39.  *  You should have received a copy of the GNU Library General Public
  40.  *  License along with this library; if not, write to the Free
  41.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42.  *
  43.  *  $Id$
  44.  *
  45.  *  $Log$
  46.  *
  47.  *  Since the code originated from Berkeley, the following copyright
  48.  *  header applies as well. The code has been changed, it's not the
  49.  *  original Berkeley code!
  50.  */
  51.  
  52. /*
  53.  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  54.  * All rights reserved.
  55.  *
  56.  * Redistribution is only permitted until one year after the first shipment
  57.  * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
  58.  * binary forms are permitted provided that: (1) source distributions retain
  59.  * this entire copyright notice and comment, and (2) distributions including
  60.  * binaries display the following acknowledgement:  This product includes
  61.  * software developed by the University of California, Berkeley and its
  62.  * contributors'' in the documentation or other materials provided with the
  63.  * distribution and in all advertising materials mentioning features or use
  64.  * of this software.  Neither the name of the University nor the names of
  65.  * its contributors may be used to endorse or promote products derived from
  66.  * this software without specific prior written permission.
  67.  * THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  68.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  69.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  70.  *
  71.  *    @@(#)kern_time.c    7.13 (Berkeley) 6/28/90
  72.  */
  73.  
  74. #define KERNEL
  75. #include "ixemul.h"
  76.  
  77. #include <sys/time.h>
  78.  
  79. #define err_return(code) \
  80. {                    \
  81.   errno = code;                \
  82.   return errno == 0 ? 0 : -1;        \
  83. }
  84.  
  85. int
  86. getitimer(u_int    which, struct itimerval *itv)
  87. {
  88.   if (which > ITIMER_PROF || !itv)
  89.     err_return (EINVAL);
  90.  
  91.   Disable ();
  92.   *itv = u.u_timer[which];
  93.   Enable();
  94.  
  95.   err_return (0);
  96. }
  97.  
  98. int
  99. setitimer(u_int    which, const struct itimerval *itv, struct itimerval *oitv)
  100. {
  101.     if (which > ITIMER_PROF)
  102.         err_return (EINVAL);
  103.  
  104.     if (oitv && getitimer(which, oitv))
  105.         return -1;
  106.  
  107.     if (itv == 0)
  108.         err_return (0);
  109.  
  110.     if (itimerfix(&itv->it_value) || itimerfix(&itv->it_interval))
  111.         err_return (EINVAL);
  112.  
  113.     Disable ();
  114.     u.u_timer[which] = *itv;
  115.     Enable ();
  116.  
  117.     err_return (0);
  118. }
  119.  
  120. /*
  121.  * Check that a proposed value to load into the .it_value or
  122.  * .it_interval part of an interval timer is acceptable, and
  123.  * fix it to have at least minimal value (i.e. if it is less
  124.  * than the resolution of the clock, round it up.)
  125.  */
  126. itimerfix(struct timeval *tv)
  127. {
  128.  
  129.     if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
  130.         tv->tv_usec < 0 || tv->tv_usec >= 1000000)
  131.         err_return (EINVAL);
  132.     if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < ITIMER_RESOLUTION)
  133.         tv->tv_usec = ITIMER_RESOLUTION;
  134.     err_return (0);
  135. }
  136.  
  137. /*
  138.  * Decrement an interval timer by a specified number
  139.  * of microseconds, which must be less than a second,
  140.  * i.e. < 1000000.  If the timer expires, then reload
  141.  * it.  In this case, carry over (usec - old value) to
  142.  * reducint the value reloaded into the timer so that
  143.  * the timer does not drift.  This routine assumes
  144.  * that it is called in a context where the timers
  145.  * on which it is operating cannot change in value.
  146.  */
  147.  
  148. /* on 0 return, send a signal to process */
  149.  
  150. int
  151. itimerdecr (struct itimerval *itp, int usec)    /* CALLED FROM INTERRUPT !! */
  152. {
  153.   if (itp->it_value.tv_usec < usec) 
  154.     {
  155.       if (itp->it_value.tv_sec == 0) 
  156.     {
  157.       /* expired, and already in next interval */
  158.       usec -= itp->it_value.tv_usec;
  159.       goto expire;
  160.     }
  161.  
  162.       itp->it_value.tv_usec += 1000000;
  163.       itp->it_value.tv_sec--;
  164.     }
  165.  
  166.   itp->it_value.tv_usec -= usec;
  167.   usec = 0;
  168.   if (timerisset(&itp->it_value))
  169.     return (1);
  170.  
  171.   /* expired, exactly at end of interval */
  172. expire:
  173.   if (timerisset(&itp->it_interval)) 
  174.     {
  175.       itp->it_value = itp->it_interval;
  176.       itp->it_value.tv_usec -= usec;
  177.       if (itp->it_value.tv_usec < 0) 
  178.         {
  179.       itp->it_value.tv_usec += 1000000;
  180.       itp->it_value.tv_sec--;
  181.     }
  182.     } 
  183.   else
  184.     itp->it_value.tv_usec = 0;        /* sec is already 0 */
  185.  
  186.   return (0);
  187. }
  188.  
  189. /*
  190.  * Add and subtract routines for timevals.
  191.  * N.B.: subtract routine doesn't deal with
  192.  * results which are before the beginning,
  193.  * it just gets very confused in this case.
  194.  * Caveat emptor.
  195.  */
  196. timevaladd(struct timeval *t1, const struct timeval *t2)
  197. {
  198.  
  199.     t1->tv_sec += t2->tv_sec;
  200.     t1->tv_usec += t2->tv_usec;
  201.     timevalfix(t1);
  202. }
  203.  
  204. timevalsub(struct timeval *t1, const struct timeval *t2)
  205. {
  206.  
  207.     t1->tv_sec -= t2->tv_sec;
  208.     t1->tv_usec -= t2->tv_usec;
  209.     timevalfix(t1);
  210. }
  211.  
  212. timevalfix(struct timeval *t1)
  213. {
  214.  
  215.     if (t1->tv_usec < 0) {
  216.         t1->tv_sec--;
  217.         t1->tv_usec += 1000000;
  218.     }
  219.     if (t1->tv_usec >= 1000000) {
  220.         t1->tv_sec++;
  221.         t1->tv_usec -= 1000000;
  222.     }
  223. }
  224. @
  225.