MySQL 9.1.0
Source Code Documentation
os0thread.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work 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 either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
47class IB_thread {
48 public:
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 */
71
72namespace ut {
73/** The hash value of the current thread's id */
74const 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*/
85std::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
88copyable according to
89https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable means we can
90copy the underlying representation to array of chars, and back and consider
91it a valid copy. It is thread-safe when changing, but no modifications must be
92assured during reading the stored value. */
93template <typename T_thing, typename T_digit>
95 public:
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. */
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. */
145class 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 != nullptr);
157 }
158 ~Scoped_event() {
159 if (m_event != nullptr) {
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. */
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 */
180
181#endif /* !os0thread_h */
A class to allow any trivially copyable object to be XOR'ed.
Definition: os0thread.h:94
static constexpr size_t digits_count
Definition: os0thread.h:132
T_thing recover_if_single()
Returns an object that was XOR'ed odd number of times.
Definition: os0thread.h:120
Atomic_xor_of_things()
Definition: os0thread.h:96
void xor_thing(T_thing id)
Definition: os0thread.h:104
std::array< std::atomic< T_digit >, digits_count > acc
Definition: os0thread.h:137
Wrapper for a callable, it will count the number of registered Runnable instances and will register t...
Definition: os0thread-create.h:165
Definition: os0thread.h:47
void set_state(State state)
Definition: os0thread.cc:103
std::shared_ptr< std::atomic< State > > m_state
Definition: os0thread.h:61
std::shared_future< void > m_shared_future
Definition: os0thread.h:60
void init(std::promise< void > &promise)
Definition: os0thread.cc:98
State
Definition: os0thread.h:49
void wait(State state_to_wait_for=State::STOPPED)
Definition: os0thread.cc:85
void start()
Definition: os0thread.cc:58
void join()
Definition: os0thread.cc:96
State state() const
Definition: os0thread.h:51
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:41
static my_thread_id thread_id
Definition: my_thr_init.cc:63
int native_handle_type
Definition: socket_constants.h:51
pid_type get_id()
Definition: process.h:48
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
const thread_local size_t this_thread_hash
The hash value of the current thread's id.
Definition: os0thread.h:74
constexpr T div_ceil(T numerator, T denominator)
Computes the result of division rounded towards positive infinity.
Definition: ut0math.h:49
std::conditional< sizeof(std::thread::id) >=sizeof(uint64_t), uint64_t, uint32_t >::type Xor_digit_for_thread_id
A type for std::thread::id digit to store XOR efficiently.
Definition: os0thread.h:175
std::thread::native_handle_type os_thread_id_t
Operating system thread native handle.
Definition: os0thread.h:70
std::string to_string(std::thread::id thread_id, bool hex_value=false)
Returns the string representation of the thread ID supplied.
Definition: os0thread.cc:108
required string type
Definition: replication_group_member_actions.proto:34
#define TRUE
Definition: types.h:51
#define FALSE
Definition: types.h:47
Utilities related to class lifecycle.
Debug utilities for Innobase.
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:93
Math functions.
#define HANDLE
Definition: violite.h:159
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510