Back to index

glibc  2.9
clock_nanosleep.c
Go to the documentation of this file.
00001 /* High-resolution sleep with the specified clock.
00002    Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <time.h>
00023 #include <hp-timing.h>
00024 #include <sysdep-cancel.h>
00025 
00026 #if HP_TIMING_AVAIL
00027 # define CPUCLOCK_P(clock) \
00028   ((clock) == CLOCK_PROCESS_CPUTIME_ID                                      \
00029    || ((clock) & ((1 << CLOCK_IDFIELD_SIZE) - 1)) == CLOCK_THREAD_CPUTIME_ID)
00030 #else
00031 # define CPUCLOCK_P(clock) 0
00032 #endif
00033 
00034 #ifndef INVALID_CLOCK_P
00035 # define INVALID_CLOCK_P(cl) \
00036   ((cl) < CLOCK_REALTIME || (cl) > CLOCK_THREAD_CPUTIME_ID)
00037 #endif
00038 
00039 
00040 /* This implementation assumes that these is only a `nanosleep' system
00041    call.  So we have to remap all other activities.  */
00042 int
00043 clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
00044                struct timespec *rem)
00045 {
00046   struct timespec now;
00047 
00048   if (__builtin_expect (req->tv_nsec, 0) < 0
00049       || __builtin_expect (req->tv_nsec, 0) >= 1000000000)
00050     return EINVAL;
00051 
00052   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
00053     return EINVAL;          /* POSIX specifies EINVAL for this case.  */
00054 
00055 #ifdef SYSDEP_NANOSLEEP
00056   SYSDEP_NANOSLEEP;
00057 #endif
00058 
00059   if (CPUCLOCK_P (clock_id))
00060     return ENOTSUP;
00061 
00062   if (INVALID_CLOCK_P (clock_id))
00063     return EINVAL;
00064 
00065   /* If we got an absolute time, remap it.  */
00066   if (flags == TIMER_ABSTIME)
00067     {
00068       long int nsec;
00069       long int sec;
00070 
00071       /* Make sure we use safe data types.  */
00072       assert (sizeof (sec) >= sizeof (now.tv_sec));
00073 
00074       /* Get the current time for this clock.  */
00075       if (__builtin_expect (clock_gettime (clock_id, &now), 0) != 0)
00076        return errno;
00077 
00078       /* Compute the difference.  */
00079       nsec = req->tv_nsec - now.tv_nsec;
00080       sec = req->tv_sec - now.tv_sec - (nsec < 0);
00081       if (sec < 0)
00082        /* The time has already elapsed.  */
00083        return 0;
00084 
00085       now.tv_sec = sec;
00086       now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0);
00087 
00088       /* From now on this is our time.  */
00089       req = &now;
00090 
00091       /* Make sure we are not modifying the struct pointed to by REM.  */
00092       rem = NULL;
00093     }
00094   else if (__builtin_expect (flags, 0) != 0)
00095     return EINVAL;
00096   else if (clock_id != CLOCK_REALTIME)
00097     /* Not supported.  */
00098     return ENOTSUP;
00099 
00100   return __builtin_expect (nanosleep (req, rem), 0) ? errno : 0;
00101 }