chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / mach / hurd / getitimer.c
1 /* Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <stddef.h>
20 #include <errno.h>
21 #include <sys/time.h>
22 #include <hurd.h>
23
24 /* XXX Temporary cheezoid implementation; see __setitmr.c.  */
25
26 /* These are defined in __setitmr.c.  */
27 extern spin_lock_t _hurd_itimer_lock;
28 extern struct itimerval _hurd_itimerval;
29 extern struct timeval _hurd_itimer_started;
30
31 static inline void
32 subtract_timeval (struct timeval *from, const struct timeval *subtract)
33 {
34   from->tv_usec -= subtract->tv_usec;
35   from->tv_sec -= subtract->tv_sec;
36   while (from->tv_usec < 0)
37     {
38       --from->tv_sec;
39       from->tv_usec += 1000000;
40     }
41 }
42
43 /* Set *VALUE to the current setting of timer WHICH.
44    Return 0 on success, -1 on errors.  */
45 int
46 __getitimer (which, value)
47      enum __itimer_which which;
48      struct itimerval *value;
49 {
50   struct itimerval val;
51   struct timeval elapsed;
52
53   switch (which)
54     {
55     default:
56       return __hurd_fail (EINVAL);
57
58     case ITIMER_VIRTUAL:
59     case ITIMER_PROF:
60       return __hurd_fail (ENOSYS);
61
62     case ITIMER_REAL:
63       break;
64     }
65
66   /* Get the time now.  */
67   if (__gettimeofday (&elapsed, NULL) < 0)
68     return -1;
69
70   /* Extract the current timer setting; and the time it was set, so we can
71      calculate the time elapsed so far.  */
72   HURD_CRITICAL_BEGIN;
73   __spin_lock (&_hurd_itimer_lock);
74   val = _hurd_itimerval;
75   subtract_timeval (&elapsed, &_hurd_itimer_started);
76   __spin_unlock (&_hurd_itimer_lock);
77   HURD_CRITICAL_END;
78
79   if ((val.it_value.tv_sec | val.it_value.tv_usec) != 0)
80     {
81       /* There is a pending alarm set.  VAL indicates the interval it was
82          set for, relative to the time recorded in _hurd_itimer_started.
83          Now compensate for the time elapsed since to get the user's
84          conception of the current value of the timer (as if the value
85          stored decreased every microsecond).  */
86       if (timercmp (&val.it_value, &elapsed, <))
87         {
88           /* Hmm.  The timer should have just gone off, but has not been
89              reset.  This is a possible timing glitch.  The alarm will signal
90              soon, so fabricate a value for how soon.  */
91           val.it_value.tv_sec = 0;
92           val.it_value.tv_usec = 10; /* Random.  */
93         }
94       else
95         /* Subtract the time elapsed since the timer was set
96            from the current timer value the user sees.  */
97         subtract_timeval (&val.it_value, &elapsed);
98     }
99
100   *value = val;
101   return 0;
102 }
103
104 weak_alias (__getitimer, getitimer)