summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <monty@narttu.mysql.fi>2003-03-04 13:36:59 +0200
committerunknown <monty@narttu.mysql.fi>2003-03-04 13:36:59 +0200
commit8970a67d520a499d150d0d90c45e7ac74e398cac (patch)
tree013fc022f7bca8766f16eff8ae9714126b96160f
parentd292ce6b09b69bac234461d1a31347c3ce085a4d (diff)
parent5603e43291b9e82037e345edae7f8036f92ebcae (diff)
downloadmariadb-git-8970a67d520a499d150d0d90c45e7ac74e398cac.tar.gz
Merge with 3.23 to get fix for deadlock when doing LOCK TABLES in one thread and DROP TABLE in another thread
sql/lock.cc: Auto merged sql/mysql_priv.h: Auto merged sql/sql_rename.cc: merge sql/sql_table.cc: merge
-rw-r--r--sql/lock.cc76
-rw-r--r--sql/mysql_priv.h3
-rw-r--r--sql/sql_rename.cc34
-rw-r--r--sql/sql_table.cc11
4 files changed, 95 insertions, 29 deletions
diff --git a/sql/lock.cc b/sql/lock.cc
index 64456e6ec36..41a76007289 100644
--- a/sql/lock.cc
+++ b/sql/lock.cc
@@ -487,11 +487,12 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list)
{
TABLE *table;
char key[MAX_DBKEY_LENGTH];
+ char *db= table_list->db ? table_list->db : (thd->db ? thd->db : (char*) "");
uint key_length;
DBUG_ENTER("lock_table_name");
safe_mutex_assert_owner(&LOCK_open);
- key_length=(uint) (strmov(strmov(key,table_list->db)+1,table_list->real_name)
+ key_length=(uint) (strmov(strmov(key,db)+1,table_list->real_name)
-key)+ 1;
/* Only insert the table if we haven't insert it already */
@@ -520,7 +521,7 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list)
my_free((gptr) table,MYF(0));
DBUG_RETURN(-1);
}
- if (remove_table_from_cache(thd, table_list->db, table_list->real_name))
+ if (remove_table_from_cache(thd, db, table_list->real_name))
DBUG_RETURN(1); // Table is in use
DBUG_RETURN(0);
}
@@ -564,6 +565,77 @@ bool wait_for_locked_table_names(THD *thd, TABLE_LIST *table_list)
DBUG_RETURN(result);
}
+
+/*
+ Lock all tables in list with a name lock
+
+ SYNOPSIS
+ lock_table_names()
+ thd Thread handle
+ table_list Names of tables to lock
+
+ NOTES
+ One must have a lock on LOCK_open when calling this
+
+ RETURN
+ 0 ok
+ 1 Fatal error (end of memory ?)
+*/
+
+bool lock_table_names(THD *thd, TABLE_LIST *table_list)
+{
+ bool got_all_locks=1;
+ TABLE_LIST *lock_table;
+
+ for (lock_table=table_list ; lock_table ; lock_table=lock_table->next)
+ {
+ int got_lock;
+ if ((got_lock=lock_table_name(thd,lock_table)) < 0)
+ goto end; // Fatal error
+ if (got_lock)
+ got_all_locks=0; // Someone is using table
+ }
+
+ /* If some table was in use, wait until we got the lock */
+ if (!got_all_locks && wait_for_locked_table_names(thd, table_list))
+ goto end;
+ return 0;
+
+end:
+ unlock_table_names(thd, table_list, lock_table);
+ return 1;
+}
+
+
+/*
+ Unlock all tables in list with a name lock
+
+ SYNOPSIS
+ unlock_table_names()
+ thd Thread handle
+ table_list Names of tables to unlock
+ last_table Don't unlock any tables after this one.
+ (default 0, which will unlock all tables)
+
+ NOTES
+ One must have a lock on LOCK_open when calling this
+ This function will send a COND_refresh signal to inform other threads
+ that the name locks are removed
+
+ RETURN
+ 0 ok
+ 1 Fatal error (end of memory ?)
+*/
+
+void unlock_table_names(THD *thd, TABLE_LIST *table_list,
+ TABLE_LIST *last_table)
+{
+ for (TABLE_LIST *table=table_list ; table != last_table ; table=table->next)
+ unlock_table_name(thd,table);
+ pthread_cond_broadcast(&COND_refresh);
+}
+
+
static void print_lock_error(int error)
{
int textno;
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index 7d830859fb2..c6e205f4729 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -723,6 +723,9 @@ int lock_and_wait_for_table_name(THD *thd, TABLE_LIST *table_list);
int lock_table_name(THD *thd, TABLE_LIST *table_list);
void unlock_table_name(THD *thd, TABLE_LIST *table_list);
bool wait_for_locked_table_names(THD *thd, TABLE_LIST *table_list);
+bool lock_table_names(THD *thd, TABLE_LIST *table_list);
+void unlock_table_names(THD *thd, TABLE_LIST *table_list,
+ TABLE_LIST *last_table= 0);
/* old unireg functions */
diff --git a/sql/sql_rename.cc b/sql/sql_rename.cc
index 3eddd2646d5..d7e998264f3 100644
--- a/sql/sql_rename.cc
+++ b/sql/sql_rename.cc
@@ -31,8 +31,8 @@ static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list)
{
- bool error=1,got_all_locks=1;
- TABLE_LIST *lock_table,*ren_table=0;
+ bool error= 1;
+ TABLE_LIST *ren_table= 0;
DBUG_ENTER("mysql_rename_tables");
/*
@@ -47,23 +47,11 @@ bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list)
}
VOID(pthread_mutex_lock(&LOCK_open));
- for (lock_table=table_list ; lock_table ; lock_table=lock_table->next)
- {
- int got_lock;
- if ((got_lock=lock_table_name(thd,lock_table)) < 0)
- goto end;
- if (got_lock)
- got_all_locks=0;
- }
-
- if (!got_all_locks && wait_for_locked_table_names(thd,table_list))
- goto end;
-
- if (!(ren_table=rename_tables(thd,table_list,0)))
- error=0;
-
-end:
- if (ren_table)
+ if (lock_table_names(thd, table_list))
+ goto err;
+
+ error=0;
+ if ((ren_table=rename_tables(thd,table_list,0)))
{
/* Rename didn't succeed; rename back the tables in reverse order */
TABLE_LIST *prev=0,*table;
@@ -85,7 +73,7 @@ end:
table=table->next->next; // Skip error table
/* Revert to old names */
rename_tables(thd, table, 1);
- /* Note that lock_table == 0 here, so the unlock loop will work */
+ error= 1;
}
/* Lets hope this doesn't fail as the result will be messy */
@@ -100,9 +88,9 @@ end:
send_ok(&thd->net);
}
- for (TABLE_LIST *table=table_list ; table != lock_table ; table=table->next)
- unlock_table_name(thd,table);
- pthread_cond_broadcast(&COND_refresh);
+ unlock_table_names(thd,table_list);
+
+err:
pthread_mutex_unlock(&LOCK_open);
DBUG_RETURN(error);
}
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 4c92c35632a..f84dd82ca65 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -80,7 +80,7 @@ int mysql_rm_table(THD *thd,TABLE_LIST *tables, my_bool if_exists)
{
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE,MYF(0),
tables->real_name);
- error = 1;
+ error= 1;
goto err;
}
while (global_read_lock && ! thd->killed)
@@ -93,7 +93,6 @@ int mysql_rm_table(THD *thd,TABLE_LIST *tables, my_bool if_exists)
err:
pthread_mutex_unlock(&LOCK_open);
- VOID(pthread_cond_broadcast(&COND_refresh)); // Signal to refresh
pthread_mutex_lock(&thd->mysys_var->mutex);
thd->mysys_var->current_mutex= 0;
@@ -138,7 +137,6 @@ int mysql_rm_table_part2_with_lock(THD *thd,
error=mysql_rm_table_part2(thd,tables, if_exists, dont_log_query);
pthread_mutex_unlock(&LOCK_open);
- VOID(pthread_cond_broadcast(&COND_refresh)); // Signal to refresh
pthread_mutex_lock(&thd->mysys_var->mutex);
thd->mysys_var->current_mutex= 0;
@@ -171,9 +169,12 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists,
bool some_tables_deleted=0, tmp_table_deleted=0;
DBUG_ENTER("mysql_rm_table_part2");
+ if (lock_table_names(thd, tables))
+ DBUG_RETURN(1);
+
for (table=tables ; table ; table=table->next)
{
- char *db=table->db ? table->db : thd->db;
+ char *db=table->db;
mysql_ha_closeall(thd, table);
if (!close_temporary_table(thd, db, table->real_name))
{
@@ -248,6 +249,8 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists,
my_error(ER_BAD_TABLE_ERROR,MYF(0),wrong_tables.c_ptr());
error=1;
}
+ unlock_table_names(thd, tables);
+
DBUG_RETURN(error);
}