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