1 | #ifndef _LINUX_TIME_H
|
---|
2 | #define _LINUX_TIME_H
|
---|
3 |
|
---|
4 | #include <asm/param.h>
|
---|
5 | #include <linux/types.h>
|
---|
6 | #include <linux/ktime.h>
|
---|
7 | #include <linux/time64.h>
|
---|
8 | #include <linux/math64.h>
|
---|
9 |
|
---|
10 | #define NSEC_PER_SEC 1000000000L
|
---|
11 |
|
---|
12 |
|
---|
13 | #ifndef _STRUCT_TIMESPEC
|
---|
14 | #define _STRUCT_TIMESPEC
|
---|
15 | struct timespec {
|
---|
16 | time_t tv_sec; /* seconds */
|
---|
17 | long tv_nsec; /* nanoseconds */
|
---|
18 | };
|
---|
19 | #endif /* _STRUCT_TIMESPEC */
|
---|
20 |
|
---|
21 | static inline int timespec_equal(const struct timespec *a,
|
---|
22 | const struct timespec *b)
|
---|
23 | {
|
---|
24 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
|
---|
25 | }
|
---|
26 |
|
---|
27 | /*
|
---|
28 | * Change timeval to jiffies, trying to avoid the
|
---|
29 | * most obvious overflows..
|
---|
30 | *
|
---|
31 | * And some not so obvious.
|
---|
32 | *
|
---|
33 | * Note that we don't want to return MAX_LONG, because
|
---|
34 | * for various timeout reasons we often end up having
|
---|
35 | * to wait "jiffies+1" in order to guarantee that we wait
|
---|
36 | * at _least_ "jiffies" - so "jiffies+1" had better still
|
---|
37 | * be positive.
|
---|
38 | */
|
---|
39 | #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
|
---|
40 |
|
---|
41 | static __inline__ unsigned long
|
---|
42 | timespec_to_jiffies(struct timespec *value)
|
---|
43 | {
|
---|
44 | unsigned long sec = value->tv_sec;
|
---|
45 | long nsec = value->tv_nsec;
|
---|
46 |
|
---|
47 | if (sec >= (MAX_JIFFY_OFFSET / HZ))
|
---|
48 | return MAX_JIFFY_OFFSET;
|
---|
49 | nsec += 1000000000L / HZ - 1;
|
---|
50 | nsec /= 1000000000L / HZ;
|
---|
51 | return HZ * sec + nsec;
|
---|
52 | }
|
---|
53 |
|
---|
54 | static __inline__ void
|
---|
55 | jiffies_to_timespec(unsigned long jiffies, struct timespec *value)
|
---|
56 | {
|
---|
57 | value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
|
---|
58 | value->tv_sec = jiffies / HZ;
|
---|
59 | }
|
---|
60 |
|
---|
61 | struct timeval {
|
---|
62 | time_t tv_sec; /* seconds */
|
---|
63 | suseconds_t tv_usec; /* microseconds */
|
---|
64 | };
|
---|
65 |
|
---|
66 | struct timezone {
|
---|
67 | int tz_minuteswest; /* minutes west of Greenwich */
|
---|
68 | int tz_dsttime; /* type of dst correction */
|
---|
69 | };
|
---|
70 |
|
---|
71 | #define NFDBITS __NFDBITS
|
---|
72 |
|
---|
73 | #ifdef __KERNEL__
|
---|
74 | extern void do_gettimeofday(struct timeval *tv);
|
---|
75 | extern void do_settimeofday(struct timeval *tv);
|
---|
76 | extern void get_fast_time(struct timeval *tv);
|
---|
77 | extern void (*do_get_fast_time)(struct timeval *);
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | #define FD_SETSIZE __FD_SETSIZE
|
---|
81 | #define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp)
|
---|
82 | #define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp)
|
---|
83 | #define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp)
|
---|
84 | #define FD_ZERO(fdsetp) __FD_ZERO(fdsetp)
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Names of the interval timers, and structure
|
---|
88 | * defining a timer setting.
|
---|
89 | */
|
---|
90 | #define ITIMER_REAL 0
|
---|
91 | #define ITIMER_VIRTUAL 1
|
---|
92 | #define ITIMER_PROF 2
|
---|
93 |
|
---|
94 | struct itimerspec {
|
---|
95 | struct timespec it_interval; /* timer period */
|
---|
96 | struct timespec it_value; /* timer expiration */
|
---|
97 | };
|
---|
98 |
|
---|
99 | struct itimerval {
|
---|
100 | struct timeval it_interval; /* timer interval */
|
---|
101 | struct timeval it_value; /* current value */
|
---|
102 | };
|
---|
103 |
|
---|
104 | /* msecs_to_jiffies */
|
---|
105 | #ifndef CONFIG_HAVE_MSECS_TO_JIFFIES
|
---|
106 | static __inline__ unsigned int jiffies_to_msecs(const unsigned long j)
|
---|
107 | {
|
---|
108 | #if (HZ <= 1000 && !(1000 % HZ))
|
---|
109 | return (1000 / HZ) * j;
|
---|
110 | #elif (HZ > 1000 && !(HZ % 1000))
|
---|
111 | return (j + (HZ / 1000) - 1)/(HZ / 1000);
|
---|
112 | #else
|
---|
113 | return (j * 1000) / HZ;
|
---|
114 | #endif
|
---|
115 | }
|
---|
116 | static __inline__ unsigned long msecs_to_jiffies(const unsigned int m)
|
---|
117 | {
|
---|
118 | if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
|
---|
119 | return MAX_JIFFY_OFFSET;
|
---|
120 | #if (HZ <= 1000 && !(1000 % HZ))
|
---|
121 | return (m + (1000 / HZ) - 1) / (1000 / HZ);
|
---|
122 | #elif (HZ > 1000 && !(HZ % 1000))
|
---|
123 | return m * (HZ / 1000);
|
---|
124 | #else
|
---|
125 | return (m * HZ + 999) / 1000;
|
---|
126 | #endif
|
---|
127 | }
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /* wrapper for getnstimeofday()
|
---|
131 | * it's needed for recent 2.6 kernels, too, due to lack of EXPORT_SYMBOL
|
---|
132 | */
|
---|
133 | #define getnstimeofday(x) do { \
|
---|
134 | struct timeval __x; \
|
---|
135 | do_gettimeofday(&__x); \
|
---|
136 | (x)->tv_sec = __x.tv_sec; \
|
---|
137 | (x)->tv_nsec = __x.tv_usec * 1000; \
|
---|
138 | } while (0)
|
---|
139 |
|
---|
140 | #define do_posix_clock_monotonic_gettime getnstimeofday
|
---|
141 |
|
---|
142 | void msleep(unsigned int msecs);
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * ns_to_timespec - Convert nanoseconds to timespec
|
---|
146 | * @nsec: the nanoseconds value to be converted
|
---|
147 | *
|
---|
148 | * Returns the timespec representation of the nsec parameter.
|
---|
149 | */
|
---|
150 | extern struct timespec ns_to_timespec(const s64 nsec);
|
---|
151 | #define getrawmonotonic(ts) do_posix_clock_monotonic_gettime(ts)
|
---|
152 | #endif
|
---|