Debian Codegraph @

storage/innobase/include/os0thread.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 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/os0thread.h
29 The interface to the operating system
30 process and thread control primitives
31
32 Created 9/8/1995 Heikki Tuuri
33 *******************************************************/
34
35 #ifndef os0thread_h
36 #define os0thread_h
37
38 #include <atomic>
39 #include <cstring>
40 #include <functional>
41 #include <future>
42 #include <sstream>
43 #include <thread>
44 #include "ut0dbg.h"
45 #include "ut0math.h"
46
47 class IB_thread {
48 public:
49 enum class State { INVALID, NOT_STARTED, ALLOWED_TO_START, STARTED, STOPPED };
50
51 State state() const {
52 return (m_state == nullptr ? State::INVALID : m_state->load());
53 }
54
55 void start();
56 void wait(State state_to_wait_for = State::STOPPED);
57 void join();
58
59 private:
60 std::shared_future<void> m_shared_future;
61 std::shared_ptr<std::atomic<State>> m_state;
62
63 void init(std::promise<void> &promise);
64 void set_state(State state);
65
66 friend class Detached_thread;
67 };
68
69 /** Operating system thread native handle */
70 using os_thread_id_t = std::thread::native_handle_type;
71
72 namespace ut {
73 /** The hash value of the current thread's id */
74 const inline thread_local size_t this_thread_hash =
75 std::hash<std::thread::id>{}(std::this_thread::get_id());
76 } // namespace ut
77
78 /** Returns the string representation of the thread ID supplied. It uses the
79 only standard-compliant way of printing the thread ID.
80 @param thread_id The thread ID to convert to string.
81 @param hex_value If true, the conversion will be asked to output in
82 hexadecimal format. The support for it is OS-implementation-dependent and may
83 be ignored.
84 */
85 std::string to_string(std::thread::id thread_id, bool hex_value = false);
86
87 /** A class to allow any trivially copyable object to be XOR'ed. Trivially
88 copyable according to
89 https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable means we can
90 copy the underlying representation to array of chars, and back and consider
91 it a valid copy. It is thread-safe when changing, but no modifications must be
92 assured during reading the stored value. */
93 template <typename T_thing, typename T_digit>
94 class Atomic_xor_of_things {
95 public:
96 Atomic_xor_of_things() {
97 /* We could just default-initialize the acc with acc{}, if not the
98 SunStudio. */
99 for (size_t i = 0; i < digits_count; i++) {
100 acc[i].store(0);
101 }
102 }
103
104 void xor_thing(T_thing id) {
105 /* A buffer filled with zeros to pad the thing to next sizeof(T_digit)
106 bytes. It is thread-safe. */
107 char buff[sizeof(T_digit) * digits_count]{};
108 memcpy(buff, &id, sizeof(T_thing));
109 for (size_t i = 0; i < digits_count; i++) {
110 T_digit x;
111 memcpy(&x, buff + i * sizeof(T_digit), sizeof(T_digit));
112 acc[i].fetch_xor(x);
113 }
114 }
115
116 /** Returns an object that was XOR'ed odd number of times. This function
117 assumes there is exactly one such object, and caller must assure this. This
118 method is not thread-safe and caller must ensure no other thread is trying
119 to modify the value. */
120 T_thing recover_if_single() {
121 T_thing res;
122 char buff[sizeof(T_digit) * digits_count];
123 for (size_t i = 0; i < acc.size(); ++i) {
124 T_digit x = acc[i].load(std::memory_order_acquire);
125 memcpy(buff + i * sizeof(T_digit), &x, sizeof(T_digit));
126 }
127 memcpy(&res, buff, sizeof(T_thing));
128 return res;
129 }
130
131 private:
132 static constexpr size_t digits_count =
133 ut::div_ceil(sizeof(T_thing), sizeof(T_digit));
134 /* initial value must be all zeros, as opposed to the representation of
135 thing{}, because we care about "neutral element of the XOR operation", and not
136 "default value of a thing". */
137 std::array<std::atomic<T_digit>, digits_count> acc;
138 };
139
140 #ifdef _WIN32
141 #include <Windows.h>
142 #include "ut0class_life_cycle.h"
143
144 /** Manages a Windows Event object. Destroys it when leaving a scope. */
145 class Scoped_event : private ut::Non_copyable {
146 public:
147 /** Creates a new Windows Event object. It is created in manual-reset mode and
148 non-signalled start state. It asserts the Event object is created
149 successfully. */
150 Scoped_event() : m_event(CreateEvent(nullptr, TRUE, FALSE, nullptr)) {
151 /* In case different params are specified, like for example event name, then
152 the errors may be possible and could be handled. The m_event stored could be
153 NULL, for the application to test successful event creation with the
154 get_handle() method, but this is currently not supported (and thus not
155 tested) by this implementation. */
156 ut_a(m_event != NULL);
157 }
158 ~Scoped_event() {
159 if (m_event != NULL) {
160 CloseHandle(m_event);
161 }
162 }
163 /** Returns a HANDLE to managed Event. */
164 HANDLE get_handle() const { return m_event; }
165
166 private:
167 HANDLE m_event;
168 };
169 #endif
170
171 /** A type for std::thread::id digit to store XOR efficiently. This will make
172 the compiler to optimize the operations hopefully to single instruction. */
173 using Xor_digit_for_thread_id =
174 std::conditional<sizeof(std::thread::id) >= sizeof(uint64_t), uint64_t,
175 uint32_t>::type;
176
177 /** A type to store XORed objects of type std::thread::id */
178 using Atomic_xor_of_thread_id =
179 Atomic_xor_of_things<std::thread::id, Xor_digit_for_thread_id>;
180
181 #endif /* !os0thread_h */