| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ut0ut.ic |
| 29 |
Various utilities |
| 30 |
|
| 31 |
Created 5/30/1994 Heikki Tuuri |
| 32 |
*******************************************************************/ |
| 33 |
|
| 34 |
#include <algorithm> |
| 35 |
|
| 36 |
#include "os0thread.h" |
| 37 |
#include "ut0dbg.h" |
| 38 |
|
| 39 |
#ifndef UNIV_HOTBACKUP |
| 40 |
/** Calculate the minimum of two pairs. |
| 41 |
@param[out] min_hi MSB of the minimum pair |
| 42 |
@param[out] min_lo LSB of the minimum pair |
| 43 |
@param[in] a_hi MSB of the first pair |
| 44 |
@param[in] a_lo LSB of the first pair |
| 45 |
@param[in] b_hi MSB of the second pair |
| 46 |
@param[in] b_lo LSB of the second pair */ |
| 47 |
static inline void ut_pair_min(ulint *min_hi, ulint *min_lo, ulint a_hi, |
| 48 |
ulint a_lo, ulint b_hi, ulint b_lo) { |
| 49 |
if (a_hi == b_hi) { |
| 50 |
*min_hi = a_hi; |
| 51 |
*min_lo = std::min(a_lo, b_lo); |
| 52 |
} else if (a_hi < b_hi) { |
| 53 |
*min_hi = a_hi; |
| 54 |
*min_lo = a_lo; |
| 55 |
} else { |
| 56 |
*min_hi = b_hi; |
| 57 |
*min_lo = b_lo; |
| 58 |
} |
| 59 |
} |
| 60 |
#endif /* !UNIV_HOTBACKUP */ |
| 61 |
|
| 62 |
/** Compares two ulints. |
| 63 |
@return 1 if a > b, 0 if a == b, -1 if a < b */ |
| 64 |
static inline int ut_ulint_cmp(ulint a, /*!< in: ulint */ |
| 65 |
ulint b) /*!< in: ulint */ |
| 66 |
{ |
| 67 |
if (a < b) { |
| 68 |
return (-1); |
| 69 |
} else if (a == b) { |
| 70 |
return (0); |
| 71 |
} else { |
| 72 |
return (1); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** Compare two pairs of integers. |
| 77 |
@param[in] a_h more significant part of first pair |
| 78 |
@param[in] a_l less significant part of first pair |
| 79 |
@param[in] b_h more significant part of second pair |
| 80 |
@param[in] b_l less significant part of second pair |
| 81 |
@return comparison result of (a_h,a_l) and (b_h,b_l) |
| 82 |
@retval -1 if (a_h,a_l) is less than (b_h,b_l) |
| 83 |
@retval 0 if (a_h,a_l) is equal to (b_h,b_l) |
| 84 |
@retval 1 if (a_h,a_l) is greater than (b_h,b_l) */ |
| 85 |
static inline int ut_pair_cmp(ulint a_h, ulint a_l, ulint b_h, ulint b_l) { |
| 86 |
if (a_h < b_h) { |
| 87 |
return (-1); |
| 88 |
} |
| 89 |
if (a_h > b_h) { |
| 90 |
return (1); |
| 91 |
} |
| 92 |
return (ut_ulint_cmp(a_l, b_l)); |
| 93 |
} |
| 94 |
|
| 95 |
/** Calculates fast the 2-logarithm of a number, rounded upward to an |
| 96 |
integer. |
| 97 |
@return logarithm in the base 2, rounded upward */ |
| 98 |
constexpr ulint ut_2_log(ulint n) /*!< in: number != 0 */ |
| 99 |
{ |
| 100 |
ulint res = 0; |
| 101 |
|
| 102 |
ut_ad(n > 0); |
| 103 |
|
| 104 |
n = n - 1; |
| 105 |
|
| 106 |
for (;;) { |
| 107 |
n = n / 2; |
| 108 |
|
| 109 |
if (n == 0) { |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
res++; |
| 114 |
} |
| 115 |
|
| 116 |
return (res + 1); |
| 117 |
} |
| 118 |
|
| 119 |
/** Calculates 2 to power n. |
| 120 |
@param[in] n power of 2 |
| 121 |
@return 2 to power n */ |
| 122 |
static inline uint32_t ut_2_exp(uint32_t n) { return (1 << n); } |
| 123 |
|
| 124 |
namespace ut { |
| 125 |
/** Delays execution for at most max_wait or returns earlier if cond becomes |
| 126 |
true. |
| 127 |
@param cond in: condition to wait for; evaluated every 2 ms |
| 128 |
@param max_wait in: maximum delay to wait |
| 129 |
@return Returns true if condition have evaluated to true, false on timeout. */ |
| 130 |
template <typename TCondition> |
| 131 |
bool wait_for(TCondition cond, std::chrono::steady_clock::duration max_wait) { |
| 132 |
const auto start = std::chrono::steady_clock::now(); |
| 133 |
while (!cond()) { |
| 134 |
const auto diff = std::chrono::steady_clock::now() - start; |
| 135 |
if (max_wait <= std::chrono::seconds::zero() || diff > max_wait) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 139 |
} |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
#ifndef UNIV_HOTBACKUP |
| 144 |
/** Waits in loop until given condition is satisfied. It starts waiting |
| 145 |
using spin loop with pauses and after reaching maximum iterations, it |
| 146 |
switches to loop with sleeps. The sleep time is multiplied by two after |
| 147 |
every k-sleeps, until it reaches 100ms (starting at provided value). |
| 148 |
@param[in] spins_limit maximum iterations without sleep |
| 149 |
@param[in] sleep initial sleep time |
| 150 |
@param[in] condition returns true when condition is satisfied |
| 151 |
@return number of loops with sleep that have been used */ |
| 152 |
template <typename Condition> |
| 153 |
inline static Wait_stats wait_for(uint64_t spins_limit, |
| 154 |
std::chrono::microseconds sleep, |
| 155 |
Condition condition = {}) { |
| 156 |
uint64_t sleeps = 0, spins = 0; |
| 157 |
#ifdef _WIN32 |
| 158 |
constexpr uint64_t base_level = 64; |
| 159 |
#else |
| 160 |
constexpr uint64_t base_level = 4; |
| 161 |
#endif |
| 162 |
uint64_t next_level = base_level; |
| 163 |
|
| 164 |
while (!condition(spins == spins_limit)) { |
| 165 |
if (spins == spins_limit) { |
| 166 |
++sleeps; |
| 167 |
|
| 168 |
if (sleeps == next_level) { |
| 169 |
sleep = std::min<std::chrono::microseconds>( |
| 170 |
sleep * 2, std::chrono::milliseconds{100}); |
| 171 |
|
| 172 |
next_level += base_level; |
| 173 |
} |
| 174 |
|
| 175 |
std::this_thread::sleep_for(sleep); |
| 176 |
|
| 177 |
} else { |
| 178 |
++spins; |
| 179 |
|
| 180 |
UT_RELAX_CPU(); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return (Wait_stats{sleeps}); |
| 185 |
} |
| 186 |
|
| 187 |
#endif /* !UNIV_HOTBACKUP */ |
| 188 |
|
| 189 |
} // namespace ut |