MySQL 8.0.31
Source Code Documentation
Go to the documentation of this file.
3Copyright (c) 1995, 2022, Oracle and/or its affiliates.
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.
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27/** @file include/os0thread.h
28 The interface to the operating system
29 process and thread control primitives
31 Created 9/8/1995 Heikki Tuuri
32 *******************************************************/
34#ifndef os0thread_h
35#define os0thread_h
37#include <atomic>
38#include <cstring>
39#include <functional>
40#include <future>
41#include <sstream>
42#include <thread>
43#include "ut0dbg.h"
45class IB_thread {
46 public:
49 State state() const {
50 return (m_state == nullptr ? State::INVALID : m_state->load());
51 }
53 void start();
54 void wait(State state_to_wait_for = State::STOPPED);
55 void join();
57 private:
58 std::shared_future<void> m_shared_future;
59 std::shared_ptr<std::atomic<State>> m_state;
61 void init(std::promise<void> &promise);
62 void set_state(State state);
64 friend class Detached_thread;
67/** Operating system thread native handle */
70namespace ut {
71/** The hash value of the current thread's id */
72const inline thread_local size_t this_thread_hash =
73 std::hash<std::thread::id>{}(std::this_thread::get_id());
74} // namespace ut
76/** Returns the string representation of the thread ID supplied. It uses the
77 only standard-compliant way of printing the thread ID.
78 @param thread_id The thread ID to convert to string.
79 @param hex_value If true, the conversion will be asked to output in
80 hexadecimal format. The support for it is OS-implementation-dependent and may
81 be ignored.
83std::string to_string(std::thread::id thread_id, bool hex_value = false);
85/** A class to allow any trivially copyable object to be XOR'ed. Trivially
86copyable according to
87 means we can
88copy the underlying representation to array of chars, and back and consider
89it a valid copy. It is thread-safe when changing, but no modifications must be
90assured during reading the stored value. */
91template <typename T_thing, typename T_digit>
93 public:
95 /* We could just default-initialize the acc with acc{}, if not the
96 SunStudio. */
97 for (size_t i = 0; i < digits_count; i++) {
98 acc[i].store(0);
99 }
100 }
102 void xor_thing(T_thing id) {
103 /* A buffer filled with zeros to pad the thing to next sizeof(T_digit)
104 bytes. It is thread-safe. */
105 char buff[sizeof(T_digit) * digits_count]{};
106 memcpy(buff, &id, sizeof(T_thing));
107 for (size_t i = 0; i < digits_count; i++) {
108 T_digit x;
109 memcpy(&x, buff + i * sizeof(T_digit), sizeof(T_digit));
110 acc[i].fetch_xor(x);
111 }
112 }
114 /** Returns an object that was XOR'ed odd number of times. This function
115 assumes there is exactly one such object, and caller must assure this. This
116 method is not thread-safe and caller must ensure no other thread is trying
117 to modify the value. */
119 T_thing res;
120 char buff[sizeof(T_digit) * digits_count];
121 for (size_t i = 0; i < acc.size(); ++i) {
122 T_digit x = acc[i].load(std::memory_order_acquire);
123 memcpy(buff + i * sizeof(T_digit), &x, sizeof(T_digit));
124 }
125 memcpy(&res, buff, sizeof(T_thing));
126 return res;
127 }
129 private:
130 static constexpr size_t digits_count =
131 (sizeof(T_thing) + sizeof(T_digit) - 1) / sizeof(T_digit);
132 /* initial value must be all zeros, as opposed to the representation of
133 thing{}, because we care about "neutral element of the XOR operation", and not
134 "default value of a thing". */
135 std::array<std::atomic<T_digit>, digits_count> acc;
138#ifdef _WIN32
139#include <Windows.h>
140#include "ut0class_life_cycle.h"
142/** Manages a Windows Event object. Destroys it when leaving a scope. */
143class Scoped_event : private ut::Non_copyable {
144 public:
145 /** Creates a new Windows Event object. It is created in manual-reset mode and
146 non-signalled start state. It asserts the Event object is created
147 successfully. */
148 Scoped_event() : m_event(CreateEvent(nullptr, TRUE, FALSE, nullptr)) {
149 /* In case different params are specified, like for example event name, then
150 the errors may be possible and could be handled. The m_event stored could be
151 NULL, for the application to test successful event creation with the
152 get_handle() method, but this is currently not supported (and thus not
153 tested) by this implementation. */
154 ut_a(m_event != NULL);
155 }
156 ~Scoped_event() {
157 if (m_event != NULL) {
158 CloseHandle(m_event);
159 }
160 }
161 /** Returns a HANDLE to managed Event. */
162 HANDLE get_handle() const { return m_event; }
164 private:
165 HANDLE m_event;
169/** A type for std::thread::id digit to store XOR efficiently. This will make
170 the compiler to optimize the operations hopefully to single instruction. */
172 std::conditional<sizeof(std::thread::id) >= sizeof(uint64_t), uint64_t,
173 uint32_t>::type;
175/** A type to store XORed objects of type std::thread::id */
179#endif /* !os0thread_h */
A class to allow any trivially copyable object to be XOR'ed.
Definition: os0thread.h:92
static constexpr size_t digits_count
Definition: os0thread.h:130
T_thing recover_if_single()
Returns an object that was XOR'ed odd number of times.
Definition: os0thread.h:118
Definition: os0thread.h:94
void xor_thing(T_thing id)
Definition: os0thread.h:102
std::array< std::atomic< T_digit >, digits_count > acc
Definition: os0thread.h:135
Wrapper for a callable, it will count the number of registered Runnable instances and will register t...
Definition: os0thread-create.h:166
Definition: os0thread.h:45
void set_state(State state)
std::shared_ptr< std::atomic< State > > m_state
Definition: os0thread.h:59
std::shared_future< void > m_shared_future
Definition: os0thread.h:58
void init(std::promise< void > &promise)
Definition: os0thread.h:47
void wait(State state_to_wait_for=State::STOPPED)
void start()
void join()
State state() const
Definition: os0thread.h:49
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:40
static my_thread_id thread_id
int native_handle_type
Definition: socket_constants.h:50
pid_type get_id()
Definition: process.h:47
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:47
const thread_local size_t this_thread_hash
The hash value of the current thread's id.
Definition: os0thread.h:72
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:173
std::thread::native_handle_type os_thread_id_t
Operating system thread native handle.
Definition: os0thread.h:68
std::string to_string(std::thread::id thread_id, bool hex_value=false)
Returns the string representation of the thread ID supplied.
required string type
Definition: replication_group_member_actions.proto:33
#define NULL
Definition: types.h:54
#define TRUE
Definition: types.h:50
#define FALSE
Definition: types.h:46
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:56
#define HANDLE
Definition: violite.h:158
unsigned long id[MAX_DEAD]