Back to index

glibc  2.9
getitimer.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stddef.h>
00020 #include <errno.h>
00021 #include <sys/time.h>
00022 #include <hurd.h>
00023 
00024 /* XXX Temporary cheezoid implementation; see __setitmr.c.  */
00025 
00026 /* These are defined in __setitmr.c.  */
00027 extern spin_lock_t _hurd_itimer_lock;
00028 extern struct itimerval _hurd_itimerval;
00029 extern struct timeval _hurd_itimer_started;
00030 
00031 static inline void
00032 subtract_timeval (struct timeval *from, const struct timeval *subtract)
00033 {
00034   from->tv_usec -= subtract->tv_usec;
00035   from->tv_sec -= subtract->tv_sec;
00036   while (from->tv_usec < 0)
00037     {
00038       --from->tv_sec;
00039       from->tv_usec += 1000000;
00040     }
00041 }
00042 
00043 /* Set *VALUE to the current setting of timer WHICH.
00044    Return 0 on success, -1 on errors.  */
00045 int
00046 __getitimer (which, value)
00047      enum __itimer_which which;
00048      struct itimerval *value;
00049 {
00050   struct itimerval val;
00051   struct timeval elapsed;
00052 
00053   switch (which)
00054     {
00055     default:
00056       return __hurd_fail (EINVAL);
00057 
00058     case ITIMER_VIRTUAL:
00059     case ITIMER_PROF:
00060       return __hurd_fail (ENOSYS);
00061 
00062     case ITIMER_REAL:
00063       break;
00064     }
00065 
00066   /* Get the time now.  */
00067   if (__gettimeofday (&elapsed, NULL) < 0)
00068     return -1;
00069 
00070   /* Extract the current timer setting; and the time it was set, so we can
00071      calculate the time elapsed so far.  */
00072   HURD_CRITICAL_BEGIN;
00073   __spin_lock (&_hurd_itimer_lock);
00074   val = _hurd_itimerval;
00075   subtract_timeval (&elapsed, &_hurd_itimer_started);
00076   __spin_unlock (&_hurd_itimer_lock);
00077   HURD_CRITICAL_END;
00078 
00079   if ((val.it_value.tv_sec | val.it_value.tv_usec) != 0)
00080     {
00081       /* There is a pending alarm set.  VAL indicates the interval it was
00082         set for, relative to the time recorded in _hurd_itimer_started.
00083         Now compensate for the time elapsed since to get the user's
00084         conception of the current value of the timer (as if the value
00085         stored decreased every microsecond).  */
00086       if (timercmp (&val.it_value, &elapsed, <))
00087        {
00088          /* Hmm.  The timer should have just gone off, but has not been
00089             reset.  This is a possible timing glitch.  The alarm will signal
00090             soon, so fabricate a value for how soon.  */
00091          val.it_value.tv_sec = 0;
00092          val.it_value.tv_usec = 10; /* Random.  */
00093        }
00094       else
00095        /* Subtract the time elapsed since the timer was set
00096           from the current timer value the user sees.  */
00097        subtract_timeval (&val.it_value, &elapsed);
00098     }
00099 
00100   *value = val;
00101   return 0;
00102 }
00103 
00104 weak_alias (__getitimer, getitimer)