1/***************************************************************************** 2 3Copyright (c) 2017, 2020, Oracle and/or its affiliates. All Rights Reserved. 4 5This program is free software; you can redistribute it and/or modify 6it under the terms of the GNU General Public License, version 2.0, 7as published by the Free Software Foundation. 8 9This program is also distributed with certain software (including 10but not limited to OpenSSL) that is licensed under separate terms, 11as designated in a particular file or component or in included license 12documentation. The authors of MySQL hereby grant you an additional 13permission to link the program and your derivative works with the 14separately licensed software that they have included with MySQL. 15 16This program is distributed in the hope that it will be useful, 17but WITHOUT ANY WARRANTY; without even the implied warranty of 18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19GNU General Public License, version 2.0, for more details. 20 21You should have received a copy of the GNU General Public License 22along with this program; if not, write to the Free Software 23Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 25*****************************************************************************/ 26 27/**************************************************************/ /** 28 @file include/os0event.ic 29 30 Inlined implementation for os_event_* 31 *******************************************************************/ 32 33/** Waits in loop until a provided condition is satisfied. Combines usage 34of spin-delay and event. 35 36@remarks 37First it uses a spin loop with PAUSE instructions. In each spin iteration 38it checks the condition and stops as soon as it returned true. 39 40When a provided number of spin iterations is reached, and the condition 41still has not returned true, waiting on a provided event starts. 42 43Each wait uses a provided timeout. After each wake-up the condition is 44re-checked and function stops as soon as the condition returned true. 45 46Every k-waits (ended on wake-up or timeout), the timeout is multiplied by two 47(but it's limited up to maximum value of 100ms). 48 49@param[in,out] event event on which function may wait 50@param[in] spins_limit maximum spin iterations 51@param[in] timeout initial timeout value 52@param[in] condition returns true when condition is 53 satisfied 54 55@return number of loops with wait on event that have been used */ 56template <typename Condition> 57inline static Wait_stats os_event_wait_for(os_event_t &event, 58 uint64_t spins_limit, 59 uint64_t timeout, 60 Condition condition = {}) { 61#ifdef _WIN32 62 uint32_t next_level = 64; 63#else 64 uint32_t next_level = 4; 65#endif 66 uint32_t waits = 0; 67 68 constexpr uint64_t MIN_TIMEOUT_US = 1; 69 constexpr uint64_t MAX_TIMEOUT_US = 100 * 1000; 70 71 while (true) { 72 /* Store current sig_count before checking the 73 condition, not to miss notification. */ 74 const bool wait = spins_limit == 0; 75 76 const int64_t sig_count = !wait ? 0 : os_event_reset(event); 77 78 /* Important: we do not want to split this loop to two 79 loops (one for spin-delay and one for event), because 80 we assume the condition is inlined below, and we don't 81 want to make it inlined in two places. */ 82 83 if (condition(wait)) { 84 return (Wait_stats{waits}); 85 } 86 87 if (!wait) { 88 /* It's still spin-delay loop. */ 89 --spins_limit; 90 91 UT_RELAX_CPU(); 92 93 } else { 94 /* Event-based loop. */ 95 ++waits; 96 if (timeout < MIN_TIMEOUT_US) { 97 /* If timeout = 0, then timeout * 2 = 0 and 98 we would not keep increasing timeout below. 99 Therefore we need some limitation for min. 100 101 Moreover, we measured how long does it take 102 to wake up on timeout, depending on timeout: 103 1us -> 57us, 104 10us -> 66us, 105 20us -> 76us, 106 50us -> 106us, 107 100us -> 156us, 108 1000us -> 1100us. 109 110 (Oracle Linux 4.14.28) */ 111 112 timeout = MIN_TIMEOUT_US; 113 } 114 115 if (waits == next_level) { 116 timeout = std::min(timeout * 2, MAX_TIMEOUT_US); 117 118#ifdef _WIN32 119 /* On Windows timeout is expressed in ms, 120 so it's divided by 1000 and rounded down 121 to 0 when it's smaller than 1000. 122 123 In such case, it takes in average 10us to 124 perform single SleepConditionVariableCS. 125 126 So we need to perform more such 10us waits 127 to simulate given number of timeout waits. */ 128 next_level += 64; 129#else 130 next_level += 4; 131#endif 132 } 133 134 /* This translates to pthread_cond_wait (linux). */ 135 os_event_wait_time_low(event, timeout, sig_count); 136 } 137 } 138} 139