source: rtems-libbsd/rtemsbsd/powerpc/include/linux/time.h @ 12a885c

55-freebsd-126-freebsd-12
Last change on this file since 12a885c was 12a885c, checked in by Sebastian Huber <sebastian.huber@…>, on 08/24/18 at 06:55:00

Update due to <sys/time.h> API changes

Changes correspond to FreeBSD commit:

"Make timespecadd(3) and friends public

The timespecadd(3) family of macros were imported from NetBSD back in
r35029. However, they were initially guarded by #ifdef _KERNEL. In the
meantime, we have grown at least 28 syscalls that use timespecs in some
way, leading many programs both inside and outside of the base system to
redefine those macros. It's better just to make the definitions public.

Our kernel currently defines two-argument versions of timespecadd and
timespecsub. NetBSD, OpenBSD, and FreeDesktop?.org's libbsd, however, define
three-argument versions. Solaris also defines a three-argument version, but
only in its kernel. This revision changes our definition to match the
common three-argument version.

Bump _FreeBSD_version due to the breaking KPI change.

Discussed with: cem, jilles, ian, bde
Differential Revision: https://reviews.freebsd.org/D14725"

Update #3472.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*-
2 * Copyright (c) 2014-2015 François Tigeot
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28#ifndef _LINUX_TIME_H_
29#define _LINUX_TIME_H_
30
31#define NSEC_PER_USEC   1000L
32#define NSEC_PER_SEC    1000000000L
33
34#include <sys/time.h>
35#include <sys/stdint.h>
36
37static inline struct timeval
38ns_to_timeval(const int64_t nsec)
39{
40        struct timeval tv;
41        long rem;
42
43        if (nsec == 0) {
44                tv.tv_sec = 0;
45                tv.tv_usec = 0;
46                return (tv);
47        }
48
49        tv.tv_sec = nsec / NSEC_PER_SEC;
50        rem = nsec % NSEC_PER_SEC;
51        if (rem < 0) {
52                tv.tv_sec--;
53                rem += NSEC_PER_SEC;
54        }
55        tv.tv_usec = rem / 1000;
56        return (tv);
57}
58
59static inline int64_t
60timeval_to_ns(const struct timeval *tv)
61{
62        return ((int64_t)tv->tv_sec * NSEC_PER_SEC) +
63                tv->tv_usec * NSEC_PER_USEC;
64}
65
66#define getrawmonotonic(ts)     nanouptime(ts)
67
68static inline struct timespec
69timespec_sub(struct timespec lhs, struct timespec rhs)
70{
71        struct timespec ts;
72
73        timespecsub(&lhs, &rhs, &ts);
74
75        return ts;
76}
77
78static inline void
79set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
80{
81        /* XXX: this doesn't actually normalize anything */
82        ts->tv_sec = sec;
83        ts->tv_nsec = nsec;
84}
85
86static inline int64_t
87timespec_to_ns(const struct timespec *ts)
88{
89        return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec);
90}
91
92static inline struct timespec
93ns_to_timespec(const int64_t nsec)
94{
95        struct timespec ts;
96        int32_t rem;
97
98        if (nsec == 0) {
99                ts.tv_sec = 0;
100                ts.tv_nsec = 0;
101                return (ts);
102        }
103
104        ts.tv_sec = nsec / NSEC_PER_SEC;
105        rem = nsec % NSEC_PER_SEC;
106        if (rem < 0) {
107                ts.tv_sec--;
108                rem += NSEC_PER_SEC;
109        }
110        ts.tv_nsec = rem;
111        return (ts);
112}
113
114static inline int
115timespec_valid(const struct timespec *ts)
116{
117        if (ts->tv_sec < 0 || ts->tv_sec > 100000000 ||
118            ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
119                return (0);
120        return (1);
121}
122
123static inline unsigned long
124get_seconds(void)
125{
126        return time_uptime;
127}
128
129#endif /* _LINUX_TIME_H_ */
Note: See TracBrowser for help on using the repository browser.