1 /* Computing deadlines for timeouts.
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <net-internal.h>
27 struct deadline_current_time internal_function
28 __deadline_current_time (void)
30 struct deadline_current_time result
;
31 if (__clock_gettime (CLOCK_MONOTONIC
, &result
.current
) != 0)
33 struct timeval current_tv
;
34 if (__gettimeofday (¤t_tv
, NULL
) == 0)
35 __libc_fatal ("Fatal error: gettimeofday system call failed\n");
36 result
.current
.tv_sec
= current_tv
.tv_sec
;
37 result
.current
.tv_nsec
= current_tv
.tv_usec
* 1000;
39 assert (result
.current
.tv_sec
>= 0);
43 /* A special deadline value for which __deadline_is_infinite is
45 static inline struct deadline
46 infinite_deadline (void)
48 return (struct deadline
) { { -1, -1 } };
51 struct deadline internal_function
52 __deadline_from_timeval (struct deadline_current_time current
,
55 assert (__is_timeval_valid_timeout (tv
));
57 /* Compute second-based deadline. Perform the addition in
58 uintmax_t, which is unsigned, to simply overflow detection. */
59 uintmax_t sec
= current
.current
.tv_sec
;
61 if (sec
< (uintmax_t) tv
.tv_sec
)
62 return infinite_deadline ();
64 /* Compute nanosecond deadline. */
65 int nsec
= current
.current
.tv_nsec
+ tv
.tv_usec
* 1000;
66 if (nsec
>= 1000 * 1000 * 1000)
68 /* Carry nanosecond overflow to seconds. */
69 nsec
-= 1000 * 1000 * 1000;
71 return infinite_deadline ();
74 /* This uses a GCC extension, otherwise these casts for detecting
75 overflow would not be defined. */
76 if ((time_t) sec
< 0 || sec
!= (uintmax_t) (time_t) sec
)
77 return infinite_deadline ();
79 return (struct deadline
) { { sec
, nsec
} };
83 __deadline_to_ms (struct deadline_current_time current
,
84 struct deadline deadline
)
86 if (__deadline_is_infinite (deadline
))
89 if (current
.current
.tv_sec
> deadline
.absolute
.tv_sec
90 || (current
.current
.tv_sec
== deadline
.absolute
.tv_sec
91 && current
.current
.tv_nsec
>= deadline
.absolute
.tv_nsec
))
93 time_t sec
= deadline
.absolute
.tv_sec
- current
.current
.tv_sec
;
95 /* This value will overflow below. */
97 int nsec
= deadline
.absolute
.tv_nsec
- current
.current
.tv_nsec
;
100 /* Borrow from the seconds field. */
103 nsec
+= 1000 * 1000 * 1000;
106 /* Prepare for rounding up to milliseconds. */
108 if (nsec
> 1000 * 1000 * 1000)
110 assert (sec
< INT_MAX
);
112 nsec
-= 1000 * 1000 * 1000;
115 unsigned int msec
= nsec
/ (1000 * 1000);
116 if (sec
> INT_MAX
/ 1000)