xref: /aosp_15_r20/external/mesa3d/src/util/os_time.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2008-2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 /**
29  * @file
30  * OS independent time-manipulation functions.
31  *
32  * @author Jose Fonseca <[email protected]>
33  */
34 
35 #include "os_time.h"
36 #include "detect_os.h"
37 
38 #include "c11/time.h"
39 
40 #include "util/u_atomic.h"
41 
42 #if DETECT_OS_POSIX_LITE
43 #  include <unistd.h> /* usleep */
44 #  include <time.h> /* timeval */
45 #  include <sys/time.h> /* timeval */
46 #  include <sched.h> /* sched_yield */
47 #  include <errno.h>
48 #elif DETECT_OS_WINDOWS
49 #  include <windows.h>
50 #else
51 #  error Unsupported OS
52 #endif
53 
54 
55 int64_t
os_time_get_nano(void)56 os_time_get_nano(void)
57 {
58    struct timespec ts;
59    timespec_get(&ts, TIME_MONOTONIC);
60    return ts.tv_nsec + ts.tv_sec*INT64_C(1000000000);
61 }
62 
63 
64 
65 void
os_time_sleep(int64_t usecs)66 os_time_sleep(int64_t usecs)
67 {
68 #if DETECT_OS_LINUX || DETECT_OS_MANAGARM || DETECT_OS_FUCHSIA
69    struct timespec time;
70    time.tv_sec = usecs / 1000000;
71    time.tv_nsec = (usecs % 1000000) * 1000;
72    while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR);
73 
74 #elif DETECT_OS_POSIX
75    usleep(usecs);
76 
77 #elif DETECT_OS_WINDOWS
78    DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000);
79    /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */
80    if (dwMilliseconds) {
81       Sleep(dwMilliseconds);
82    }
83 #else
84 #  error Unsupported OS
85 #endif
86 }
87 
88 
89 
90 int64_t
os_time_get_absolute_timeout(uint64_t timeout)91 os_time_get_absolute_timeout(uint64_t timeout)
92 {
93    int64_t time, abs_timeout;
94 
95    /* Also check for the type upper bound. */
96    if (timeout == OS_TIMEOUT_INFINITE || timeout > INT64_MAX)
97       return OS_TIMEOUT_INFINITE;
98 
99    time = os_time_get_nano();
100 
101    /* Do the addition in unsigned, because signed overflow is UB, then convert to signed again. */
102    abs_timeout = (uint64_t)time + (uint64_t)timeout;
103 
104    /* Check for overflow. */
105    if (abs_timeout < time)
106       return OS_TIMEOUT_INFINITE;
107 
108    return abs_timeout;
109 }
110 
111 
112 bool
os_wait_until_zero(volatile int * var,uint64_t timeout)113 os_wait_until_zero(volatile int *var, uint64_t timeout)
114 {
115    if (!p_atomic_read(var))
116       return true;
117 
118    if (!timeout)
119       return false;
120 
121    if (timeout == OS_TIMEOUT_INFINITE) {
122       while (p_atomic_read(var)) {
123 #if DETECT_OS_POSIX_LITE
124          sched_yield();
125 #endif
126       }
127       return true;
128    }
129    else {
130       int64_t start_time = os_time_get_nano();
131       int64_t end_time = start_time + timeout;
132 
133       while (p_atomic_read(var)) {
134          if (os_time_timeout(start_time, end_time, os_time_get_nano()))
135             return false;
136 
137 #if DETECT_OS_POSIX_LITE
138          sched_yield();
139 #endif
140       }
141       return true;
142    }
143 }
144 
145 
146 bool
os_wait_until_zero_abs_timeout(volatile int * var,int64_t timeout)147 os_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout)
148 {
149    if (!p_atomic_read(var))
150       return true;
151 
152    if (timeout == OS_TIMEOUT_INFINITE)
153       return os_wait_until_zero(var, OS_TIMEOUT_INFINITE);
154 
155    while (p_atomic_read(var)) {
156       if (os_time_get_nano() >= timeout)
157          return false;
158 
159 #if DETECT_OS_POSIX_LITE
160       sched_yield();
161 #endif
162    }
163    return true;
164 }
165