source: trunk/libs/newlib/src/newlib/libc/sys/linux/clock_settime.c

Last change on this file was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.9 KB
Line 
1/* Copyright (C) 1999, 2000, 2001 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 <errno.h>
20#include <time.h>
21#include <sys/time.h>
22#include <libc-internal.h>
23
24
25#if HP_TIMING_AVAIL
26/* Clock frequency of the processor.  We make it a 64-bit variable
27   because some jokers are already playing with processors with more
28   than 4GHz.  */
29static hp_timing_t freq;
30
31
32/* We need the starting time for the process.  */
33extern hp_timing_t _dl_cpuclock_offset;
34
35
36/* This function is defined in the thread library.  */
37extern void __pthread_clock_settime (hp_timing_t offset)
38     __attribute__ ((__weak__));
39#endif
40
41
42/* Set CLOCK to value TP.  */
43int
44clock_settime (clockid_t clock_id, const struct timespec *tp)
45{
46  struct timeval tv;
47  int retval;
48
49  /* Make sure the time cvalue is OK.  */
50  if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
51    {
52      __set_errno (EINVAL);
53      return -1;
54    }
55
56  switch (clock_id)
57    {
58    case CLOCK_REALTIME:
59      TIMESPEC_TO_TIMEVAL (&tv, tp);
60
61      retval = settimeofday (&tv, NULL);
62      break;
63
64#if HP_TIMING_AVAIL
65    case CLOCK_PROCESS_CPUTIME_ID:
66    case CLOCK_THREAD_CPUTIME_ID:
67      {
68        hp_timing_t tsc;
69        hp_timing_t usertime;
70
71        /* First thing is to get the current time.  */
72        HP_TIMING_NOW (tsc);
73
74        if (__builtin_expect (freq == 0, 0))
75          {
76            /* This can only happen if we haven't initialized the `freq'
77               variable yet.  Do this now. We don't have to protect this
78               code against multiple execution since all of them should
79               lead to the same result.  */
80            freq = __get_clockfreq ();
81            if (__builtin_expect (freq == 0, 0))
82              {
83                /* Something went wrong.  */
84                retval = -1;
85                break;
86              }
87          }
88
89        /* Convert the user-provided time into CPU ticks.  */
90        usertime = tp->tv_sec * freq + (tp->tv_nsec * freq) / 1000000000ull;
91
92        /* Determine the offset and use it as the new base value.  */
93        if (clock_id != CLOCK_THREAD_CPUTIME_ID
94            || __pthread_clock_settime == NULL)
95          _dl_cpuclock_offset = tsc - usertime;
96        else
97          __pthread_clock_settime (tsc - usertime);
98
99        retval = 0;
100      }
101      break;
102#endif
103
104    default:
105      __set_errno (EINVAL);
106      retval = -1;
107      break;
108    }
109
110  return retval;
111}
Note: See TracBrowser for help on using the repository browser.