Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151078
  • 博文数量: 12
  • 博客积分: 226
  • 博客等级: 二等列兵
  • 技术积分: 221
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-10 23:15
文章分类

全部博文(12)

分类: Mysql/postgreSQL

2013-03-05 17:01:54


trx_struct中和锁相关的成员:

  1. lock_t* wait_lock;
  2. /*!< if trx execution state is TRX_QUE_LOCK_WAIT, this points to
  3. the lock request, otherwise this is NULL */

  4. UT_LIST_BASE_NODE_T(lock_t) trx_locks;
  5. /*!< locks reserved by the transaction */

wait_lock是一个node,即一个trx最多只会有一个wait lock,锁等待时执行trx的线程会suspend
trx_locks是一个链表,记录事务执行过程中已经获得的锁

lock_t的原型是lock_struct,表锁(lock_table_struct)和记录锁(lock_rec_struct)共享这个数据结构

  1. /** Lock struct */
  2. struct lock_struct {
  3.      trx_t* trx;                           /*!< transaction owning the lock */
  4.      
  5.      UT_LIST_NODE_T(lock_t) trx_locks;     /*!< list of the locks of the transaction */

  6.      ulint type_mode;
  7.      /*!< lock type, mode, LOCK_GAP or LOCK_REC_NOT_GAP,LOCK_INSERT_INTENTION, wait flag, ORed */

  8.      hash_node_t   hash;  /*!< hash chain node for a record lock */
  9.      dict_index_t* index; /*!< index for a record lock */
  10.      union {
  11.           lock_table_t tab_lock; /*!< table lock */
  12.           lock_rec_t   rec_lock; /*!< record lock */
  13.      } un_member;                /*!< lock details */
  14. };

Innodb通过一个hash table维护所有的锁,数据结构lock_sys_struct

  1. /** The lock system struct */
  2. struct lock_sys_struct{
  3.     hash_table_t*    rec_hash;    /*!< hash table of the record locks */
  4.     ulint            rec_num;
  5. };
lock_struct中的hash(void*)类似一个next指针,将拉链法实现的hash table中位于同一个桶的所有元素链起来

关于锁的类型,兼容矩阵将在以后写文详细描述

下图是Innodb加锁/解锁流程,以select和update为例介绍,重点在锁等待/释放与线程状态之间的关系:

阅读(3178) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~