MySQL 9.0.0
Source Code Documentation
Global_THD_manager Class Reference

This class maintains THD object of all registered threads. More...

#include <mysqld_thd_manager.h>

Public Member Functions

void set_unit_test ()
 Internally used to bypass code. More...
 
void add_thd (THD *thd)
 Adds THD to global THD list. More...
 
void remove_thd (THD *thd)
 Removes THD from global THD list. More...
 
int get_num_thread_running () const
 Retrieves thread running statistic variable. More...
 
void inc_thread_running ()
 Increments thread running statistic variable. More...
 
void dec_thread_running ()
 Decrements thread running statistic variable. More...
 
ulonglong get_num_thread_created () const
 Retrieves thread created statistic variable. More...
 
void inc_thread_created ()
 Increments thread created statistic variable. More...
 
my_thread_id get_new_thread_id ()
 Returns an unused thread id. More...
 
void release_thread_id (my_thread_id thread_id)
 Releases a thread id so that it can be reused. More...
 
my_thread_id get_thread_id () const
 Retrieves thread id counter value. More...
 
void set_thread_id_counter (my_thread_id new_id)
 Sets thread id counter value. More...
 
void wait_till_no_thd ()
 Waits until all THDs are removed from global THD lists (all partitions). More...
 
void do_for_all_thd_copy (Do_THD_Impl *func)
 This function calls func() for all THDs in every thd list partition after taking local copy of the THD list partition. More...
 
void do_for_all_thd (Do_THD_Impl *func)
 This function calls func() for all THDs in all THD list partitions. More...
 
void do_for_first_n_thd (Do_THD_Impl *func, uint n)
 This function calls func() for all first "n" THDs across all THD list partitions. More...
 
THD_ptr find_thd (Find_THD_Impl *func)
 Returns a THD_ptr containing first THD for which operator() returns true. More...
 
THD_ptr find_thd (Find_thd_with_id *func)
 

Static Public Member Functions

static Global_THD_managerget_instance ()
 Retrieves singleton instance. More...
 
static bool is_initialized ()
 Checks if the singleton is not already deinitialized. More...
 
static bool create_instance ()
 Initializes the thd manager. More...
 
static void destroy_instance ()
 Destroys the singleton instance. More...
 
static uint get_thd_count ()
 Retrieves total number of items in global THD lists (all partitions). More...
 

Static Public Attributes

static const my_thread_id reserved_thread_id = 0
 Value for thread_id reserved for THDs which does not have an assigned value yet. More...
 
static std::atomic< uint > atomic_global_thd_count {0U}
 
static const int NUM_PARTITIONS = 8
 

Private Types

typedef Prealloced_array< THD *, 60 > THD_array
 
typedef Prealloced_array< my_thread_id, 1000 > Thread_id_array
 

Private Member Functions

 Global_THD_manager ()
 
 ~Global_THD_manager ()
 

Private Attributes

THD_array thd_list [NUM_PARTITIONS]
 
Thread_id_array thread_ids
 
mysql_cond_t COND_thd_list [NUM_PARTITIONS]
 
mysql_mutex_t LOCK_thd_list [NUM_PARTITIONS]
 
mysql_mutex_t LOCK_thd_remove [NUM_PARTITIONS]
 
mysql_mutex_t LOCK_thread_ids
 
std::atomic< int > atomic_num_thread_running
 
std::atomic< ulonglongatomic_thread_created
 
my_thread_id thread_id_counter
 
bool unit_test
 

Static Private Attributes

static Global_THD_managerthd_manager = nullptr
 

Friends

void thd_lock_thread_count ()
 
void thd_unlock_thread_count ()
 

Detailed Description

This class maintains THD object of all registered threads.

It provides interface to perform functions such as find, count, perform some action for each THD object in the list.

It also provide mutators for inserting, and removing an element: add_thd() inserts a THD into the set, and increments the counter. remove_thd() removes a THD from the set, and decrements the counter. Method remove_thd() also broadcasts COND_thd_list.

Member Typedef Documentation

◆ THD_array

◆ Thread_id_array

Constructor & Destructor Documentation

◆ Global_THD_manager()

Global_THD_manager::Global_THD_manager ( )
private

◆ ~Global_THD_manager()

Global_THD_manager::~Global_THD_manager ( )
private

Member Function Documentation

◆ add_thd()

void Global_THD_manager::add_thd ( THD thd)

Adds THD to global THD list.

Parameters
thdTHD object

◆ create_instance()

bool Global_THD_manager::create_instance ( )
static

Initializes the thd manager.

Must be called before get_instance() can be used.

Returns
true if initialization failed, false otherwise.

◆ dec_thread_running()

void Global_THD_manager::dec_thread_running ( )
inline

Decrements thread running statistic variable.

◆ destroy_instance()

void Global_THD_manager::destroy_instance ( )
static

Destroys the singleton instance.

◆ do_for_all_thd()

void Global_THD_manager::do_for_all_thd ( Do_THD_Impl func)

This function calls func() for all THDs in all THD list partitions.

Parameters
funcObject of class which overrides operator()
Note
One list partition is unlocked before the next partition is locked.

◆ do_for_all_thd_copy()

void Global_THD_manager::do_for_all_thd_copy ( Do_THD_Impl func)

This function calls func() for all THDs in every thd list partition after taking local copy of the THD list partition.

It acquires LOCK_thd_remove to prevent removal of the THD.

Parameters
funcObject of class which overrides operator()

◆ do_for_first_n_thd()

void Global_THD_manager::do_for_first_n_thd ( Do_THD_Impl func,
uint  n 
)

This function calls func() for all first "n" THDs across all THD list partitions.

Parameters
funcObject of class which overrides operator()
nnumber of elements we want to call func for

◆ find_thd() [1/2]

THD_ptr Global_THD_manager::find_thd ( Find_THD_Impl func)

Returns a THD_ptr containing first THD for which operator() returns true.

Parameters
funcObject of class which overrides operator()
Returns
THD_ptr
Return values
THD_ptr{THD*}When matching THD is found.
THD_ptr{nullptr}When THD is not found.

◆ find_thd() [2/2]

THD_ptr Global_THD_manager::find_thd ( Find_thd_with_id func)

◆ get_instance()

static Global_THD_manager * Global_THD_manager::get_instance ( )
inlinestatic

Retrieves singleton instance.

◆ get_new_thread_id()

my_thread_id Global_THD_manager::get_new_thread_id ( )

Returns an unused thread id.

◆ get_num_thread_created()

ulonglong Global_THD_manager::get_num_thread_created ( ) const
inline

Retrieves thread created statistic variable.

Returns
ulonglong Returns the total number of threads created after server start

◆ get_num_thread_running()

int Global_THD_manager::get_num_thread_running ( ) const
inline

Retrieves thread running statistic variable.

Returns
int Returns the total number of threads currently running

◆ get_thd_count()

static uint Global_THD_manager::get_thd_count ( )
inlinestatic

Retrieves total number of items in global THD lists (all partitions).

Returns
uint Returns the count of items in global THD lists.

◆ get_thread_id()

my_thread_id Global_THD_manager::get_thread_id ( ) const
inline

Retrieves thread id counter value.

Returns
my_thread_id Returns the thread id counter value
Note
This is a dirty read.

◆ inc_thread_created()

void Global_THD_manager::inc_thread_created ( )
inline

Increments thread created statistic variable.

◆ inc_thread_running()

void Global_THD_manager::inc_thread_running ( )
inline

Increments thread running statistic variable.

◆ is_initialized()

static bool Global_THD_manager::is_initialized ( )
inlinestatic

Checks if the singleton is not already deinitialized.

◆ release_thread_id()

void Global_THD_manager::release_thread_id ( my_thread_id  thread_id)

Releases a thread id so that it can be reused.

Note that this is done automatically by remove_thd().

◆ remove_thd()

void Global_THD_manager::remove_thd ( THD thd)

Removes THD from global THD list.

Parameters
thdTHD object

◆ set_thread_id_counter()

void Global_THD_manager::set_thread_id_counter ( my_thread_id  new_id)

Sets thread id counter value.

Only used in testing for now.

Parameters
new_idThe next ID to hand out (if it's unused).

◆ set_unit_test()

void Global_THD_manager::set_unit_test ( )
inline

Internally used to bypass code.

It enables unit test scripts to create dummy THD object for testing.

◆ wait_till_no_thd()

void Global_THD_manager::wait_till_no_thd ( )

Waits until all THDs are removed from global THD lists (all partitions).

In other words, get_thd_count() to become zero.

Friends And Related Function Documentation

◆ thd_lock_thread_count

void thd_lock_thread_count ( )
friend

◆ thd_unlock_thread_count

void thd_unlock_thread_count ( )
friend

Member Data Documentation

◆ atomic_global_thd_count

std::atomic< uint > Global_THD_manager::atomic_global_thd_count {0U}
static

◆ atomic_num_thread_running

std::atomic<int> Global_THD_manager::atomic_num_thread_running
private

◆ atomic_thread_created

std::atomic<ulonglong> Global_THD_manager::atomic_thread_created
private

◆ COND_thd_list

mysql_cond_t Global_THD_manager::COND_thd_list[NUM_PARTITIONS]
private

◆ LOCK_thd_list

mysql_mutex_t Global_THD_manager::LOCK_thd_list[NUM_PARTITIONS]
private

◆ LOCK_thd_remove

mysql_mutex_t Global_THD_manager::LOCK_thd_remove[NUM_PARTITIONS]
private

◆ LOCK_thread_ids

mysql_mutex_t Global_THD_manager::LOCK_thread_ids
private

◆ NUM_PARTITIONS

const int Global_THD_manager::NUM_PARTITIONS = 8
static

◆ reserved_thread_id

const my_thread_id Global_THD_manager::reserved_thread_id = 0
static

Value for thread_id reserved for THDs which does not have an assigned value yet.

get_new_thread_id() will never return this value.

◆ thd_list

THD_array Global_THD_manager::thd_list[NUM_PARTITIONS]
private

◆ thd_manager

Global_THD_manager * Global_THD_manager::thd_manager = nullptr
staticprivate

◆ thread_id_counter

my_thread_id Global_THD_manager::thread_id_counter
private

◆ thread_ids

Thread_id_array Global_THD_manager::thread_ids
private

◆ unit_test

bool Global_THD_manager::unit_test
private

The documentation for this class was generated from the following files: