| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 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 |
#define LOCK_MODULE_IMPLEMENTATION |
| 29 |
|
| 30 |
#include "lock0latches.h" |
| 31 |
#include "lock0lock.h" |
| 32 |
#include "lock0priv.h" |
| 33 |
|
| 34 |
namespace locksys { |
| 35 |
|
| 36 |
size_t Latches::Page_shards::get_shard(const page_id_t &page_id) { |
| 37 |
/* We always use lock_sys->rec_hash regardless of the exact type of the |
| 38 |
lock. It may happen that the lock is a predicate lock, in which case, it would |
| 39 |
make more sense to use hash_calc_cell_id with proper hash table size. The |
| 40 |
current implementation works, because the size of all three hashmaps is always |
| 41 |
the same. This allows an interface with less arguments. */ |
| 42 |
ut_ad(lock_sys->rec_hash.get_n_cells() == lock_sys->prdt_hash.get_n_cells()); |
| 43 |
ut_ad(lock_sys->rec_hash.get_n_cells() == |
| 44 |
lock_sys->prdt_page_hash.get_n_cells()); |
| 45 |
/* We need a property that if two pages are mapped to the same bucket of the |
| 46 |
hash table, and thus their lock queues are merged, then these two lock queues |
| 47 |
are protected by the same shard. This is why to compute the shard we use the |
| 48 |
cell_id as the input and not the original lock_rec_hash_value's result. */ |
| 49 |
return lock_sys->rec_hash.get_cell_id(lock_rec_hash_value(page_id)) % |
| 50 |
SHARDS_COUNT; |
| 51 |
} |
| 52 |
|
| 53 |
const Lock_mutex &Latches::Page_shards::get_mutex( |
| 54 |
const page_id_t &page_id) const { |
| 55 |
return mutexes[get_shard(page_id)]; |
| 56 |
} |
| 57 |
|
| 58 |
Lock_mutex &Latches::Page_shards::get_mutex(const page_id_t &page_id) { |
| 59 |
/* See "Effective C++ item 3: Use const whenever possible" for explanation of |
| 60 |
this pattern, which avoids code duplication by reusing const version. */ |
| 61 |
return const_cast<Lock_mutex &>( |
| 62 |
const_cast<const Latches::Page_shards *>(this)->get_mutex(page_id)); |
| 63 |
} |
| 64 |
|
| 65 |
Lock_mutex &Latches::Page_shards::get_mutex(const uint64_t cell_id) { |
| 66 |
return mutexes[cell_id % SHARDS_COUNT]; |
| 67 |
} |
| 68 |
|
| 69 |
size_t Latches::Table_shards::get_shard(const table_id_t table_id) { |
| 70 |
return table_id % SHARDS_COUNT; |
| 71 |
} |
| 72 |
|
| 73 |
const Lock_mutex &Latches::Table_shards::get_mutex( |
| 74 |
const table_id_t table_id) const { |
| 75 |
return mutexes[get_shard(table_id)]; |
| 76 |
} |
| 77 |
|
| 78 |
Lock_mutex &Latches::Table_shards::get_mutex(const table_id_t table_id) { |
| 79 |
/* See "Effective C++ item 3: Use const whenever possible" for explanation of |
| 80 |
this pattern, which avoids code duplication by reusing const version. */ |
| 81 |
return const_cast<Lock_mutex &>( |
| 82 |
const_cast<const Latches::Table_shards *>(this)->get_mutex(table_id)); |
| 83 |
} |
| 84 |
|
| 85 |
const Lock_mutex &Latches::Table_shards::get_mutex( |
| 86 |
const dict_table_t &table) const { |
| 87 |
return get_mutex(table.id); |
| 88 |
} |
| 89 |
|
| 90 |
thread_local size_t Latches::Unique_sharded_rw_lock::m_shard_id{NOT_IN_USE}; |
| 91 |
|
| 92 |
Latches::Unique_sharded_rw_lock::Unique_sharded_rw_lock() { |
| 93 |
rw_lock.create( |
| 94 |
#ifdef UNIV_PFS_RWLOCK |
| 95 |
lock_sys_global_rw_lock_key, |
| 96 |
#endif |
| 97 |
LATCH_ID_LOCK_SYS_GLOBAL, 64); |
| 98 |
} |
| 99 |
|
| 100 |
Latches::Unique_sharded_rw_lock::~Unique_sharded_rw_lock() { rw_lock.free(); } |
| 101 |
|
| 102 |
Latches::Page_shards::Page_shards() { |
| 103 |
for (size_t i = 0; i < SHARDS_COUNT; ++i) { |
| 104 |
mutex_create(LATCH_ID_LOCK_SYS_PAGE, mutexes + i); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
Latches::Page_shards::~Page_shards() { |
| 109 |
for (size_t i = 0; i < SHARDS_COUNT; ++i) { |
| 110 |
mutex_destroy(mutexes + i); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
Latches::Table_shards::Table_shards() { |
| 115 |
for (size_t i = 0; i < SHARDS_COUNT; ++i) { |
| 116 |
mutex_create(LATCH_ID_LOCK_SYS_TABLE, mutexes + i); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
Latches::Table_shards::~Table_shards() { |
| 121 |
for (size_t i = 0; i < SHARDS_COUNT; ++i) { |
| 122 |
mutex_destroy(mutexes + i); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
} // namespace locksys |