lock0lock.c 135 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/******************************************************
The transaction lock system

(c) 1996 Innobase Oy

Created 5/7/1996 Heikki Tuuri
*******************************************************/

#include "lock0lock.h"

#ifdef UNIV_NONINL
#include "lock0lock.ic"
#endif

#include "usr0sess.h"
16
#include "trx0purge.h"
unknown's avatar
unknown committed
17 18
#include "dict0mem.h"
#include "trx0sys.h"
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

/* 2 function prototypes copied from ha_innodb.cc: */

/*****************************************************************
If you want to print a thd that is not associated with the current thread,
you must call this function before reserving the InnoDB kernel_mutex, to
protect MySQL from setting thd->query NULL. If you print a thd of the current
thread, we know that MySQL cannot modify thd->query, and it is not necessary
to call this. Call innobase_mysql_end_print_arbitrary_thd() after you release
the kernel_mutex.
NOTE that /mysql/innobase/lock/lock0lock.c must contain the prototype for this
function! */

void
innobase_mysql_prepare_print_arbitrary_thd(void);
/*============================================*/

/*****************************************************************
Relases the mutex reserved by innobase_mysql_prepare_print_arbitrary_thd().
NOTE that /mysql/innobase/lock/lock0lock.c must contain the prototype for this
function! */

void
innobase_mysql_end_print_arbitrary_thd(void);
/*========================================*/

46 47 48 49
/* Restricts the length of search we will do in the waits-for
graph of transactions */
#define LOCK_MAX_N_STEPS_IN_DEADLOCK_CHECK 1000000

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* When releasing transaction locks, this specifies how often we release
the kernel mutex for a moment to give also others access to it */

#define LOCK_RELEASE_KERNEL_INTERVAL	1000

/* Safety margin when creating a new record lock: this many extra records
can be inserted to the page without need to create a lock with a bigger
bitmap */

#define LOCK_PAGE_BITMAP_MARGIN		64

/* An explicit record lock affects both the record and the gap before it.
An implicit x-lock does not affect the gap, it only locks the index
record from read or update. 

If a transaction has modified or inserted an index record, then
it owns an implicit x-lock on the record. On a secondary index record,
a transaction has an implicit x-lock also if it has modified the
clustered index record, the max trx id of the page where the secondary
index record resides is >= trx id of the transaction (or database recovery
is running), and there are no explicit non-gap lock requests on the
secondary index record.

This complicated definition for a secondary index comes from the
implementation: we want to be able to determine if a secondary index
record has an implicit x-lock, just by looking at the present clustered
index record, not at the historical versions of the record. The
complicated definition can be explained to the user so that there is
nondeterminism in the access path when a query is answered: we may,
or may not, access the clustered index record and thus may, or may not,
bump into an x-lock set there.

Different transaction can have conflicting locks set on the gap at the
same time. The locks on the gap are purely inhibitive: an insert cannot
unknown's avatar
unknown committed
84
be made, or a select cursor may have to wait if a different transaction
85
has a conflicting lock on the gap. An x-lock on the gap does not give
unknown's avatar
unknown committed
86
the right to insert into the gap.
87 88 89 90 91 92 93 94 95 96 97 98

An explicit lock can be placed on a user record or the supremum record of
a page. The locks on the supremum record are always thought to be of the gap
type, though the gap bit is not set. When we perform an update of a record
where the size of the record changes, we may temporarily store its explicit
locks on the infimum record of the page, though the infimum otherwise never
carries locks.

A waiting record lock can also be of the gap type. A waiting lock request
can be granted when there is no conflicting mode lock request by another
transaction ahead of it in the explicit lock queue.

unknown's avatar
unknown committed
99 100 101 102 103
In version 4.0.5 we added yet another explicit lock type: LOCK_REC_NOT_GAP.
It only locks the record it is placed on, not the gap before the record.
This lock type is necessary to emulate an Oracle-like READ COMMITTED isolation
level.

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
-------------------------------------------------------------------------
RULE 1: If there is an implicit x-lock on a record, and there are non-gap
-------
lock requests waiting in the queue, then the transaction holding the implicit
x-lock also has an explicit non-gap record x-lock. Therefore, as locks are
released, we can grant locks to waiting lock requests purely by looking at
the explicit lock requests in the queue.

RULE 3: Different transactions cannot have conflicting granted non-gap locks
-------
on a record at the same time. However, they can have conflicting granted gap
locks.
RULE 4: If a there is a waiting lock request in a queue, no lock request,
-------
gap or not, can be inserted ahead of it in the queue. In record deletes
unknown's avatar
unknown committed
119
and page splits new gap type locks can be created by the database manager
120 121 122 123 124 125 126
for a transaction, and without rule 4, the waits-for graph of transactions
might become cyclic without the database noticing it, as the deadlock check
is only performed when a transaction itself requests a lock!
-------------------------------------------------------------------------

An insert is allowed to a gap if there are no explicit lock requests by
other transactions on the next record. It does not matter if these lock
unknown's avatar
unknown committed
127 128 129
requests are granted or waiting, gap bit set or not, with the exception
that a gap type request set by another transaction to wait for
its turn to do an insert is ignored. On the other hand, an
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
implicit x-lock by another transaction does not prevent an insert, which
allows for more concurrency when using an Oracle-style sequence number
generator for the primary key with many transactions doing inserts
concurrently.

A modify of a record is allowed if the transaction has an x-lock on the
record, or if other transactions do not have any non-gap lock requests on the
record.

A read of a single user record with a cursor is allowed if the transaction
has a non-gap explicit, or an implicit lock on the record, or if the other
transactions have no x-lock requests on the record. At a page supremum a
read is always allowed.

In summary, an implicit lock is seen as a granted x-lock only on the
record, not on the gap. An explicit lock with no gap bit set is a lock
both on the record and the gap. If the gap bit is set, the lock is only
on the gap. Different transaction cannot own conflicting locks on the
record at the same time, but they may own conflicting locks on the gap.
Granted locks on a record give an access right to the record, but gap type
locks just inhibit operations.

NOTE: Finding out if some transaction has an implicit x-lock on a secondary
index record can be cumbersome. We may have to look at previous versions of
the corresponding clustered index record to find out if a delete marked
secondary index record was delete marked by an active transaction, not by
a committed one.

FACT A: If a transaction has inserted a row, it can delete it any time
without need to wait for locks.

PROOF: The transaction has an implicit x-lock on every index record inserted
for the row, and can thus modify each record without the need to wait. Q.E.D.

FACT B: If a transaction has read some result set with a cursor, it can read
it again, and retrieves the same result set, if it has not modified the
result set in the meantime. Hence, there is no phantom problem. If the
biggest record, in the alphabetical order, touched by the cursor is removed,
a lock wait may occur, otherwise not.

PROOF: When a read cursor proceeds, it sets an s-lock on each user record
it passes, and a gap type s-lock on each page supremum. The cursor must
wait until it has these locks granted. Then no other transaction can
have a granted x-lock on any of the user records, and therefore cannot
modify the user records. Neither can any other transaction insert into
the gaps which were passed over by the cursor. Page splits and merges,
and removal of obsolete versions of records do not affect this, because
when a user record or a page supremum is removed, the next record inherits
its locks as gap type locks, and therefore blocks inserts to the same gap.
Also, if a page supremum is inserted, it inherits its locks from the successor
record. When the cursor is positioned again at the start of the result set,
the records it will touch on its course are either records it touched
during the last pass or new inserted page supremums. It can immediately
access all these records, and when it arrives at the biggest record, it
notices that the result set is complete. If the biggest record was removed,
lock wait can occur because the next record only inherits a gap type lock,
and a wait may be needed. Q.E.D. */

/* If an index record should be changed or a new inserted, we must check
the lock on the record or the next. When a read cursor starts reading,
we will set a record level s-lock on each record it passes, except on the
initial record on which the cursor is positioned before we start to fetch
records. Our index tree search has the convention that the B-tree
cursor is positioned BEFORE the first possibly matching record in
the search. Optimizations are possible here: if the record is searched
on an equality condition to a unique key, we could actually set a special
lock on the record, a lock which would not prevent any insert before
this record. In the next key locking an x-lock set on a record also
prevents inserts just before that record.
	There are special infimum and supremum records on each page.
A supremum record can be locked by a read cursor. This records cannot be
updated but the lock prevents insert of a user record to the end of
the page.
	Next key locks will prevent the phantom problem where new rows
could appear to SELECT result sets after the select operation has been
performed. Prevention of phantoms ensures the serilizability of
transactions.
	What should we check if an insert of a new record is wanted?
Only the lock on the next record on the same page, because also the
supremum record can carry a lock. An s-lock prevents insertion, but
what about an x-lock? If it was set by a searched update, then there
is implicitly an s-lock, too, and the insert should be prevented.
What if our transaction owns an x-lock to the next record, but there is
a waiting s-lock request on the next record? If this s-lock was placed
by a read cursor moving in the ascending order in the index, we cannot
do the insert immediately, because when we finally commit our transaction,
the read cursor should see also the new inserted record. So we should
move the read cursor backward from the the next record for it to pass over
the new inserted record. This move backward may be too cumbersome to
implement. If we in this situation just enqueue a second x-lock request
for our transaction on the next record, then the deadlock mechanism
notices a deadlock between our transaction and the s-lock request
transaction. This seems to be an ok solution.
	We could have the convention that granted explicit record locks,
lock the corresponding records from changing, and also lock the gaps
before them from inserting. A waiting explicit lock request locks the gap
before from inserting. Implicit record x-locks, which we derive from the
transaction id in the clustered index record, only lock the record itself
from modification, not the gap before it from inserting.
	How should we store update locks? If the search is done by a unique
key, we could just modify the record trx id. Otherwise, we could put a record
x-lock on the record. If the update changes ordering fields of the
clustered index record, the inserted new record needs no record lock in
lock table, the trx id is enough. The same holds for a secondary index
record. Searched delete is similar to update.

PROBLEM:
What about waiting lock requests? If a transaction is waiting to make an
update to a record which another modified, how does the other transaction
know to send the end-lock-wait signal to the waiting transaction? If we have
the convention that a transaction may wait for just one lock at a time, how
do we preserve it if lock wait ends?

PROBLEM:
Checking the trx id label of a secondary index record. In the case of a
modification, not an insert, is this necessary? A secondary index record
is modified only by setting or resetting its deleted flag. A secondary index
record contains fields to uniquely determine the corresponding clustered
index record. A secondary index record is therefore only modified if we
also modify the clustered index record, and the trx id checking is done
on the clustered index record, before we come to modify the secondary index
record. So, in the case of delete marking or unmarking a secondary index
record, we do not have to care about trx ids, only the locks in the lock
table must be checked. In the case of a select from a secondary index, the
trx id is relevant, and in this case we may have to search the clustered
index record.

PROBLEM: How to update record locks when page is split or merged, or
--------------------------------------------------------------------
a record is deleted or updated?
If the size of fields in a record changes, we perform the update by
a delete followed by an insert. How can we retain the locks set or
waiting on the record? Because a record lock is indexed in the bitmap
by the heap number of the record, when we remove the record from the
record list, it is possible still to keep the lock bits. If the page
is reorganized, we could make a table of old and new heap numbers,
and permute the bitmaps in the locks accordingly. We can add to the
table a row telling where the updated record ended. If the update does
not require a reorganization of the page, we can simply move the lock
bits for the updated record to the position determined by its new heap
number (we may have to allocate a new lock, if we run out of the bitmap
in the old one).
	A more complicated case is the one where the reinsertion of the
updated record is done pessimistically, because the structure of the
tree may change.

PROBLEM: If a supremum record is removed in a page merge, or a record
---------------------------------------------------------------------
removed in a purge, what to do to the waiting lock requests? In a split to
the right, we just move the lock requests to the new supremum. If a record
is removed, we could move the waiting lock request to its inheritor, the
next record in the index. But, the next record may already have lock
requests on its own queue. A new deadlock check should be made then. Maybe
it is easier just to release the waiting transactions. They can then enqueue
new lock requests on appropriate records.

PROBLEM: When a record is inserted, what locks should it inherit from the
-------------------------------------------------------------------------
upper neighbor? An insert of a new supremum record in a page split is
always possible, but an insert of a new user record requires that the upper
neighbor does not have any lock requests by other transactions, granted or
waiting, in its lock queue. Solution: We can copy the locks as gap type
locks, so that also the waiting locks are transformed to granted gap type
locks on the inserted record. */

ibool	lock_print_waits	= FALSE;

/* The lock system */
lock_sys_t*	lock_sys	= NULL;

/* A table lock */
typedef struct lock_table_struct	lock_table_t;
struct lock_table_struct{
	dict_table_t*	table;	/* database table in dictionary cache */
	UT_LIST_NODE_T(lock_t)
			locks; 	/* list of locks on the same table */
};

/* Record lock for a page */
typedef struct lock_rec_struct		lock_rec_t;
struct lock_rec_struct{
	ulint	space;		/* space id */
	ulint	page_no;	/* page number */
	ulint	n_bits;		/* number of bits in the lock bitmap */
				/* NOTE: the lock bitmap is placed immediately
				after the lock struct */
};

/* Lock struct */
struct lock_struct{
	trx_t*		trx;		/* transaction owning the lock */
	UT_LIST_NODE_T(lock_t)		
			trx_locks;	/* list of the locks of the
					transaction */
unknown's avatar
unknown committed
324 325 326
	ulint		type_mode;	/* lock type, mode, LOCK_GAP or
					LOCK_REC_NOT_GAP,
					LOCK_INSERT_INTENTION,
327 328 329 330 331 332 333 334 335
					wait flag, ORed */
	hash_node_t	hash;		/* hash chain node for a record lock */
	dict_index_t*	index;		/* index for a record lock */
	union {
		lock_table_t	tab_lock;/* table lock */
		lock_rec_t	rec_lock;/* record lock */
	} un_member;
};

unknown's avatar
unknown committed
336 337 338
/* We store info on the latest deadlock error to this buffer. InnoDB
Monitor will then fetch it and print */
ibool	lock_deadlock_found = FALSE;
339
FILE*	lock_latest_err_file;
unknown's avatar
unknown committed
340

unknown's avatar
unknown committed
341 342 343 344
/* Flags for recursive deadlock search */
#define LOCK_VICTIM_IS_START	1
#define LOCK_VICTIM_IS_OTHER	2

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/************************************************************************
Checks if a lock request results in a deadlock. */
static
ibool
lock_deadlock_occurs(
/*=================*/
			/* out: TRUE if a deadlock was detected */
	lock_t*	lock,	/* in: lock the transaction is requesting */
	trx_t*	trx);	/* in: transaction */
/************************************************************************
Looks recursively for a deadlock. */
static
ibool
lock_deadlock_recursive(
/*====================*/
360 361
				/* out: TRUE if a deadlock was detected
				or the calculation took too long */
362 363
	trx_t*	start,		/* in: recursion starting point */
	trx_t*	trx,		/* in: a transaction waiting for a lock */
364 365 366 367
	lock_t*	wait_lock,	/* in: the lock trx is waiting to be granted */
	ulint*	cost);		/* in/out: number of calculation steps thus
				far: if this exceeds LOCK_MAX_N_STEPS_...
				we return TRUE */
unknown's avatar
unknown committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

/*************************************************************************
Gets the type of a lock. */
UNIV_INLINE
ulint
lock_get_type(
/*==========*/
			/* out: LOCK_TABLE or LOCK_REC */
	lock_t*	lock)	/* in: lock */
{
	ut_ad(lock);

	return(lock->type_mode & LOCK_TYPE_MASK);
}

unknown's avatar
unknown committed
383 384 385 386 387 388 389 390
/*************************************************************************
Gets the nth bit of a record lock. */
UNIV_INLINE
ibool
lock_rec_get_nth_bit(
/*=================*/
			/* out: TRUE if bit set */
	lock_t*	lock,	/* in: record lock */
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	ulint	i)	/* in: index of the bit */
{
	ulint	byte_index;
	ulint	bit_index;
	ulint	b;

	ut_ad(lock);
	ut_ad(lock_get_type(lock) == LOCK_REC);

	if (i >= lock->un_member.rec_lock.n_bits) {

		return(FALSE);
	}

	byte_index = i / 8;
	bit_index = i % 8;

	b = (ulint)*((byte*)lock + sizeof(lock_t) + byte_index);

	return(ut_bit_get_nth(b, bit_index));
}	

/*************************************************************************/
unknown's avatar
unknown committed
414 415 416 417

#define lock_mutex_enter_kernel()	mutex_enter(&kernel_mutex)
#define lock_mutex_exit_kernel()	mutex_exit(&kernel_mutex)

unknown's avatar
unknown committed
418 419 420 421 422 423 424 425 426
/*************************************************************************
Checks that a transaction id is sensible, i.e., not in the future. */

ibool
lock_check_trx_id_sanity(
/*=====================*/
					/* out: TRUE if ok */
	dulint		trx_id,		/* in: trx id */
	rec_t*		rec,		/* in: user record */
427
	dict_index_t*	index,		/* in: index */
unknown's avatar
unknown committed
428
	const ulint*	offsets,	/* in: rec_get_offsets(rec, index) */
unknown's avatar
unknown committed
429 430 431 432 433
	ibool		has_kernel_mutex)/* in: TRUE if the caller owns the
					kernel mutex */
{
	ibool	is_ok		= TRUE;
	
unknown's avatar
unknown committed
434 435
	ut_ad(rec_offs_validate(rec, index, offsets));

unknown's avatar
unknown committed
436 437 438 439 440 441 442 443 444
	if (!has_kernel_mutex) {
		mutex_enter(&kernel_mutex);
	}

	/* A sanity check: the trx_id in rec must be smaller than the global
	trx id counter */

	if (ut_dulint_cmp(trx_id, trx_sys->max_trx_id) >= 0) {
		ut_print_timestamp(stderr);
445 446 447
		fputs("  InnoDB: Error: transaction id associated"
			" with record\n",
			stderr);
448
		rec_print_new(stderr, rec, offsets);
449
		fputs("InnoDB: in ", stderr);
450
		dict_index_name_print(stderr, NULL, index);
451
		fprintf(stderr, "\n"
unknown's avatar
unknown committed
452 453
"InnoDB: is %lu %lu which is higher than the global trx id counter %lu %lu!\n"
"InnoDB: The table is corrupt. You have to do dump + drop + reimport.\n",
454 455 456 457
			       (ulong) ut_dulint_get_high(trx_id),
			       (ulong) ut_dulint_get_low(trx_id),
			       (ulong) ut_dulint_get_high(trx_sys->max_trx_id),
			       (ulong) ut_dulint_get_low(trx_sys->max_trx_id));
unknown's avatar
unknown committed
458 459 460 461 462 463 464 465 466 467 468

		is_ok = FALSE;
	}
	
	if (!has_kernel_mutex) {
		mutex_exit(&kernel_mutex);
	}

	return(is_ok);
}

469 470 471 472 473 474 475 476 477 478 479
/*************************************************************************
Checks that a record is seen in a consistent read. */

ibool
lock_clust_rec_cons_read_sees(
/*==========================*/
				/* out: TRUE if sees, or FALSE if an earlier
				version of the record should be retrieved */
	rec_t*		rec,	/* in: user record which should be read or
				passed over by a read cursor */
	dict_index_t*	index,	/* in: clustered index */
unknown's avatar
unknown committed
480
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
481 482 483 484 485 486
	read_view_t*	view)	/* in: consistent read view */
{
	dulint	trx_id;

	ut_ad(index->type & DICT_CLUSTERED);
	ut_ad(page_rec_is_user_rec(rec));
unknown's avatar
unknown committed
487
	ut_ad(rec_offs_validate(rec, index, offsets));
488

unknown's avatar
unknown committed
489 490 491 492
	/* NOTE that we call this function while holding the search
	system latch. To obey the latching order we must NOT reserve the
	kernel mutex here! */

unknown's avatar
unknown committed
493
	trx_id = row_get_rec_trx_id(rec, index, offsets);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	
	if (read_view_sees_trx_id(view, trx_id)) {

		return(TRUE);
	}

	return(FALSE);
}

/*************************************************************************
Checks that a non-clustered index record is seen in a consistent read. */

ulint
lock_sec_rec_cons_read_sees(
/*========================*/
				/* out: TRUE if certainly sees, or FALSE if an
				earlier version of the clustered index record
				might be needed: NOTE that a non-clustered
				index page contains so little information on
				its modifications that also in the case FALSE,
				the present version of rec may be the right,
				but we must check this from the clustered
				index record */
	rec_t*		rec,	/* in: user record which should be read or
				passed over by a read cursor */
unknown's avatar
unknown committed
519
	dict_index_t*	index,	/* in: non-clustered index */
520 521 522
	read_view_t*	view)	/* in: consistent read view */
{
	dulint	max_trx_id;
unknown's avatar
unknown committed
523 524 525
	
	UT_NOT_USED(index);
	
526 527 528
	ut_ad(!(index->type & DICT_CLUSTERED));
	ut_ad(page_rec_is_user_rec(rec));

unknown's avatar
unknown committed
529 530 531 532
	/* NOTE that we might call this function while holding the search
	system latch. To obey the latching order we must NOT reserve the
	kernel mutex here! */

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	if (recv_recovery_is_on()) {

		return(FALSE);
	}

	max_trx_id = page_get_max_trx_id(buf_frame_align(rec));

	if (ut_dulint_cmp(max_trx_id, view->up_limit_id) >= 0) {

		return(FALSE);
	}

	return(TRUE);
}

/*************************************************************************
Creates the lock system at database start. */

void
lock_sys_create(
/*============*/
	ulint	n_cells)	/* in: number of slots in lock hash table */
{
	lock_sys = mem_alloc(sizeof(lock_sys_t));

	lock_sys->rec_hash = hash_create(n_cells);

	/* hash_create_mutexes(lock_sys->rec_hash, 2, SYNC_REC_LOCK); */
unknown's avatar
unknown committed
561

562
	lock_latest_err_file = os_file_create_tmpfile();
563
	ut_a(lock_latest_err_file);
564 565
}

566 567 568 569 570 571 572 573 574 575 576
/*************************************************************************
Gets the size of a lock struct. */

ulint
lock_get_size(void)
/*===============*/
			/* out: size in bytes */
{
	return((ulint)sizeof(lock_t));
}

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
/*************************************************************************
Gets the mode of a lock. */
UNIV_INLINE
ulint
lock_get_mode(
/*==========*/
			/* out: mode */
	lock_t*	lock)	/* in: lock */
{
	ut_ad(lock);

	return(lock->type_mode & LOCK_MODE_MASK);
}

/*************************************************************************
Gets the wait flag of a lock. */
UNIV_INLINE
ibool
lock_get_wait(
/*==========*/
			/* out: TRUE if waiting */
	lock_t*	lock)	/* in: lock */
{
	ut_ad(lock);

	if (lock->type_mode & LOCK_WAIT) {

		return(TRUE);
	}

	return(FALSE);
}

610
/*************************************************************************
611 612
Gets the source table of an ALTER TABLE transaction.  The table must be
covered by an IX or IS table lock. */
613 614

dict_table_t*
615 616 617 618 619 620 621 622 623 624
lock_get_src_table(
/*===============*/
				/* out: the source table of transaction,
				if it is covered by an IX or IS table lock;
				dest if there is no source table, and
				NULL if the transaction is locking more than
				two tables or an inconsistency is found */
	trx_t*		trx,	/* in: transaction */
	dict_table_t*	dest,	/* in: destination of ALTER TABLE */
	ulint*		mode)	/* out: lock mode of the source table */
625
{
626 627
	dict_table_t*	src;
	lock_t*		lock;
628

629
	src = NULL;
630 631
	*mode = LOCK_NONE;

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	for (lock = UT_LIST_GET_FIRST(trx->trx_locks);
	     lock;
	     lock = UT_LIST_GET_NEXT(trx_locks, lock)) {
		lock_table_t*	tab_lock;
		ulint		lock_mode;
		if (!(lock_get_type(lock) & LOCK_TABLE)) {
			/* We are only interested in table locks. */
			continue;
		}
		tab_lock = &lock->un_member.tab_lock;
		if (dest == tab_lock->table) {
			/* We are not interested in the destination table. */
			continue;
		} else if (!src) {
			/* This presumably is the source table. */
			src = tab_lock->table;
			if (UT_LIST_GET_LEN(src->locks) != 1 ||
			    UT_LIST_GET_FIRST(src->locks) != lock) {
				/* We only support the case when
				there is only one lock on this table. */
				return(NULL);
			}
		} else if (src != tab_lock->table) {
			/* The transaction is locking more than
			two tables (src and dest): abort */
			return(NULL);
		}

		/* Check that the source table is locked by
		LOCK_IX or LOCK_IS. */
		lock_mode = lock_get_mode(lock);
		switch (lock_mode) {
		case LOCK_IX:
		case LOCK_IS:
			if (*mode != LOCK_NONE && *mode != lock_mode) {
				/* There are multiple locks on src. */
				return(NULL);
			}
			*mode = lock_mode;
			break;
		}
	}

	if (!src) {
		/* No source table lock found: flag the situation to caller */
		src = dest;
678 679
	}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	return(src);
}

/*************************************************************************
Determine if the given table is exclusively "owned" by the given
transaction, i.e., transaction holds LOCK_IX and possibly LOCK_AUTO_INC
on the table. */

ibool
lock_is_table_exclusive(
/*====================*/
				/* out: TRUE if table is only locked by trx,
				with LOCK_IX, and possibly LOCK_AUTO_INC */
	dict_table_t*	table,	/* in: table */
	trx_t*		trx)	/* in: transaction */
{
	lock_t*	lock;
	bool	ok	= FALSE;

	ut_ad(table && trx);

	for (lock = UT_LIST_GET_FIRST(table->locks);
	     lock;
	     lock = UT_LIST_GET_NEXT(locks, &lock->un_member.tab_lock)) {
		if (lock->trx != trx) {
			/* A lock on the table is held
			by some other transaction. */
			return(FALSE);
		}

		if (!(lock_get_type(lock) & LOCK_TABLE)) {
			/* We are interested in table locks only. */
			continue;
		}

		switch (lock_get_mode(lock)) {
		case LOCK_IX:
			ok = TRUE;
			break;
		case LOCK_AUTO_INC:
			/* It is allowed for trx to hold an
			auto_increment lock. */
			break;
		default:
			/* Other table locks than LOCK_IX are not allowed. */
			return(FALSE);
726 727
		}
	}
728 729

	return(ok);
730 731
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
/*************************************************************************
Sets the wait flag of a lock and the back pointer in trx to lock. */
UNIV_INLINE
void
lock_set_lock_and_trx_wait(
/*=======================*/
	lock_t*	lock,	/* in: lock */
	trx_t*	trx)	/* in: trx */
{
	ut_ad(lock);
	ut_ad(trx->wait_lock == NULL);
	
	trx->wait_lock = lock;
 	lock->type_mode = lock->type_mode | LOCK_WAIT;
}

/**************************************************************************
The back pointer to a waiting lock request in the transaction is set to NULL
and the wait bit in lock type_mode is reset. */
UNIV_INLINE
void
lock_reset_lock_and_trx_wait(
/*=========================*/
	lock_t*	lock)	/* in: record lock */
{
	ut_ad((lock->trx)->wait_lock == lock);
	ut_ad(lock_get_wait(lock));

	/* Reset the back pointer in trx to this waiting lock request */

	(lock->trx)->wait_lock = NULL;
 	lock->type_mode = lock->type_mode & ~LOCK_WAIT;
}

/*************************************************************************
Gets the gap flag of a record lock. */
UNIV_INLINE
ibool
lock_rec_get_gap(
/*=============*/
			/* out: TRUE if gap flag set */
	lock_t*	lock)	/* in: record lock */
{
	ut_ad(lock);
	ut_ad(lock_get_type(lock) == LOCK_REC);

	if (lock->type_mode & LOCK_GAP) {

		return(TRUE);
	}

	return(FALSE);
}

/*************************************************************************
unknown's avatar
unknown committed
787
Gets the LOCK_REC_NOT_GAP flag of a record lock. */
788
UNIV_INLINE
unknown's avatar
unknown committed
789 790 791 792 793
ibool
lock_rec_get_rec_not_gap(
/*=====================*/
			/* out: TRUE if LOCK_REC_NOT_GAP flag set */
	lock_t*	lock)	/* in: record lock */
794 795 796 797
{
	ut_ad(lock);
	ut_ad(lock_get_type(lock) == LOCK_REC);

unknown's avatar
unknown committed
798 799 800
	if (lock->type_mode & LOCK_REC_NOT_GAP) {

		return(TRUE);
801
	}
unknown's avatar
unknown committed
802 803

	return(FALSE);
804 805
}

unknown's avatar
unknown committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
/*************************************************************************
Gets the waiting insert flag of a record lock. */
UNIV_INLINE
ibool
lock_rec_get_insert_intention(
/*==========================*/
			/* out: TRUE if gap flag set */
	lock_t*	lock)	/* in: record lock */
{
	ut_ad(lock);
	ut_ad(lock_get_type(lock) == LOCK_REC);

	if (lock->type_mode & LOCK_INSERT_INTENTION) {

		return(TRUE);
	}

	return(FALSE);
}

826 827 828 829 830 831 832 833 834 835
/*************************************************************************
Calculates if lock mode 1 is stronger or equal to lock mode 2. */
UNIV_INLINE
ibool
lock_mode_stronger_or_eq(
/*=====================*/
			/* out: TRUE if mode1 stronger or equal to mode2 */
	ulint	mode1,	/* in: lock mode */
	ulint	mode2)	/* in: lock mode */
{
unknown's avatar
unknown committed
836
	ut_ad(mode1 == LOCK_X || mode1 == LOCK_S || mode1 == LOCK_IX
837
				|| mode1 == LOCK_IS || mode1 == LOCK_AUTO_INC);
unknown's avatar
unknown committed
838
	ut_ad(mode2 == LOCK_X || mode2 == LOCK_S || mode2 == LOCK_IX
839
				|| mode2 == LOCK_IS || mode2 == LOCK_AUTO_INC);
840 841 842 843
	if (mode1 == LOCK_X) {

		return(TRUE);

844 845 846 847
	} else if (mode1 == LOCK_AUTO_INC && mode2 == LOCK_AUTO_INC) {

		return(TRUE);

unknown's avatar
unknown committed
848 849
	} else if (mode1 == LOCK_S
				&& (mode2 == LOCK_S || mode2 == LOCK_IS)) {
850 851
		return(TRUE);

unknown's avatar
unknown committed
852
	} else if (mode1 == LOCK_IS && mode2 == LOCK_IS) {
853 854 855

		return(TRUE);

unknown's avatar
unknown committed
856 857
	} else if (mode1 == LOCK_IX && (mode2 == LOCK_IX
						|| mode2 == LOCK_IS)) {
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
		return(TRUE);
	}

	return(FALSE);
}

/*************************************************************************
Calculates if lock mode 1 is compatible with lock mode 2. */
UNIV_INLINE
ibool
lock_mode_compatible(
/*=================*/
			/* out: TRUE if mode1 compatible with mode2 */
	ulint	mode1,	/* in: lock mode */
	ulint	mode2)	/* in: lock mode */
{
unknown's avatar
unknown committed
874
	ut_ad(mode1 == LOCK_X || mode1 == LOCK_S || mode1 == LOCK_IX
875
				|| mode1 == LOCK_IS || mode1 == LOCK_AUTO_INC);
unknown's avatar
unknown committed
876
	ut_ad(mode2 == LOCK_X || mode2 == LOCK_S || mode2 == LOCK_IX
877
				|| mode2 == LOCK_IS || mode2 == LOCK_AUTO_INC);
878

unknown's avatar
unknown committed
879
	if (mode1 == LOCK_S && (mode2 == LOCK_IS || mode2 == LOCK_S)) {
880 881 882 883 884 885 886

		return(TRUE);

	} else if (mode1 == LOCK_X) {

		return(FALSE);

887 888 889 890
	} else if (mode1 == LOCK_AUTO_INC && (mode2 == LOCK_IS
					  	|| mode2 == LOCK_IX)) {
		return(TRUE);

unknown's avatar
unknown committed
891 892
	} else if (mode1 == LOCK_IS && (mode2 == LOCK_IS
					  	|| mode2 == LOCK_IX
893
					  	|| mode2 == LOCK_AUTO_INC
unknown's avatar
unknown committed
894
					  	|| mode2 == LOCK_S)) {
895 896
		return(TRUE);

unknown's avatar
unknown committed
897
	} else if (mode1 == LOCK_IX && (mode2 == LOCK_IS
898
					  	|| mode2 == LOCK_AUTO_INC
unknown's avatar
unknown committed
899
					  	|| mode2 == LOCK_IX)) {
900 901 902 903 904 905 906
		return(TRUE);
	}

	return(FALSE);
}

/*************************************************************************
unknown's avatar
unknown committed
907
Checks if a lock request for a new lock has to wait for request lock2. */
908
UNIV_INLINE
unknown's avatar
unknown committed
909 910 911 912 913 914
ibool
lock_rec_has_to_wait(
/*=================*/
			/* out: TRUE if new lock has to wait for lock2 to be
			removed */
	trx_t*	trx,	/* in: trx of new lock */
unknown's avatar
unknown committed
915 916 917
	ulint	type_mode,/* in: precise mode of the new lock to set:
			LOCK_S or LOCK_X, possibly ORed to
			LOCK_GAP or LOCK_REC_NOT_GAP, LOCK_INSERT_INTENTION */
918
	lock_t*	lock2,	/* in: another record lock; NOTE that it is assumed
unknown's avatar
unknown committed
919
			that this has a lock bit set on the same record as
unknown's avatar
unknown committed
920
			in the new lock we are setting */
921 922 923 924
	ibool lock_is_on_supremum)  /* in: TRUE if we are setting the lock
			on the 'supremum' record of an index
			page: we know then that the lock request
			is really for a 'gap' type lock */
925
{
unknown's avatar
unknown committed
926 927 928
	ut_ad(trx && lock2);
	ut_ad(lock_get_type(lock2) == LOCK_REC);

unknown's avatar
unknown committed
929 930
	if (trx != lock2->trx
	    && !lock_mode_compatible(LOCK_MODE_MASK & type_mode,
unknown's avatar
unknown committed
931 932
				     		lock_get_mode(lock2))) {

unknown's avatar
unknown committed
933 934
		/* We have somewhat complex rules when gap type record locks
		cause waits */
unknown's avatar
unknown committed
935

936
		if ((lock_is_on_supremum || (type_mode & LOCK_GAP))
937
			&& !(type_mode & LOCK_INSERT_INTENTION)) {
938

939
			/* Gap type locks without LOCK_INSERT_INTENTION flag
940 941 942
			do not need to wait for anything. This is because 
			different users can have conflicting lock types 
			on gaps. */
943 944 945 946
						  
			return(FALSE);
		}
		
947
		if (!(type_mode & LOCK_INSERT_INTENTION)
unknown's avatar
unknown committed
948
						&& lock_rec_get_gap(lock2)) {
949 950 951

			/* Record lock (LOCK_ORDINARY or LOCK_REC_NOT_GAP
			does not need to wait for a gap type lock */
unknown's avatar
unknown committed
952 953 954 955 956 957 958 959 960

			return(FALSE);
		}

		if ((type_mode & LOCK_GAP)
					&& lock_rec_get_rec_not_gap(lock2)) {
		
			/* Lock on gap does not need to wait for
			a LOCK_REC_NOT_GAP type lock */
961

unknown's avatar
unknown committed
962 963 964
			return(FALSE);
		}

unknown's avatar
unknown committed
965
		if (lock_rec_get_insert_intention(lock2)) {
unknown's avatar
unknown committed
966

unknown's avatar
unknown committed
967 968 969 970 971 972 973
			/* No lock request needs to wait for an insert
			intention lock to be removed. This is ok since our
			rules allow conflicting locks on gaps. This eliminates
			a spurious deadlock caused by a next-key lock waiting
			for an insert intention lock; when the insert
			intention lock was granted, the insert deadlocked on
			the waiting next-key lock.
unknown's avatar
unknown committed
974

unknown's avatar
unknown committed
975 976 977
			Also, insert intention locks do not disturb each
			other. */
				
unknown's avatar
unknown committed
978 979
			return(FALSE);
		}
980

unknown's avatar
unknown committed
981
		return(TRUE);
982 983
	}

unknown's avatar
unknown committed
984
	return(FALSE);
985 986 987
}

/*************************************************************************
unknown's avatar
unknown committed
988 989
Checks if a lock request lock1 has to wait for request lock2. */
static
990 991 992
ibool
lock_has_to_wait(
/*=============*/
unknown's avatar
unknown committed
993 994 995
			/* out: TRUE if lock1 has to wait for lock2 to be
			removed */
	lock_t*	lock1,	/* in: waiting lock */
996
	lock_t*	lock2)	/* in: another lock; NOTE that it is assumed that this
unknown's avatar
unknown committed
997 998
			has a lock bit set on the same record as in lock1 if
			the locks are record locks */
999
{
unknown's avatar
unknown committed
1000 1001
	ut_ad(lock1 && lock2);

unknown's avatar
unknown committed
1002
	if (lock1->trx != lock2->trx
1003 1004
			&& !lock_mode_compatible(lock_get_mode(lock1),
				     		lock_get_mode(lock2))) {
unknown's avatar
unknown committed
1005 1006
		if (lock_get_type(lock1) == LOCK_REC) {
			ut_ad(lock_get_type(lock2) == LOCK_REC);
1007

1008 1009 1010
			/* If this lock request is for a supremum record
			then the second bit on the lock bitmap is set */
			
unknown's avatar
unknown committed
1011
			return(lock_rec_has_to_wait(lock1->trx,
1012 1013
					lock1->type_mode, lock2,
					lock_rec_get_nth_bit(lock1,1)));
unknown's avatar
unknown committed
1014 1015
		}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
		return(TRUE);
	}

	return(FALSE);
}

/*============== RECORD LOCK BASIC FUNCTIONS ============================*/

/*************************************************************************
Gets the number of bits in a record lock bitmap. */
UNIV_INLINE
ulint
lock_rec_get_n_bits(
/*================*/
			/* out: number of bits */
	lock_t*	lock)	/* in: record lock */
{
	return(lock->un_member.rec_lock.n_bits);
}

/**************************************************************************
Sets the nth bit of a record lock to TRUE. */
UNIV_INLINE
void
lock_rec_set_nth_bit(
/*==================*/
	lock_t*	lock,	/* in: record lock */
	ulint	i)	/* in: index of the bit */
{
	ulint	byte_index;
	ulint	bit_index;
	byte*	ptr;
	ulint	b;
	
	ut_ad(lock);
	ut_ad(lock_get_type(lock) == LOCK_REC);
	ut_ad(i < lock->un_member.rec_lock.n_bits);
	
	byte_index = i / 8;
	bit_index = i % 8;

	ptr = (byte*)lock + sizeof(lock_t) + byte_index;
		
	b = (ulint)*ptr;

	b = ut_bit_set_nth(b, bit_index, TRUE);

	*ptr = (byte)b;
}	

/**************************************************************************
Looks for a set bit in a record lock bitmap. Returns ULINT_UNDEFINED,
if none found. */
static
ulint
lock_rec_find_set_bit(
/*==================*/
			/* out: bit index == heap number of the record, or
			ULINT_UNDEFINED if none found */
	lock_t*	lock)	/* in: record lock with at least one bit set */
{
	ulint	i;

	for (i = 0; i < lock_rec_get_n_bits(lock); i++) {

		if (lock_rec_get_nth_bit(lock, i)) {

			return(i);
		}
	}

	return(ULINT_UNDEFINED);
}

/**************************************************************************
Resets the nth bit of a record lock. */
UNIV_INLINE
void
lock_rec_reset_nth_bit(
/*===================*/
	lock_t*	lock,	/* in: record lock */
	ulint	i)	/* in: index of the bit which must be set to TRUE
			when this function is called */
{
	ulint	byte_index;
	ulint	bit_index;
	byte*	ptr;
	ulint	b;
	
	ut_ad(lock);
	ut_ad(lock_get_type(lock) == LOCK_REC);
	ut_ad(i < lock->un_member.rec_lock.n_bits);
	
	byte_index = i / 8;
	bit_index = i % 8;

	ptr = (byte*)lock + sizeof(lock_t) + byte_index;
		
	b = (ulint)*ptr;

	b = ut_bit_set_nth(b, bit_index, FALSE);

	*ptr = (byte)b;
}	

/*************************************************************************
Gets the first or next record lock on a page. */
UNIV_INLINE
lock_t*
lock_rec_get_next_on_page(
/*======================*/
			/* out: next lock, NULL if none exists */
	lock_t*	lock)	/* in: a record lock */
{
	ulint	space;
	ulint	page_no;

1133
#ifdef UNIV_SYNC_DEBUG
1134
	ut_ad(mutex_own(&kernel_mutex));
1135
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1136
	ut_ad(lock_get_type(lock) == LOCK_REC);
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

	space = lock->un_member.rec_lock.space;
	page_no = lock->un_member.rec_lock.page_no;
	
	for (;;) {
		lock = HASH_GET_NEXT(hash, lock);

		if (!lock) {

			break;
		}

		if ((lock->un_member.rec_lock.space == space) 
	    	    && (lock->un_member.rec_lock.page_no == page_no)) {

			break;
		}
	}
	
	return(lock);
}

/*************************************************************************
Gets the first record lock on a page, where the page is identified by its
file address. */
UNIV_INLINE
lock_t*
lock_rec_get_first_on_page_addr(
/*============================*/
			/* out: first lock, NULL if none exists */
	ulint	space,	/* in: space */
	ulint	page_no)/* in: page number */
{
	lock_t*	lock;

1172
#ifdef UNIV_SYNC_DEBUG
1173
	ut_ad(mutex_own(&kernel_mutex));
1174
#endif /* UNIV_SYNC_DEBUG */
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231

	lock = HASH_GET_FIRST(lock_sys->rec_hash,
					lock_rec_hash(space, page_no));
	while (lock) {
		if ((lock->un_member.rec_lock.space == space) 
	    	    && (lock->un_member.rec_lock.page_no == page_no)) {

			break;
		}

		lock = HASH_GET_NEXT(hash, lock);
	}

	return(lock);
}
	
/*************************************************************************
Returns TRUE if there are explicit record locks on a page. */

ibool
lock_rec_expl_exist_on_page(
/*========================*/
			/* out: TRUE if there are explicit record locks on
			the page */
	ulint	space,	/* in: space id */
	ulint	page_no)/* in: page number */
{
	ibool	ret;

	mutex_enter(&kernel_mutex);

	if (lock_rec_get_first_on_page_addr(space, page_no)) {
		ret = TRUE;
	} else {
		ret = FALSE;
	}

	mutex_exit(&kernel_mutex);
	
	return(ret);
}

/*************************************************************************
Gets the first record lock on a page, where the page is identified by a
pointer to it. */
UNIV_INLINE
lock_t*
lock_rec_get_first_on_page(
/*=======================*/
			/* out: first lock, NULL if none exists */
	byte*	ptr)	/* in: pointer to somewhere on the page */
{
	ulint	hash;
	lock_t*	lock;
	ulint	space;
	ulint	page_no;

1232
#ifdef UNIV_SYNC_DEBUG
1233
	ut_ad(mutex_own(&kernel_mutex));
1234
#endif /* UNIV_SYNC_DEBUG */
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
	
	hash = buf_frame_get_lock_hash_val(ptr);

	lock = HASH_GET_FIRST(lock_sys->rec_hash, hash);

	while (lock) {
		space = buf_frame_get_space_id(ptr);
		page_no = buf_frame_get_page_no(ptr);

		if ((lock->un_member.rec_lock.space == space) 
	    		&& (lock->un_member.rec_lock.page_no == page_no)) {

			break;
		}

		lock = HASH_GET_NEXT(hash, lock);
	}

	return(lock);
}

/*************************************************************************
Gets the next explicit lock request on a record. */
UNIV_INLINE
lock_t*
lock_rec_get_next(
/*==============*/
			/* out: next lock, NULL if none exists */
	rec_t*	rec,	/* in: record on a page */
unknown's avatar
unknown committed
1264
	ibool	comp,	/* in: TRUE=compact page format */
1265 1266
	lock_t*	lock)	/* in: lock */
{
1267
#ifdef UNIV_SYNC_DEBUG
1268
	ut_ad(mutex_own(&kernel_mutex));
1269
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1270
	ut_ad(lock_get_type(lock) == LOCK_REC);
1271 1272 1273 1274 1275 1276 1277 1278 1279

	for (;;) {
		lock = lock_rec_get_next_on_page(lock);

		if (lock == NULL) {

			return(NULL);
		}

unknown's avatar
unknown committed
1280
		if (lock_rec_get_nth_bit(lock, rec_get_heap_no(rec, comp))) {
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

			return(lock);
		}
	}
}

/*************************************************************************
Gets the first explicit lock request on a record. */
UNIV_INLINE
lock_t*
lock_rec_get_first(
/*===============*/
			/* out: first lock, NULL if none exists */
	rec_t*	rec)	/* in: record on a page */
{
	lock_t*	lock;
unknown's avatar
unknown committed
1297
	ibool	comp;
1298

1299
#ifdef UNIV_SYNC_DEBUG
1300
	ut_ad(mutex_own(&kernel_mutex));
1301
#endif /* UNIV_SYNC_DEBUG */
1302 1303

	lock = lock_rec_get_first_on_page(rec);
unknown's avatar
unknown committed
1304
	comp = page_is_comp(buf_frame_align(rec));
1305 1306

	while (lock) {
unknown's avatar
unknown committed
1307
		if (lock_rec_get_nth_bit(lock, rec_get_heap_no(rec, comp))) {
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

			break;
		}

		lock = lock_rec_get_next_on_page(lock);
	}

	return(lock);
}

/*************************************************************************
Resets the record lock bitmap to zero. NOTE: does not touch the wait_lock
pointer in the transaction! This function is used in lock object creation
and resetting. */
static
void
lock_rec_bitmap_reset(
/*==================*/
	lock_t*	lock)	/* in: record lock */
{
	byte*	ptr;
	ulint	n_bytes;
	ulint	i;

unknown's avatar
unknown committed
1332 1333
	ut_ad(lock_get_type(lock) == LOCK_REC);

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
	/* Reset to zero the bitmap which resides immediately after the lock
	struct */

	ptr = (byte*)lock + sizeof(lock_t);

	n_bytes = lock_rec_get_n_bits(lock) / 8;

	ut_ad((lock_rec_get_n_bits(lock) % 8) == 0);
	
	for (i = 0; i < n_bytes; i++) {

		*ptr = 0;
		ptr++;
	}
}

/*************************************************************************
Copies a record lock to heap. */
static
lock_t*
lock_rec_copy(
/*==========*/
				/* out: copy of lock */
	lock_t*		lock,	/* in: record lock */
	mem_heap_t*	heap)	/* in: memory heap */
{
	lock_t*	dupl_lock;
	ulint	size;

unknown's avatar
unknown committed
1363 1364
	ut_ad(lock_get_type(lock) == LOCK_REC);

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	size = sizeof(lock_t) + lock_rec_get_n_bits(lock) / 8;	

	dupl_lock = mem_heap_alloc(heap, size);

	ut_memcpy(dupl_lock, lock, size);

	return(dupl_lock);
}

/*************************************************************************
Gets the previous record lock set on a record. */
static
lock_t*
lock_rec_get_prev(
/*==============*/
			/* out: previous lock on the same record, NULL if
			none exists */
	lock_t*	in_lock,/* in: record lock */
	ulint	heap_no)/* in: heap number of the record */
{
	lock_t*	lock;
	ulint	space;
	ulint	page_no;
	lock_t*	found_lock 	= NULL;

1390
#ifdef UNIV_SYNC_DEBUG
1391
	ut_ad(mutex_own(&kernel_mutex));
1392
#endif /* UNIV_SYNC_DEBUG */
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
	ut_ad(lock_get_type(in_lock) == LOCK_REC);

	space = in_lock->un_member.rec_lock.space;
	page_no = in_lock->un_member.rec_lock.page_no;

	lock = lock_rec_get_first_on_page_addr(space, page_no);

	for (;;) {
		ut_ad(lock);
		
		if (lock == in_lock) {

			return(found_lock);
		}

		if (lock_rec_get_nth_bit(lock, heap_no)) {

			found_lock = lock;
		}

		lock = lock_rec_get_next_on_page(lock);
	}	
}

/*============= FUNCTIONS FOR ANALYZING TABLE LOCK QUEUE ================*/

/*************************************************************************
Checks if a transaction has the specified table lock, or stronger. */
UNIV_INLINE
lock_t*
lock_table_has(
/*===========*/
				/* out: lock or NULL */
	trx_t*		trx,	/* in: transaction */
	dict_table_t*	table,	/* in: table */
	ulint		mode)	/* in: lock mode */
{
	lock_t*	lock;

1432
#ifdef UNIV_SYNC_DEBUG
1433
	ut_ad(mutex_own(&kernel_mutex));
1434
#endif /* UNIV_SYNC_DEBUG */
1435 1436 1437 1438 1439 1440 1441

	/* Look for stronger locks the same trx already has on the table */

	lock = UT_LIST_GET_LAST(table->locks);

	while (lock != NULL) {

unknown's avatar
unknown committed
1442 1443
		if (lock->trx == trx
		    && lock_mode_stronger_or_eq(lock_get_mode(lock), mode)) {
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

			/* The same trx already has locked the table in 
			a mode stronger or equal to the mode given */

			ut_ad(!lock_get_wait(lock)); 

			return(lock);
		}

		lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
	}

	return(NULL);
}
	
/*============= FUNCTIONS FOR ANALYZING RECORD LOCK QUEUE ================*/

/*************************************************************************
unknown's avatar
unknown committed
1462 1463
Checks if a transaction has a GRANTED explicit lock on rec stronger or equal
to precise_mode. */
1464 1465 1466 1467 1468
UNIV_INLINE
lock_t*
lock_rec_has_expl(
/*==============*/
			/* out: lock or NULL */
unknown's avatar
unknown committed
1469 1470 1471 1472
	ulint	precise_mode,/* in: LOCK_S or LOCK_X possibly ORed to
			LOCK_GAP or LOCK_REC_NOT_GAP,
			for a supremum record we regard this always a gap
			type request */
1473
	rec_t*	rec,	/* in: record */
unknown's avatar
unknown committed
1474
	ibool	comp,	/* in: TRUE=compact page format */
1475 1476 1477 1478
	trx_t*	trx)	/* in: transaction */
{
	lock_t*	lock;

1479
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
1480
	ut_ad(mutex_own(&kernel_mutex));
1481
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1482 1483 1484 1485
	ut_ad((precise_mode & LOCK_MODE_MASK) == LOCK_S
	      || (precise_mode & LOCK_MODE_MASK) == LOCK_X);
	ut_ad(!(precise_mode & LOCK_INSERT_INTENTION));
	
1486 1487 1488
	lock = lock_rec_get_first(rec);

	while (lock) {
unknown's avatar
unknown committed
1489
		if (lock->trx == trx
unknown's avatar
unknown committed
1490 1491
		    && lock_mode_stronger_or_eq(lock_get_mode(lock),
		    				precise_mode & LOCK_MODE_MASK)
1492
		    && !lock_get_wait(lock)
unknown's avatar
unknown committed
1493 1494 1495 1496 1497 1498 1499
		    && (!lock_rec_get_rec_not_gap(lock)
		    		|| (precise_mode & LOCK_REC_NOT_GAP)
		    		|| page_rec_is_supremum(rec))
		    && (!lock_rec_get_gap(lock)
				|| (precise_mode & LOCK_GAP)
				|| page_rec_is_supremum(rec))
		    && (!lock_rec_get_insert_intention(lock))) {
unknown's avatar
unknown committed
1500

1501 1502 1503
		    	return(lock);
		}

unknown's avatar
unknown committed
1504
		lock = lock_rec_get_next(rec, comp, lock);
1505 1506 1507 1508
	}

	return(NULL);
}
1509
			
1510
/*************************************************************************
unknown's avatar
unknown committed
1511 1512
Checks if some other transaction has a lock request in the queue. */
static
1513 1514 1515 1516
lock_t*
lock_rec_other_has_expl_req(
/*========================*/
			/* out: lock or NULL */
unknown's avatar
unknown committed
1517
	ulint	mode,	/* in: LOCK_S or LOCK_X */
1518 1519 1520 1521 1522
	ulint	gap,	/* in: LOCK_GAP if also gap locks are taken
			into account, or 0 if not */
	ulint	wait,	/* in: LOCK_WAIT if also waiting locks are
			taken into account, or 0 if not */
	rec_t*	rec,	/* in: record to look at */	
unknown's avatar
unknown committed
1523
	ibool	comp,	/* in: TRUE=compact record format */
unknown's avatar
unknown committed
1524 1525
	trx_t*	trx)	/* in: transaction, or NULL if requests by all
			transactions are taken into account */
1526 1527 1528
{
	lock_t*	lock;
	
1529
#ifdef UNIV_SYNC_DEBUG
1530
	ut_ad(mutex_own(&kernel_mutex));
1531
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1532 1533 1534
	ut_ad(mode == LOCK_X || mode == LOCK_S);
	ut_ad(gap == 0 || gap == LOCK_GAP);
	ut_ad(wait == 0 || wait == LOCK_WAIT);
unknown's avatar
unknown committed
1535

1536 1537 1538
	lock = lock_rec_get_first(rec);

	while (lock) {
unknown's avatar
unknown committed
1539
		if (lock->trx != trx
1540 1541 1542 1543 1544
		    && (gap ||
			!(lock_rec_get_gap(lock) || page_rec_is_supremum(rec)))
		    && (wait || !lock_get_wait(lock))
		    && lock_mode_stronger_or_eq(lock_get_mode(lock), mode)) {

unknown's avatar
unknown committed
1545
		    	return(lock);
1546 1547
		}

unknown's avatar
unknown committed
1548
		lock = lock_rec_get_next(rec, comp, lock);
1549 1550 1551 1552 1553
	}

	return(NULL);
}

unknown's avatar
unknown committed
1554 1555 1556 1557 1558 1559 1560 1561
/*************************************************************************
Checks if some other transaction has a conflicting explicit lock request
in the queue, so that we have to wait. */
static
lock_t*
lock_rec_other_has_conflicting(
/*===========================*/
			/* out: lock or NULL */
unknown's avatar
unknown committed
1562 1563 1564
	ulint	mode,	/* in: LOCK_S or LOCK_X,
			possibly ORed to LOCK_GAP or LOC_REC_NOT_GAP,
			LOCK_INSERT_INTENTION */
unknown's avatar
unknown committed
1565 1566 1567 1568
	rec_t*	rec,	/* in: record to look at */	
	trx_t*	trx)	/* in: our transaction */
{
	lock_t*	lock;
unknown's avatar
unknown committed
1569
	ibool	comp;
1570
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
1571
	ut_ad(mutex_own(&kernel_mutex));
1572
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1573

unknown's avatar
unknown committed
1574
	lock = lock_rec_get_first(rec);
unknown's avatar
unknown committed
1575
	comp = page_is_comp(buf_frame_align(rec));
unknown's avatar
unknown committed
1576 1577

	while (lock) {
1578 1579
		if (lock_rec_has_to_wait(trx, mode, lock,
			page_rec_is_supremum(rec))) {
unknown's avatar
unknown committed
1580

unknown's avatar
unknown committed
1581 1582 1583
			return(lock);
		}
		
unknown's avatar
unknown committed
1584
		lock = lock_rec_get_next(rec, comp, lock);
unknown's avatar
unknown committed
1585 1586 1587 1588 1589
	}

	return(NULL);
}

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
/*************************************************************************
Looks for a suitable type record lock struct by the same trx on the same page.
This can be used to save space when a new record lock should be set on a page:
no new struct is needed, if a suitable old is found. */
UNIV_INLINE
lock_t*
lock_rec_find_similar_on_page(
/*==========================*/
				/* out: lock or NULL */
	ulint	type_mode,	/* in: lock type_mode field */
	rec_t*	rec,		/* in: record */
	trx_t*	trx)		/* in: transaction */
{
	lock_t*	lock;
	ulint	heap_no;

1606
#ifdef UNIV_SYNC_DEBUG
1607
	ut_ad(mutex_own(&kernel_mutex));
1608
#endif /* UNIV_SYNC_DEBUG */
1609

unknown's avatar
unknown committed
1610
	heap_no = rec_get_heap_no(rec, page_is_comp(buf_frame_align(rec)));
1611 1612 1613
	lock = lock_rec_get_first_on_page(rec);

	while (lock != NULL) {
unknown's avatar
unknown committed
1614 1615 1616
		if (lock->trx == trx
		    && lock->type_mode == type_mode
		    && lock_rec_get_n_bits(lock) > heap_no) {
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
		    	
			return(lock);
		}
		
		lock = lock_rec_get_next_on_page(lock);
	}

	return(NULL);
}

/*************************************************************************
Checks if some transaction has an implicit x-lock on a record in a secondary
index. */

trx_t*
lock_sec_rec_some_has_impl_off_kernel(
/*==================================*/
				/* out: transaction which has the x-lock, or
				NULL */
	rec_t*		rec,	/* in: user record */
unknown's avatar
unknown committed
1637 1638
	dict_index_t*	index,	/* in: secondary index */
	const ulint*	offsets)/* in: rec_get_offsets(rec, index) */
1639 1640 1641
{
	page_t*	page;
	
1642
#ifdef UNIV_SYNC_DEBUG
1643
	ut_ad(mutex_own(&kernel_mutex));
1644
#endif /* UNIV_SYNC_DEBUG */
1645 1646
	ut_ad(!(index->type & DICT_CLUSTERED));
	ut_ad(page_rec_is_user_rec(rec));
unknown's avatar
unknown committed
1647
	ut_ad(rec_offs_validate(rec, index, offsets));
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666

	page = buf_frame_align(rec);

	/* Some transaction may have an implicit x-lock on the record only
	if the max trx id for the page >= min trx id for the trx list, or
	database recovery is running. We do not write the changes of a page
	max trx id to the log, and therefore during recovery, this value
	for a page may be incorrect. */

	if (!(ut_dulint_cmp(page_get_max_trx_id(page),
					trx_list_get_min_trx_id()) >= 0)
	   		&& !recv_recovery_is_on()) {

		return(NULL);
	}

	/* Ok, in this case it is possible that some transaction has an
	implicit x-lock. We have to look in the clustered index. */
			
unknown's avatar
unknown committed
1667 1668
	if (!lock_check_trx_id_sanity(page_get_max_trx_id(page),
				rec, index, offsets, TRUE)) {
unknown's avatar
unknown committed
1669 1670 1671 1672 1673 1674 1675
		buf_page_print(page);
		
		/* The page is corrupt: try to avoid a crash by returning
		NULL */
		return(NULL);
	}

unknown's avatar
unknown committed
1676
	return(row_vers_impl_x_locked_off_kernel(rec, index, offsets));
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
}

/*============== RECORD LOCK CREATION AND QUEUE MANAGEMENT =============*/

/*************************************************************************
Creates a new record lock and inserts it to the lock queue. Does NOT check
for deadlocks or lock compatibility! */
static
lock_t*
lock_rec_create(
/*============*/
				/* out: created lock, NULL if out of memory */
	ulint		type_mode,/* in: lock mode and wait flag, type is
				ignored and replaced by LOCK_REC */
	rec_t*		rec,	/* in: record on page */
	dict_index_t*	index,	/* in: index of record */
	trx_t*		trx)	/* in: transaction */
{
	page_t*	page;
	lock_t*	lock;
	ulint	page_no;
	ulint	heap_no;
	ulint	space;
	ulint	n_bits;
	ulint	n_bytes;
	
1703
#ifdef UNIV_SYNC_DEBUG
1704
	ut_ad(mutex_own(&kernel_mutex));
1705
#endif /* UNIV_SYNC_DEBUG */
1706 1707 1708 1709

	page = buf_frame_align(rec);
	space = buf_frame_get_space_id(page);
	page_no	= buf_frame_get_page_no(page);
unknown's avatar
unknown committed
1710
	heap_no = rec_get_heap_no(rec, page_is_comp(page));
1711

unknown's avatar
unknown committed
1712 1713 1714
	/* If rec is the supremum record, then we reset the gap and
	LOCK_REC_NOT_GAP bits, as all locks on the supremum are
	automatically of the gap type */
1715 1716

	if (rec == page_get_supremum_rec(page)) {
unknown's avatar
unknown committed
1717
		ut_ad(!(type_mode & LOCK_REC_NOT_GAP));
1718

unknown's avatar
unknown committed
1719
		type_mode = type_mode & ~(LOCK_GAP | LOCK_REC_NOT_GAP);
1720 1721 1722
	}

	/* Make lock bitmap bigger by a safety margin */
unknown's avatar
unknown committed
1723
	n_bits = page_dir_get_n_heap(page) + LOCK_PAGE_BITMAP_MARGIN;
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
	n_bytes = 1 + n_bits / 8;

	lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t) + n_bytes);
	
	if (lock == NULL) {

		return(NULL);
	}

	UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock);

	lock->trx = trx;

	lock->type_mode = (type_mode & ~LOCK_TYPE_MASK) | LOCK_REC;
	lock->index = index;
	
	lock->un_member.rec_lock.space = space;
	lock->un_member.rec_lock.page_no = page_no;
	lock->un_member.rec_lock.n_bits = n_bytes * 8;

	/* Reset to zero the bitmap which resides immediately after the
	lock struct */

	lock_rec_bitmap_reset(lock);

	/* Set the bit corresponding to rec */
	lock_rec_set_nth_bit(lock, heap_no);

	HASH_INSERT(lock_t, hash, lock_sys->rec_hash,
					lock_rec_fold(space, page_no), lock); 
1754 1755 1756
	/* Note that we have create a new lock */
	trx->trx_create_lock = TRUE;

1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
	if (type_mode & LOCK_WAIT) {

		lock_set_lock_and_trx_wait(lock, trx);
	}
	
	return(lock);
}

/*************************************************************************
Enqueues a waiting request for a lock which cannot be granted immediately.
Checks for deadlocks. */
static
ulint
lock_rec_enqueue_waiting(
/*=====================*/
				/* out: DB_LOCK_WAIT, DB_DEADLOCK, or
unknown's avatar
unknown committed
1773 1774 1775 1776 1777
				DB_QUE_THR_SUSPENDED, or DB_SUCCESS;
				DB_SUCCESS means that there was a deadlock,
				but another transaction was chosen as a
				victim, and we got the lock immediately:
				no need to wait then */
1778
	ulint		type_mode,/* in: lock mode this transaction is
unknown's avatar
unknown committed
1779 1780
				requesting: LOCK_S or LOCK_X, possibly ORed
				with LOCK_GAP or LOCK_REC_NOT_GAP, ORed
unknown's avatar
unknown committed
1781
				with LOCK_INSERT_INTENTION if this waiting
unknown's avatar
unknown committed
1782 1783
				lock request is set when performing an
				insert of an index record */
1784 1785 1786 1787 1788 1789 1790
	rec_t*		rec,	/* in: record */
	dict_index_t*	index,	/* in: index of record */
	que_thr_t*	thr)	/* in: query thread */
{
	lock_t*	lock;
	trx_t*	trx;
	
1791
#ifdef UNIV_SYNC_DEBUG
1792
	ut_ad(mutex_own(&kernel_mutex));
1793
#endif /* UNIV_SYNC_DEBUG */
1794 1795 1796 1797 1798 1799 1800

	/* Test if there already is some other reason to suspend thread:
	we do not enqueue a lock request if the query thread should be
	stopped anyway */

	if (que_thr_stop(thr)) {

1801
		ut_error;
unknown's avatar
unknown committed
1802

1803 1804 1805 1806 1807
		return(DB_QUE_THR_SUSPENDED);
	}
		
	trx = thr_get_trx(thr);

unknown's avatar
unknown committed
1808 1809
	if (trx->dict_operation) {
		ut_print_timestamp(stderr);
1810
		fputs(
unknown's avatar
unknown committed
1811
"  InnoDB: Error: a record lock wait happens in a dictionary operation!\n"
1812
"InnoDB: Table name ", stderr);
1813
		ut_print_name(stderr, trx, index->table_name);
1814 1815
		fputs(".\n"
"InnoDB: Submit a detailed bug report to http://bugs.mysql.com\n",
1816
			stderr);
unknown's avatar
unknown committed
1817 1818
	}
	
1819 1820 1821 1822 1823 1824 1825 1826 1827
	/* Enqueue the lock request that will wait to be granted */
	lock = lock_rec_create(type_mode | LOCK_WAIT, rec, index, trx);

	/* Check if a deadlock occurs: if yes, remove the lock request and
	return an error code */
	
	if (lock_deadlock_occurs(lock, trx)) {

		lock_reset_lock_and_trx_wait(lock);
unknown's avatar
unknown committed
1828 1829
		lock_rec_reset_nth_bit(lock, rec_get_heap_no(rec,
					page_is_comp(buf_frame_align(rec))));
1830 1831 1832 1833

		return(DB_DEADLOCK);
	}

unknown's avatar
unknown committed
1834 1835 1836 1837 1838 1839 1840 1841
	/* If there was a deadlock but we chose another transaction as a
	victim, it is possible that we already have the lock now granted! */

	if (trx->wait_lock == NULL) {

		return(DB_SUCCESS);
	}

1842
	trx->que_state = TRX_QUE_LOCK_WAIT;
unknown's avatar
unknown committed
1843
	trx->was_chosen_as_deadlock_victim = FALSE;
unknown's avatar
unknown committed
1844
	trx->wait_started = time(NULL);
1845 1846 1847 1848

	ut_a(que_thr_stop(thr));

	if (lock_print_waits) {
1849
		fprintf(stderr, "Lock wait for trx %lu in index ",
unknown's avatar
unknown committed
1850
			(ulong) ut_dulint_get_low(trx->id));
1851
		ut_print_name(stderr, trx, index->name);
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
	}
	
	return(DB_LOCK_WAIT);	
}

/*************************************************************************
Adds a record lock request in the record queue. The request is normally
added as the last in the queue, but if there are no waiting lock requests
on the record, and the request to be added is not a waiting request, we
can reuse a suitable record lock object already existing on the same page,
just setting the appropriate bit in its bitmap. This is a low-level function
which does NOT check for deadlocks or lock compatibility! */
static
lock_t*
lock_rec_add_to_queue(
/*==================*/
				/* out: lock where the bit was set, NULL if out
				of memory */
unknown's avatar
unknown committed
1870 1871
	ulint		type_mode,/* in: lock mode, wait, gap etc. flags;
				type is ignored and replaced by LOCK_REC */
1872 1873 1874 1875 1876 1877 1878
	rec_t*		rec,	/* in: record on page */
	dict_index_t*	index,	/* in: index of record */
	trx_t*		trx)	/* in: transaction */
{
	lock_t*	lock;
	lock_t*	similar_lock	= NULL;
	ulint	heap_no;
unknown's avatar
unknown committed
1879
	page_t*	page		= buf_frame_align(rec);
1880 1881
	ibool	somebody_waits	= FALSE;
	
1882
#ifdef UNIV_SYNC_DEBUG
1883
	ut_ad(mutex_own(&kernel_mutex));
1884
#endif /* UNIV_SYNC_DEBUG */
1885 1886
	ut_ad((type_mode & (LOCK_WAIT | LOCK_GAP))
	      || ((type_mode & LOCK_MODE_MASK) != LOCK_S)
unknown's avatar
unknown committed
1887 1888
	      || !lock_rec_other_has_expl_req(LOCK_X, 0, LOCK_WAIT,
					rec, page_is_comp(page), trx));
1889 1890
	ut_ad((type_mode & (LOCK_WAIT | LOCK_GAP))
	      || ((type_mode & LOCK_MODE_MASK) != LOCK_X)
unknown's avatar
unknown committed
1891 1892
	      || !lock_rec_other_has_expl_req(LOCK_S, 0, LOCK_WAIT,
					rec, page_is_comp(page), trx));
unknown's avatar
unknown committed
1893

1894 1895 1896 1897 1898 1899 1900 1901
	type_mode = type_mode | LOCK_REC;

	/* If rec is the supremum record, then we can reset the gap bit, as
	all locks on the supremum are automatically of the gap type, and we
	try to avoid unnecessary memory consumption of a new record lock
	struct for a gap type lock */

	if (rec == page_get_supremum_rec(page)) {
unknown's avatar
unknown committed
1902
		ut_ad(!(type_mode & LOCK_REC_NOT_GAP));
1903

unknown's avatar
unknown committed
1904 1905 1906 1907
		/* There should never be LOCK_REC_NOT_GAP on a supremum
		record, but let us play safe */
		
		type_mode = type_mode & ~(LOCK_GAP | LOCK_REC_NOT_GAP);
1908 1909
	}

unknown's avatar
unknown committed
1910
	/* Look for a waiting lock request on the same record or on a gap */
1911

unknown's avatar
unknown committed
1912
	heap_no = rec_get_heap_no(rec, page_is_comp(page));
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
	lock = lock_rec_get_first_on_page(rec);

	while (lock != NULL) {
		if (lock_get_wait(lock)
				&& (lock_rec_get_nth_bit(lock, heap_no))) {

			somebody_waits = TRUE;
		}

		lock = lock_rec_get_next_on_page(lock);
	}

unknown's avatar
unknown committed
1925 1926 1927
	/* Look for a similar record lock on the same page: if one is found
	and there are no waiting lock requests, we can just set the bit */

1928 1929 1930 1931
	similar_lock = lock_rec_find_similar_on_page(type_mode, rec, trx);

	if (similar_lock && !somebody_waits && !(type_mode & LOCK_WAIT)) {

1932 1933 1934 1935 1936 1937 1938 1939 1940
		/* If the nth bit of a record lock is already set then we
		do not set a new lock bit, otherwice we set */

		if (lock_rec_get_nth_bit(similar_lock, heap_no)) {
			trx->trx_create_lock = FALSE;
		} else {
			trx->trx_create_lock = TRUE;
		}

1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
		lock_rec_set_nth_bit(similar_lock, heap_no);

		return(similar_lock);
	}

	return(lock_rec_create(type_mode, rec, index, trx));
}

/*************************************************************************
This is a fast routine for locking a record in the most common cases:
there are no explicit locks on the page, or there is just one lock, owned
by this transaction, and of the right type_mode. This is a low-level function
which does NOT look at implicit locks! Checks lock compatibility within
unknown's avatar
unknown committed
1954 1955
explicit locks. This function sets a normal next-key lock, or in the case of
a page supremum record, a gap type lock. */
1956 1957 1958 1959 1960 1961 1962 1963
UNIV_INLINE
ibool
lock_rec_lock_fast(
/*===============*/
				/* out: TRUE if locking succeeded */
	ibool		impl,	/* in: if TRUE, no lock is set if no wait
				is necessary: we assume that the caller will
				set an implicit lock */
unknown's avatar
unknown committed
1964 1965
	ulint		mode,	/* in: lock mode: LOCK_X or LOCK_S possibly
				ORed to either LOCK_GAP or LOCK_REC_NOT_GAP */
1966 1967 1968 1969 1970 1971
	rec_t*		rec,	/* in: record */
	dict_index_t*	index,	/* in: index of record */
	que_thr_t* 	thr)	/* in: query thread */
{
	lock_t*	lock;
	ulint	heap_no;
1972
	trx_t*  trx;
1973

1974
#ifdef UNIV_SYNC_DEBUG
1975
	ut_ad(mutex_own(&kernel_mutex));
1976
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
		|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_X
		|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
	ut_ad((LOCK_MODE_MASK & mode) == LOCK_S
		|| (LOCK_MODE_MASK & mode) == LOCK_X);
	ut_ad(mode - (LOCK_MODE_MASK & mode) == LOCK_GAP
			|| mode - (LOCK_MODE_MASK & mode) == 0
			|| mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP);
			
unknown's avatar
unknown committed
1987
	heap_no = rec_get_heap_no(rec, page_is_comp(buf_frame_align(rec)));
1988 1989 1990
	
	lock = lock_rec_get_first_on_page(rec);

1991 1992 1993
	trx = thr_get_trx(thr);
	trx->trx_create_lock = FALSE;

1994 1995
	if (lock == NULL) {
		if (!impl) {
1996
			lock_rec_create(mode, rec, index, trx);
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
		}
		
		return(TRUE);
	}
	
	if (lock_rec_get_next_on_page(lock)) {

		return(FALSE);
	}

2007
	if (lock->trx != trx
unknown's avatar
unknown committed
2008 2009
				|| lock->type_mode != (mode | LOCK_REC)
				|| lock_rec_get_n_bits(lock) <= heap_no) {
2010 2011 2012 2013
	    	return(FALSE);
	}

	if (!impl) {
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023

		/* If the nth bit of a record lock is already set then we
		do not set a new lock bit, otherwice we set */

		if (lock_rec_get_nth_bit(lock, heap_no)) {
			trx->trx_create_lock = FALSE;
		} else {
			trx->trx_create_lock = TRUE;
		}

2024 2025 2026 2027 2028 2029 2030 2031 2032
		lock_rec_set_nth_bit(lock, heap_no);
	}

	return(TRUE);
}

/*************************************************************************
This is the general, and slower, routine for locking a record. This is a
low-level function which does NOT look at implicit locks! Checks lock
unknown's avatar
unknown committed
2033 2034
compatibility within explicit locks. This function sets a normal next-key
lock, or in the case of a page supremum record, a gap type lock. */
2035 2036 2037 2038 2039 2040 2041 2042 2043
static
ulint
lock_rec_lock_slow(
/*===============*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT, or error
				code */
	ibool		impl,	/* in: if TRUE, no lock is set if no wait is
				necessary: we assume that the caller will set
				an implicit lock */
unknown's avatar
unknown committed
2044 2045
	ulint		mode,	/* in: lock mode: LOCK_X or LOCK_S possibly
				ORed to either LOCK_GAP or LOCK_REC_NOT_GAP */
2046 2047 2048 2049 2050 2051 2052
	rec_t*		rec,	/* in: record */
	dict_index_t*	index,	/* in: index of record */
	que_thr_t* 	thr)	/* in: query thread */
{
	trx_t*	trx;
	ulint	err;

2053
#ifdef UNIV_SYNC_DEBUG
2054
	ut_ad(mutex_own(&kernel_mutex));
2055
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
		|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_X
		|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
	ut_ad((LOCK_MODE_MASK & mode) == LOCK_S
		|| (LOCK_MODE_MASK & mode) == LOCK_X);
	ut_ad(mode - (LOCK_MODE_MASK & mode) == LOCK_GAP
			|| mode - (LOCK_MODE_MASK & mode) == 0
			|| mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP);
			
2066
	trx = thr_get_trx(thr);
unknown's avatar
unknown committed
2067
		
unknown's avatar
unknown committed
2068 2069
	if (lock_rec_has_expl(mode, rec,
				page_is_comp(buf_frame_align(rec)), trx)) {
2070 2071 2072 2073
		/* The trx already has a strong enough lock on rec: do
		nothing */

		err = DB_SUCCESS;
unknown's avatar
unknown committed
2074
	} else if (lock_rec_other_has_conflicting(mode, rec, trx)) {
unknown's avatar
unknown committed
2075

2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
		/* If another transaction has a non-gap conflicting request in
		the queue, as this transaction does not have a lock strong
		enough already granted on the record, we have to wait. */
    				
		err = lock_rec_enqueue_waiting(mode, rec, index, thr);
	} else {
		if (!impl) {
			/* Set the requested lock on the record */

			lock_rec_add_to_queue(LOCK_REC | mode, rec, index,
									trx);
		}

		err = DB_SUCCESS;
	}

	return(err);
}

/*************************************************************************
Tries to lock the specified record in the mode requested. If not immediately
possible, enqueues a waiting lock request. This is a low-level function
which does NOT look at implicit locks! Checks lock compatibility within
unknown's avatar
unknown committed
2099 2100
explicit locks. This function sets a normal next-key lock, or in the case
of a page supremum record, a gap type lock. */
unknown's avatar
unknown committed
2101
static
2102 2103 2104 2105 2106 2107 2108 2109
ulint
lock_rec_lock(
/*==========*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT, or error
				code */
	ibool		impl,	/* in: if TRUE, no lock is set if no wait is
				necessary: we assume that the caller will set
				an implicit lock */
unknown's avatar
unknown committed
2110 2111
	ulint		mode,	/* in: lock mode: LOCK_X or LOCK_S possibly
				ORed to either LOCK_GAP or LOCK_REC_NOT_GAP */
2112 2113 2114 2115 2116 2117
	rec_t*		rec,	/* in: record */
	dict_index_t*	index,	/* in: index of record */
	que_thr_t* 	thr)	/* in: query thread */
{
	ulint	err;

2118
#ifdef UNIV_SYNC_DEBUG
2119
	ut_ad(mutex_own(&kernel_mutex));
2120
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
		|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_X
		|| lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
	ut_ad((LOCK_MODE_MASK & mode) == LOCK_S
		|| (LOCK_MODE_MASK & mode) == LOCK_X);
	ut_ad(mode - (LOCK_MODE_MASK & mode) == LOCK_GAP
			|| mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP
			|| mode - (LOCK_MODE_MASK & mode) == 0);
			
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
	if (lock_rec_lock_fast(impl, mode, rec, index, thr)) {

		/* We try a simplified and faster subroutine for the most
		common cases */

		err = DB_SUCCESS;
	} else {
		err = lock_rec_lock_slow(impl, mode, rec, index, thr);
	}

	return(err);
}

/*************************************************************************
unknown's avatar
unknown committed
2145
Checks if a waiting record lock request still has to wait in a queue. */
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
static
ibool
lock_rec_has_to_wait_in_queue(
/*==========================*/
				/* out: TRUE if still has to wait */
	lock_t*	wait_lock)	/* in: waiting record lock */
{
	lock_t*	lock;
	ulint	space;
	ulint	page_no;
	ulint	heap_no;

2158
#ifdef UNIV_SYNC_DEBUG
2159
	ut_ad(mutex_own(&kernel_mutex));
2160
#endif /* UNIV_SYNC_DEBUG */
2161
 	ut_ad(lock_get_wait(wait_lock));
unknown's avatar
unknown committed
2162
	ut_ad(lock_get_type(wait_lock) == LOCK_REC);
2163 2164 2165 2166 2167 2168 2169 2170 2171
 	
	space = wait_lock->un_member.rec_lock.space;
	page_no = wait_lock->un_member.rec_lock.page_no;
	heap_no = lock_rec_find_set_bit(wait_lock);

	lock = lock_rec_get_first_on_page_addr(space, page_no);

	while (lock != wait_lock) {

unknown's avatar
unknown committed
2172 2173
		if (lock_rec_get_nth_bit(lock, heap_no)
		    && lock_has_to_wait(wait_lock, lock)) {
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186

		    	return(TRUE);
		}

		lock = lock_rec_get_next_on_page(lock);
	}

	return(FALSE);
}

/*****************************************************************
Grants a lock to a waiting lock request and releases the waiting
transaction. */
unknown's avatar
unknown committed
2187
static
2188 2189 2190 2191 2192
void
lock_grant(
/*=======*/
	lock_t*	lock)	/* in: waiting lock request */
{
2193
#ifdef UNIV_SYNC_DEBUG
2194
	ut_ad(mutex_own(&kernel_mutex));
2195
#endif /* UNIV_SYNC_DEBUG */
2196 2197

	lock_reset_lock_and_trx_wait(lock);
unknown's avatar
unknown committed
2198

unknown's avatar
unknown committed
2199
        if (lock_get_mode(lock) == LOCK_AUTO_INC) {
unknown's avatar
unknown committed
2200

unknown's avatar
unknown committed
2201 2202 2203 2204
                if (lock->trx->auto_inc_lock != NULL) {
                        fprintf(stderr,
                   "InnoDB: Error: trx already had an AUTO-INC lock!\n");
                }
unknown's avatar
unknown committed
2205

unknown's avatar
unknown committed
2206 2207 2208 2209
                /* Store pointer to lock to trx so that we know to
                release it at the end of the SQL statement */

                lock->trx->auto_inc_lock = lock;
2210
        } else if (lock_get_type(lock) == LOCK_TABLE_EXP) {
unknown's avatar
unknown committed
2211
		ut_a(lock_get_mode(lock) == LOCK_S
2212 2213
			|| lock_get_mode(lock) == LOCK_X);
	}
2214 2215

	if (lock_print_waits) {
2216
		fprintf(stderr, "Lock wait for trx %lu ends\n",
2217
		       (ulong) ut_dulint_get_low(lock->trx->id));
2218
	}
unknown's avatar
unknown committed
2219 2220 2221 2222 2223

	/* If we are resolving a deadlock by choosing another transaction
	as a victim, then our original transaction may not be in the
	TRX_QUE_LOCK_WAIT state, and there is no need to end the lock wait
	for it */
2224
	
unknown's avatar
unknown committed
2225 2226 2227
	if (lock->trx->que_state == TRX_QUE_LOCK_WAIT) {	
		trx_end_lock_wait(lock->trx);
	}
2228 2229 2230 2231 2232 2233
}

/*****************************************************************
Cancels a waiting record lock request and releases the waiting transaction
that requested it. NOTE: does NOT check if waiting lock requests behind this
one can now be granted! */
2234
static
2235 2236 2237 2238 2239
void
lock_rec_cancel(
/*============*/
	lock_t*	lock)	/* in: waiting record lock request */
{
2240
#ifdef UNIV_SYNC_DEBUG
2241
	ut_ad(mutex_own(&kernel_mutex));
2242
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
2243
	ut_ad(lock_get_type(lock) == LOCK_REC);
2244

unknown's avatar
unknown committed
2245
	/* Reset the bit (there can be only one set bit) in the lock bitmap */
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
	lock_rec_reset_nth_bit(lock, lock_rec_find_set_bit(lock));

	/* Reset the wait flag and the back pointer to lock in trx */

	lock_reset_lock_and_trx_wait(lock);

	/* The following function releases the trx from lock wait */

	trx_end_lock_wait(lock->trx);
}
	
/*****************************************************************
Removes a record lock request, waiting or granted, from the queue and
grants locks to other transactions in the queue if they now are entitled
to a lock. NOTE: all record locks contained in in_lock are removed. */
unknown's avatar
unknown committed
2261
static
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
void
lock_rec_dequeue_from_page(
/*=======================*/
	lock_t*	in_lock)/* in: record lock object: all record locks which
			are contained in this lock object are removed;
			transactions waiting behind will get their lock
			requests granted, if they are now qualified to it */
{
	ulint	space;
	ulint	page_no;
	lock_t*	lock;
	trx_t*	trx;
	
2275
#ifdef UNIV_SYNC_DEBUG
2276
	ut_ad(mutex_own(&kernel_mutex));
2277
#endif /* UNIV_SYNC_DEBUG */
2278 2279 2280
	ut_ad(lock_get_type(in_lock) == LOCK_REC);

	trx = in_lock->trx;
unknown's avatar
unknown committed
2281

2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
	space = in_lock->un_member.rec_lock.space;
	page_no = in_lock->un_member.rec_lock.page_no;

	HASH_DELETE(lock_t, hash, lock_sys->rec_hash,
				lock_rec_fold(space, page_no), in_lock);

	UT_LIST_REMOVE(trx_locks, trx->trx_locks, in_lock);

	/* Check if waiting locks in the queue can now be granted: grant
	locks if there are no conflicting locks ahead. */

	lock = lock_rec_get_first_on_page_addr(space, page_no);

unknown's avatar
unknown committed
2295
	while (lock != NULL) {		
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
		if (lock_get_wait(lock)
				&& !lock_rec_has_to_wait_in_queue(lock)) {

			/* Grant the lock */
			lock_grant(lock);
		}

		lock = lock_rec_get_next_on_page(lock);
	}
}	

unknown's avatar
unknown committed
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
/*****************************************************************
Removes a record lock request, waiting or granted, from the queue. */
static
void
lock_rec_discard(
/*=============*/
	lock_t*	in_lock)/* in: record lock object: all record locks which
			are contained in this lock object are removed */
{
	ulint	space;
	ulint	page_no;
	trx_t*	trx;
	
2320
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
2321
	ut_ad(mutex_own(&kernel_mutex));
2322
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
	ut_ad(lock_get_type(in_lock) == LOCK_REC);

	trx = in_lock->trx;
	
	space = in_lock->un_member.rec_lock.space;
	page_no = in_lock->un_member.rec_lock.page_no;

	HASH_DELETE(lock_t, hash, lock_sys->rec_hash,
				lock_rec_fold(space, page_no), in_lock);

	UT_LIST_REMOVE(trx_locks, trx->trx_locks, in_lock);
}

2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
/*****************************************************************
Removes record lock objects set on an index page which is discarded. This
function does not move locks, or check for waiting locks, therefore the
lock bitmaps must already be reset when this function is called. */
static
void
lock_rec_free_all_from_discard_page(
/*================================*/
	page_t*	page)	/* in: page to be discarded */
{
	ulint	space;
	ulint	page_no;
	lock_t*	lock;
	lock_t*	next_lock;
	
2351
#ifdef UNIV_SYNC_DEBUG
2352
	ut_ad(mutex_own(&kernel_mutex));
2353
#endif /* UNIV_SYNC_DEBUG */
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
	
	space = buf_frame_get_space_id(page);
	page_no = buf_frame_get_page_no(page);

	lock = lock_rec_get_first_on_page_addr(space, page_no);

	while (lock != NULL) {
		ut_ad(lock_rec_find_set_bit(lock) == ULINT_UNDEFINED);
		ut_ad(!lock_get_wait(lock));

		next_lock = lock_rec_get_next_on_page(lock);
		
unknown's avatar
unknown committed
2366
		lock_rec_discard(lock);
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
		
		lock = next_lock;
	}
}	

/*============= RECORD LOCK MOVING AND INHERITING ===================*/

/*****************************************************************
Resets the lock bits for a single record. Releases transactions waiting for
lock requests here. */

void
lock_rec_reset_and_release_wait(
/*============================*/
	rec_t*	rec)	/* in: record whose locks bits should be reset */
{
	lock_t*	lock;
	ulint	heap_no;
unknown's avatar
unknown committed
2385 2386
	ibool	comp;

2387
#ifdef UNIV_SYNC_DEBUG
2388
	ut_ad(mutex_own(&kernel_mutex));
2389
#endif /* UNIV_SYNC_DEBUG */
2390

unknown's avatar
unknown committed
2391 2392
	comp = page_is_comp(buf_frame_align(rec));
	heap_no = rec_get_heap_no(rec, comp);
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
	
	lock = lock_rec_get_first(rec);

	while (lock != NULL) {
		if (lock_get_wait(lock)) {
			lock_rec_cancel(lock);
		} else {
			lock_rec_reset_nth_bit(lock, heap_no);
		}

unknown's avatar
unknown committed
2403
		lock = lock_rec_get_next(rec, comp, lock);
2404 2405 2406 2407
	}
}	

/*****************************************************************
unknown's avatar
unknown committed
2408 2409 2410 2411
Makes a record to inherit the locks (except LOCK_INSERT_INTENTION type)
of another record as gap type locks, but does not reset the lock bits of
the other record. Also waiting lock requests on rec are inherited as
GRANTED gap locks. */
2412 2413 2414 2415 2416 2417 2418 2419 2420

void
lock_rec_inherit_to_gap(
/*====================*/
	rec_t*	heir,	/* in: record which inherits */
	rec_t*	rec)	/* in: record from which inherited; does NOT reset
			the locks on this record */
{
	lock_t*	lock;
unknown's avatar
unknown committed
2421
	ibool	comp;
2422
#ifdef UNIV_SYNC_DEBUG
2423
	ut_ad(mutex_own(&kernel_mutex));
2424
#endif /* UNIV_SYNC_DEBUG */
2425 2426
	
	lock = lock_rec_get_first(rec);
unknown's avatar
unknown committed
2427
	comp = page_is_comp(buf_frame_align(rec));
2428 2429

	while (lock != NULL) {
unknown's avatar
unknown committed
2430 2431 2432 2433
		if (!lock_rec_get_insert_intention(lock)) {
			
			lock_rec_add_to_queue(LOCK_REC | lock_get_mode(lock)
						| LOCK_GAP,
2434
	 			     		heir, lock->index, lock->trx);
unknown's avatar
unknown committed
2435 2436
	 	}
	 	
unknown's avatar
unknown committed
2437
		lock = lock_rec_get_next(rec, comp, lock);
unknown's avatar
unknown committed
2438 2439 2440 2441 2442 2443 2444
	}
}	

/*****************************************************************
Makes a record to inherit the gap locks (except LOCK_INSERT_INTENTION type)
of another record as gap type locks, but does not reset the lock bits of the
other record. Also waiting lock requests are inherited as GRANTED gap locks. */
unknown's avatar
unknown committed
2445
static
unknown's avatar
unknown committed
2446 2447 2448 2449 2450 2451 2452 2453
void
lock_rec_inherit_to_gap_if_gap_lock(
/*================================*/
	rec_t*	heir,	/* in: record which inherits */
	rec_t*	rec)	/* in: record from which inherited; does NOT reset
			the locks on this record */
{
	lock_t*	lock;
unknown's avatar
unknown committed
2454
	ibool	comp;
2455
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
2456
	ut_ad(mutex_own(&kernel_mutex));
2457
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
2458 2459
	
	lock = lock_rec_get_first(rec);
unknown's avatar
unknown committed
2460
	comp = page_is_comp(buf_frame_align(rec));
unknown's avatar
unknown committed
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471

	while (lock != NULL) {
		if (!lock_rec_get_insert_intention(lock)
		    && (page_rec_is_supremum(rec)
			|| !lock_rec_get_rec_not_gap(lock))) {
			
			lock_rec_add_to_queue(LOCK_REC | lock_get_mode(lock)
						| LOCK_GAP,
	 			     		heir, lock->index, lock->trx);
	 	}

unknown's avatar
unknown committed
2472
		lock = lock_rec_get_next(rec, comp, lock);
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
	}
}	

/*****************************************************************
Moves the locks of a record to another record and resets the lock bits of
the donating record. */
static
void
lock_rec_move(
/*==========*/
	rec_t*	receiver,	/* in: record which gets locks; this record
				must have no lock requests on it! */
unknown's avatar
unknown committed
2485 2486
	rec_t*	donator,	/* in: record which gives locks */
	ibool	comp)		/* in: TRUE=compact page format */
2487 2488 2489 2490 2491
{
	lock_t*	lock;
	ulint	heap_no;
	ulint	type_mode;
	
2492
#ifdef UNIV_SYNC_DEBUG
2493
	ut_ad(mutex_own(&kernel_mutex));
2494
#endif /* UNIV_SYNC_DEBUG */
2495

unknown's avatar
unknown committed
2496
	heap_no = rec_get_heap_no(donator, comp);
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
	
	lock = lock_rec_get_first(donator);

	ut_ad(lock_rec_get_first(receiver) == NULL);

	while (lock != NULL) {
		type_mode = lock->type_mode;
	
		lock_rec_reset_nth_bit(lock, heap_no);

		if (lock_get_wait(lock)) {
			lock_reset_lock_and_trx_wait(lock);
		}	

		/* Note that we FIRST reset the bit, and then set the lock:
		the function works also if donator == receiver */

		lock_rec_add_to_queue(type_mode, receiver, lock->index,
								lock->trx);
unknown's avatar
unknown committed
2516
		lock = lock_rec_get_next(donator, comp, lock);
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
	}

	ut_ad(lock_rec_get_first(donator) == NULL);
}	

/*****************************************************************
Updates the lock table when we have reorganized a page. NOTE: we copy
also the locks set on the infimum of the page; the infimum may carry
locks if an update of a record is occurring on the page, and its locks
were temporarily stored on the infimum. */

void
lock_move_reorganize_page(
/*======================*/
	page_t*	page,		/* in: old index page, now reorganized */
	page_t*	old_page)	/* in: copy of the old, not reorganized page */
{
	lock_t*		lock;
	lock_t*		old_lock;
	page_cur_t	cur1;
	page_cur_t	cur2;
	ulint		old_heap_no;
	UT_LIST_BASE_NODE_T(lock_t)	old_locks;
	mem_heap_t*	heap		= NULL;
	rec_t*		sup;
unknown's avatar
unknown committed
2542
	ibool		comp;
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582

	lock_mutex_enter_kernel();

	lock = lock_rec_get_first_on_page(page);

	if (lock == NULL) {
		lock_mutex_exit_kernel();

		return;
	}

	heap = mem_heap_create(256);
	
	/* Copy first all the locks on the page to heap and reset the
	bitmaps in the original locks; chain the copies of the locks
	using the trx_locks field in them. */

	UT_LIST_INIT(old_locks);
	
	while (lock != NULL) {

		/* Make a copy of the lock */
		old_lock = lock_rec_copy(lock, heap);

		UT_LIST_ADD_LAST(trx_locks, old_locks, old_lock);

		/* Reset bitmap of lock */
		lock_rec_bitmap_reset(lock);

		if (lock_get_wait(lock)) {
			lock_reset_lock_and_trx_wait(lock);
		}		

		lock = lock_rec_get_next_on_page(lock);
	}

	sup = page_get_supremum_rec(page);
	
	lock = UT_LIST_GET_FIRST(old_locks);

unknown's avatar
unknown committed
2583 2584 2585
	comp = page_is_comp(page);
	ut_ad(comp == page_is_comp(old_page));

2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
	while (lock) {
		/* NOTE: we copy also the locks set on the infimum and
		supremum of the page; the infimum may carry locks if an
		update of a record is occurring on the page, and its locks
		were temporarily stored on the infimum */
		
		page_cur_set_before_first(page, &cur1);
		page_cur_set_before_first(old_page, &cur2);

		/* Set locks according to old locks */
		for (;;) {
unknown's avatar
unknown committed
2597
			ut_ad(comp || 0 == ut_memcmp(page_cur_get_rec(&cur1),
2598
						page_cur_get_rec(&cur2),
unknown's avatar
unknown committed
2599
						rec_get_data_size_old(
2600
						   page_cur_get_rec(&cur2))));
unknown's avatar
unknown committed
2601 2602
			old_heap_no = rec_get_heap_no(page_cur_get_rec(&cur2),
							comp);
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614

			if (lock_rec_get_nth_bit(lock, old_heap_no)) {

				/* NOTE that the old lock bitmap could be too
				small for the new heap number! */

				lock_rec_add_to_queue(lock->type_mode,
						page_cur_get_rec(&cur1),
						lock->index, lock->trx);

				/* if ((page_cur_get_rec(&cur1) == sup)
						&& lock_get_wait(lock)) {
2615
					fprintf(stderr,
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
				"---\n--\n!!!Lock reorg: supr type %lu\n",
					lock->type_mode);
				} */
			}

			if (page_cur_get_rec(&cur1) == sup) {

				break;
			}

			page_cur_move_to_next(&cur1);
			page_cur_move_to_next(&cur2);
		}

		/* Remember that we chained old locks on the trx_locks field: */

		lock = UT_LIST_GET_NEXT(trx_locks, lock);
	}

	lock_mutex_exit_kernel();

	mem_heap_free(heap);

/* 	ut_ad(lock_rec_validate_page(buf_frame_get_space_id(page),
					buf_frame_get_page_no(page))); */
}	

/*****************************************************************
Moves the explicit locks on user records to another page if a record
list end is moved to another page. */

void
lock_move_rec_list_end(
/*===================*/
	page_t*	new_page,	/* in: index page to move to */
	page_t*	page,		/* in: index page */
	rec_t*	rec)		/* in: record on page: this is the
				first record moved */
{
	lock_t*		lock;
	page_cur_t	cur1;
	page_cur_t	cur2;
	ulint		heap_no;
	rec_t*		sup;
	ulint		type_mode;
unknown's avatar
unknown committed
2661
	ibool		comp;
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
	
	lock_mutex_enter_kernel();

	/* Note: when we move locks from record to record, waiting locks
	and possible granted gap type locks behind them are enqueued in
	the original order, because new elements are inserted to a hash
	table to the end of the hash chain, and lock_rec_add_to_queue
	does not reuse locks if there are waiters in the queue. */

	sup = page_get_supremum_rec(page);
	
	lock = lock_rec_get_first_on_page(page);

unknown's avatar
unknown committed
2675 2676
	comp = page_is_comp(page);

2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
	while (lock != NULL) {
		
		page_cur_position(rec, &cur1);

		if (page_cur_is_before_first(&cur1)) {
			page_cur_move_to_next(&cur1);
		}

		page_cur_set_before_first(new_page, &cur2);
		page_cur_move_to_next(&cur2);
	
		/* Copy lock requests on user records to new page and
		reset the lock bits on the old */

		while (page_cur_get_rec(&cur1) != sup) {
unknown's avatar
unknown committed
2692
			ut_ad(comp || 0 == ut_memcmp(page_cur_get_rec(&cur1),
2693
						page_cur_get_rec(&cur2),
unknown's avatar
unknown committed
2694
						rec_get_data_size_old(
2695
						   page_cur_get_rec(&cur2))));
unknown's avatar
unknown committed
2696 2697
			heap_no = rec_get_heap_no(page_cur_get_rec(&cur1),
									comp);
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734

			if (lock_rec_get_nth_bit(lock, heap_no)) {
				type_mode = lock->type_mode;

				lock_rec_reset_nth_bit(lock, heap_no);

				if (lock_get_wait(lock)) {
					lock_reset_lock_and_trx_wait(lock);
				}	

				lock_rec_add_to_queue(type_mode,
						page_cur_get_rec(&cur2),
						lock->index, lock->trx);
			}

			page_cur_move_to_next(&cur1);
			page_cur_move_to_next(&cur2);
		}

		lock = lock_rec_get_next_on_page(lock);
	}
	
	lock_mutex_exit_kernel();

/*	ut_ad(lock_rec_validate_page(buf_frame_get_space_id(page),
					buf_frame_get_page_no(page)));
	ut_ad(lock_rec_validate_page(buf_frame_get_space_id(new_page),
					buf_frame_get_page_no(new_page))); */
}	

/*****************************************************************
Moves the explicit locks on user records to another page if a record
list start is moved to another page. */

void
lock_move_rec_list_start(
/*=====================*/
unknown's avatar
unknown committed
2735
	page_t*	new_page,	/* in: index page to move to */
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
	page_t*	page,		/* in: index page */
	rec_t*	rec,		/* in: record on page: this is the
				first record NOT copied */
	rec_t*	old_end)	/* in: old previous-to-last record on
				new_page before the records were copied */
{
	lock_t*		lock;
	page_cur_t	cur1;
	page_cur_t	cur2;
	ulint		heap_no;
	ulint		type_mode;
unknown's avatar
unknown committed
2747
	ibool		comp;
2748

unknown's avatar
unknown committed
2749
	ut_a(new_page);
2750 2751 2752 2753

	lock_mutex_enter_kernel();

	lock = lock_rec_get_first_on_page(page);
unknown's avatar
unknown committed
2754 2755
	comp = page_is_comp(page);
	ut_ad(comp == page_is_comp(new_page));
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768

	while (lock != NULL) {
		
		page_cur_set_before_first(page, &cur1);
		page_cur_move_to_next(&cur1);

		page_cur_position(old_end, &cur2);
		page_cur_move_to_next(&cur2);

		/* Copy lock requests on user records to new page and
		reset the lock bits on the old */

		while (page_cur_get_rec(&cur1) != rec) {
unknown's avatar
unknown committed
2769
			ut_ad(comp || 0 == ut_memcmp(page_cur_get_rec(&cur1),
2770
						page_cur_get_rec(&cur2),
unknown's avatar
unknown committed
2771
						rec_get_data_size_old(
2772
						   page_cur_get_rec(&cur2))));
unknown's avatar
unknown committed
2773 2774
			heap_no = rec_get_heap_no(page_cur_get_rec(&cur1),
									comp);
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813

			if (lock_rec_get_nth_bit(lock, heap_no)) {
				type_mode = lock->type_mode;

				lock_rec_reset_nth_bit(lock, heap_no);

				if (lock_get_wait(lock)) {
					lock_reset_lock_and_trx_wait(lock);
				}			

				lock_rec_add_to_queue(type_mode,
						page_cur_get_rec(&cur2),
						lock->index, lock->trx);
			}

			page_cur_move_to_next(&cur1);
			page_cur_move_to_next(&cur2);
		}

		lock = lock_rec_get_next_on_page(lock);
	}
	
	lock_mutex_exit_kernel();

/*	ut_ad(lock_rec_validate_page(buf_frame_get_space_id(page),
					buf_frame_get_page_no(page)));
	ut_ad(lock_rec_validate_page(buf_frame_get_space_id(new_page),
					buf_frame_get_page_no(new_page))); */
}	

/*****************************************************************
Updates the lock table when a page is split to the right. */

void
lock_update_split_right(
/*====================*/
	page_t*	right_page,	/* in: right page */
	page_t*	left_page)	/* in: left page */
{
unknown's avatar
unknown committed
2814
	ibool	comp;
2815
	lock_mutex_enter_kernel();
unknown's avatar
unknown committed
2816 2817 2818
	comp = page_is_comp(left_page);
	ut_ad(comp == page_is_comp(right_page));

2819 2820 2821 2822
	/* Move the locks on the supremum of the left page to the supremum
	of the right page */

	lock_rec_move(page_get_supremum_rec(right_page),
unknown's avatar
unknown committed
2823
				page_get_supremum_rec(left_page), comp);
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
	
	/* Inherit the locks to the supremum of left page from the successor
	of the infimum on right page */

	lock_rec_inherit_to_gap(page_get_supremum_rec(left_page),
			page_rec_get_next(page_get_infimum_rec(right_page)));

	lock_mutex_exit_kernel();
}	

/*****************************************************************
Updates the lock table when a page is merged to the right. */

void
lock_update_merge_right(
/*====================*/
	rec_t*	orig_succ,	/* in: original successor of infimum
				on the right page before merge */
	page_t*	left_page)	/* in: merged index page which will be
				discarded */
{
	lock_mutex_enter_kernel();
	
	/* Inherit the locks from the supremum of the left page to the
	original successor of infimum on the right page, to which the left
	page was merged */

	lock_rec_inherit_to_gap(orig_succ, page_get_supremum_rec(left_page));

	/* Reset the locks on the supremum of the left page, releasing
	waiting transactions */

	lock_rec_reset_and_release_wait(page_get_supremum_rec(left_page));
	
	lock_rec_free_all_from_discard_page(left_page);

	lock_mutex_exit_kernel();
}

/*****************************************************************
Updates the lock table when the root page is copied to another in
btr_root_raise_and_insert. Note that we leave lock structs on the
root page, even though they do not make sense on other than leaf
pages: the reason is that in a pessimistic update the infimum record
of the root page will act as a dummy carrier of the locks of the record
to be updated. */

void
lock_update_root_raise(
/*===================*/
	page_t*	new_page,	/* in: index page to which copied */
	page_t*	root)		/* in: root page */
{
unknown's avatar
unknown committed
2877
	ibool	comp;
2878
	lock_mutex_enter_kernel();
unknown's avatar
unknown committed
2879 2880 2881
	comp = page_is_comp(root);
	ut_ad(comp == page_is_comp(new_page));

2882 2883 2884 2885
	/* Move the locks on the supremum of the root to the supremum
	of new_page */

	lock_rec_move(page_get_supremum_rec(new_page),
unknown's avatar
unknown committed
2886
					page_get_supremum_rec(root), comp);
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
	lock_mutex_exit_kernel();
}

/*****************************************************************
Updates the lock table when a page is copied to another and the original page
is removed from the chain of leaf pages, except if page is the root! */

void
lock_update_copy_and_discard(
/*=========================*/
	page_t*	new_page,	/* in: index page to which copied */
	page_t*	page)		/* in: index page; NOT the root! */
{
unknown's avatar
unknown committed
2900
	ibool	comp;
2901
	lock_mutex_enter_kernel();
unknown's avatar
unknown committed
2902 2903 2904
	comp = page_is_comp(page);
	ut_ad(comp == page_is_comp(new_page));

2905 2906 2907 2908
	/* Move the locks on the supremum of the old page to the supremum
	of new_page */

	lock_rec_move(page_get_supremum_rec(new_page),
unknown's avatar
unknown committed
2909
					page_get_supremum_rec(page), comp);
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
	lock_rec_free_all_from_discard_page(page);

	lock_mutex_exit_kernel();
}

/*****************************************************************
Updates the lock table when a page is split to the left. */

void
lock_update_split_left(
/*===================*/
	page_t*	right_page,	/* in: right page */
	page_t*	left_page)	/* in: left page */
{
	lock_mutex_enter_kernel();
	
	/* Inherit the locks to the supremum of the left page from the
	successor of the infimum on the right page */

	lock_rec_inherit_to_gap(page_get_supremum_rec(left_page),
			page_rec_get_next(page_get_infimum_rec(right_page)));

	lock_mutex_exit_kernel();
}

/*****************************************************************
Updates the lock table when a page is merged to the left. */

void
lock_update_merge_left(
/*===================*/
	page_t*	left_page,	/* in: left page to which merged */
	rec_t*	orig_pred,	/* in: original predecessor of supremum
				on the left page before merge */
	page_t*	right_page)	/* in: merged index page which will be
				discarded */
{
unknown's avatar
unknown committed
2947
	ibool	comp;
2948
	lock_mutex_enter_kernel();
unknown's avatar
unknown committed
2949 2950 2951
	comp = page_is_comp(left_page);
	ut_ad(comp == page_is_comp(right_page));

2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
	if (page_rec_get_next(orig_pred) != page_get_supremum_rec(left_page)) {

		/* Inherit the locks on the supremum of the left page to the
		first record which was moved from the right page */

		lock_rec_inherit_to_gap(page_rec_get_next(orig_pred),
					page_get_supremum_rec(left_page));

		/* Reset the locks on the supremum of the left page,
		releasing waiting transactions */

		lock_rec_reset_and_release_wait(page_get_supremum_rec(
								left_page));
	}

	/* Move the locks from the supremum of right page to the supremum
	of the left page */
	
	lock_rec_move(page_get_supremum_rec(left_page),
unknown's avatar
unknown committed
2971
				page_get_supremum_rec(right_page), comp);
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052

	lock_rec_free_all_from_discard_page(right_page);

	lock_mutex_exit_kernel();
}

/*****************************************************************
Resets the original locks on heir and replaces them with gap type locks
inherited from rec. */

void
lock_rec_reset_and_inherit_gap_locks(
/*=================================*/
	rec_t*	heir,	/* in: heir record */
	rec_t*	rec)	/* in: record */
{
	mutex_enter(&kernel_mutex);	      				

	lock_rec_reset_and_release_wait(heir);
	
	lock_rec_inherit_to_gap(heir, rec);

	mutex_exit(&kernel_mutex);	      				
}

/*****************************************************************
Updates the lock table when a page is discarded. */

void
lock_update_discard(
/*================*/
	rec_t*	heir,	/* in: record which will inherit the locks */
	page_t*	page)	/* in: index page which will be discarded */
{
	rec_t*	rec;

	lock_mutex_enter_kernel();
	
	if (NULL == lock_rec_get_first_on_page(page)) {
		/* No locks exist on page, nothing to do */

		lock_mutex_exit_kernel();

		return;
	}
	
	/* Inherit all the locks on the page to the record and reset all
	the locks on the page */

	rec = page_get_infimum_rec(page);

	for (;;) {
		lock_rec_inherit_to_gap(heir, rec);

		/* Reset the locks on rec, releasing waiting transactions */

		lock_rec_reset_and_release_wait(rec);

		if (rec == page_get_supremum_rec(page)) {

			break;
		}
		
		rec = page_rec_get_next(rec);
	}

	lock_rec_free_all_from_discard_page(page);

	lock_mutex_exit_kernel();
}

/*****************************************************************
Updates the lock table when a new user record is inserted. */

void
lock_update_insert(
/*===============*/
	rec_t*	rec)	/* in: the inserted record */
{
	lock_mutex_enter_kernel();

unknown's avatar
unknown committed
3053 3054
	/* Inherit the gap-locking locks for rec, in gap mode, from the next
	record */
3055

unknown's avatar
unknown committed
3056
	lock_rec_inherit_to_gap_if_gap_lock(rec, page_rec_get_next(rec));
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097

	lock_mutex_exit_kernel();
}	

/*****************************************************************
Updates the lock table when a record is removed. */

void
lock_update_delete(
/*===============*/
	rec_t*	rec)	/* in: the record to be removed */
{
	lock_mutex_enter_kernel();

	/* Let the next record inherit the locks from rec, in gap mode */

	lock_rec_inherit_to_gap(page_rec_get_next(rec), rec);

	/* Reset the lock bits on rec and release waiting transactions */

	lock_rec_reset_and_release_wait(rec);	

	lock_mutex_exit_kernel();
}
 
/*************************************************************************
Stores on the page infimum record the explicit locks of another record.
This function is used to store the lock state of a record when it is
updated and the size of the record changes in the update. The record
is moved in such an update, perhaps to another page. The infimum record
acts as a dummy carrier record, taking care of lock releases while the
actual record is being moved. */

void
lock_rec_store_on_page_infimum(
/*===========================*/
	rec_t*	rec)	/* in: record whose lock state is stored
			on the infimum record of the same page; lock
			bits are reset on the record */
{
	page_t*	page;
unknown's avatar
unknown committed
3098
	ibool	comp;
3099 3100

	page = buf_frame_align(rec);
unknown's avatar
unknown committed
3101
	comp = page_is_comp(page);
3102 3103 3104

	lock_mutex_enter_kernel();
	
unknown's avatar
unknown committed
3105
	lock_rec_move(page_get_infimum_rec(page), rec, comp);
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121

	lock_mutex_exit_kernel();	
}

/*************************************************************************
Restores the state of explicit lock requests on a single record, where the
state was stored on the infimum of the page. */

void
lock_rec_restore_from_page_infimum(
/*===============================*/
	rec_t*	rec,	/* in: record whose lock state is restored */
	page_t*	page)	/* in: page (rec is not necessarily on this page)
			whose infimum stored the lock state; lock bits are
			reset on the infimum */ 
{
unknown's avatar
unknown committed
3122
	ibool	comp;
3123
	lock_mutex_enter_kernel();
unknown's avatar
unknown committed
3124 3125 3126 3127
	comp = page_is_comp(page);
	ut_ad(comp == page_is_comp(buf_frame_align(rec)));

	lock_rec_move(rec, page_get_infimum_rec(page), comp);
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139
	
	lock_mutex_exit_kernel();
}

/*=========== DEADLOCK CHECKING ======================================*/

/************************************************************************
Checks if a lock request results in a deadlock. */
static
ibool
lock_deadlock_occurs(
/*=================*/
unknown's avatar
unknown committed
3140 3141 3142 3143
			/* out: TRUE if a deadlock was detected and we
			chose trx as a victim; FALSE if no deadlock, or
			there was a deadlock, but we chose other
			transaction(s) as victim(s) */
3144 3145 3146 3147 3148
	lock_t*	lock,	/* in: lock the transaction is requesting */
	trx_t*	trx)	/* in: transaction */
{
	dict_table_t*	table;
	dict_index_t*	index;
3149
	trx_t*		mark_trx;
unknown's avatar
unknown committed
3150
	ulint		ret;
3151
	ulint		cost	= 0;
3152 3153

	ut_ad(trx && lock);
3154
#ifdef UNIV_SYNC_DEBUG
3155
	ut_ad(mutex_own(&kernel_mutex));
3156
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
3157
retry:
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
	/* We check that adding this trx to the waits-for graph
	does not produce a cycle. First mark all active transactions
	with 0: */

	mark_trx = UT_LIST_GET_FIRST(trx_sys->trx_list);

	while (mark_trx) {
		mark_trx->deadlock_mark = 0;
		mark_trx = UT_LIST_GET_NEXT(trx_list, mark_trx);
	}

	ret = lock_deadlock_recursive(trx, trx, lock, &cost);
3170

unknown's avatar
unknown committed
3171 3172 3173 3174 3175 3176 3177 3178
	if (ret == LOCK_VICTIM_IS_OTHER) {
		/* We chose some other trx as a victim: retry if there still
		is a deadlock */

		goto retry;
	}

	if (ret == LOCK_VICTIM_IS_START) {
3179
		if (lock_get_type(lock) & LOCK_TABLE) {
3180 3181 3182 3183 3184 3185
			table = lock->un_member.tab_lock.table;
			index = NULL;
		} else {
			index = lock->index;
			table = index->table;
		}
unknown's avatar
unknown committed
3186 3187 3188

		lock_deadlock_found = TRUE;

3189 3190
		fputs("*** WE ROLL BACK TRANSACTION (2)\n",
			lock_latest_err_file);
3191

unknown's avatar
unknown committed
3192
		return(TRUE);
3193 3194
	}
	
unknown's avatar
unknown committed
3195
	return(FALSE);
3196 3197 3198 3199 3200
}

/************************************************************************
Looks recursively for a deadlock. */
static
unknown's avatar
unknown committed
3201
ulint
3202 3203
lock_deadlock_recursive(
/*====================*/
unknown's avatar
unknown committed
3204 3205 3206 3207 3208 3209 3210 3211
				/* out: 0 if no deadlock found,
				LOCK_VICTIM_IS_START if there was a deadlock
				and we chose 'start' as the victim,
				LOCK_VICTIM_IS_OTHER if a deadlock
				was found and we chose some other trx as a
				victim: we must do the search again in this
				last case because there may be another
				deadlock! */
3212 3213
	trx_t*	start,		/* in: recursion starting point */
	trx_t*	trx,		/* in: a transaction waiting for a lock */
3214 3215 3216
	lock_t*	wait_lock,	/* in: the lock trx is waiting to be granted */
	ulint*	cost)		/* in/out: number of calculation steps thus
				far: if this exceeds LOCK_MAX_N_STEPS_...
unknown's avatar
unknown committed
3217
				we return LOCK_VICTIM_IS_START */
3218 3219
{
	lock_t*	lock;
unknown's avatar
unknown committed
3220
	ulint	bit_no		= ULINT_UNDEFINED;
3221
	trx_t*	lock_trx;
unknown's avatar
unknown committed
3222
	ulint	ret;
3223 3224
	
	ut_a(trx && start && wait_lock);
3225
#ifdef UNIV_SYNC_DEBUG
3226
	ut_ad(mutex_own(&kernel_mutex));
3227
#endif /* UNIV_SYNC_DEBUG */
3228
	
3229 3230 3231 3232
	if (trx->deadlock_mark == 1) {
		/* We have already exhaustively searched the subtree starting
		from this trx */

unknown's avatar
unknown committed
3233
		return(0);
3234 3235 3236 3237 3238 3239
	}

	*cost = *cost + 1;

	if (*cost > LOCK_MAX_N_STEPS_IN_DEADLOCK_CHECK) {

unknown's avatar
unknown committed
3240
		return(LOCK_VICTIM_IS_START);
3241 3242
	}

3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254
	lock = wait_lock;

	if (lock_get_type(wait_lock) == LOCK_REC) {

		bit_no = lock_rec_find_set_bit(wait_lock);

		ut_a(bit_no != ULINT_UNDEFINED);
	}

	/* Look at the locks ahead of wait_lock in the lock queue */

	for (;;) {
3255
		if (lock_get_type(lock) & LOCK_TABLE) {
3256 3257 3258 3259

			lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
		} else {
			ut_ad(lock_get_type(lock) == LOCK_REC);
unknown's avatar
unknown committed
3260
			ut_a(bit_no != ULINT_UNDEFINED);
3261 3262 3263 3264 3265

			lock = lock_rec_get_prev(lock, bit_no);
		}

		if (lock == NULL) {
3266 3267
			/* We can mark this subtree as searched */
			trx->deadlock_mark = 1;
3268 3269 3270 3271 3272 3273 3274 3275 3276

			return(FALSE);
		}

		if (lock_has_to_wait(wait_lock, lock)) {

			lock_trx = lock->trx;

			if (lock_trx == start) {
unknown's avatar
unknown committed
3277 3278
				/* We came back to the recursion starting
				point: a deadlock detected */
3279
				FILE*	ef = lock_latest_err_file;
unknown's avatar
unknown committed
3280
				
3281 3282
				rewind(ef);
				ut_print_timestamp(ef);
unknown's avatar
unknown committed
3283

3284
				fputs("\n*** (1) TRANSACTION:\n", ef);
unknown's avatar
unknown committed
3285

3286
				trx_print(ef, wait_lock->trx);
unknown's avatar
unknown committed
3287

3288 3289
				fputs(
			"*** (1) WAITING FOR THIS LOCK TO BE GRANTED:\n", ef);
unknown's avatar
unknown committed
3290 3291
			
				if (lock_get_type(wait_lock) == LOCK_REC) {
3292
					lock_rec_print(ef, wait_lock);
unknown's avatar
unknown committed
3293
				} else {
3294
					lock_table_print(ef, wait_lock);
unknown's avatar
unknown committed
3295 3296
				}
			
3297
				fputs("*** (2) TRANSACTION:\n", ef);
unknown's avatar
unknown committed
3298

3299
				trx_print(ef, lock->trx);
unknown's avatar
unknown committed
3300

3301
				fputs("*** (2) HOLDS THE LOCK(S):\n", ef);
unknown's avatar
unknown committed
3302 3303
			
				if (lock_get_type(lock) == LOCK_REC) {
3304
					lock_rec_print(ef, lock);
unknown's avatar
unknown committed
3305
				} else {
3306
					lock_table_print(ef, lock);
unknown's avatar
unknown committed
3307 3308
				}
			
3309 3310
				fputs(
			"*** (2) WAITING FOR THIS LOCK TO BE GRANTED:\n", ef);
unknown's avatar
unknown committed
3311 3312 3313
			
				if (lock_get_type(start->wait_lock)
								== LOCK_REC) {
3314
					lock_rec_print(ef, start->wait_lock);
unknown's avatar
unknown committed
3315
				} else {
3316
					lock_table_print(ef, start->wait_lock);
unknown's avatar
unknown committed
3317 3318
				}

3319
				if (lock_print_waits) {
3320
					fputs("Deadlock detected\n", stderr);
3321 3322
				}

unknown's avatar
unknown committed
3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
				if (ut_dulint_cmp(wait_lock->trx->undo_no,
							start->undo_no) >= 0) {
					/* Our recursion starting point
					transaction is 'smaller', let us
					choose 'start' as the victim and roll
					back it */

					return(LOCK_VICTIM_IS_START);
				}		

				lock_deadlock_found = TRUE;

				/* Let us choose the transaction of wait_lock
				as a victim to try to avoid deadlocking our
				recursion starting point transaction */
				
3339 3340
				fputs("*** WE ROLL BACK TRANSACTION (1)\n",
					ef);
unknown's avatar
unknown committed
3341
				
unknown's avatar
unknown committed
3342 3343
				wait_lock->trx->was_chosen_as_deadlock_victim
								= TRUE;
unknown's avatar
unknown committed
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
				
				lock_cancel_waiting_and_release(wait_lock);

				/* Since trx and wait_lock are no longer
				in the waits-for graph, we can return FALSE;
				note that our selective algorithm can choose
				several transactions as victims, but still
				we may end up rolling back also the recursion
				starting point transaction! */

				return(LOCK_VICTIM_IS_OTHER);
3355 3356 3357 3358 3359 3360 3361 3362
			}
	
			if (lock_trx->que_state == TRX_QUE_LOCK_WAIT) {

				/* Another trx ahead has requested lock	in an
				incompatible mode, and is itself waiting for
				a lock */

unknown's avatar
unknown committed
3363 3364 3365
				ret = lock_deadlock_recursive(start, lock_trx,
						lock_trx->wait_lock, cost);
				if (ret != 0) {
3366

unknown's avatar
unknown committed
3367
					return(ret);
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
				}
			}
		}
	}/* end of the 'for (;;)'-loop */
}

/*========================= TABLE LOCKS ==============================*/

/*************************************************************************
Creates a table lock object and adds it as the last in the lock queue
of the table. Does NOT check for deadlocks or lock compatibility. */
UNIV_INLINE
lock_t*
lock_table_create(
/*==============*/
				/* out, own: new lock object, or NULL if
				out of memory */
	dict_table_t*	table,	/* in: database table in dictionary cache */
	ulint		type_mode,/* in: lock mode possibly ORed with
				LOCK_WAIT */
	trx_t*		trx)	/* in: trx */
{
	lock_t*	lock;

	ut_ad(table && trx);
3393
#ifdef UNIV_SYNC_DEBUG
3394
	ut_ad(mutex_own(&kernel_mutex));
3395
#endif /* UNIV_SYNC_DEBUG */
3396

3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
	if (type_mode == LOCK_AUTO_INC) {
		/* Only one trx can have the lock on the table
		at a time: we may use the memory preallocated
		to the table object */

		lock = table->auto_inc_lock;

		ut_a(trx->auto_inc_lock == NULL);
		trx->auto_inc_lock = lock;
	} else {
		lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t));
	}
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419

	if (lock == NULL) {

		return(NULL);
	}

	UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock);

	lock->type_mode = type_mode | LOCK_TABLE;
	lock->trx = trx;

unknown's avatar
unknown committed
3420
	if (lock_get_type(lock) == LOCK_TABLE_EXP) {
unknown's avatar
unknown committed
3421 3422 3423
		lock->trx->n_lock_table_exp++;
	}

3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
	lock->un_member.tab_lock.table = table;

	UT_LIST_ADD_LAST(un_member.tab_lock.locks, table->locks, lock);

	if (type_mode & LOCK_WAIT) {

		lock_set_lock_and_trx_wait(lock, trx);
	}

	return(lock);
}

/*****************************************************************
Removes a table lock request from the queue and the trx list of locks;
this is a low-level function which does NOT check if waiting requests
can now be granted. */
UNIV_INLINE
void
lock_table_remove_low(
/*==================*/
	lock_t*	lock)	/* in: table lock */
{
	dict_table_t*	table;
	trx_t*		trx;

3449
#ifdef UNIV_SYNC_DEBUG
3450
	ut_ad(mutex_own(&kernel_mutex));
3451
#endif /* UNIV_SYNC_DEBUG */
3452 3453 3454 3455

	table = lock->un_member.tab_lock.table;
	trx = lock->trx;

3456 3457 3458
	if (lock == trx->auto_inc_lock) {
		trx->auto_inc_lock = NULL;
	}
unknown's avatar
unknown committed
3459 3460 3461 3462 3463

	if (lock_get_type(lock) == LOCK_TABLE_EXP) {
		lock->trx->n_lock_table_exp--;
	}

3464 3465 3466 3467 3468 3469 3470
	UT_LIST_REMOVE(trx_locks, trx->trx_locks, lock);
	UT_LIST_REMOVE(un_member.tab_lock.locks, table->locks, lock);
}	

/*************************************************************************
Enqueues a waiting request for a table lock which cannot be granted
immediately. Checks for deadlocks. */
unknown's avatar
unknown committed
3471
static
3472 3473 3474 3475
ulint
lock_table_enqueue_waiting(
/*=======================*/
				/* out: DB_LOCK_WAIT, DB_DEADLOCK, or
unknown's avatar
unknown committed
3476 3477 3478 3479 3480
				DB_QUE_THR_SUSPENDED, or DB_SUCCESS;
				DB_SUCCESS means that there was a deadlock,
				but another transaction was chosen as a
				victim, and we got the lock immediately:
				no need to wait then */
3481 3482 3483 3484 3485 3486 3487 3488
	ulint		mode,	/* in: lock mode this transaction is
				requesting */
	dict_table_t*	table,	/* in: table */
	que_thr_t*	thr)	/* in: query thread */
{
	lock_t*	lock;
	trx_t*	trx;
	
3489
#ifdef UNIV_SYNC_DEBUG
3490
	ut_ad(mutex_own(&kernel_mutex));
3491
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
3492
	
3493 3494 3495 3496 3497
	/* Test if there already is some other reason to suspend thread:
	we do not enqueue a lock request if the query thread should be
	stopped anyway */

	if (que_thr_stop(thr)) {
3498
		ut_error;
3499 3500 3501 3502 3503

		return(DB_QUE_THR_SUSPENDED);
	}

	trx = thr_get_trx(thr);
unknown's avatar
unknown committed
3504 3505 3506

	if (trx->dict_operation) {
		ut_print_timestamp(stderr);
3507
		fputs(
unknown's avatar
unknown committed
3508
"  InnoDB: Error: a table lock wait happens in a dictionary operation!\n"
3509
"InnoDB: Table name ", stderr);
3510
		ut_print_name(stderr, trx, table->name);
3511 3512
		fputs(".\n"
"InnoDB: Submit a detailed bug report to http://bugs.mysql.com\n",
3513
			stderr);
unknown's avatar
unknown committed
3514
	}
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530
	
	/* Enqueue the lock request that will wait to be granted */

	lock = lock_table_create(table, mode | LOCK_WAIT, trx);

	/* Check if a deadlock occurs: if yes, remove the lock request and
	return an error code */

	if (lock_deadlock_occurs(lock, trx)) {

		lock_reset_lock_and_trx_wait(lock);
		lock_table_remove_low(lock);

		return(DB_DEADLOCK);
	}

unknown's avatar
unknown committed
3531 3532 3533 3534 3535 3536 3537
	if (trx->wait_lock == NULL) {
		/* Deadlock resolution chose another transaction as a victim,
		and we accidentally got our lock granted! */
	
		return(DB_SUCCESS);
	}
	
3538
	trx->que_state = TRX_QUE_LOCK_WAIT;
unknown's avatar
unknown committed
3539
	trx->was_chosen_as_deadlock_victim = FALSE;
unknown's avatar
unknown committed
3540
	trx->wait_started = time(NULL);
3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562

	ut_a(que_thr_stop(thr));

	return(DB_LOCK_WAIT);
}

/*************************************************************************
Checks if other transactions have an incompatible mode lock request in
the lock queue. */
UNIV_INLINE
ibool
lock_table_other_has_incompatible(
/*==============================*/
	trx_t*		trx,	/* in: transaction, or NULL if all
				transactions should be included */
	ulint		wait,	/* in: LOCK_WAIT if also waiting locks are
				taken into account, or 0 if not */
	dict_table_t*	table,	/* in: table */
	ulint		mode)	/* in: lock mode */
{
	lock_t*	lock;

3563
#ifdef UNIV_SYNC_DEBUG
3564
	ut_ad(mutex_own(&kernel_mutex));
3565
#endif /* UNIV_SYNC_DEBUG */
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593

	lock = UT_LIST_GET_LAST(table->locks);

	while (lock != NULL) {

		if ((lock->trx != trx) 
		    && (!lock_mode_compatible(lock_get_mode(lock), mode))
		    && (wait || !(lock_get_wait(lock)))) {

		    	return(TRUE);
		}

		lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
	}

	return(FALSE);
}

/*************************************************************************
Locks the specified database table in the mode given. If the lock cannot
be granted immediately, the query thread is put to wait. */

ulint
lock_table(
/*=======*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
	ulint		flags,	/* in: if BTR_NO_LOCKING_FLAG bit is set,
3594 3595 3596
				does nothing;
				if LOCK_TABLE_EXP bits are set,
				creates an explicit table lock */
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610
	dict_table_t*	table,	/* in: database table in dictionary cache */
	ulint		mode,	/* in: lock mode */
	que_thr_t*	thr)	/* in: query thread */
{
	trx_t*	trx;
	ulint	err;
	
	ut_ad(table && thr);

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

unknown's avatar
unknown committed
3611
	ut_a(flags == 0 || flags == LOCK_TABLE_EXP);
3612

3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630
	trx = thr_get_trx(thr);

	lock_mutex_enter_kernel();

	/* Look for stronger locks the same trx already has on the table */

	if (lock_table_has(trx, table, mode)) {

		lock_mutex_exit_kernel();

		return(DB_SUCCESS);
	}

	/* We have to check if the new lock is compatible with any locks
	other transactions have in the table lock queue. */

	if (lock_table_other_has_incompatible(trx, LOCK_WAIT, table, mode)) {
	
3631
		/* Another trx has a request on the table in an incompatible
unknown's avatar
unknown committed
3632
		mode: this trx may have to wait */
3633 3634 3635 3636 3637 3638 3639 3640

		err = lock_table_enqueue_waiting(mode, table, thr);
			
		lock_mutex_exit_kernel();

		return(err);
	}

3641 3642
	lock_table_create(table, mode | flags, trx);

unknown's avatar
unknown committed
3643
	ut_a(!flags || mode == LOCK_S || mode == LOCK_X);
3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710

	lock_mutex_exit_kernel();

	return(DB_SUCCESS);
}

/*************************************************************************
Checks if there are any locks set on the table. */

ibool
lock_is_on_table(
/*=============*/
				/* out: TRUE if there are lock(s) */
	dict_table_t*	table)	/* in: database table in dictionary cache */
{
	ibool	ret;

	ut_ad(table);

	lock_mutex_enter_kernel();

	if (UT_LIST_GET_LAST(table->locks)) {
		ret = TRUE;
	} else {
		ret = FALSE;
	}

	lock_mutex_exit_kernel();

	return(ret);
}

/*************************************************************************
Checks if a waiting table lock request still has to wait in a queue. */
static
ibool
lock_table_has_to_wait_in_queue(
/*============================*/
				/* out: TRUE if still has to wait */
	lock_t*	wait_lock)	/* in: waiting table lock */
{
	dict_table_t*	table;
	lock_t*		lock;

 	ut_ad(lock_get_wait(wait_lock));
 	
	table = wait_lock->un_member.tab_lock.table;

	lock = UT_LIST_GET_FIRST(table->locks);

	while (lock != wait_lock) {

		if (lock_has_to_wait(wait_lock, lock)) {

		    	return(TRUE);
		}

		lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
	}

	return(FALSE);
}

/*****************************************************************
Removes a table lock request, waiting or granted, from the queue and grants
locks to other transactions in the queue, if they now are entitled to a
lock. */
unknown's avatar
unknown committed
3711
static
3712 3713 3714 3715 3716 3717 3718 3719 3720
void
lock_table_dequeue(
/*===============*/
	lock_t*	in_lock)/* in: table lock object; transactions waiting
			behind will get their lock requests granted, if
			they are now qualified to it */
{
	lock_t*	lock;
	
3721
#ifdef UNIV_SYNC_DEBUG
3722
	ut_ad(mutex_own(&kernel_mutex));
3723
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
3724
	ut_a(lock_get_type(in_lock) == LOCK_TABLE ||
3725
		lock_get_type(in_lock) == LOCK_TABLE_EXP);
3726 3727 3728 3729

	lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, in_lock);

	lock_table_remove_low(in_lock);
3730

3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
	/* Check if waiting locks in the queue can now be granted: grant
	locks if there are no conflicting locks ahead. */

	while (lock != NULL) {

		if (lock_get_wait(lock)
				&& !lock_table_has_to_wait_in_queue(lock)) {

			/* Grant the lock */
			lock_grant(lock);
		}

		lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
	}
}	

/*=========================== LOCK RELEASE ==============================*/

3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764
/*************************************************************************
Releases a table lock.
Releases possible other transactions waiting for this lock. */

void
lock_table_unlock(
/*==============*/
	lock_t*	lock)	/* in: lock */
{
	mutex_enter(&kernel_mutex);

	lock_table_dequeue(lock);

	mutex_exit(&kernel_mutex);
}

3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
/*************************************************************************
Releases an auto-inc lock a transaction possibly has on a table.
Releases possible other transactions waiting for this lock. */

void
lock_table_unlock_auto_inc(
/*=======================*/
	trx_t*	trx)	/* in: transaction */
{
	if (trx->auto_inc_lock) {
		mutex_enter(&kernel_mutex);

		lock_table_dequeue(trx->auto_inc_lock);

		mutex_exit(&kernel_mutex);
	}
}

3783 3784 3785 3786 3787 3788 3789 3790 3791
/*************************************************************************
Releases transaction locks, and releases possible other transactions waiting
because of these locks. */

void
lock_release_off_kernel(
/*====================*/
	trx_t*	trx)	/* in: transaction */
{
unknown's avatar
unknown committed
3792 3793 3794
	dict_table_t*	table;
	ulint		count;
	lock_t*		lock;
3795

3796
#ifdef UNIV_SYNC_DEBUG
3797
	ut_ad(mutex_own(&kernel_mutex));
3798
#endif /* UNIV_SYNC_DEBUG */
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811

	lock = UT_LIST_GET_LAST(trx->trx_locks);
	
	count = 0;

	while (lock != NULL) {

		count++;

		if (lock_get_type(lock) == LOCK_REC) {
			
			lock_rec_dequeue_from_page(lock);
		} else {
3812
			ut_ad(lock_get_type(lock) & LOCK_TABLE);
3813

unknown's avatar
unknown committed
3814
			if (lock_get_mode(lock) != LOCK_IS
unknown's avatar
unknown committed
3815 3816
			    && 0 != ut_dulint_cmp(trx->undo_no,
						  ut_dulint_zero)) {
unknown's avatar
unknown committed
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827

				/* The trx may have modified the table.
				We block the use of the MySQL query cache
				for all currently active transactions. */

				table = lock->un_member.tab_lock.table;
			
				table->query_cache_inv_trx_id =
							trx_sys->max_trx_id;
			}

3828
			lock_table_dequeue(lock);
3829
			if (lock_get_type(lock) == LOCK_TABLE_EXP) {
unknown's avatar
unknown committed
3830
				ut_a(lock_get_mode(lock) == LOCK_S
3831 3832
					|| lock_get_mode(lock) == LOCK_X);
			}
3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
		}

		if (count == LOCK_RELEASE_KERNEL_INTERVAL) {
			/* Release the kernel mutex for a while, so that we
			do not monopolize it */

			lock_mutex_exit_kernel();

			lock_mutex_enter_kernel();

			count = 0;
		}	

		lock = UT_LIST_GET_LAST(trx->trx_locks);
	}

	mem_heap_empty(trx->lock_heap);
3850 3851

	ut_a(trx->auto_inc_lock == NULL);
unknown's avatar
unknown committed
3852
	ut_a(trx->n_lock_table_exp == 0);
3853 3854 3855
}

/*************************************************************************
unknown's avatar
unknown committed
3856 3857
Releases table locks explicitly requested with LOCK TABLES (indicated by
lock type LOCK_TABLE_EXP), and releases possible other transactions waiting
3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
because of these locks. */

void
lock_release_tables_off_kernel(
/*===========================*/
	trx_t*	trx)	/* in: transaction */
{
	dict_table_t*	table;
	ulint		count;
	lock_t*		lock;

#ifdef UNIV_SYNC_DEBUG
	ut_ad(mutex_own(&kernel_mutex));
#endif /* UNIV_SYNC_DEBUG */

	lock = UT_LIST_GET_LAST(trx->trx_locks);

	count = 0;

	while (lock != NULL) {

		count++;

		if (lock_get_type(lock) == LOCK_TABLE_EXP) {
unknown's avatar
unknown committed
3882
			ut_a(lock_get_mode(lock) == LOCK_S
3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897
				|| lock_get_mode(lock) == LOCK_X);
			if (trx->insert_undo || trx->update_undo) {

				/* The trx may have modified the table.
				We block the use of the MySQL query
				cache for all currently active
				transactions. */

				table = lock->un_member.tab_lock.table;

				table->query_cache_inv_trx_id =
							trx_sys->max_trx_id;
			}

			lock_table_dequeue(lock);
unknown's avatar
unknown committed
3898

3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
			lock = UT_LIST_GET_LAST(trx->trx_locks);
			continue;
		}

		if (count == LOCK_RELEASE_KERNEL_INTERVAL) {
			/* Release the kernel mutex for a while, so that we
			do not monopolize it */

			lock_mutex_exit_kernel();

			lock_mutex_enter_kernel();

			count = 0;
		}

		lock = UT_LIST_GET_PREV(trx_locks, lock);
	}

unknown's avatar
unknown committed
3917
	ut_a(trx->n_lock_table_exp == 0);
3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928
}

/*************************************************************************
Cancels a waiting lock request and releases possible other transactions
waiting behind it. */

void
lock_cancel_waiting_and_release(
/*============================*/
	lock_t*	lock)	/* in: waiting lock request */
{
3929
#ifdef UNIV_SYNC_DEBUG
3930
	ut_ad(mutex_own(&kernel_mutex));
3931
#endif /* UNIV_SYNC_DEBUG */
3932 3933 3934 3935 3936

	if (lock_get_type(lock) == LOCK_REC) {
			
		lock_rec_dequeue_from_page(lock);
	} else {
3937
		ut_ad(lock_get_type(lock) & LOCK_TABLE);
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948

		lock_table_dequeue(lock);
	}

	/* Reset the wait flag and the back pointer to lock in trx */

	lock_reset_lock_and_trx_wait(lock);

	/* The following function releases the trx from lock wait */

	trx_end_lock_wait(lock->trx);
3949 3950
}

unknown's avatar
unknown committed
3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
/*************************************************************************
Resets all record and table locks of a transaction on a table to be dropped.
No lock is allowed to be a wait lock. */
static
void
lock_reset_all_on_table_for_trx(
/*============================*/
	dict_table_t*	table,	/* in: table to be dropped */
	trx_t*		trx)	/* in: a transaction */
{
	lock_t*	lock;
	lock_t*	prev_lock;

3964
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
3965
	ut_ad(mutex_own(&kernel_mutex));
3966
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977

	lock = UT_LIST_GET_LAST(trx->trx_locks);
	
	while (lock != NULL) {
		prev_lock = UT_LIST_GET_PREV(trx_locks, lock);
		
		if (lock_get_type(lock) == LOCK_REC
				&& lock->index->table == table) {
			ut_a(!lock_get_wait(lock));
			
			lock_rec_discard(lock);
3978
		} else if (lock_get_type(lock) & LOCK_TABLE
unknown's avatar
unknown committed
3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015
				&& lock->un_member.tab_lock.table == table) {

			ut_a(!lock_get_wait(lock));
			
			lock_table_remove_low(lock);
		}

		lock = prev_lock;
	}
}

/*************************************************************************
Resets all locks, both table and record locks, on a table to be dropped.
No lock is allowed to be a wait lock. */

void
lock_reset_all_on_table(
/*====================*/
	dict_table_t*	table)	/* in: table to be dropped */
{
	lock_t*	lock;

	mutex_enter(&kernel_mutex);

	lock = UT_LIST_GET_FIRST(table->locks);

	while (lock) {	
		ut_a(!lock_get_wait(lock));

		lock_reset_all_on_table_for_trx(table, lock->trx);

		lock = UT_LIST_GET_FIRST(table->locks);
	}

	mutex_exit(&kernel_mutex);
}

4016 4017 4018 4019 4020 4021 4022 4023
/*===================== VALIDATION AND DEBUGGING  ====================*/

/*************************************************************************
Prints info of a table lock. */

void
lock_table_print(
/*=============*/
4024
	FILE*	file,	/* in: file where to print */
4025 4026
	lock_t*	lock)	/* in: table type lock */
{
4027
#ifdef UNIV_SYNC_DEBUG
4028
	ut_ad(mutex_own(&kernel_mutex));
4029
#endif /* UNIV_SYNC_DEBUG */
4030 4031
	ut_a(lock_get_type(lock) == LOCK_TABLE ||
		lock_get_type(lock) == LOCK_TABLE_EXP);
4032

4033 4034 4035
	if (lock_get_type(lock) == LOCK_TABLE_EXP) {
		fputs("EXPLICIT ", file);
	}
4036
	fputs("TABLE LOCK table ", file);
4037
	ut_print_name(file, lock->trx, lock->un_member.tab_lock.table->name);
4038
	fprintf(file, " trx id %lu %lu",
4039
		(ulong) (lock->trx)->id.high, (ulong) (lock->trx)->id.low);
4040 4041

	if (lock_get_mode(lock) == LOCK_S) {
4042
		fputs(" lock mode S", file);
4043
	} else if (lock_get_mode(lock) == LOCK_X) {
4044
		fputs(" lock mode X", file);
4045
	} else if (lock_get_mode(lock) == LOCK_IS) {
4046
		fputs(" lock mode IS", file);
4047
	} else if (lock_get_mode(lock) == LOCK_IX) {
4048
		fputs(" lock mode IX", file);
4049
	} else if (lock_get_mode(lock) == LOCK_AUTO_INC) {
4050
		fputs(" lock mode AUTO-INC", file);
4051
	} else {
unknown's avatar
unknown committed
4052
		fprintf(file, " unknown lock mode %lu", (ulong) lock_get_mode(lock));
4053 4054 4055
	}

	if (lock_get_wait(lock)) {
4056
		fputs(" waiting", file);
4057 4058
	}

4059
	putc('\n', file);
4060 4061 4062 4063 4064 4065 4066 4067
}						
				
/*************************************************************************
Prints info of a record lock. */

void
lock_rec_print(
/*===========*/
4068
	FILE*	file,	/* in: file where to print */
4069 4070
	lock_t*	lock)	/* in: record type lock */
{
unknown's avatar
unknown committed
4071 4072 4073 4074 4075
	page_t*		page;
	ulint		space;
	ulint		page_no;
	ulint		i;
	mtr_t		mtr;
4076 4077 4078
	mem_heap_t*	heap		= NULL;
	ulint		offsets_[100]	= { 100, };
	ulint*		offsets		= offsets_;
4079

4080
#ifdef UNIV_SYNC_DEBUG
4081
	ut_ad(mutex_own(&kernel_mutex));
4082
#endif /* UNIV_SYNC_DEBUG */
4083 4084 4085 4086 4087
	ut_a(lock_get_type(lock) == LOCK_REC);

	space = lock->un_member.rec_lock.space;
 	page_no = lock->un_member.rec_lock.page_no;

4088
	fprintf(file, "RECORD LOCKS space id %lu page no %lu n bits %lu ",
4089 4090
		       (ulong) space, (ulong) page_no,
		       (ulong) lock_rec_get_n_bits(lock));
4091
	dict_index_name_print(file, lock->trx, lock->index);
4092
	fprintf(file, " trx id %lu %lu",
4093 4094
		       (ulong) (lock->trx)->id.high,
		       (ulong) (lock->trx)->id.low);
4095 4096

	if (lock_get_mode(lock) == LOCK_S) {
4097
		fputs(" lock mode S", file);
4098
	} else if (lock_get_mode(lock) == LOCK_X) {
4099
		fputs(" lock_mode X", file);
4100 4101 4102 4103 4104
	} else {
		ut_error;
	}

	if (lock_rec_get_gap(lock)) {
4105
		fputs(" locks gap before rec", file);
unknown's avatar
unknown committed
4106 4107 4108
	}

	if (lock_rec_get_rec_not_gap(lock)) {
4109
		fputs(" locks rec but not gap", file);
4110 4111
	}

unknown's avatar
unknown committed
4112
	if (lock_rec_get_insert_intention(lock)) {
4113
		fputs(" insert intention", file);
unknown's avatar
unknown committed
4114 4115
	}

4116
	if (lock_get_wait(lock)) {
4117
		fputs(" waiting", file);
4118 4119 4120 4121
	}

	mtr_start(&mtr);

4122
	putc('\n', file);
4123

4124 4125 4126 4127 4128 4129
	/* If the page is not in the buffer pool, we cannot load it
	because we have the kernel mutex and ibuf operations would
	break the latching order */
	
	page = buf_page_get_gen(space, page_no, RW_NO_LATCH,
					NULL, BUF_GET_IF_IN_POOL,
4130
					__FILE__, __LINE__, &mtr);
4131 4132
	if (page) {
		page = buf_page_get_nowait(space, page_no, RW_S_LATCH, &mtr);
unknown's avatar
unknown committed
4133 4134 4135 4136 4137 4138 4139 4140 4141

		if (!page) {
			/* Let us try to get an X-latch. If the current thread
			is holding an X-latch on the page, we cannot get an
			S-latch. */
			
			page = buf_page_get_nowait(space, page_no, RW_X_LATCH,
									&mtr);
		}
4142 4143 4144
	}
				
	if (page) {
4145
#ifdef UNIV_SYNC_DEBUG
4146
		buf_page_dbg_add_level(page, SYNC_NO_ORDER_CHECK);
4147
#endif /* UNIV_SYNC_DEBUG */
4148 4149 4150 4151 4152 4153
	}

	for (i = 0; i < lock_rec_get_n_bits(lock); i++) {

		if (lock_rec_get_nth_bit(lock, i)) {

unknown's avatar
unknown committed
4154
			fprintf(file, "Record lock, heap no %lu ", (ulong) i);
4155 4156

			if (page) {
unknown's avatar
unknown committed
4157 4158
				rec_t*	rec
					= page_find_rec_with_heap_no(page, i);
4159 4160 4161
				offsets = rec_get_offsets(rec, lock->index,
					offsets, ULINT_UNDEFINED, &heap);
				rec_print_new(file, rec, offsets);
4162 4163
			}

4164
			putc('\n', file);
4165
		}
4166
	}
unknown's avatar
unknown committed
4167

4168
	mtr_commit(&mtr);
4169 4170 4171 4172 4173
	if (heap) {
		mem_heap_free(heap);
	}
}

4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184
/*************************************************************************
Calculates the number of record lock structs in the record lock hash table. */
static
ulint
lock_get_n_rec_locks(void)
/*======================*/
{
	lock_t*	lock;
	ulint	n_locks	= 0;
	ulint	i;

4185
#ifdef UNIV_SYNC_DEBUG
4186
	ut_ad(mutex_own(&kernel_mutex));
4187
#endif /* UNIV_SYNC_DEBUG */
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206

	for (i = 0; i < hash_get_n_cells(lock_sys->rec_hash); i++) {

		lock = HASH_GET_FIRST(lock_sys->rec_hash, i);

		while (lock) {
			n_locks++;

			lock = HASH_GET_NEXT(hash, lock);
		}
	}

	return(n_locks);
}
	
/*************************************************************************
Prints info of locks for all transactions. */

void
unknown's avatar
unknown committed
4207 4208
lock_print_info(
/*============*/
4209
	FILE*	file)	/* in: file where to print */
4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220
{
	lock_t*	lock;
	trx_t*	trx;
	ulint	space;
	ulint	page_no;
	page_t*	page;
	ibool	load_page_first = TRUE;
	ulint	nth_trx		= 0;
	ulint	nth_lock	= 0;
	ulint	i;
	mtr_t	mtr;
4221

4222 4223 4224 4225 4226
	/* We must protect the MySQL thd->query field with a MySQL mutex, and
	because the MySQL mutex must be reserved before the kernel_mutex of
	InnoDB, we call innobase_mysql_prepare_print_arbitrary_thd() here. */

	innobase_mysql_prepare_print_arbitrary_thd();
unknown's avatar
Merge  
unknown committed
4227
	lock_mutex_enter_kernel();
4228

unknown's avatar
unknown committed
4229
	if (lock_deadlock_found) {
4230
		fputs(
unknown's avatar
unknown committed
4231 4232
"------------------------\n" 
"LATEST DETECTED DEADLOCK\n"
4233
"------------------------\n", file);
unknown's avatar
unknown committed
4234

4235
		ut_copy_file(file, lock_latest_err_file);
unknown's avatar
unknown committed
4236 4237
	}

4238
	fputs(
unknown's avatar
unknown committed
4239 4240
"------------\n" 
"TRANSACTIONS\n"
4241
"------------\n", file);
unknown's avatar
unknown committed
4242

4243
	fprintf(file, "Trx id counter %lu %lu\n",
4244 4245
		       (ulong) ut_dulint_get_high(trx_sys->max_trx_id),
		       (ulong) ut_dulint_get_low(trx_sys->max_trx_id));
unknown's avatar
unknown committed
4246

4247
	fprintf(file,
unknown's avatar
unknown committed
4248
	"Purge done for trx's n:o < %lu %lu undo n:o < %lu %lu\n",
4249 4250 4251 4252
		(ulong) ut_dulint_get_high(purge_sys->purge_trx_no),
		(ulong) ut_dulint_get_low(purge_sys->purge_trx_no),
		(ulong) ut_dulint_get_high(purge_sys->purge_undo_no),
		(ulong) ut_dulint_get_low(purge_sys->purge_undo_no));
unknown's avatar
unknown committed
4253

unknown's avatar
unknown committed
4254 4255 4256
	fprintf(file,
	"History list length %lu\n", (ulong) trx_sys->rseg_history_len);

4257
	fprintf(file,
unknown's avatar
unknown committed
4258
		"Total number of lock structs in row lock hash table %lu\n",
4259
					 (ulong) lock_get_n_rec_locks());
unknown's avatar
unknown committed
4260

4261
	fprintf(file, "LIST OF TRANSACTIONS FOR EACH SESSION:\n");
4262 4263 4264 4265 4266 4267 4268

	/* First print info on non-active transactions */

	trx = UT_LIST_GET_FIRST(trx_sys->mysql_trx_list);

	while (trx) {
		if (trx->conc_state == TRX_NOT_STARTED) {
4269 4270
			fputs("---", file);
			trx_print(file, trx);
4271 4272 4273 4274 4275
		}
			
		trx = UT_LIST_GET_NEXT(mysql_trx_list, trx);
	}

4276 4277 4278 4279
loop:
	trx = UT_LIST_GET_FIRST(trx_sys->trx_list);

	i = 0;
4280 4281 4282 4283 4284

	/* Since we temporarily release the kernel mutex when
	reading a database page in below, variable trx may be
	obsolete now and we must loop through the trx list to
	get probably the same trx, or some other trx. */
4285 4286 4287 4288 4289 4290 4291 4292
	
	while (trx && (i < nth_trx)) {
		trx = UT_LIST_GET_NEXT(trx_list, trx);
		i++;
	}

	if (trx == NULL) {
		lock_mutex_exit_kernel();
4293
		innobase_mysql_end_print_arbitrary_thd();
4294

4295
		ut_ad(lock_validate());
unknown's avatar
unknown committed
4296

unknown's avatar
unknown committed
4297 4298 4299
		return;
	}

4300
	if (nth_lock == 0) {
4301 4302
		fputs("---", file);
		trx_print(file, trx);
unknown's avatar
unknown committed
4303
		
4304
	        if (trx->read_view) {
4305
			fprintf(file,
4306
       "Trx read view will not see trx with id >= %lu %lu, sees < %lu %lu\n",
4307 4308 4309 4310
		      (ulong) ut_dulint_get_high(trx->read_view->low_limit_id),
       		      (ulong) ut_dulint_get_low(trx->read_view->low_limit_id),
       		      (ulong) ut_dulint_get_high(trx->read_view->up_limit_id),
       		      (ulong) ut_dulint_get_low(trx->read_view->up_limit_id));
4311 4312
	        }

4313
		if (trx->que_state == TRX_QUE_LOCK_WAIT) {
4314
			fprintf(file,
unknown's avatar
unknown committed
4315
 "------- TRX HAS BEEN WAITING %lu SEC FOR THIS LOCK TO BE GRANTED:\n",
4316
		   (ulong)difftime(time(NULL), trx->wait_started));
4317 4318

			if (lock_get_type(trx->wait_lock) == LOCK_REC) {
4319
				lock_rec_print(file, trx->wait_lock);
4320
			} else {
4321
				lock_table_print(file, trx->wait_lock);
4322 4323
			}

4324
			fputs("------------------\n", file);
4325
		}
4326 4327
	}

4328 4329 4330 4331 4332
	if (!srv_print_innodb_lock_monitor) {
	  	nth_trx++;
	  	goto loop;
	}

4333 4334
	i = 0;

4335 4336 4337
	/* Look at the note about the trx loop above why we loop here:
	lock may be an obsolete pointer now. */
	
4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
	lock = UT_LIST_GET_FIRST(trx->trx_locks);
		
	while (lock && (i < nth_lock)) {
		lock = UT_LIST_GET_NEXT(trx_locks, lock);
		i++;
	}

	if (lock == NULL) {
		nth_trx++;
		nth_lock = 0;

		goto loop;
	}

	if (lock_get_type(lock) == LOCK_REC) {
		space = lock->un_member.rec_lock.space;
 		page_no = lock->un_member.rec_lock.page_no;

 		if (load_page_first) {
			lock_mutex_exit_kernel();
4358
			innobase_mysql_end_print_arbitrary_thd();
4359 4360 4361 4362 4363 4364 4365 4366 4367

			mtr_start(&mtr);
			
			page = buf_page_get_with_no_latch(space, page_no, &mtr);

			mtr_commit(&mtr);

			load_page_first = FALSE;

4368
			innobase_mysql_prepare_print_arbitrary_thd();
4369 4370 4371 4372 4373
			lock_mutex_enter_kernel();

			goto loop;
		}
		
4374
		lock_rec_print(file, lock);
4375
	} else {
4376
		ut_ad(lock_get_type(lock) & LOCK_TABLE);
4377
	
4378
		lock_table_print(file, lock);
4379 4380 4381 4382 4383 4384
	}

	load_page_first = TRUE;

	nth_lock++;

4385
	if (nth_lock >= 10) {
4386 4387 4388
		fputs(
		"10 LOCKS PRINTED FOR THIS TRX: SUPPRESSING FURTHER PRINTS\n",
			file);
4389 4390 4391 4392 4393 4394 4395
	
		nth_trx++;
		nth_lock = 0;

		goto loop;
	}

4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410
	goto loop;
}

/*************************************************************************
Validates the lock queue on a table. */

ibool
lock_table_queue_validate(
/*======================*/
				/* out: TRUE if ok */
	dict_table_t*	table)	/* in: table */
{
	lock_t*	lock;
	ibool	is_waiting;

4411
#ifdef UNIV_SYNC_DEBUG
4412
	ut_ad(mutex_own(&kernel_mutex));
4413
#endif /* UNIV_SYNC_DEBUG */
4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448

	is_waiting = FALSE;

	lock = UT_LIST_GET_FIRST(table->locks);

	while (lock) {
		ut_a(((lock->trx)->conc_state == TRX_ACTIVE)
		     || ((lock->trx)->conc_state == TRX_COMMITTED_IN_MEMORY));
	
		if (!lock_get_wait(lock)) {

			ut_a(!is_waiting);
		
			ut_a(!lock_table_other_has_incompatible(lock->trx, 0,
						table, lock_get_mode(lock)));
		} else {
			is_waiting = TRUE;

			ut_a(lock_table_has_to_wait_in_queue(lock));
		}

		lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
	}

	return(TRUE);
}

/*************************************************************************
Validates the lock queue on a single record. */

ibool
lock_rec_queue_validate(
/*====================*/
				/* out: TRUE if ok */
	rec_t*		rec,	/* in: record to look at */
unknown's avatar
unknown committed
4449 4450
	dict_index_t*	index,	/* in: index, or NULL if not known */
	const ulint*	offsets)/* in: rec_get_offsets(rec, index) */
4451 4452 4453
{
	trx_t*	impl_trx;	
	lock_t*	lock;
unknown's avatar
unknown committed
4454 4455
	ibool	comp;

4456
	ut_a(rec);
unknown's avatar
unknown committed
4457 4458
	ut_ad(rec_offs_validate(rec, index, offsets));
	comp = page_is_comp(buf_frame_align(rec));
4459 4460 4461 4462 4463 4464 4465 4466 4467 4468

	lock_mutex_enter_kernel();

	if (page_rec_is_supremum(rec) || page_rec_is_infimum(rec)) {

		lock = lock_rec_get_first(rec);

		while (lock) {
			ut_a(lock->trx->conc_state == TRX_ACTIVE
		     	     || lock->trx->conc_state
unknown's avatar
unknown committed
4469
						== TRX_COMMITTED_IN_MEMORY);
4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480
	
			ut_a(trx_in_trx_list(lock->trx));
			
			if (lock_get_wait(lock)) {
				ut_a(lock_rec_has_to_wait_in_queue(lock));
			}

			if (index) {
				ut_a(lock->index == index);
			}

unknown's avatar
unknown committed
4481
			lock = lock_rec_get_next(rec, comp, lock);
4482 4483 4484 4485 4486 4487 4488
		}

		lock_mutex_exit_kernel();

	    	return(TRUE);
	}

unknown's avatar
unknown committed
4489
	if (index && (index->type & DICT_CLUSTERED)) {
4490
	
unknown's avatar
unknown committed
4491
		impl_trx = lock_clust_rec_some_has_impl(rec, index, offsets);
4492 4493

		if (impl_trx && lock_rec_other_has_expl_req(LOCK_S, 0,
unknown's avatar
unknown committed
4494
				LOCK_WAIT, rec, comp, impl_trx)) {
4495

unknown's avatar
unknown committed
4496
			ut_a(lock_rec_has_expl(LOCK_X | LOCK_REC_NOT_GAP, rec,
unknown's avatar
unknown committed
4497
							comp, impl_trx));
4498 4499 4500 4501 4502 4503 4504 4505 4506
		}
	}

	if (index && !(index->type & DICT_CLUSTERED)) {
		
		/* The kernel mutex may get released temporarily in the
		next function call: we have to release lock table mutex
		to obey the latching order */
		
unknown's avatar
unknown committed
4507 4508
		impl_trx = lock_sec_rec_some_has_impl_off_kernel(
				rec, index, offsets);
4509 4510

		if (impl_trx && lock_rec_other_has_expl_req(LOCK_S, 0,
unknown's avatar
unknown committed
4511
				LOCK_WAIT, rec, comp, impl_trx)) {
4512

unknown's avatar
unknown committed
4513 4514
			ut_a(lock_rec_has_expl(LOCK_X | LOCK_REC_NOT_GAP,
						rec, comp, impl_trx));
4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532
		}
	}

	lock = lock_rec_get_first(rec);

	while (lock) {
		ut_a(lock->trx->conc_state == TRX_ACTIVE
		     || lock->trx->conc_state == TRX_COMMITTED_IN_MEMORY);
		ut_a(trx_in_trx_list(lock->trx));
	
		if (index) {
			ut_a(lock->index == index);
		}

		if (!lock_rec_get_gap(lock) && !lock_get_wait(lock)) {
		
			if (lock_get_mode(lock) == LOCK_S) {
				ut_a(!lock_rec_other_has_expl_req(LOCK_X,
unknown's avatar
unknown committed
4533
						0, 0, rec, comp, lock->trx));
4534 4535
			} else {
				ut_a(!lock_rec_other_has_expl_req(LOCK_S,
unknown's avatar
unknown committed
4536
						0, 0, rec, comp, lock->trx));
4537 4538 4539 4540 4541 4542 4543
			}

		} else if (lock_get_wait(lock) && !lock_rec_get_gap(lock)) {

			ut_a(lock_rec_has_to_wait_in_queue(lock));
		}

unknown's avatar
unknown committed
4544
		lock = lock_rec_get_next(rec, comp, lock);
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565
	}

	lock_mutex_exit_kernel();

	return(TRUE);
}

/*************************************************************************
Validates the record lock queues on a page. */

ibool
lock_rec_validate_page(
/*===================*/
			/* out: TRUE if ok */
	ulint	space,	/* in: space id */
	ulint	page_no)/* in: page number */
{
	dict_index_t*	index;
	page_t*	page;
	lock_t*	lock;
	rec_t*	rec;
4566 4567
	ulint	nth_lock		= 0;
	ulint	nth_bit			= 0;
4568 4569
	ulint	i;
	mtr_t	mtr;
4570 4571 4572
	mem_heap_t*	heap		= NULL;
	ulint		offsets_[100]	= { 100, };
	ulint*		offsets		= offsets_;
4573

4574
#ifdef UNIV_SYNC_DEBUG
4575
	ut_ad(!mutex_own(&kernel_mutex));
4576
#endif /* UNIV_SYNC_DEBUG */
4577 4578 4579 4580

	mtr_start(&mtr);
	
	page = buf_page_get(space, page_no, RW_X_LATCH, &mtr);
4581
#ifdef UNIV_SYNC_DEBUG
4582
	buf_page_dbg_add_level(page, SYNC_NO_ORDER_CHECK);
4583
#endif /* UNIV_SYNC_DEBUG */
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602

	lock_mutex_enter_kernel();
loop:	
	lock = lock_rec_get_first_on_page_addr(space, page_no);

	if (!lock) {
		goto function_exit;
	}

	for (i = 0; i < nth_lock; i++) {

		lock = lock_rec_get_next_on_page(lock);

		if (!lock) {
			goto function_exit;
		}
	}

	ut_a(trx_in_trx_list(lock->trx));
unknown's avatar
unknown committed
4603 4604
	ut_a(lock->trx->conc_state == TRX_ACTIVE
		     || lock->trx->conc_state == TRX_COMMITTED_IN_MEMORY);
4605 4606 4607
	
	for (i = nth_bit; i < lock_rec_get_n_bits(lock); i++) {

unknown's avatar
unknown committed
4608
		if (i == 1 || lock_rec_get_nth_bit(lock, i)) {
4609 4610 4611

			index = lock->index;
			rec = page_find_rec_with_heap_no(page, i);
4612 4613
			offsets = rec_get_offsets(rec, index, offsets,
						ULINT_UNDEFINED, &heap);
4614

4615
			fprintf(stderr,
unknown's avatar
unknown committed
4616
				"Validating %lu %lu\n", (ulong) space, (ulong) page_no);
4617 4618 4619

			lock_mutex_exit_kernel();

unknown's avatar
unknown committed
4620
			lock_rec_queue_validate(rec, index, offsets);
4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639

			lock_mutex_enter_kernel();

			nth_bit = i + 1;

			goto loop;
		}
	}

	nth_bit = 0;
	nth_lock++;

	goto loop;

function_exit:
	lock_mutex_exit_kernel();

	mtr_commit(&mtr);

4640 4641 4642
	if (heap) {
		mem_heap_free(heap);
	}
4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668
	return(TRUE);
}						
				
/*************************************************************************
Validates the lock system. */

ibool
lock_validate(void)
/*===============*/
			/* out: TRUE if ok */
{
	lock_t*	lock;
	trx_t*	trx;
	dulint	limit;
	ulint	space;
	ulint	page_no;
	ulint	i;

	lock_mutex_enter_kernel();
	
	trx = UT_LIST_GET_FIRST(trx_sys->trx_list);

	while (trx) {
		lock = UT_LIST_GET_FIRST(trx->trx_locks);
		
		while (lock) {
4669
			if (lock_get_type(lock) & LOCK_TABLE) {
4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
	
				lock_table_queue_validate(
					lock->un_member.tab_lock.table);
			}
	
			lock = UT_LIST_GET_NEXT(trx_locks, lock);
		}
	
		trx = UT_LIST_GET_NEXT(trx_list, trx);
	}

	for (i = 0; i < hash_get_n_cells(lock_sys->rec_hash); i++) {

		limit = ut_dulint_zero;

		for (;;) {
			lock = HASH_GET_FIRST(lock_sys->rec_hash, i);

			while (lock) {
				ut_a(trx_in_trx_list(lock->trx));

				space = lock->un_member.rec_lock.space;
				page_no = lock->un_member.rec_lock.page_no;
		
				if (ut_dulint_cmp(
					ut_dulint_create(space, page_no),
							limit) >= 0) {
					break;
				}

				lock = HASH_GET_NEXT(hash, lock);
			}

			if (!lock) {

				break;
			}
			
			lock_mutex_exit_kernel();

			lock_rec_validate_page(space, page_no);

			lock_mutex_enter_kernel();

			limit = ut_dulint_create(space, page_no + 1);
		}
	}

	lock_mutex_exit_kernel();

	return(TRUE);
}

/*============ RECORD LOCK CHECKS FOR ROW OPERATIONS ====================*/

/*************************************************************************
Checks if locks of other transactions prevent an immediate insert of
a record. If they do, first tests if the query thread should anyway
be suspended for some reason; if not, then puts the transaction and
the query thread to the lock wait state and inserts a waiting request
for a gap x-lock to the lock queue. */

ulint
lock_rec_insert_check_and_lock(
/*===========================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
	ulint		flags,	/* in: if BTR_NO_LOCKING_FLAG bit is set,
				does nothing */
	rec_t*		rec,	/* in: record after which to insert */
	dict_index_t*	index,	/* in: index */
	que_thr_t*	thr,	/* in: query thread */
	ibool*		inherit)/* out: set to TRUE if the new inserted
				record maybe should inherit LOCK_GAP type
				locks from the successor record */
{
	rec_t*	next_rec;
	trx_t*	trx;
	lock_t*	lock;
	ulint	err;

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	ut_ad(rec);

	trx = thr_get_trx(thr);
	next_rec = page_rec_get_next(rec);

	*inherit = FALSE;

	lock_mutex_enter_kernel();

	ut_ad(lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));

	lock = lock_rec_get_first(next_rec);

	if (lock == NULL) {
		/* We optimize CPU time usage in the simplest case */

		lock_mutex_exit_kernel();

		if (!(index->type & DICT_CLUSTERED)) {

			/* Update the page max trx id field */
			page_update_max_trx_id(buf_frame_align(rec),
							thr_get_trx(thr)->id);
		}
		
		return(DB_SUCCESS);
	}

	*inherit = TRUE;

unknown's avatar
unknown committed
4786 4787
	/* If another transaction has an explicit lock request which locks
	the gap, waiting or granted, on the successor, the insert has to wait.
unknown's avatar
unknown committed
4788 4789 4790 4791 4792 4793 4794 4795

	An exception is the case where the lock by the another transaction
	is a gap type lock which it placed to wait for its turn to insert. We
	do not consider that kind of a lock conflicting with our insert. This
	eliminates an unnecessary deadlock which resulted when 2 transactions
	had to wait for their insert. Both had waiting gap type lock requests
	on the successor, which produced an unnecessary deadlock. */

unknown's avatar
unknown committed
4796 4797 4798 4799
	if (lock_rec_other_has_conflicting(LOCK_X | LOCK_GAP
				| LOCK_INSERT_INTENTION, next_rec, trx)) {

		/* Note that we may get DB_SUCCESS also here! */
unknown's avatar
unknown committed
4800
		err = lock_rec_enqueue_waiting(LOCK_X | LOCK_GAP
unknown's avatar
unknown committed
4801 4802
						| LOCK_INSERT_INTENTION,
						next_rec, index, thr);
4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814
	} else {
		err = DB_SUCCESS;
	}

	lock_mutex_exit_kernel();

	if (!(index->type & DICT_CLUSTERED) && (err == DB_SUCCESS)) {

		/* Update the page max trx id field */
		page_update_max_trx_id(buf_frame_align(rec),
							thr_get_trx(thr)->id);
	}
unknown's avatar
unknown committed
4815 4816 4817

#ifdef UNIV_DEBUG
	{
4818 4819 4820 4821 4822
		mem_heap_t*	heap		= NULL;
		ulint		offsets_[100]	= { 100, };
		const ulint*	offsets		= rec_get_offsets(
						next_rec, index, offsets_,
						ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
4823
		ut_ad(lock_rec_queue_validate(next_rec, index, offsets));
4824 4825 4826
		if (heap) {
			mem_heap_free(heap);
		}
unknown's avatar
unknown committed
4827 4828
	}
#endif /* UNIV_DEBUG */
4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841

	return(err);
}

/*************************************************************************
If a transaction has an implicit x-lock on a record, but no explicit x-lock
set on the record, sets one for it. NOTE that in the case of a secondary
index, the kernel mutex may get temporarily released. */
static
void
lock_rec_convert_impl_to_expl(
/*==========================*/
	rec_t*		rec,	/* in: user record on page */
unknown's avatar
unknown committed
4842 4843
	dict_index_t*	index,	/* in: index of record */
	const ulint*	offsets)/* in: rec_get_offsets(rec, index) */
4844 4845 4846
{
	trx_t*	impl_trx;

4847
#ifdef UNIV_SYNC_DEBUG
4848
	ut_ad(mutex_own(&kernel_mutex));
4849
#endif /* UNIV_SYNC_DEBUG */
4850
	ut_ad(page_rec_is_user_rec(rec));
unknown's avatar
unknown committed
4851 4852
	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(page_is_comp(buf_frame_align(rec)) == index->table->comp);
4853 4854

	if (index->type & DICT_CLUSTERED) {
unknown's avatar
unknown committed
4855
		impl_trx = lock_clust_rec_some_has_impl(rec, index, offsets);
4856
	} else {
unknown's avatar
unknown committed
4857 4858
		impl_trx = lock_sec_rec_some_has_impl_off_kernel(
							rec, index, offsets);
4859 4860 4861 4862 4863 4864
	}

	if (impl_trx) {
		/* If the transaction has no explicit x-lock set on the
		record, set one for it */

unknown's avatar
unknown committed
4865
		if (!lock_rec_has_expl(LOCK_X | LOCK_REC_NOT_GAP, rec,
unknown's avatar
unknown committed
4866
					index->table->comp, impl_trx)) {
4867

unknown's avatar
unknown committed
4868 4869
			lock_rec_add_to_queue(LOCK_REC | LOCK_X
					      | LOCK_REC_NOT_GAP, rec, index,
4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891
								impl_trx);
		}
	}
}

/*************************************************************************
Checks if locks of other transactions prevent an immediate modify (update,
delete mark, or delete unmark) of a clustered index record. If they do,
first tests if the query thread should anyway be suspended for some
reason; if not, then puts the transaction and the query thread to the
lock wait state and inserts a waiting request for a record x-lock to the
lock queue. */

ulint
lock_clust_rec_modify_check_and_lock(
/*=================================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
	ulint		flags,	/* in: if BTR_NO_LOCKING_FLAG bit is set,
				does nothing */
	rec_t*		rec,	/* in: record which should be modified */
	dict_index_t*	index,	/* in: clustered index */
unknown's avatar
unknown committed
4892
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
4893 4894 4895
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;
unknown's avatar
unknown committed
4896 4897 4898 4899

	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(index->type & DICT_CLUSTERED);

4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911
	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	lock_mutex_enter_kernel();

	ut_ad(lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));

	/* If a transaction has no explicit x-lock set on the record, set one
	for it */

unknown's avatar
unknown committed
4912
	lock_rec_convert_impl_to_expl(rec, index, offsets);
4913

unknown's avatar
unknown committed
4914
	err = lock_rec_lock(TRUE, LOCK_X | LOCK_REC_NOT_GAP, rec, index, thr);
4915 4916 4917

	lock_mutex_exit_kernel();

unknown's avatar
unknown committed
4918
	ut_ad(lock_rec_queue_validate(rec, index, offsets));
4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958

	return(err);
}

/*************************************************************************
Checks if locks of other transactions prevent an immediate modify (delete
mark or delete unmark) of a secondary index record. */

ulint
lock_sec_rec_modify_check_and_lock(
/*===============================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
	ulint		flags,	/* in: if BTR_NO_LOCKING_FLAG bit is set,
				does nothing */
	rec_t*		rec,	/* in: record which should be modified;
				NOTE: as this is a secondary index, we
				always have to modify the clustered index
				record first: see the comment below */
	dict_index_t*	index,	/* in: secondary index */
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;
	
	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	ut_ad(!(index->type & DICT_CLUSTERED));

	/* Another transaction cannot have an implicit lock on the record,
	because when we come here, we already have modified the clustered
	index record, and this would not have been possible if another active
	transaction had modified this secondary index record. */

	lock_mutex_enter_kernel();

	ut_ad(lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));

unknown's avatar
unknown committed
4959
	err = lock_rec_lock(TRUE, LOCK_X | LOCK_REC_NOT_GAP, rec, index, thr);
4960 4961

	lock_mutex_exit_kernel();
unknown's avatar
unknown committed
4962 4963 4964

#ifdef UNIV_DEBUG
	{
4965 4966 4967 4968
		mem_heap_t*	heap		= NULL;
		ulint		offsets_[100]	= { 100, };
		const ulint*	offsets		= rec_get_offsets(
			rec, index, offsets_, ULINT_UNDEFINED, &heap);
unknown's avatar
unknown committed
4969
		ut_ad(lock_rec_queue_validate(rec, index, offsets));
4970 4971 4972
		if (heap) {
			mem_heap_free(heap);
		}
unknown's avatar
unknown committed
4973 4974
	}
#endif /* UNIV_DEBUG */
4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000

	if (err == DB_SUCCESS) {
		/* Update the page max trx id field */

		page_update_max_trx_id(buf_frame_align(rec),
							thr_get_trx(thr)->id);
	}

	return(err);
}

/*************************************************************************
Like the counterpart for a clustered index below, but now we read a
secondary index record. */

ulint
lock_sec_rec_read_check_and_lock(
/*=============================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
	ulint		flags,	/* in: if BTR_NO_LOCKING_FLAG bit is set,
				does nothing */
	rec_t*		rec,	/* in: user record or page supremum record
				which should be read or passed over by a read
				cursor */
	dict_index_t*	index,	/* in: secondary index */
unknown's avatar
unknown committed
5001
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
5002 5003 5004
	ulint		mode,	/* in: mode of the lock which the read cursor
				should set on records: LOCK_S or LOCK_X; the
				latter is possible in SELECT FOR UPDATE */
unknown's avatar
unknown committed
5005 5006
	ulint		gap_mode,/* in: LOCK_ORDINARY, LOCK_GAP, or
				LOCK_REC_NOT_GAP */
5007 5008 5009 5010 5011 5012
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;

	ut_ad(!(index->type & DICT_CLUSTERED));
	ut_ad(page_rec_is_user_rec(rec) || page_rec_is_supremum(rec));
unknown's avatar
unknown committed
5013
	ut_ad(rec_offs_validate(rec, index, offsets));
5014 5015 5016 5017 5018 5019 5020 5021

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	lock_mutex_enter_kernel();

unknown's avatar
unknown committed
5022
	ut_ad(mode != LOCK_X
5023
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
unknown's avatar
unknown committed
5024
	ut_ad(mode != LOCK_S
5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));

	/* Some transaction may have an implicit x-lock on the record only
	if the max trx id for the page >= min trx id for the trx list or a
	database recovery is running. */

	if (((ut_dulint_cmp(page_get_max_trx_id(buf_frame_align(rec)),
					trx_list_get_min_trx_id()) >= 0)
	     		|| recv_recovery_is_on())
	     && !page_rec_is_supremum(rec)) {

unknown's avatar
unknown committed
5036
 		lock_rec_convert_impl_to_expl(rec, index, offsets);
5037 5038
	}

unknown's avatar
unknown committed
5039
	err = lock_rec_lock(FALSE, mode | gap_mode, rec, index, thr);
5040 5041 5042

	lock_mutex_exit_kernel();

unknown's avatar
unknown committed
5043
	ut_ad(lock_rec_queue_validate(rec, index, offsets));
5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066

	return(err);
}

/*************************************************************************
Checks if locks of other transactions prevent an immediate read, or passing
over by a read cursor, of a clustered index record. If they do, first tests
if the query thread should anyway be suspended for some reason; if not, then
puts the transaction and the query thread to the lock wait state and inserts a
waiting request for a record lock to the lock queue. Sets the requested mode
lock on the record. */

ulint
lock_clust_rec_read_check_and_lock(
/*===============================*/
				/* out: DB_SUCCESS, DB_LOCK_WAIT,
				DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
	ulint		flags,	/* in: if BTR_NO_LOCKING_FLAG bit is set,
				does nothing */
	rec_t*		rec,	/* in: user record or page supremum record
				which should be read or passed over by a read
				cursor */
	dict_index_t*	index,	/* in: clustered index */
unknown's avatar
unknown committed
5067
	const ulint*	offsets,/* in: rec_get_offsets(rec, index) */
5068 5069 5070
	ulint		mode,	/* in: mode of the lock which the read cursor
				should set on records: LOCK_S or LOCK_X; the
				latter is possible in SELECT FOR UPDATE */
unknown's avatar
unknown committed
5071 5072
	ulint		gap_mode,/* in: LOCK_ORDINARY, LOCK_GAP, or
				LOCK_REC_NOT_GAP */
5073 5074 5075 5076 5077 5078
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;

	ut_ad(index->type & DICT_CLUSTERED);
	ut_ad(page_rec_is_user_rec(rec) || page_rec_is_supremum(rec));
unknown's avatar
unknown committed
5079 5080
	ut_ad(gap_mode == LOCK_ORDINARY || gap_mode == LOCK_GAP
					|| gap_mode == LOCK_REC_NOT_GAP);
unknown's avatar
unknown committed
5081 5082
	ut_ad(rec_offs_validate(rec, index, offsets));

5083 5084 5085 5086 5087 5088 5089
	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	lock_mutex_enter_kernel();

unknown's avatar
unknown committed
5090
	ut_ad(mode != LOCK_X
5091
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
unknown's avatar
unknown committed
5092
	ut_ad(mode != LOCK_S
5093 5094 5095 5096
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
	
	if (!page_rec_is_supremum(rec)) {
	      
unknown's avatar
unknown committed
5097
		lock_rec_convert_impl_to_expl(rec, index, offsets);
5098 5099
	}

unknown's avatar
unknown committed
5100
	err = lock_rec_lock(FALSE, mode | gap_mode, rec, index, thr);
5101 5102 5103

	lock_mutex_exit_kernel();

unknown's avatar
unknown committed
5104 5105
	ut_ad(lock_rec_queue_validate(rec, index, offsets));

5106 5107
	return(err);
}