| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 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 |
/** @file ha/hash0hash.cc |
| 29 |
The simple hash table utility |
| 30 |
|
| 31 |
Created 5/20/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "hash0hash.h" |
| 35 |
#include "mem0mem.h" |
| 36 |
#include "sync0sync.h" |
| 37 |
#include "ut0math.h" |
| 38 |
|
| 39 |
#ifndef UNIV_HOTBACKUP |
| 40 |
|
| 41 |
#ifdef UNIV_DEBUG |
| 42 |
|
| 43 |
bool hash_lock_has_all_x(const hash_table_t *table) { |
| 44 |
ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); |
| 45 |
|
| 46 |
for (size_t i = 0; i < table->n_sync_obj; i++) { |
| 47 |
if (!rw_lock_own(table->rw_locks + i, RW_LOCK_X)) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
} |
| 51 |
return true; |
| 52 |
} |
| 53 |
#endif |
| 54 |
/** Reserves all the locks of a hash table, in an ascending order. */ |
| 55 |
void hash_lock_x_all(hash_table_t *table) /*!< in: hash table */ |
| 56 |
{ |
| 57 |
ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); |
| 58 |
|
| 59 |
for (size_t i = 0; i < table->n_sync_obj; i++) { |
| 60 |
rw_lock_t *lock = table->rw_locks + i; |
| 61 |
|
| 62 |
ut_ad(!rw_lock_own(lock, RW_LOCK_S)); |
| 63 |
ut_ad(!rw_lock_own(lock, RW_LOCK_X)); |
| 64 |
|
| 65 |
rw_lock_x_lock(lock, UT_LOCATION_HERE); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** Releases all the locks of a hash table, in an ascending order. */ |
| 70 |
void hash_unlock_x_all(hash_table_t *table) /*!< in: hash table */ |
| 71 |
{ |
| 72 |
ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); |
| 73 |
|
| 74 |
for (size_t i = 0; i < table->n_sync_obj; i++) { |
| 75 |
rw_lock_t *lock = table->rw_locks + i; |
| 76 |
|
| 77 |
ut_ad(rw_lock_own(lock, RW_LOCK_X)); |
| 78 |
|
| 79 |
rw_lock_x_unlock(lock); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** Releases all but passed in lock of a hash table, */ |
| 84 |
void hash_unlock_x_all_but(hash_table_t *table, /*!< in: hash table */ |
| 85 |
rw_lock_t *keep_lock) /*!< in: lock to keep */ |
| 86 |
{ |
| 87 |
ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); |
| 88 |
|
| 89 |
for (size_t i = 0; i < table->n_sync_obj; i++) { |
| 90 |
rw_lock_t *lock = table->rw_locks + i; |
| 91 |
|
| 92 |
ut_ad(rw_lock_own(lock, RW_LOCK_X)); |
| 93 |
|
| 94 |
if (keep_lock != lock) { |
| 95 |
rw_lock_x_unlock(lock); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#endif /* !UNIV_HOTBACKUP */ |
| 101 |
|
| 102 |
#ifndef UNIV_HOTBACKUP |
| 103 |
|
| 104 |
void hash_create_sync_obj(hash_table_t *table, latch_id_t id, |
| 105 |
size_t n_sync_obj) { |
| 106 |
ut_a(n_sync_obj > 0); |
| 107 |
ut_a(ut_is_2pow(n_sync_obj)); |
| 108 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 109 |
table->type = HASH_TABLE_SYNC_RW_LOCK; |
| 110 |
|
| 111 |
ut_a(sync_latch_get_level(id) != SYNC_UNKNOWN); |
| 112 |
|
| 113 |
table->rw_locks = static_cast<rw_lock_t *>(ut::malloc_withkey( |
| 114 |
UT_NEW_THIS_FILE_PSI_KEY, n_sync_obj * sizeof(rw_lock_t))); |
| 115 |
|
| 116 |
for (size_t i = 0; i < n_sync_obj; i++) { |
| 117 |
rw_lock_create(hash_table_locks_key, table->rw_locks + i, id); |
| 118 |
} |
| 119 |
|
| 120 |
table->n_sync_obj = n_sync_obj; |
| 121 |
} |
| 122 |
#endif /* !UNIV_HOTBACKUP */ |