Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 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 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 29 | #pragma once |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 30 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 31 | #include <errno.h> |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #include <sys/cdefs.h> |
| 34 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 35 | #include "private/bionic_constants.h" |
| 36 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 37 | bool timespec_from_timeval(timespec& ts, const timeval& tv); |
| 38 | void timespec_from_ms(timespec& ts, const int ms); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 40 | void timeval_from_timespec(timeval& tv, const timespec& ts); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 42 | void monotonic_time_from_realtime_time(timespec& monotonic_time, const timespec& realtime_time); |
| 43 | void realtime_time_from_monotonic_time(timespec& realtime_time, const timespec& monotonic_time); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 45 | static inline int64_t to_ns(const timespec& ts) { |
| 46 | return ts.tv_sec * NS_PER_S + ts.tv_nsec; |
| 47 | } |
Tom Cherry | ac49ced | 2017-08-17 13:18:52 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 49 | static inline int64_t to_us(const timeval& tv) { |
| 50 | return tv.tv_sec * US_PER_S + tv.tv_usec; |
| 51 | } |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 53 | static inline int check_timespec(const timespec* ts, bool null_allowed) { |
| 54 | if (null_allowed && ts == nullptr) { |
| 55 | return 0; |
| 56 | } |
| 57 | // glibc just segfaults if you pass a null timespec. |
| 58 | // That seems a lot more likely to catch bad code than returning EINVAL. |
| 59 | if (ts->tv_nsec < 0 || ts->tv_nsec >= NS_PER_S) { |
| 60 | return EINVAL; |
| 61 | } |
| 62 | if (ts->tv_sec < 0) { |
| 63 | return ETIMEDOUT; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
Elliott Hughes | 5db4b6e | 2016-04-04 16:15:37 -0700 | [diff] [blame] | 68 | #if !defined(__LP64__) |
| 69 | static inline void absolute_timespec_from_timespec(timespec& abs_ts, const timespec& ts, clockid_t clock) { |
| 70 | clock_gettime(clock, &abs_ts); |
| 71 | abs_ts.tv_sec += ts.tv_sec; |
| 72 | abs_ts.tv_nsec += ts.tv_nsec; |
| 73 | if (abs_ts.tv_nsec >= NS_PER_S) { |
| 74 | abs_ts.tv_nsec -= NS_PER_S; |
| 75 | abs_ts.tv_sec++; |
| 76 | } |
| 77 | } |
| 78 | #endif |