940
923
*****************************************************************************/
943
Lock and wait for the named lock.
945
@param thd Thread handler
946
@param table_list Lock first table in this list
950
Works together with global read lock.
958
int lock_and_wait_for_table_name(THD *thd, TABLE_LIST *table_list)
962
DBUG_ENTER("lock_and_wait_for_table_name");
964
if (wait_if_global_read_lock(thd, 0, 1))
966
VOID(pthread_mutex_lock(&LOCK_open));
967
if ((lock_retcode = lock_table_name(thd, table_list, TRUE)) < 0)
969
if (lock_retcode && wait_for_locked_table_names(thd, table_list))
971
unlock_table_name(thd, table_list);
977
pthread_mutex_unlock(&LOCK_open);
978
start_waiting_global_read_lock(thd);
984
Put a not open table with an old refresh version in the table cache.
986
@param thd Thread handler
987
@param table_list Lock first table in this list
988
@param check_in_use Do we need to check if table already in use by us
991
One must have a lock on LOCK_open!
994
If you are going to update the table, you should use
995
lock_and_wait_for_table_name instead of this function as this works
996
together with 'FLUSH TABLES WITH READ LOCK'
999
This will force any other threads that uses the table to release it
1000
as soon as possible.
1007
> 0 table locked, but someone is using it
1010
int lock_table_name(THD *thd, TABLE_LIST *table_list, bool check_in_use)
1013
char key[MAX_DBKEY_LENGTH];
1014
char *db= table_list->db;
1016
bool found_locked_table= FALSE;
1017
HASH_SEARCH_STATE state;
1018
DBUG_ENTER("lock_table_name");
1019
DBUG_PRINT("enter",("db: %s name: %s", db, table_list->table_name));
1021
key_length= create_table_def_key(thd, key, table_list, 0);
1025
/* Only insert the table if we haven't insert it already */
1026
for (table=(TABLE*) hash_first(&open_cache, (uchar*)key,
1027
key_length, &state);
1029
table = (TABLE*) hash_next(&open_cache,(uchar*) key,
1030
key_length, &state))
1032
if (table->reginfo.lock_type < TL_WRITE)
1034
if (table->in_use == thd)
1035
found_locked_table= TRUE;
1039
if (table->in_use == thd)
1041
DBUG_PRINT("info", ("Table is in use"));
1042
table->s->version= 0; // Ensure no one can use this
1043
table->locked_by_name= 1;
1049
if (thd->locked_tables && thd->locked_tables->table_count &&
1050
! find_temporary_table(thd, table_list->db, table_list->table_name))
1052
if (found_locked_table)
1053
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_list->alias);
1055
my_error(ER_TABLE_NOT_LOCKED, MYF(0), table_list->alias);
1060
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
1063
table_list->table=table;
1065
/* Return 1 if table is in use */
1066
DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
1067
check_in_use ? RTFC_NO_FLAG : RTFC_WAIT_OTHER_THREAD_FLAG)));
1071
void unlock_table_name(THD *thd, TABLE_LIST *table_list)
1073
if (table_list->table)
1075
hash_delete(&open_cache, (uchar*) table_list->table);
1076
broadcast_refresh();
1081
static bool locked_named_table(THD *thd, TABLE_LIST *table_list)
1083
for (; table_list ; table_list=table_list->next_local)
1085
TABLE *table= table_list->table;
1088
TABLE *save_next= table->next;
1091
result= table_is_used(table_list->table, 0);
1092
table->next= save_next;
1097
return 0; // All tables are locked
1101
bool wait_for_locked_table_names(THD *thd, TABLE_LIST *table_list)
1104
DBUG_ENTER("wait_for_locked_table_names");
1106
safe_mutex_assert_owner(&LOCK_open);
1108
while (locked_named_table(thd,table_list))
1115
wait_for_condition(thd, &LOCK_open, &COND_refresh);
1116
pthread_mutex_lock(&LOCK_open);
1118
DBUG_RETURN(result);
1123
Lock all tables in list with a name lock.
1126
- One must have a lock on LOCK_open when calling this
1128
@param thd Thread handle
1129
@param table_list Names of tables to lock
1132
If you are just locking one table, you should use
1133
lock_and_wait_for_table_name().
1138
1 Fatal error (end of memory ?)
926
Obtain exclusive metadata locks on the list of tables.
928
@param thd Thread handle
929
@param table_list List of tables to lock
931
@note This function assumes that no metadata locks were acquired
932
before calling it. Also it cannot be called while holding
933
LOCK_open mutex. Both these invariants are enforced by asserts
934
in mdl_acquire_exclusive_locks() functions.
936
@retval FALSE Success.
937
@retval TRUE Failure (OOM or thread was killed).
1141
940
bool lock_table_names(THD *thd, TABLE_LIST *table_list)
1143
bool got_all_locks=1;
1144
942
TABLE_LIST *lock_table;
943
MDL_LOCK_DATA *mdl_lock_data;
945
DEBUG_SYNC(thd, "before_wait_locked_tname");
1146
946
for (lock_table= table_list; lock_table; lock_table= lock_table->next_local)
1149
if ((got_lock=lock_table_name(thd,lock_table, TRUE)) < 0)
1150
goto end; // Fatal error
1152
got_all_locks=0; // Someone is using table
948
if (!(mdl_lock_data= mdl_alloc_lock(0, lock_table->db,
949
lock_table->table_name,
952
mdl_set_lock_type(mdl_lock_data, MDL_EXCLUSIVE);
953
mdl_add_lock(&thd->mdl_context, mdl_lock_data);
954
lock_table->mdl_lock_data= mdl_lock_data;
1155
/* If some table was in use, wait until we got the lock */
1156
if (!got_all_locks && wait_for_locked_table_names(thd, table_list))
956
if (mdl_acquire_exclusive_locks(&thd->mdl_context))
1161
unlock_table_names(thd, table_list, lock_table);
961
mdl_remove_all_locks(&thd->mdl_context);
1167
Unlock all tables in list with a name lock.
1169
@param thd Thread handle.
1170
@param table_list Names of tables to lock.
1173
This function needs to be protected by LOCK_open. If we're
1174
under LOCK TABLES, this function does not work as advertised. Namely,
1175
it does not exclude other threads from using this table and does not
1176
put an exclusive name lock on this table into the table cache.
1178
@see lock_table_names
1179
@see unlock_table_names
1181
@retval TRUE An error occured.
1182
@retval FALSE Name lock successfully acquired.
1185
bool lock_table_names_exclusively(THD *thd, TABLE_LIST *table_list)
1187
if (lock_table_names(thd, table_list))
1191
Upgrade the table name locks from semi-exclusive to exclusive locks.
1193
for (TABLE_LIST *table= table_list; table; table= table->next_global)
1196
table->table->open_placeholder= 1;
1203
Test is 'table' is protected by an exclusive name lock.
1205
@param[in] thd The current thread handler
1206
@param[in] table_list Table container containing the single table to be
1209
@note Needs to be protected by LOCK_open mutex.
1211
@return Error status code
1212
@retval TRUE Table is protected
1213
@retval FALSE Table is not protected
1217
is_table_name_exclusively_locked_by_this_thread(THD *thd,
1218
TABLE_LIST *table_list)
1220
char key[MAX_DBKEY_LENGTH];
1223
key_length= create_table_def_key(thd, key, table_list, 0);
1225
return is_table_name_exclusively_locked_by_this_thread(thd, (uchar *)key,
1231
Test is 'table key' is protected by an exclusive name lock.
1233
@param[in] thd The current thread handler.
1235
@param[in] key_length
1237
@note Needs to be protected by LOCK_open mutex
1239
@retval TRUE Table is protected
1240
@retval FALSE Table is not protected
1244
is_table_name_exclusively_locked_by_this_thread(THD *thd, uchar *key,
1247
HASH_SEARCH_STATE state;
1250
for (table= (TABLE*) hash_first(&open_cache, key,
1251
key_length, &state);
1253
table= (TABLE*) hash_next(&open_cache, key,
1254
key_length, &state))
1256
if (table->in_use == thd &&
1257
table->open_placeholder == 1 &&
1258
table->s->version == 0)
1266
Unlock all tables in list with a name lock.
1271
table_list Names of tables to unlock
1273
last_table Don't unlock any tables after this one.
1274
(default 0, which will unlock all tables)
1277
One must have a lock on LOCK_open when calling this.
1280
This function will broadcast refresh signals to inform other threads
1281
that the name locks are removed.
1286
1 Fatal error (end of memory ?)
1289
void unlock_table_names(THD *thd, TABLE_LIST *table_list,
1290
TABLE_LIST *last_table)
967
Release all metadata locks previously obtained by lock_table_names().
969
@param thd Thread handle.
971
@note Cannot be called while holding LOCK_open mutex.
974
void unlock_table_names(THD *thd)
1292
976
DBUG_ENTER("unlock_table_names");
1293
for (TABLE_LIST *table= table_list;
1294
table != last_table;
1295
table= table->next_local)
1296
unlock_table_name(thd,table);
1297
broadcast_refresh();
977
mdl_release_locks(&thd->mdl_context);
978
mdl_remove_all_locks(&thd->mdl_context);
1298
979
DBUG_VOID_RETURN;