Debian Codegraph @

storage/innobase/os/os0thread.cc

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 os/os0thread.cc
29 The interface to the operating system thread control primitives
30
31 Created 9/8/1995 Heikki Tuuri
32 *******************************************************/
33
34 #include "univ.i"
35
36 #include <atomic>
37 #include <ios>
38 #include <sstream>
39 #include <thread>
40
41 #ifdef UNIV_LINUX
42 /* include defs for CPU time priority settings */
43 #include <sys/resource.h>
44 #include <sys/syscall.h>
45 #include <sys/time.h>
46 #include <unistd.h>
47 #endif /* UNIV_LINUX */
48
49 #include "ut0ut.h"
50
51 /** We are prepared for a situation that we have this many threads waiting for
52 a semaphore inside InnoDB. innodb_init_params() sets the value. */
53 uint32_t srv_max_n_threads = 0;
54
55 /** Number of threads active. */
56 std::atomic_int os_thread_count;
57
58 void IB_thread::start() {
59 ut_a(state() == State::NOT_STARTED);
60 m_state->store(State::ALLOWED_TO_START);
61
62 #ifdef _WIN32
63 unsigned int cnt = 0;
64 #endif /* _WIN32 */
65
66 while (state() == State::ALLOWED_TO_START) {
67 UT_RELAX_CPU();
68
69 #ifdef _WIN32
70 /* When the number of threads to be spawned exceeds the number of cores of
71 a machine, it's seen that we cannot just rely on UT_RELAX_CPU(). So in such
72 a case, allow the thread to release its time slice to any thread wanting
73 control. */
74 if (++cnt > 500) {
75 std::this_thread::yield();
76 }
77 #endif /* _WIN32 */
78 }
79 const auto state_after_start = state();
80
81 ut_a(state_after_start == State::STARTED ||
82 state_after_start == State::STOPPED);
83 }
84
85 void IB_thread::wait(State state_to_wait_for) {
86 ut_a(state() != State::INVALID);
87 ut_a(m_shared_future.valid());
88 if (state_to_wait_for >= State::STOPPED) {
89 m_shared_future.wait();
90 }
91 while (state() < state_to_wait_for) {
92 UT_RELAX_CPU();
93 }
94 }
95
96 void IB_thread::join() { wait(State::STOPPED); }
97
98 void IB_thread::init(std::promise<void> &promise) {
99 m_shared_future = promise.get_future();
100 m_state.reset(new std::atomic<State>(State::NOT_STARTED));
101 }
102
103 void IB_thread::set_state(State new_state) {
104 ut_a(state() != State::INVALID);
105 m_state->store(new_state);
106 }
107
108 std::string to_string(std::thread::id thread_id, bool hex_value) {
109 try {
110 std::stringstream ss;
111 if (hex_value) {
112 ss << std::hex << std::showbase;
113 }
114 /* The documentation for this says "May throw implementation-defined
115 exceptions." - we catch anything that might be thrown here. */
116 ss << thread_id;
117 return ss.str();
118 } catch (...) {
119 return "invalid_thread_id";
120 }
121 }