Debian Codegraph @

storage/innobase/include/os0event.ic

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