Skip to content

Commit ccdfced

Browse files
committed
MDEV-22693 - InnoDB: get rid of casts for rw_trx_hash iterator
Less error prone, stricter type control.
1 parent 043828b commit ccdfced

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

storage/innobase/include/trx0sys.h

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ class rw_trx_hash_t
385385
LF_HASH hash;
386386

387387

388+
template <typename T>
389+
using walk_action= my_bool(rw_trx_hash_element_t *element, T *action);
390+
391+
388392
/**
389393
Constructor callback for lock-free allocator.
390394
@@ -487,18 +491,19 @@ class rw_trx_hash_t
487491
}
488492

489493

490-
struct eliminate_duplicates_arg
494+
template <typename T> struct eliminate_duplicates_arg
491495
{
492496
trx_ids_t ids;
493-
my_hash_walk_action action;
494-
void *argument;
495-
eliminate_duplicates_arg(size_t size, my_hash_walk_action act, void* arg):
497+
walk_action<T> *action;
498+
T *argument;
499+
eliminate_duplicates_arg(size_t size, walk_action<T> *act, T *arg):
496500
action(act), argument(arg) { ids.reserve(size); }
497501
};
498502

499503

504+
template <typename T>
500505
static my_bool eliminate_duplicates(rw_trx_hash_element_t *element,
501-
eliminate_duplicates_arg *arg)
506+
eliminate_duplicates_arg<T> *arg)
502507
{
503508
for (trx_ids_t::iterator it= arg->ids.begin(); it != arg->ids.end(); it++)
504509
{
@@ -525,15 +530,16 @@ class rw_trx_hash_t
525530
}
526531

527532

528-
struct debug_iterator_arg
533+
template <typename T> struct debug_iterator_arg
529534
{
530-
my_hash_walk_action action;
531-
void *argument;
535+
walk_action<T> *action;
536+
T *argument;
532537
};
533538

534539

540+
template <typename T>
535541
static my_bool debug_iterator(rw_trx_hash_element_t *element,
536-
debug_iterator_arg *arg)
542+
debug_iterator_arg<T> *arg)
537543
{
538544
mutex_enter(&element->mutex);
539545
if (element->trx)
@@ -748,23 +754,28 @@ class rw_trx_hash_t
748754
@retval 1 iteration was interrupted (action returned 1)
749755
*/
750756

751-
int iterate(trx_t *caller_trx, my_hash_walk_action action, void *argument)
757+
template <typename T>
758+
int iterate(trx_t *caller_trx, walk_action<T> *action, T *argument= nullptr)
752759
{
753760
LF_PINS *pins= caller_trx ? get_pins(caller_trx) : lf_hash_get_pins(&hash);
754761
ut_a(pins);
755762
#ifdef UNIV_DEBUG
756-
debug_iterator_arg debug_arg= { action, argument };
757-
action= reinterpret_cast<my_hash_walk_action>(debug_iterator);
758-
argument= &debug_arg;
763+
debug_iterator_arg<T> debug_arg= { action, argument };
764+
action= reinterpret_cast<decltype(action)>(debug_iterator<T>);
765+
argument= reinterpret_cast<T*>(&debug_arg);
759766
#endif
760-
int res= lf_hash_iterate(&hash, pins, action, argument);
767+
int res= lf_hash_iterate(&hash, pins,
768+
reinterpret_cast<my_hash_walk_action>(action),
769+
const_cast<void*>(static_cast<const void*>
770+
(argument)));
761771
if (!caller_trx)
762772
lf_hash_put_pins(pins);
763773
return res;
764774
}
765775

766776

767-
int iterate(my_hash_walk_action action, void *argument)
777+
template <typename T>
778+
int iterate(walk_action<T> *action, T *argument= nullptr)
768779
{
769780
return iterate(current_trx(), action, argument);
770781
}
@@ -776,16 +787,17 @@ class rw_trx_hash_t
776787
@sa iterate()
777788
*/
778789

779-
int iterate_no_dups(trx_t *caller_trx, my_hash_walk_action action,
780-
void *argument)
790+
template <typename T>
791+
int iterate_no_dups(trx_t *caller_trx, walk_action<T> *action,
792+
T *argument= nullptr)
781793
{
782-
eliminate_duplicates_arg arg(size() + 32, action, argument);
783-
return iterate(caller_trx, reinterpret_cast<my_hash_walk_action>
784-
(eliminate_duplicates), &arg);
794+
eliminate_duplicates_arg<T> arg(size() + 32, action, argument);
795+
return iterate(caller_trx, eliminate_duplicates<T>, &arg);
785796
}
786797

787798

788-
int iterate_no_dups(my_hash_walk_action action, void *argument)
799+
template <typename T>
800+
int iterate_no_dups(walk_action<T> *action, T *argument= nullptr)
789801
{
790802
return iterate_no_dups(current_trx(), action, argument);
791803
}
@@ -881,8 +893,7 @@ class trx_sys_t
881893
trx_id_t get_min_trx_id()
882894
{
883895
trx_id_t id= get_max_trx_id();
884-
rw_trx_hash.iterate(reinterpret_cast<my_hash_walk_action>
885-
(get_min_trx_id_callback), &id);
896+
rw_trx_hash.iterate(get_min_trx_id_callback, &id);
886897
return id;
887898
}
888899

@@ -976,9 +987,7 @@ class trx_sys_t
976987

977988
ids->clear();
978989
ids->reserve(rw_trx_hash.size() + 32);
979-
rw_trx_hash.iterate(caller_trx,
980-
reinterpret_cast<my_hash_walk_action>(copy_one_id),
981-
&arg);
990+
rw_trx_hash.iterate(caller_trx, copy_one_id, &arg);
982991

983992
*max_trx_id= arg.m_id;
984993
*min_trx_no= arg.m_no;

storage/innobase/lock/lock0lock.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5157,8 +5157,7 @@ lock_validate()
51575157
lock_mutex_enter();
51585158

51595159
/* Validate table locks */
5160-
trx_sys.rw_trx_hash.iterate(reinterpret_cast<my_hash_walk_action>
5161-
(lock_validate_table_locks), 0);
5160+
trx_sys.rw_trx_hash.iterate(lock_validate_table_locks);
51625161

51635162
/* Iterate over all the record locks and validate the locks. We
51645163
don't want to hog the lock_sys_t::mutex. Release it during the
@@ -5452,9 +5451,7 @@ static void lock_rec_other_trx_holds_expl(trx_t *caller_trx, trx_t *trx,
54525451
lock_rec_other_trx_holds_expl_arg arg= { page_rec_get_heap_no(rec), block,
54535452
trx };
54545453
trx_sys.rw_trx_hash.iterate(caller_trx,
5455-
reinterpret_cast<my_hash_walk_action>
5456-
(lock_rec_other_trx_holds_expl_callback),
5457-
&arg);
5454+
lock_rec_other_trx_holds_expl_callback, &arg);
54585455
lock_mutex_exit();
54595456
}
54605457
}
@@ -6233,10 +6230,7 @@ lock_table_has_locks(
62336230

62346231
#ifdef UNIV_DEBUG
62356232
if (!has_locks) {
6236-
trx_sys.rw_trx_hash.iterate(
6237-
reinterpret_cast<my_hash_walk_action>
6238-
(lock_table_locks_lookup),
6239-
const_cast<dict_table_t*>(table));
6233+
trx_sys.rw_trx_hash.iterate(lock_table_locks_lookup, table);
62406234
}
62416235
#endif /* UNIV_DEBUG */
62426236

storage/innobase/trx/trx0roll.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,7 @@ void trx_roll_report_progress()
717717
rows they modified. Numbers must be accurate, because only this
718718
thread is allowed to touch recovered transactions. */
719719
trx_sys.rw_trx_hash.iterate_no_dups(
720-
reinterpret_cast<my_hash_walk_action>
721-
(trx_roll_count_callback), &arg);
720+
trx_roll_count_callback, &arg);
722721

723722
if (arg.n_rows > 0) {
724723
service_manager_extend_timeout(
@@ -776,8 +775,7 @@ void trx_rollback_recovered(bool all)
776775
other thread is allowed to modify or remove these transactions from
777776
rw_trx_hash.
778777
*/
779-
trx_sys.rw_trx_hash.iterate_no_dups(reinterpret_cast<my_hash_walk_action>
780-
(trx_rollback_recovered_callback),
778+
trx_sys.rw_trx_hash.iterate_no_dups(trx_rollback_recovered_callback,
781779
&trx_list);
782780

783781
while (!trx_list.empty())

storage/innobase/trx/trx0trx.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,8 +2135,7 @@ int trx_recover_for_mysql(XID *xid_list, uint len)
21352135
ut_ad(len);
21362136

21372137
/* Fill xid_list with PREPARED transactions. */
2138-
trx_sys.rw_trx_hash.iterate_no_dups(reinterpret_cast<my_hash_walk_action>
2139-
(trx_recover_for_mysql_callback), &arg);
2138+
trx_sys.rw_trx_hash.iterate_no_dups(trx_recover_for_mysql_callback, &arg);
21402139
if (arg.count)
21412140
{
21422141
ib::info() << arg.count
@@ -2146,8 +2145,7 @@ int trx_recover_for_mysql(XID *xid_list, uint len)
21462145
transactions twice, by first calling tc_log->open() and then
21472146
ha_recover() directly. */
21482147
if (arg.count <= len)
2149-
trx_sys.rw_trx_hash.iterate(reinterpret_cast<my_hash_walk_action>
2150-
(trx_recover_reset_callback), NULL);
2148+
trx_sys.rw_trx_hash.iterate(trx_recover_reset_callback);
21512149
}
21522150
return int(std::min(arg.count, len));
21532151
}
@@ -2201,8 +2199,7 @@ trx_t* trx_get_trx_by_xid(const XID* xid)
22012199
trx_get_trx_by_xid_callback_arg arg= { xid, 0 };
22022200

22032201
if (xid)
2204-
trx_sys.rw_trx_hash.iterate(reinterpret_cast<my_hash_walk_action>
2205-
(trx_get_trx_by_xid_callback), &arg);
2202+
trx_sys.rw_trx_hash.iterate(trx_get_trx_by_xid_callback, &arg);
22062203
return arg.trx;
22072204
}
22082205

0 commit comments

Comments
 (0)