MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lock.cc File Reference
#include "sql_priv.h"
#include "debug_sync.h"
#include "unireg.h"
#include "lock.h"
#include "sql_base.h"
#include "sql_parse.h"
#include "sql_acl.h"
#include <hash.h>
#include <assert.h>
Include dependency graph for lock.cc:

Go to the source code of this file.

Macros

#define GET_LOCK_UNLOCK   1
#define GET_LOCK_STORE_LOCKS   2

Functions

MYSQL_LOCKmysql_lock_tables (THD *thd, TABLE **tables, uint count, uint flags)
void mysql_unlock_tables (THD *thd, MYSQL_LOCK *sql_lock)
void mysql_unlock_some_tables (THD *thd, TABLE **table, uint count)
void mysql_unlock_read_tables (THD *thd, MYSQL_LOCK *sql_lock)
void mysql_lock_remove (THD *thd, MYSQL_LOCK *locked, TABLE *table)
void mysql_lock_abort (THD *thd, TABLE *table, bool upgrade_lock)
bool mysql_lock_abort_for_thread (THD *thd, TABLE *table)
MYSQL_LOCKmysql_lock_merge (MYSQL_LOCK *a, MYSQL_LOCK *b)
bool lock_schema_name (THD *thd, const char *db)
bool lock_object_name (THD *thd, MDL_key::enum_mdl_namespace mdl_type, const char *db, const char *name)

Variables

HASH open_cache

Detailed Description

Locking functions for mysql.

Because of the new concurrent inserts, we must first get external locks before getting internal locks. If we do it in the other order, the status information is not up to date when called from the lock handler.

GENERAL DESCRIPTION OF LOCKING

When not using LOCK TABLES:

  • For each SQL statement mysql_lock_tables() is called for all involved tables.
    • mysql_lock_tables() will call table_handler->external_lock(thd,locktype) for each table. This is followed by a call to thr_multi_lock() for all tables.
  • When statement is done, we call mysql_unlock_tables(). This will call thr_multi_unlock() followed by table_handler->external_lock(thd, F_UNLCK) for each table.
  • Note that mysql_unlock_tables() may be called several times as MySQL in some cases can free some tables earlier than others.
  • The above is true both for normal and temporary tables.
  • Temporary non transactional tables are never passed to thr_multi_lock() and we never call external_lock(thd, F_UNLOCK) on these.

When using LOCK TABLES:

  • For each statement, we will call table_handler->start_stmt(THD) to inform the table handler that we are using the table.

    The tables used can only be tables used in LOCK TABLES or a temporary table.

  • When statement is done, we will call ha_commit_stmt(thd);
  • When calling UNLOCK TABLES we call mysql_unlock_tables() for all tables used in LOCK TABLES

If table_handler->external_lock(thd, locktype) fails, we call table_handler->external_lock(thd, F_UNLCK) for each table that was locked, excluding one that caused failure. That means handler must cleanup itself in case external_lock() fails.

Todo:
Change to use my_malloc() ONLY when using LOCK TABLES command or when we are forced to use mysql_lock_merge.

Definition in file lock.cc.