MySQL source code: how to process read/write locks

Source: Internet
Author: User
Recently, I encountered a problem where an online machine waited for the semaphore for a long time. The mysql monitoring thread thought that mysqld had been hang, so it committed suicide and restarted. Here involves 1

Recently, I encountered a problem where an online machine waited for the semaphore for a long time. The mysql monitoring thread thought that mysqld had been hang, so it committed suicide and restarted. Here involves 1

Recently, I encountered a problem where an online machine waited for the semaphore for a long time. The mysql monitoring thread thought that mysqld had been hang at this time, so it committed suicide and restarted. This involves an interesting question, that is, how mysql handles read/write locks.

It consists of three parts:

1. Lock Creation

2. Lock

3. Unlock

4. Monitoring lock

The following content is analyzed based on Percona5.5.18

1. Create a lock

The creation of the lock is actually to initialize a RW struct (rw_lock_t). The actual call function is as follows:

[Cpp]

  • # Define rw_lock_create (K, L, level )\
  • Rw_lock_create_func (L), # L)
  • There are three parameters in rw_lock_create. In actual scenarios, only 2nd parameters are used.

    K indicates mysql_pfs_key_t, and level indicates the current operation type (at least it looks like yes, in the file sync0sync. h). It seems that k is prepared for performance schema, and k represents the level of the current operation.

    For example, create a read/write lock for the purge thread:

    [Html]

  • Rw_lock_create (trx_purge_latch_key,
  • Let's go to rw_lock_create_func to see how it was created.

    We can see that the logic of this function is actually very simple:

    Lock-> lock_word = X_LOCK_DECR; // key field

    Used to limit the maximum number of concurrent read/write locks. The comments in the Code are as follows:

    [Cpp]

    When trying to lock, rw_lock_lock_word_decr will be called to reduce lock_word

    After initializing a series of variables, execute:

    [Cpp]

  • Lock-> event = OS _event_create (NULL );
  • Lock-> wait_ex_event = OS _event_create (NULL );
  • OS _event_create is used to create a system signal. The mutex is actually created (OS _fast_mutex_init (& (event-> OS _mutex )); and condition variables (OS _cond_init (& (event-> cond_var ));)

    Add the lock to the global linked list rw_lock_list.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.