From 97b2392ede450d4608c392d335c625c4fe9b308f Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Tue, 10 Sep 2024 14:31:26 +0200 Subject: [PATCH] cleanup: TABLE_SHARE::lock_share() helper also: renames, s/const/constexpr/ for consistency --- sql/sql_base.cc | 20 ++++++-------------- sql/sql_table.cc | 16 +++++++++------- sql/sql_table.h | 10 +++++----- sql/table.cc | 6 ++---- sql/table.h | 3 +++ sql/vector_mhnsw.cc | 10 ++++------ 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 1961e9c195e..664b58d5afc 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -9845,12 +9845,10 @@ int TABLE::hlindex_open(uint nr) DBUG_ASSERT(nr == s->keys); if (!hlindex) { - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_lock(&s->LOCK_share); + s->lock_share(); if (!s->hlindex) { - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_unlock(&s->LOCK_share); + s->unlock_share(); TABLE_SHARE *share; char *path= NULL; size_t path_len= s->normalized_path.length + HLINDEX_BUF_LEN; @@ -9874,26 +9872,20 @@ int TABLE::hlindex_open(uint nr) return 1; } - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_lock(&s->LOCK_share); + s->lock_share(); if (!s->hlindex) { s->hlindex= share; - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_unlock(&s->LOCK_share); + s->unlock_share(); } else { - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_unlock(&s->LOCK_share); + s->unlock_share(); free_table_share(share); } } else - { - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_unlock(&s->LOCK_share); - } + s->unlock_share(); TABLE *table= (TABLE*)alloc_root(&mem_root, sizeof(*table)); if (!table || open_table_from_share(in_use, s->hlindex, &empty_clex_str, db_stat, EXTRA_RECORD, in_use->open_options, table, 0)) diff --git a/sql/sql_table.cc b/sql/sql_table.cc index abc4f39e5c8..dec223a1813 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -2082,13 +2082,14 @@ bool log_drop_table(THD *thd, const LEX_CSTRING *db_name, } -static int get_hlindex_keys(THD *thd, const LEX_CSTRING *db, - const LEX_CSTRING *table_name, const char *path, - uint *keys, uint *total_keys) +static int get_hlindex_keys_by_open(THD *thd, const LEX_CSTRING *db, + const LEX_CSTRING *table_name, + const char *path, uint *keys, + uint *total_keys) { TABLE_SHARE share; int error; - DBUG_ENTER("get_hlindex_keys"); + DBUG_ENTER("get_hlindex_keys_by_open"); init_tmp_table_share(thd, &share, db->str, 0, table_name->str, path, 1); error= open_table_def(thd, &share, GTS_TABLE | GTS_USE_DISCOVERY); @@ -2145,8 +2146,8 @@ bool quick_rm_table(THD *thd, handlerton *base, const LEX_CSTRING *db, if (flags & QRMT_HANDLER) { uint keys, total_keys; - int hlindex_error= get_hlindex_keys(thd, db, table_name, path, &keys, - &total_keys); + int hlindex_error= get_hlindex_keys_by_open(thd, db, table_name, path, + &keys, &total_keys); error|= ha_delete_table(thd, base, path, db, table_name, 0) > 0; if (!hlindex_error) { @@ -5450,7 +5451,8 @@ mysql_rename_table(handlerton *base, const LEX_CSTRING *old_db, char *idx_to_end= strmov(idx_to, to_base); uint keys, total_keys; - if (!get_hlindex_keys(thd, new_db, new_name, to, &keys, &total_keys)) + if (!get_hlindex_keys_by_open(thd, new_db, new_name, to, &keys, + &total_keys)) { for (uint i= keys; i < total_keys; i++) { diff --git a/sql/sql_table.h b/sql/sql_table.h index 787b8349d9d..6a9806c449a 100644 --- a/sql/sql_table.h +++ b/sql/sql_table.h @@ -63,9 +63,9 @@ enum enum_explain_filename_mode #define WFRM_BACKUP_ORIGINAL 16 /* Flags for conversion functions. */ -static const uint FN_FROM_IS_TMP= 1 << 0; -static const uint FN_TO_IS_TMP= 1 << 1; -static const uint FN_IS_TMP= FN_FROM_IS_TMP | FN_TO_IS_TMP; +static constexpr uint FN_FROM_IS_TMP= 1 << 0; +static constexpr uint FN_TO_IS_TMP= 1 << 1; +static constexpr uint FN_IS_TMP= FN_FROM_IS_TMP | FN_TO_IS_TMP; /* Remove .frm table metadata. */ static constexpr uint QRMT_FRM= 1 << 2; /* Remove .par partitioning metadata. */ @@ -75,9 +75,9 @@ static constexpr uint QRMT_HANDLER= 1 << 4; /* Default behaviour is to drop .FRM and handler, but not .par. */ static constexpr uint QRMT_DEFAULT= QRMT_FRM | QRMT_HANDLER; /** Don't resolve MySQL's fake "foo.sym" symbolic directory names. */ -static const uint SKIP_SYMDIR_ACCESS= 1 << 5; +static constexpr uint SKIP_SYMDIR_ACCESS= 1 << 5; /** Don't check foreign key constraints while renaming table */ -static const uint NO_FK_CHECKS= 1 << 6; +static constexpr uint NO_FK_CHECKS= 1 << 6; uint filename_to_tablename(const char *from, char *to, size_t to_length, bool stay_quiet = false); diff --git a/sql/table.cc b/sql/table.cc index 08ac70da752..55ac38e037e 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -8287,8 +8287,7 @@ void TABLE::mark_columns_used_by_virtual_fields(void) if (s->check_set_initialized) return; - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_lock(&s->LOCK_share); + s->lock_share(); if (s->check_set) { /* Mark fields used by check constraint */ @@ -8328,8 +8327,7 @@ void TABLE::mark_columns_used_by_virtual_fields(void) bitmap_clear_all(&tmp_set); } s->check_set_initialized= v_keys; - if (s->tmp_table == NO_TMP_TABLE) - mysql_mutex_unlock(&s->LOCK_share); + s->unlock_share(); } /* Add fields used by CHECK CONSTRAINT to read map */ diff --git a/sql/table.h b/sql/table.h index ed3815d7d9f..6cde4c8ea6d 100644 --- a/sql/table.h +++ b/sql/table.h @@ -729,6 +729,9 @@ struct TABLE_SHARE mysql_mutex_t LOCK_share; /* To protect TABLE_SHARE */ mysql_mutex_t LOCK_statistics; /* To protect against concurrent load */ + void lock_share() { if (!tmp_table) mysql_mutex_lock(&LOCK_share); } + void unlock_share() { if (!tmp_table) mysql_mutex_unlock(&LOCK_share); } + TDC_element *tdc; LEX_CUSTRING tabledef_version; diff --git a/sql/vector_mhnsw.cc b/sql/vector_mhnsw.cc index fa6bd4113d1..abe935884a0 100644 --- a/sql/vector_mhnsw.cc +++ b/sql/vector_mhnsw.cc @@ -392,13 +392,13 @@ public: virtual void reset(TABLE_SHARE *share) { - mysql_mutex_lock(&share->LOCK_share); + share->lock_share(); if (static_cast(share->hlindex->hlindex_data) == this) { share->hlindex->hlindex_data= nullptr; --refcnt; } - mysql_mutex_unlock(&share->LOCK_share); + share->unlock_share(); } void release(TABLE *table) @@ -603,8 +603,7 @@ MHNSW_Trx *MHNSW_Trx::get_from_thd(TABLE *table, bool for_update) MHNSW_Context *MHNSW_Context::get_from_share(TABLE_SHARE *share, TABLE *table) { - if (share->tmp_table == NO_TMP_TABLE) - mysql_mutex_lock(&share->LOCK_share); + share->lock_share(); auto ctx= static_cast(share->hlindex->hlindex_data); if (!ctx && table) { @@ -615,8 +614,7 @@ MHNSW_Context *MHNSW_Context::get_from_share(TABLE_SHARE *share, TABLE *table) } if (ctx) ctx->refcnt++; - if (share->tmp_table == NO_TMP_TABLE) - mysql_mutex_unlock(&share->LOCK_share); + share->unlock_share(); return ctx; }