194
194
static UnusedTables unused_tables;
195
static int open_unireg_entry(Session *session,
198
TableIdentifier &identifier);
200
196
unsigned char *table_cache_key(const unsigned char *record,
950
Open table which is already name-locked by this thread.
953
reopen_name_locked_table()
954
session Thread handle
955
table_list TableList object for table to be open, TableList::table
956
member should point to Table object which was used for
958
link_in true - if Table object for table to be opened should be
959
linked into Session::open_tables list.
960
false - placeholder used for name-locking is already in
961
this list so we only need to preserve Table::next
965
This function assumes that its caller already acquired LOCK_open mutex.
972
bool Session::reopen_name_locked_table(TableList* table_list)
974
Table *table= table_list->table;
976
safe_mutex_assert_owner(LOCK_open.native_handle());
978
if (killed || not table)
981
TableIdentifier identifier(table_list->getSchemaName(), table_list->getTableName());
982
if (open_unireg_entry(this, table, table_list->getTableName(), identifier))
984
table->intern_close_table();
989
We want to prevent other connections from opening this table until end
990
of statement as it is likely that modifications of table's metadata are
991
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
992
or we might want to drop table if CREATE TABLE ... SELECT fails).
993
This also allows us to assume that no other connection will sneak in
994
before we will get table-level lock on this table.
996
table->getMutableShare()->resetVersion();
997
table->in_use = this;
999
table->tablenr= current_tablenr++;
1000
table->used_fields= 0;
1001
table->const_table= 0;
1002
table->null_row= false;
1003
table->maybe_null= false;
1004
table->force_index= false;
1005
table->status= STATUS_NO_RECORD;
1012
946
Create and insert into table cache placeholder for table
1013
947
which will prevent its opening (or creation) (a.k.a lock
1834
Load a table definition from cursor and open unireg table
1838
session Thread handle
1839
entry Store open table definition here
1840
table_list TableList with db, table_name
1842
cache_key Key for share_cache
1843
cache_key_length length of cache_key
1846
Extra argument for open is taken from session->open_options
1847
One must have a lock on LOCK_open when calling this function
1854
static int open_unireg_entry(Session *session,
1857
TableIdentifier &identifier)
1860
TableSharePtr share;
1861
uint32_t discover_retry_count= 0;
1863
safe_mutex_assert_owner(LOCK_open.native_handle());
1865
if (not (share= TableShare::getShareCreate(session,
1870
while ((error= share->open_table_from_share(session,
1873
(uint32_t) (HA_OPEN_KEYFILE |
1877
session->open_options, *entry)))
1879
if (error == 7) // Table def changed
1881
share->resetVersion(); // Mark share as old
1882
if (discover_retry_count++) // Retry once
1884
TableShare::release(share);
1890
Here we should wait until all threads has released the table.
1891
For now we do one retry. This may cause a deadlock if there
1892
is other threads waiting for other tables used by this thread.
1894
Proper fix would be to if the second retry failed:
1895
- Mark that table def changed
1896
- Return from open table
1897
- Close all tables used by this thread
1898
- Start waiting that the share is released
1899
- Retry by opening all tables again
1904
To avoid deadlock, only wait for release if no one else is
1907
if (share->getTableCount() != 1)
1909
TableShare::release(share);
1912
/* Free share and wait until it's released by all threads */
1913
TableShare::release(share);
1915
if (!session->killed)
1917
drizzle_reset_errors(session, 1); // Clear warnings
1918
session->clear_error(); // Clear error message
1924
TableShare::release(share);
1934
1769
Open all tables in list