mysql保存锁的两个双向链表

783阅读 0评论2012-12-16 gladness
分类:Mysql/postgreSQL

mysql的表(dict_table_struct)有一个双向链表
UT_LIST_BASE_NODE_T(lock_t)
locks; /*!< list of locks on the table */

事务(trx_struct)也有一个双向链表
UT_LIST_BASE_NODE_T(lock_t)
trx_locks; /*!< locks reserved by the transaction */

创建一个表锁时,会在这两个双向链表里都加入新节点,即新创建的这个锁。

/*========================= 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.

@return own: new lock object */

UNIV_INLINE

lock_t*

lock_table_create(

/*==============*/

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);

ut_ad(mutex_own(&kernel_mutex));


if ((type_mode & LOCK_MODE_MASK) == LOCK_AUTO_INC) {

++table->n_waiting_or_granted_auto_inc_locks;

}


/* For AUTOINC locking we reuse the lock instance only if

there is no wait involved else we allocate the waiting lock

from the transaction lock heap. */

if (type_mode == LOCK_AUTO_INC) {


lock = table->autoinc_lock;


table->autoinc_trx = trx;


ib_vector_push(trx->autoinc_locks, lock);

} else {

lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t));

}


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


lock->type_mode = type_mode | LOCK_TABLE;

lock->trx = trx;


lock->un_member.tab_lock.table = table;


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


if (UNIV_UNLIKELY(type_mode & LOCK_WAIT)) {


lock_set_lock_and_trx_wait(lock, trx);

}


return(lock);

}

上一篇:写mysql技术文章或者ppt时以漂亮格式记录案例的方法
下一篇:row_ins_index_entry_low函数的注释