1733
1733
Enqueues a waiting request for a lock which cannot be granted immediately.
1734
1734
Checks for deadlocks.
1735
1735
@return DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED, or
1736
DB_SUCCESS; DB_SUCCESS means that there was a deadlock, but another
1737
transaction was chosen as a victim, and we got the lock immediately:
1738
no need to wait then */
1736
DB_SUCCESS_LOCKED_REC; DB_SUCCESS_LOCKED_REC means that
1737
there was a deadlock, but another transaction was chosen as a victim,
1738
and we got the lock immediately: no need to wait then */
1741
1741
lock_rec_enqueue_waiting(
1742
1742
/*=====================*/
1743
1743
ulint type_mode,/*!< in: lock mode this
1925
1925
return(lock_rec_create(type_mode, block, heap_no, index, trx));
1928
/** Record locking request status */
1929
enum lock_rec_req_status {
1930
/** Failed to acquire a lock */
1932
/** Succeeded in acquiring a lock (implicit or already acquired) */
1934
/** Explicitly created a new lock */
1935
LOCK_REC_SUCCESS_CREATED
1928
1938
/*********************************************************************//**
1929
1939
This is a fast routine for locking a record in the most common cases:
1930
1940
there are no explicit locks on the page, or there is just one lock, owned
1932
1942
which does NOT look at implicit locks! Checks lock compatibility within
1933
1943
explicit locks. This function sets a normal next-key lock, or in the case of
1934
1944
a page supremum record, a gap type lock.
1935
@return TRUE if locking succeeded */
1945
@return whether the locking succeeded */
1947
enum lock_rec_req_status
1938
1948
lock_rec_lock_fast(
1939
1949
/*===============*/
1940
1950
ibool impl, /*!< in: if TRUE, no lock is set
1973
1983
lock_rec_create(mode, block, heap_no, index, trx);
1986
return(LOCK_REC_SUCCESS_CREATED);
1979
1989
if (lock_rec_get_next_on_page(lock)) {
1991
return(LOCK_REC_FAIL);
1984
1994
if (lock->trx != trx
1985
1995
|| lock->type_mode != (mode | LOCK_REC)
1986
1996
|| lock_rec_get_n_bits(lock) <= heap_no) {
1998
return(LOCK_REC_FAIL);
1995
2005
if (!lock_rec_get_nth_bit(lock, heap_no)) {
1996
2006
lock_rec_set_nth_bit(lock, heap_no);
2007
return(LOCK_REC_SUCCESS_CREATED);
2011
return(LOCK_REC_SUCCESS);
2003
2014
/*********************************************************************//**
2005
2016
low-level function which does NOT look at implicit locks! Checks lock
2006
2017
compatibility within explicit locks. This function sets a normal next-key
2007
2018
lock, or in the case of a page supremum record, a gap type lock.
2008
@return DB_SUCCESS, DB_LOCK_WAIT, or error code */
2019
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, DB_LOCK_WAIT, DB_DEADLOCK,
2020
or DB_QUE_THR_SUSPENDED */
2011
2023
lock_rec_lock_slow(
2012
2024
/*===============*/
2013
2025
ibool impl, /*!< in: if TRUE, no lock is set
2043
2054
/* The trx already has a strong enough lock on rec: do
2047
2057
} else if (lock_rec_other_has_conflicting(mode, block, heap_no, trx)) {
2049
2059
/* If another transaction has a non-gap conflicting request in
2050
2060
the queue, as this transaction does not have a lock strong
2051
2061
enough already granted on the record, we have to wait. */
2053
err = lock_rec_enqueue_waiting(mode, block, heap_no,
2057
/* Set the requested lock on the record */
2059
lock_rec_add_to_queue(LOCK_REC | mode, block,
2060
heap_no, index, trx);
2063
return(lock_rec_enqueue_waiting(mode, block, heap_no,
2066
/* Set the requested lock on the record */
2068
lock_rec_add_to_queue(LOCK_REC | mode, block,
2069
heap_no, index, trx);
2070
return(DB_SUCCESS_LOCKED_REC);
2069
2076
/*********************************************************************//**
2072
2079
which does NOT look at implicit locks! Checks lock compatibility within
2073
2080
explicit locks. This function sets a normal next-key lock, or in the case
2074
2081
of a page supremum record, a gap type lock.
2075
@return DB_SUCCESS, DB_LOCK_WAIT, or error code */
2082
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, DB_LOCK_WAIT, DB_DEADLOCK,
2083
or DB_QUE_THR_SUSPENDED */
2080
2088
ibool impl, /*!< in: if TRUE, no lock is set
2090
2098
dict_index_t* index, /*!< in: index of record */
2091
2099
que_thr_t* thr) /*!< in: query thread */
2095
2101
ut_ad(mutex_own(&kernel_mutex));
2096
2102
ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
2097
2103
|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
2103
2109
|| mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP
2104
2110
|| mode - (LOCK_MODE_MASK & mode) == 0);
2106
if (lock_rec_lock_fast(impl, mode, block, heap_no, index, thr)) {
2108
/* We try a simplified and faster subroutine for the most
2113
err = lock_rec_lock_slow(impl, mode, block,
2114
heap_no, index, thr);
2112
/* We try a simplified and faster subroutine for the most
2114
switch (lock_rec_lock_fast(impl, mode, block, heap_no, index, thr)) {
2115
case LOCK_REC_SUCCESS:
2117
case LOCK_REC_SUCCESS_CREATED:
2118
return(DB_SUCCESS_LOCKED_REC);
2120
return(lock_rec_lock_slow(impl, mode, block,
2121
heap_no, index, thr));
2120
2128
/*********************************************************************//**
3947
3955
mutex_enter(&kernel_mutex);
3949
lock = lock_rec_get_first(block, heap_no);
3957
first_lock = lock_rec_get_first(block, heap_no);
3951
3959
/* Find the last lock with the same lock_mode and transaction
3952
3960
from the record. */
3954
while (lock != NULL) {
3962
for (lock = first_lock; lock != NULL;
3963
lock = lock_rec_get_next(heap_no, lock)) {
3955
3964
if (lock->trx == trx && lock_get_mode(lock) == lock_mode) {
3956
release_lock = lock;
3957
3965
ut_a(!lock_get_wait(lock));
3966
lock_rec_reset_nth_bit(lock, heap_no);
3960
lock = lock_rec_get_next(heap_no, lock);
3963
/* If a record lock is found, release the record lock */
3965
if (UNIV_LIKELY(release_lock != NULL)) {
3966
lock_rec_reset_nth_bit(release_lock, heap_no);
3968
mutex_exit(&kernel_mutex);
3969
ut_print_timestamp(stderr);
3971
" InnoDB: Error: unlock row could not"
3972
" find a %lu mode lock on the record\n",
3971
mutex_exit(&kernel_mutex);
3972
ut_print_timestamp(stderr);
3974
" InnoDB: Error: unlock row could not"
3975
" find a %lu mode lock on the record\n",
3978
3981
/* Check if we can now grant waiting lock requests */
3980
lock = lock_rec_get_first(block, heap_no);
3982
while (lock != NULL) {
3983
for (lock = first_lock; lock != NULL;
3984
lock = lock_rec_get_next(heap_no, lock)) {
3983
3985
if (lock_get_wait(lock)
3984
3986
&& !lock_rec_has_to_wait_in_queue(lock)) {
3986
3988
/* Grant the lock */
3987
3989
lock_grant(lock);
3990
lock = lock_rec_get_next(heap_no, lock);
3993
3993
mutex_exit(&kernel_mutex);
5081
5081
lock_mutex_exit_kernel();
5083
if ((err == DB_SUCCESS) && !dict_index_is_clust(index)) {
5084
case DB_SUCCESS_LOCKED_REC:
5088
if (dict_index_is_clust(index)) {
5084
5091
/* Update the page max trx id field */
5085
5092
page_update_max_trx_id(block,
5086
5093
buf_block_get_page_zip(block),
5270
5281
#endif /* UNIV_DEBUG */
5272
if (err == DB_SUCCESS) {
5283
if (err == DB_SUCCESS || err == DB_SUCCESS_LOCKED_REC) {
5273
5284
/* Update the page max trx id field */
5285
/* It might not be necessary to do this if
5286
err == DB_SUCCESS (no new lock created),
5287
but it should not cost too much performance. */
5274
5288
page_update_max_trx_id(block,
5275
5289
buf_block_get_page_zip(block),
5276
5290
thr_get_trx(thr)->id, mtr);
5282
5297
/*********************************************************************//**
5283
Like the counterpart for a clustered index below, but now we read a
5298
Like lock_clust_rec_read_check_and_lock(), but reads a
5284
5299
secondary index record.
5285
@return DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5300
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, DB_LOCK_WAIT, DB_DEADLOCK,
5301
or DB_QUE_THR_SUSPENDED */
5288
5304
lock_sec_rec_read_check_and_lock(
5289
5305
/*=============================*/
5290
5306
ulint flags, /*!< in: if BTR_NO_LOCKING_FLAG
5357
5373
puts the transaction and the query thread to the lock wait state and inserts a
5358
5374
waiting request for a record lock to the lock queue. Sets the requested mode
5359
5375
lock on the record.
5360
@return DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5376
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, DB_LOCK_WAIT, DB_DEADLOCK,
5377
or DB_QUE_THR_SUSPENDED */
5363
5380
lock_clust_rec_read_check_and_lock(
5364
5381
/*===============================*/
5365
5382
ulint flags, /*!< in: if BTR_NO_LOCKING_FLAG
5452
5469
mem_heap_t* tmp_heap = NULL;
5453
5470
ulint offsets_[REC_OFFS_NORMAL_SIZE];
5454
5471
ulint* offsets = offsets_;
5456
5473
rec_offs_init(offsets_);
5458
5475
offsets = rec_get_offsets(rec, index, offsets,
5459
5476
ULINT_UNDEFINED, &tmp_heap);
5460
ret = lock_clust_rec_read_check_and_lock(flags, block, rec, index,
5477
err = lock_clust_rec_read_check_and_lock(flags, block, rec, index,
5461
5478
offsets, mode, gap_mode, thr);
5462
5479
if (tmp_heap) {
5463
5480
mem_heap_free(tmp_heap);
5483
if (UNIV_UNLIKELY(err == DB_SUCCESS_LOCKED_REC)) {
5468
5490
/*******************************************************************//**