Commit 76ec2da6 authored by Marko Mäkelä's avatar Marko Mäkelä

Bug#11766305 - 59392: Remove thr0loc.c and ibuf_inside() [part 3]

Remove the slot_no member of struct thr_local_struct.

enum srv_thread_type: Remove unused thread types.
srv_get_thread_type(): Unused function, remove.

thr_local_get_slot_no(), thr_local_set_slot_no(): Remove.

srv_thread_type_validate(), srv_slot_get_type(): New functions, for debugging.

srv_table_reserve_slot(): Return the srv_slot_t* directly. Do not create
thread-local storage.

srv_suspend_thread(): Get the srv_slot_t* as parameter. Return void;
the caller knows slot->event already.

srv_thread_has_reserved_slot(), srv_release_threads(): Assert
srv_thread_type_validate(type).

srv_init(): Use mem_zalloc() instead of mem_alloc(). Replace
srv_table_get_nth_slot(), because it now asserts that the kernel_mutex
is being held.

srv_master_thread(), srv_purge_thread(): Remember the slot from
srv_table_reserve_slot().

rb:629 approved by Inaam Rana
parent 72ec03c2
...@@ -442,16 +442,8 @@ typedef enum srv_stats_method_name_enum srv_stats_method_name_t; ...@@ -442,16 +442,8 @@ typedef enum srv_stats_method_name_enum srv_stats_method_name_t;
#ifndef UNIV_HOTBACKUP #ifndef UNIV_HOTBACKUP
/** Types of threads existing in the system. */ /** Types of threads existing in the system. */
enum srv_thread_type { enum srv_thread_type {
SRV_COM = 1, /**< threads serving communication and queries */ SRV_WORKER = 0, /**< threads serving parallelized queries and
SRV_CONSOLE, /**< thread serving console */
SRV_WORKER, /**< threads serving parallelized queries and
queries released from lock wait */ queries released from lock wait */
#if 0
/* Utility threads */
SRV_BUFFER, /**< thread flushing dirty buffer blocks */
SRV_RECOVERY, /**< threads finishing a recovery */
SRV_INSERT, /**< thread flushing the insert buffer to disk */
#endif
SRV_MASTER /**< the master thread, (whose type number must SRV_MASTER /**< the master thread, (whose type number must
be biggest) */ be biggest) */
}; };
...@@ -490,13 +482,6 @@ ulint ...@@ -490,13 +482,6 @@ ulint
srv_get_n_threads(void); srv_get_n_threads(void);
/*===================*/ /*===================*/
/*********************************************************************//** /*********************************************************************//**
Returns the calling thread type.
@return SRV_COM, ... */
enum srv_thread_type
srv_get_thread_type(void);
/*=====================*/
/*********************************************************************//**
Check whether thread type has reserved a slot. Check whether thread type has reserved a slot.
@return slot number or UNDEFINED if not found*/ @return slot number or UNDEFINED if not found*/
UNIV_INTERN UNIV_INTERN
......
...@@ -59,22 +59,6 @@ thr_local_free( ...@@ -59,22 +59,6 @@ thr_local_free(
/*===========*/ /*===========*/
os_thread_id_t id); /*!< in: thread id */ os_thread_id_t id); /*!< in: thread id */
/*******************************************************************//** /*******************************************************************//**
Gets the slot number in the thread table of a thread.
@return slot number */
UNIV_INTERN
ulint
thr_local_get_slot_no(
/*==================*/
os_thread_id_t id); /*!< in: thread id of the thread */
/*******************************************************************//**
Sets in the local storage the slot number in the thread table of a thread. */
UNIV_INTERN
void
thr_local_set_slot_no(
/*==================*/
os_thread_id_t id, /*!< in: thread id of the thread */
ulint slot_no);/*!< in: slot number */
/*******************************************************************//**
Returns pointer to the 'in_ibuf' field within the current thread local Returns pointer to the 'in_ibuf' field within the current thread local
storage. storage.
@return pointer to the in_ibuf field */ @return pointer to the in_ibuf field */
......
...@@ -690,7 +690,7 @@ Unix.*/ ...@@ -690,7 +690,7 @@ Unix.*/
struct srv_slot_struct{ struct srv_slot_struct{
os_thread_id_t id; /*!< thread id */ os_thread_id_t id; /*!< thread id */
os_thread_t handle; /*!< thread handle */ os_thread_t handle; /*!< thread handle */
unsigned type:3; /*!< thread type: user, utility etc. */ unsigned type:1; /*!< thread type: user, utility etc. */
unsigned in_use:1; /*!< TRUE if this slot is in use */ unsigned in_use:1; /*!< TRUE if this slot is in use */
unsigned suspended:1; /*!< TRUE if the thread is waiting unsigned suspended:1; /*!< TRUE if the thread is waiting
for the event of this slot */ for the event of this slot */
...@@ -797,6 +797,7 @@ srv_table_get_nth_slot( ...@@ -797,6 +797,7 @@ srv_table_get_nth_slot(
/*===================*/ /*===================*/
ulint index) /*!< in: index of the slot */ ulint index) /*!< in: index of the slot */
{ {
ut_ad(mutex_own(&kernel_mutex));
ut_a(index < OS_THREAD_MAX_N); ut_a(index < OS_THREAD_MAX_N);
return(srv_sys->threads + index); return(srv_sys->threads + index);
...@@ -815,7 +816,7 @@ srv_get_n_threads(void) ...@@ -815,7 +816,7 @@ srv_get_n_threads(void)
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
for (i = SRV_COM; i < SRV_MASTER + 1; i++) { for (i = 0; i < SRV_MASTER + 1; i++) {
n_threads += srv_n_threads[i]; n_threads += srv_n_threads[i];
} }
...@@ -825,13 +826,46 @@ srv_get_n_threads(void) ...@@ -825,13 +826,46 @@ srv_get_n_threads(void)
return(n_threads); return(n_threads);
} }
#ifdef UNIV_DEBUG
/*********************************************************************//** /*********************************************************************//**
Reserves a slot in the thread table for the current thread. Also creates the Validates the type of a thread table slot.
thread local storage struct for the current thread. NOTE! The server mutex @return TRUE if ok */
has to be reserved by the caller!
@return reserved slot index */
static static
ulint ibool
srv_thread_type_validate(
/*=====================*/
enum srv_thread_type type) /*!< in: thread type */
{
switch (type) {
case SRV_WORKER:
case SRV_MASTER:
return(TRUE);
}
ut_error;
return(FALSE);
}
#endif /* UNIV_DEBUG */
/*********************************************************************//**
Gets the type of a thread table slot.
@return thread type */
static
enum srv_thread_type
srv_slot_get_type(
/*==============*/
const srv_slot_t* slot) /*!< in: thread slot */
{
enum srv_thread_type type = (enum srv_thread_type) slot->type;
ut_ad(srv_thread_type_validate(type));
return(type);
}
/*********************************************************************//**
Reserves a slot in the thread table for the current thread.
NOTE! The server mutex has to be reserved by the caller!
@return reserved slot */
static
srv_slot_t*
srv_table_reserve_slot( srv_table_reserve_slot(
/*===================*/ /*===================*/
enum srv_thread_type type) /*!< in: type of the thread */ enum srv_thread_type type) /*!< in: type of the thread */
...@@ -839,8 +873,7 @@ srv_table_reserve_slot( ...@@ -839,8 +873,7 @@ srv_table_reserve_slot(
srv_slot_t* slot; srv_slot_t* slot;
ulint i; ulint i;
ut_a(type > 0); ut_ad(srv_thread_type_validate(type));
ut_a(type <= SRV_MASTER);
ut_ad(mutex_own(&kernel_mutex)); ut_ad(mutex_own(&kernel_mutex));
i = 0; i = 0;
...@@ -851,53 +884,40 @@ srv_table_reserve_slot( ...@@ -851,53 +884,40 @@ srv_table_reserve_slot(
slot = srv_table_get_nth_slot(i); slot = srv_table_get_nth_slot(i);
} }
ut_a(slot->in_use == FALSE);
slot->in_use = TRUE; slot->in_use = TRUE;
slot->suspended = FALSE; slot->suspended = FALSE;
slot->type = type; slot->type = type;
ut_ad(srv_slot_get_type(slot) == type);
slot->id = os_thread_get_curr_id(); slot->id = os_thread_get_curr_id();
slot->handle = os_thread_get_curr(); slot->handle = os_thread_get_curr();
thr_local_create(); return(slot);
thr_local_set_slot_no(os_thread_get_curr_id(), i);
return(i);
} }
/*********************************************************************//** /*********************************************************************//**
Suspends the calling thread to wait for the event in its thread slot. Suspends the calling thread to wait for the event in its thread slot.
NOTE! The server mutex has to be reserved by the caller! NOTE! The server mutex has to be reserved by the caller! */
@return event for the calling thread to wait */
static static
os_event_t void
srv_suspend_thread(void) srv_suspend_thread(
/*====================*/ /*===============*/
srv_slot_t* slot) /*!< in/out: thread slot */
{ {
srv_slot_t* slot;
os_event_t event;
ulint slot_no;
enum srv_thread_type type; enum srv_thread_type type;
ut_ad(mutex_own(&kernel_mutex)); ut_ad(mutex_own(&kernel_mutex));
ut_ad(slot->in_use);
slot_no = thr_local_get_slot_no(os_thread_get_curr_id()); ut_ad(!slot->suspended);
ut_ad(slot->id == os_thread_get_curr_id());
if (srv_print_thread_releases) { if (srv_print_thread_releases) {
fprintf(stderr, fprintf(stderr,
"Suspending thread %lu to slot %lu\n", "Suspending thread %lu to slot %lu\n",
(ulong) os_thread_get_curr_id(), (ulong) slot_no); (ulong) os_thread_get_curr_id(),
(ulong) (slot - srv_sys->threads));
} }
slot = srv_table_get_nth_slot(slot_no); type = srv_slot_get_type(slot);
type = slot->type;
ut_ad(type >= SRV_WORKER);
ut_ad(type <= SRV_MASTER);
event = slot->event;
slot->suspended = TRUE; slot->suspended = TRUE;
...@@ -905,9 +925,7 @@ srv_suspend_thread(void) ...@@ -905,9 +925,7 @@ srv_suspend_thread(void)
srv_n_threads_active[type]--; srv_n_threads_active[type]--;
os_event_reset(event); os_event_reset(slot->event);
return(event);
} }
/*********************************************************************//** /*********************************************************************//**
...@@ -926,8 +944,7 @@ srv_release_threads( ...@@ -926,8 +944,7 @@ srv_release_threads(
ulint i; ulint i;
ulint count = 0; ulint count = 0;
ut_ad(type >= SRV_WORKER); ut_ad(srv_thread_type_validate(type));
ut_ad(type <= SRV_MASTER);
ut_ad(n > 0); ut_ad(n > 0);
ut_ad(mutex_own(&kernel_mutex)); ut_ad(mutex_own(&kernel_mutex));
...@@ -935,7 +952,8 @@ srv_release_threads( ...@@ -935,7 +952,8 @@ srv_release_threads(
slot = srv_table_get_nth_slot(i); slot = srv_table_get_nth_slot(i);
if (slot->in_use && slot->type == type && slot->suspended) { if (slot->in_use && slot->suspended
&& srv_slot_get_type(slot) == type) {
slot->suspended = FALSE; slot->suspended = FALSE;
...@@ -962,34 +980,6 @@ srv_release_threads( ...@@ -962,34 +980,6 @@ srv_release_threads(
return(count); return(count);
} }
/*********************************************************************//**
Returns the calling thread type.
@return SRV_COM, ... */
UNIV_INTERN
enum srv_thread_type
srv_get_thread_type(void)
/*=====================*/
{
ulint slot_no;
srv_slot_t* slot;
enum srv_thread_type type;
mutex_enter(&kernel_mutex);
slot_no = thr_local_get_slot_no(os_thread_get_curr_id());
slot = srv_table_get_nth_slot(slot_no);
type = slot->type;
ut_ad(type >= SRV_WORKER);
ut_ad(type <= SRV_MASTER);
mutex_exit(&kernel_mutex);
return(type);
}
/*********************************************************************//** /*********************************************************************//**
Check whether thread type has reserved a slot. Return the first slot that Check whether thread type has reserved a slot. Return the first slot that
is found. This works because we currently have only 1 thread of each type. is found. This works because we currently have only 1 thread of each type.
...@@ -1003,6 +993,7 @@ srv_thread_has_reserved_slot( ...@@ -1003,6 +993,7 @@ srv_thread_has_reserved_slot(
ulint i; ulint i;
ulint slot_no = ULINT_UNDEFINED; ulint slot_no = ULINT_UNDEFINED;
ut_ad(srv_thread_type_validate(type));
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
for (i = 0; i < OS_THREAD_MAX_N; i++) { for (i = 0; i < OS_THREAD_MAX_N; i++) {
...@@ -1040,22 +1031,18 @@ srv_init(void) ...@@ -1040,22 +1031,18 @@ srv_init(void)
mutex_create(srv_innodb_monitor_mutex_key, mutex_create(srv_innodb_monitor_mutex_key,
&srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK); &srv_innodb_monitor_mutex, SYNC_NO_ORDER_CHECK);
srv_sys->threads = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t)); srv_sys->threads = mem_zalloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
for (i = 0; i < OS_THREAD_MAX_N; i++) { for (i = 0; i < OS_THREAD_MAX_N; i++) {
slot = srv_table_get_nth_slot(i); slot = srv_sys->threads + i;
slot->in_use = FALSE;
slot->type=0; /* Avoid purify errors */
slot->event = os_event_create(NULL); slot->event = os_event_create(NULL);
ut_a(slot->event); ut_a(slot->event);
} }
srv_mysql_table = mem_alloc(OS_THREAD_MAX_N * sizeof(srv_slot_t)); srv_mysql_table = mem_zalloc(OS_THREAD_MAX_N * sizeof(srv_slot_t));
for (i = 0; i < OS_THREAD_MAX_N; i++) { for (i = 0; i < OS_THREAD_MAX_N; i++) {
slot = srv_mysql_table + i; slot = srv_mysql_table + i;
slot->in_use = FALSE;
slot->type = 0;
slot->event = os_event_create(NULL); slot->event = os_event_create(NULL);
ut_a(slot->event); ut_a(slot->event);
} }
...@@ -1501,7 +1488,7 @@ srv_table_reserve_slot_for_mysql(void) ...@@ -1501,7 +1488,7 @@ srv_table_reserve_slot_for_mysql(void)
while (slot->in_use) { while (slot->in_use) {
i++; i++;
if (i >= OS_THREAD_MAX_N) { if (UNIV_UNLIKELY(i >= OS_THREAD_MAX_N)) {
ut_print_timestamp(stderr); ut_print_timestamp(stderr);
...@@ -2489,7 +2476,7 @@ srv_is_any_background_thread_active(void) ...@@ -2489,7 +2476,7 @@ srv_is_any_background_thread_active(void)
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
for (i = SRV_COM; i <= SRV_MASTER; ++i) { for (i = 0; i <= SRV_MASTER; ++i) {
if (srv_n_threads_active[i] != 0) { if (srv_n_threads_active[i] != 0) {
ret = TRUE; ret = TRUE;
break; break;
...@@ -2643,7 +2630,7 @@ srv_master_thread( ...@@ -2643,7 +2630,7 @@ srv_master_thread(
os_thread_create */ os_thread_create */
{ {
buf_pool_stat_t buf_stat; buf_pool_stat_t buf_stat;
os_event_t event; srv_slot_t* slot;
ulint old_activity_count; ulint old_activity_count;
ulint n_pages_purged = 0; ulint n_pages_purged = 0;
ulint n_bytes_merged; ulint n_bytes_merged;
...@@ -2671,7 +2658,7 @@ srv_master_thread( ...@@ -2671,7 +2658,7 @@ srv_master_thread(
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
srv_table_reserve_slot(SRV_MASTER); slot = srv_table_reserve_slot(SRV_MASTER);
srv_n_threads_active[SRV_MASTER]++; srv_n_threads_active[SRV_MASTER]++;
...@@ -3060,7 +3047,7 @@ srv_master_thread( ...@@ -3060,7 +3047,7 @@ srv_master_thread(
goto loop; goto loop;
} }
event = srv_suspend_thread(); srv_suspend_thread(slot);
mutex_exit(&kernel_mutex); mutex_exit(&kernel_mutex);
...@@ -3070,7 +3057,7 @@ srv_master_thread( ...@@ -3070,7 +3057,7 @@ srv_master_thread(
manual also mentions this string in several places. */ manual also mentions this string in several places. */
srv_main_thread_op_info = "waiting for server activity"; srv_main_thread_op_info = "waiting for server activity";
os_event_wait(event); os_event_wait(slot->event);
if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) { if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
/* This is only extra safety, the thread should exit /* This is only extra safety, the thread should exit
...@@ -3100,7 +3087,6 @@ srv_purge_thread( ...@@ -3100,7 +3087,6 @@ srv_purge_thread(
{ {
srv_slot_t* slot; srv_slot_t* slot;
ulint retries = 0; ulint retries = 0;
ulint slot_no = ULINT_UNDEFINED;
ulint n_total_purged = ULINT_UNDEFINED; ulint n_total_purged = ULINT_UNDEFINED;
ut_a(srv_n_purge_threads == 1); ut_a(srv_n_purge_threads == 1);
...@@ -3116,9 +3102,7 @@ srv_purge_thread( ...@@ -3116,9 +3102,7 @@ srv_purge_thread(
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
slot_no = srv_table_reserve_slot(SRV_WORKER); slot = srv_table_reserve_slot(SRV_WORKER);
slot = srv_table_get_nth_slot(slot_no);
++srv_n_threads_active[SRV_WORKER]; ++srv_n_threads_active[SRV_WORKER];
...@@ -3137,15 +3121,13 @@ srv_purge_thread( ...@@ -3137,15 +3121,13 @@ srv_purge_thread(
|| (n_total_purged == 0 || (n_total_purged == 0
&& retries >= TRX_SYS_N_RSEGS)) { && retries >= TRX_SYS_N_RSEGS)) {
os_event_t event;
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
event = srv_suspend_thread(); srv_suspend_thread(slot);
mutex_exit(&kernel_mutex); mutex_exit(&kernel_mutex);
os_event_wait(event); os_event_wait(slot->event);
retries = 0; retries = 0;
} }
...@@ -3179,16 +3161,11 @@ srv_purge_thread( ...@@ -3179,16 +3161,11 @@ srv_purge_thread(
mutex_enter(&kernel_mutex); mutex_enter(&kernel_mutex);
ut_ad(srv_table_get_nth_slot(slot_no) == slot);
/* Decrement the active count. */ /* Decrement the active count. */
srv_suspend_thread(); srv_suspend_thread(slot);
slot->in_use = FALSE; slot->in_use = FALSE;
/* Free the thread local memory. */
thr_local_free(os_thread_get_curr_id());
mutex_exit(&kernel_mutex); mutex_exit(&kernel_mutex);
#ifdef UNIV_DEBUG_THREAD_CREATION #ifdef UNIV_DEBUG_THREAD_CREATION
......
...@@ -65,8 +65,6 @@ for the field. */ ...@@ -65,8 +65,6 @@ for the field. */
struct thr_local_struct{ struct thr_local_struct{
os_thread_id_t id; /*!< id of the thread which owns this struct */ os_thread_id_t id; /*!< id of the thread which owns this struct */
os_thread_t handle; /*!< operating system handle to the thread */ os_thread_t handle; /*!< operating system handle to the thread */
ulint slot_no;/*!< the index of the slot in the thread table
for this thread */
ibool in_ibuf;/*!< TRUE if the thread is doing an ibuf ibool in_ibuf;/*!< TRUE if the thread is doing an ibuf
operation */ operation */
hash_node_t hash; /*!< hash chain node */ hash_node_t hash; /*!< hash chain node */
...@@ -87,8 +85,6 @@ thr_local_validate( ...@@ -87,8 +85,6 @@ thr_local_validate(
const thr_local_t* local) /*!< in: data to validate */ const thr_local_t* local) /*!< in: data to validate */
{ {
ut_ad(local->magic_n == THR_LOCAL_MAGIC_N); ut_ad(local->magic_n == THR_LOCAL_MAGIC_N);
ut_ad(local->slot_no == ULINT_UNDEFINED
|| local->slot_no < OS_THREAD_MAX_N);
ut_ad(local->in_ibuf == FALSE || local->in_ibuf == TRUE); ut_ad(local->in_ibuf == FALSE || local->in_ibuf == TRUE);
return(TRUE); return(TRUE);
} }
...@@ -131,49 +127,6 @@ thr_local_get( ...@@ -131,49 +127,6 @@ thr_local_get(
return(local); return(local);
} }
/*******************************************************************//**
Gets the slot number in the thread table of a thread.
@return slot number */
UNIV_INTERN
ulint
thr_local_get_slot_no(
/*==================*/
os_thread_id_t id) /*!< in: thread id of the thread */
{
ulint slot_no;
thr_local_t* local;
mutex_enter(&thr_local_mutex);
local = thr_local_get(id);
slot_no = local->slot_no;
mutex_exit(&thr_local_mutex);
return(slot_no);
}
/*******************************************************************//**
Sets the slot number in the thread table of a thread. */
UNIV_INTERN
void
thr_local_set_slot_no(
/*==================*/
os_thread_id_t id, /*!< in: thread id of the thread */
ulint slot_no)/*!< in: slot number */
{
thr_local_t* local;
mutex_enter(&thr_local_mutex);
local = thr_local_get(id);
local->slot_no = slot_no;
mutex_exit(&thr_local_mutex);
}
/*******************************************************************//** /*******************************************************************//**
Returns pointer to the 'in_ibuf' field within the current thread local Returns pointer to the 'in_ibuf' field within the current thread local
storage. storage.
...@@ -212,7 +165,6 @@ thr_local_create(void) ...@@ -212,7 +165,6 @@ thr_local_create(void)
local->id = os_thread_get_curr_id(); local->id = os_thread_get_curr_id();
local->handle = os_thread_get_curr(); local->handle = os_thread_get_curr();
local->magic_n = THR_LOCAL_MAGIC_N; local->magic_n = THR_LOCAL_MAGIC_N;
local->slot_no = ULINT_UNDEFINED;
local->in_ibuf = FALSE; local->in_ibuf = FALSE;
mutex_enter(&thr_local_mutex); mutex_enter(&thr_local_mutex);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment