MySQL 26.10.0
Source Code Documentation
health_monitor_thread.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#pragma once
25
26#include <atomic>
27#include <condition_variable>
28#include <ctime>
29#include <functional>
30#include <mutex>
31#include <string>
32#include <thread>
33#include <vector>
34
41
42namespace hm {
43
44class Thread {
45 public:
46 Thread() = default;
47 Thread(const char *name, const char *instr_name, const char *os_name,
48 const char *desc);
49 virtual ~Thread() = 0;
50
51 const char *name() const { return m_name.c_str(); }
52 virtual bool running() const { return m_thread_running; }
53
54 // Thread control
55 virtual int start();
56 virtual void stop();
57 virtual void run_process();
58 static void cleanup();
59 void signal();
60 static int join(my_thread_handle *thread, void **value_ptr);
61 static bool joinable(const my_thread_handle &thread);
62
63 protected:
64 int start_thread();
65 void stop_thread();
67 void join_thread();
68 bool stop_requested();
69 virtual void pre_process() {}
70 virtual void process() {}
71 virtual void post_process() {}
72
73 // Synchronize start, stop and update
74 std::mutex m_command_lock;
75
76 const std::string m_name;
77 const std::string m_instr_name;
78 const std::string m_os_name;
79 const std::string m_description;
80 std::atomic<bool> m_thread_running{false};
81 bool m_stop{false};
82
87 m_instr_name.c_str(),
88 m_os_name.c_str(),
90 PSI_VOLATILITY_PROVISIONING, // instruments are created/destroyed
91 m_description.c_str()};
92
103};
104
105} // namespace hm
Definition: health_monitor_thread.h:44
const char * name() const
Definition: health_monitor_thread.h:51
static void cleanup()
Cleanup thread instrumentation.
Definition: health_monitor_thread.cc:90
virtual void process()
Definition: health_monitor_thread.h:70
virtual int start()
Definition: health_monitor_thread.cc:53
const std::string m_instr_name
Definition: health_monitor_thread.h:77
PSI_thread_key m_thread_key
Definition: health_monitor_thread.h:84
PSI_cond_key m_cond_key
Definition: health_monitor_thread.h:99
virtual void pre_process()
Definition: health_monitor_thread.h:69
static int join(my_thread_handle *thread, void **value_ptr)
Definition: health_monitor_thread.cc:97
my_thread_handle m_thread
Definition: health_monitor_thread.h:83
virtual ~Thread()=0
Definition: health_monitor_thread.cc:47
const std::string m_description
Definition: health_monitor_thread.h:79
bool stop_requested()
Return true when worker shutdown has been requested.
Definition: health_monitor_thread.cc:160
void request_thread_stop()
Request that the worker stop without waiting for it to exit.
Definition: health_monitor_thread.cc:141
const std::string m_name
Definition: health_monitor_thread.h:76
void join_thread()
Wait for a stopped worker to exit.
Definition: health_monitor_thread.cc:151
virtual void post_process()
Definition: health_monitor_thread.h:71
void signal()
Signal the processing loop to end the current cycle.
Definition: health_monitor_thread.cc:95
const std::string m_os_name
Definition: health_monitor_thread.h:78
virtual void stop()
Definition: health_monitor_thread.cc:58
std::atomic< bool > m_thread_running
Definition: health_monitor_thread.h:80
PSI_mutex_key m_mutex_key
Definition: health_monitor_thread.h:94
PSI_cond_info m_cond_info
Definition: health_monitor_thread.h:100
virtual bool running() const
Definition: health_monitor_thread.h:52
PSI_mutex_info m_mutex_info
Definition: health_monitor_thread.h:95
int start_thread()
Definition: health_monitor_thread.cc:117
Thread()=default
std::mutex m_command_lock
Definition: health_monitor_thread.h:74
bool m_stop
Definition: health_monitor_thread.h:81
mysql_mutex_t m_wait_lock
Definition: health_monitor_thread.h:93
static bool joinable(const my_thread_handle &thread)
Definition: health_monitor_thread.cc:101
PSI_thread_info m_thread_info
Definition: health_monitor_thread.h:85
virtual void run_process()
Main thread loop.
Definition: health_monitor_thread.cc:66
void stop_thread()
Definition: health_monitor_thread.cc:134
mysql_cond_t m_wait
Definition: health_monitor_thread.h:98
#define PSI_DOCUMENT_ME
Definition: component_common.h:29
#define PSI_FLAG_SINGLETON
Singleton flag.
Definition: component_common.h:35
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:44
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:52
unsigned int PSI_thread_key
Instrumented thread key.
Definition: psi_thread_bits.h:50
#define PSI_VOLATILITY_PROVISIONING
Definition: psi_bits.h:155
#define PSI_FLAG_THREAD_SYSTEM
System thread flag.
Definition: psi_bits.h:124
Definition: component_command.h:29
Condition information.
Definition: psi_cond_bits.h:88
Mutex information.
Definition: psi_mutex_bits.h:73
Thread instrument information.
Definition: psi_thread_bits.h:117
Definition: my_thread_bits.h:58
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50