~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Monty Taylor
  • Date: 2010-07-31 17:48:00 UTC
  • mto: This revision was merged to the branch mainline in revision 1678.
  • Revision ID: mordred@inaugust.com-20100731174800-py7qyreb29zn6lh2
updated to latest pandora-build - contains win32 fixes and the fix to the
boost::thread check.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
  You should have received a copy of the GNU General Public License
13
13
  along with this program; if not, write to the Free Software
14
 
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* Basic functions needed by many modules */
31
31
# endif
32
32
#endif
33
33
#include "drizzled/internal/my_pthread.h"
34
 
#include "drizzled/internal/thread_var.h"
35
34
 
36
35
#include <drizzled/sql_select.h>
37
36
#include <drizzled/error.h>
47
46
#include "drizzled/cached_directory.h"
48
47
#include <drizzled/field/timestamp.h>
49
48
#include <drizzled/field/null.h>
 
49
#include "drizzled/memory/multi_malloc.h"
50
50
#include "drizzled/sql_table.h"
51
51
#include "drizzled/global_charset_info.h"
52
52
#include "drizzled/pthread_globals.h"
53
53
#include "drizzled/internal/iocache.h"
54
54
#include "drizzled/drizzled.h"
55
55
#include "drizzled/plugin/authorization.h"
56
 
#include "drizzled/table/temporary.h"
57
 
#include "drizzled/table/placeholder.h"
58
 
#include "drizzled/table/unused.h"
 
56
#include "drizzled/table_placeholder.h"
59
57
 
60
58
using namespace std;
61
59
 
64
62
 
65
63
extern bool volatile shutdown_in_progress;
66
64
 
 
65
TableOpenCache &get_open_cache()
 
66
{
 
67
  static TableOpenCache open_cache;                             /* Used by mysql_test */
 
68
 
 
69
  return open_cache;
 
70
}
 
71
 
 
72
static void free_cache_entry(Table *entry);
 
73
 
 
74
void remove_table(Table *arg)
 
75
{
 
76
  TableOpenCacheRange ppp;
 
77
  ppp= get_open_cache().equal_range(arg->getShare()->getCacheKey());
 
78
 
 
79
  for (TableOpenCache::const_iterator iter= ppp.first;
 
80
         iter != ppp.second; ++iter)
 
81
  {
 
82
    Table *found_table= (*iter).second;
 
83
 
 
84
    if (found_table == arg)
 
85
    {
 
86
      free_cache_entry(arg);
 
87
      get_open_cache().erase(iter);
 
88
      return;
 
89
    }
 
90
  }
 
91
}
 
92
 
 
93
static bool add_table(Table *arg)
 
94
{
 
95
  TableOpenCache &open_cache(get_open_cache());
 
96
 
 
97
  TableOpenCache::iterator returnable= open_cache.insert(make_pair(arg->getShare()->getCacheKey(), arg));
 
98
 
 
99
  return not (returnable == open_cache.end());
 
100
}
 
101
 
 
102
class UnusedTables {
 
103
  Table *tables;                                /* Used by mysql_test */
 
104
 
 
105
  Table *getTable() const
 
106
  {
 
107
    return tables;
 
108
  }
 
109
 
 
110
  Table *setTable(Table *arg)
 
111
  {
 
112
    return tables= arg;
 
113
  }
 
114
 
 
115
public:
 
116
 
 
117
  void cull()
 
118
  {
 
119
    /* Free cache if too big */
 
120
    while (cached_open_tables() > table_cache_size && getTable())
 
121
      remove_table(getTable());
 
122
  }
 
123
 
 
124
  void cullByVersion()
 
125
  {
 
126
    while (getTable() && not getTable()->getShare()->getVersion())
 
127
      remove_table(getTable());
 
128
  }
 
129
  
 
130
  void link(Table *table)
 
131
  {
 
132
    if (getTable())
 
133
    {
 
134
      table->setNext(getTable());               /* Link in last */
 
135
      table->setPrev(getTable()->getPrev());
 
136
      getTable()->setPrev(table);
 
137
      table->getPrev()->setNext(table);
 
138
    }
 
139
    else
 
140
    {
 
141
      table->setPrev(setTable(table));
 
142
      table->setNext(table->getPrev());
 
143
      assert(table->getNext() == table && table->getPrev() == table);
 
144
    }
 
145
  }
 
146
 
 
147
 
 
148
  void unlink(Table *table)
 
149
  {
 
150
    table->unlink();
 
151
 
 
152
    /* Unlink the table from "unused_tables" list. */
 
153
    if (table == getTable())
 
154
    {  // First unused
 
155
      setTable(getTable()->getNext()); // Remove from link
 
156
      if (table == getTable())
 
157
        setTable(NULL);
 
158
    }
 
159
  }
 
160
 
 
161
/* move table first in unused links */
 
162
 
 
163
  void relink(Table *table)
 
164
  {
 
165
    if (table != getTable())
 
166
    {
 
167
      table->unlink();
 
168
 
 
169
      table->setNext(getTable());                       /* Link in unused tables */
 
170
      table->setPrev(getTable()->getPrev());
 
171
      getTable()->getPrev()->setNext(table);
 
172
      getTable()->setPrev(table);
 
173
      setTable(table);
 
174
    }
 
175
  }
 
176
 
 
177
 
 
178
  void clear()
 
179
  {
 
180
    while (getTable())
 
181
      remove_table(getTable());
 
182
  }
 
183
 
 
184
  UnusedTables():
 
185
    tables(NULL)
 
186
  { }
 
187
 
 
188
  ~UnusedTables()
 
189
  { 
 
190
  }
 
191
};
 
192
 
 
193
static UnusedTables unused_tables;
 
194
static int open_unireg_entry(Session *session,
 
195
                             Table *entry,
 
196
                             const char *alias,
 
197
                             TableIdentifier &identifier);
 
198
 
 
199
unsigned char *table_cache_key(const unsigned char *record,
 
200
                               size_t *length,
 
201
                               bool );
 
202
 
 
203
#if 0
 
204
static bool reopen_table(Table *table);
 
205
#endif
 
206
 
 
207
unsigned char *table_cache_key(const unsigned char *record,
 
208
                               size_t *length,
 
209
                               bool )
 
210
{
 
211
  Table *entry=(Table*) record;
 
212
  *length= entry->getShare()->getCacheKey().size();
 
213
  return (unsigned char*) &entry->getShare()->getCacheKey()[0];
 
214
}
 
215
 
67
216
bool table_cache_init(void)
68
217
{
69
218
  return false;
71
220
 
72
221
uint32_t cached_open_tables(void)
73
222
{
74
 
  return table::getCache().size();
 
223
  return get_open_cache().size();
75
224
}
76
225
 
77
226
void table_cache_free(void)
78
227
{
79
228
  refresh_version++;                            // Force close of open tables
80
229
 
81
 
  table::getUnused().clear();
82
 
  table::getCache().clear();
 
230
  unused_tables.clear();
 
231
  get_open_cache().clear();
83
232
}
84
233
 
85
234
/*
93
242
  By leaving the table in the table cache, it disallows any other thread
94
243
  to open the table
95
244
 
96
 
  session->getKilled() will be set if we run out of memory
 
245
  session->killed will be set if we run out of memory
97
246
 
98
247
  If closing a MERGE child, the calling function has to take care for
99
248
  closing the parent too, if necessary.
102
251
 
103
252
void close_handle_and_leave_table_as_lock(Table *table)
104
253
{
 
254
  TableShare *share, *old_share= table->getMutableShare();
105
255
  assert(table->db_stat);
106
256
  assert(table->getShare()->getType() == message::Table::STANDARD);
107
257
 
112
262
  */
113
263
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
114
264
  const TableIdentifier::Key &key(identifier.getKey());
115
 
  TableShare *share= new TableShare(identifier.getType(),
116
 
                                    identifier,
117
 
                                    const_cast<char *>(key.vector()),  static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
 
265
  share= new TableShare(identifier.getType(),
 
266
                        identifier,
 
267
                        const_cast<char *>(&key[0]),  static_cast<uint32_t>(old_share->getCacheKeySize()));
118
268
 
119
269
  table->cursor->close();
120
270
  table->db_stat= 0;                            // Mark cursor closed
121
271
  TableShare::release(table->getMutableShare());
122
272
  table->setShare(share);
 
273
  table->cursor->change_table_ptr(table, table->getMutableShare());
123
274
}
124
275
 
125
276
 
137
288
  }
138
289
}
139
290
 
 
291
/*
 
292
  Remove table from the open table cache
 
293
 
 
294
  SYNOPSIS
 
295
  free_cache_entry()
 
296
  entry         Table to remove
 
297
 
 
298
  NOTE
 
299
  We need to have a lock on LOCK_open when calling this
 
300
*/
 
301
 
 
302
void free_cache_entry(Table *table)
 
303
{
 
304
  table->intern_close_table();
 
305
  if (not table->in_use)
 
306
  {
 
307
    unused_tables.unlink(table);
 
308
  }
 
309
 
 
310
  delete table;
 
311
}
 
312
 
140
313
/* Free resources allocated by filesort() and read_record() */
141
314
 
142
315
void Table::free_io_cache()
143
316
{
144
317
  if (sort.io_cache)
145
318
  {
146
 
    sort.io_cache->close_cached_file();
 
319
    close_cached_file(sort.io_cache);
147
320
    delete sort.io_cache;
148
321
    sort.io_cache= 0;
149
322
  }
155
328
 
156
329
  @param session Thread context (may be NULL)
157
330
  @param tables List of tables to remove from the cache
158
 
  @param have_lock If table::Cache::singleton().mutex() is locked
 
331
  @param have_lock If LOCK_open is locked
159
332
  @param wait_for_refresh Wait for a impending flush
160
333
  @param wait_for_placeholders Wait for tables being reopened so that the GRL
161
334
  won't proceed while write-locked tables are being reopened by other
170
343
  bool result= false;
171
344
  Session *session= this;
172
345
 
173
 
  {
174
 
    table::Cache::singleton().mutex().lock(); /* Optionally lock for remove tables from open_cahe if not in use */
175
 
 
176
 
    if (tables == NULL)
177
 
    {
178
 
      refresh_version++;                                // Force close of open tables
179
 
 
180
 
      table::getUnused().clear();
181
 
 
182
 
      if (wait_for_refresh)
183
 
      {
 
346
  pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
 
347
 
 
348
  if (tables == NULL)
 
349
  {
 
350
    refresh_version++;                          // Force close of open tables
 
351
 
 
352
    unused_tables.clear();
 
353
 
 
354
    if (wait_for_refresh)
 
355
    {
 
356
      /*
 
357
        Other threads could wait in a loop in open_and_lock_tables(),
 
358
        trying to lock one or more of our tables.
 
359
 
 
360
        If they wait for the locks in thr_multi_lock(), their lock
 
361
        request is aborted. They loop in open_and_lock_tables() and
 
362
        enter open_table(). Here they notice the table is refreshed and
 
363
        wait for COND_refresh. Then they loop again in
 
364
        openTablesLock() and this time open_table() succeeds. At
 
365
        this moment, if we (the FLUSH TABLES thread) are scheduled and
 
366
        on another FLUSH TABLES enter close_cached_tables(), they could
 
367
        awake while we sleep below, waiting for others threads (us) to
 
368
        close their open tables. If this happens, the other threads
 
369
        would find the tables unlocked. They would get the locks, one
 
370
        after the other, and could do their destructive work. This is an
 
371
        issue if we have LOCK TABLES in effect.
 
372
 
 
373
        The problem is that the other threads passed all checks in
 
374
        open_table() before we refresh the table.
 
375
 
 
376
        The fix for this problem is to set some_tables_deleted for all
 
377
        threads with open tables. These threads can still get their
 
378
        locks, but will immediately release them again after checking
 
379
        this variable. They will then loop in openTablesLock()
 
380
        again. There they will wait until we update all tables version
 
381
        below.
 
382
 
 
383
        Setting some_tables_deleted is done by remove_table_from_cache()
 
384
        in the other branch.
 
385
 
 
386
        In other words (reviewer suggestion): You need this setting of
 
387
        some_tables_deleted for the case when table was opened and all
 
388
        related checks were passed before incrementing refresh_version
 
389
        (which you already have) but attempt to lock the table happened
 
390
        after the call to Session::close_old_data_files() i.e. after removal of
 
391
        current thread locks.
 
392
      */
 
393
      for (TableOpenCache::const_iterator iter= get_open_cache().begin();
 
394
           iter != get_open_cache().end();
 
395
           iter++)
 
396
      {
 
397
        Table *table= (*iter).second;
 
398
        if (table->in_use)
 
399
          table->in_use->some_tables_deleted= false;
 
400
      }
 
401
    }
 
402
  }
 
403
  else
 
404
  {
 
405
    bool found= false;
 
406
    for (TableList *table= tables; table; table= table->next_local)
 
407
    {
 
408
      TableIdentifier identifier(table->db, table->table_name);
 
409
      if (remove_table_from_cache(session, identifier,
 
410
                                  RTFC_OWNED_BY_Session_FLAG))
 
411
      {
 
412
        found= true;
 
413
      }
 
414
    }
 
415
    if (!found)
 
416
      wait_for_refresh= false;                  // Nothing to wait for
 
417
  }
 
418
 
 
419
  if (wait_for_refresh)
 
420
  {
 
421
    /*
 
422
      If there is any table that has a lower refresh_version, wait until
 
423
      this is closed (or this thread is killed) before returning
 
424
    */
 
425
    session->mysys_var->current_mutex= &LOCK_open;
 
426
    session->mysys_var->current_cond= &COND_refresh;
 
427
    session->set_proc_info("Flushing tables");
 
428
 
 
429
    session->close_old_data_files();
 
430
 
 
431
    bool found= true;
 
432
    /* Wait until all threads has closed all the tables we had locked */
 
433
    while (found && ! session->killed)
 
434
    {
 
435
      found= false;
 
436
      for (TableOpenCache::const_iterator iter= get_open_cache().begin();
 
437
           iter != get_open_cache().end();
 
438
           iter++)
 
439
      {
 
440
        Table *table= (*iter).second;
 
441
        /* Avoid a self-deadlock. */
 
442
        if (table->in_use == session)
 
443
          continue;
184
444
        /*
185
 
          Other threads could wait in a loop in open_and_lock_tables(),
186
 
          trying to lock one or more of our tables.
187
 
 
188
 
          If they wait for the locks in thr_multi_lock(), their lock
189
 
          request is aborted. They loop in open_and_lock_tables() and
190
 
          enter open_table(). Here they notice the table is refreshed and
191
 
          wait for COND_refresh. Then they loop again in
192
 
          openTablesLock() and this time open_table() succeeds. At
193
 
          this moment, if we (the FLUSH TABLES thread) are scheduled and
194
 
          on another FLUSH TABLES enter close_cached_tables(), they could
195
 
          awake while we sleep below, waiting for others threads (us) to
196
 
          close their open tables. If this happens, the other threads
197
 
          would find the tables unlocked. They would get the locks, one
198
 
          after the other, and could do their destructive work. This is an
199
 
          issue if we have LOCK TABLES in effect.
200
 
 
201
 
          The problem is that the other threads passed all checks in
202
 
          open_table() before we refresh the table.
203
 
 
204
 
          The fix for this problem is to set some_tables_deleted for all
205
 
          threads with open tables. These threads can still get their
206
 
          locks, but will immediately release them again after checking
207
 
          this variable. They will then loop in openTablesLock()
208
 
          again. There they will wait until we update all tables version
209
 
          below.
210
 
 
211
 
          Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
212
 
          in the other branch.
213
 
 
214
 
          In other words (reviewer suggestion): You need this setting of
215
 
          some_tables_deleted for the case when table was opened and all
216
 
          related checks were passed before incrementing refresh_version
217
 
          (which you already have) but attempt to lock the table happened
218
 
          after the call to Session::close_old_data_files() i.e. after removal of
219
 
          current thread locks.
 
445
          Note that we wait here only for tables which are actually open, and
 
446
          not for placeholders with Table::open_placeholder set. Waiting for
 
447
          latter will cause deadlock in the following scenario, for example:
 
448
 
 
449
          conn1-> lock table t1 write;
 
450
          conn2-> lock table t2 write;
 
451
          conn1-> flush tables;
 
452
          conn2-> flush tables;
 
453
 
 
454
          It also does not make sense to wait for those of placeholders that
 
455
          are employed by CREATE TABLE as in this case table simply does not
 
456
          exist yet.
220
457
        */
221
 
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
222
 
             iter != table::getCache().end();
223
 
             iter++)
224
 
        {
225
 
          Table *table= (*iter).second;
226
 
          if (table->in_use)
227
 
            table->in_use->some_tables_deleted= false;
228
 
        }
229
 
      }
230
 
    }
231
 
    else
232
 
    {
233
 
      bool found= false;
234
 
      for (TableList *table= tables; table; table= table->next_local)
235
 
      {
236
 
        TableIdentifier identifier(table->getSchemaName(), table->getTableName());
237
 
        if (table::Cache::singleton().removeTable(session, identifier,
238
 
                                    RTFC_OWNED_BY_Session_FLAG))
 
458
        if (table->needs_reopen_or_name_lock() && (table->db_stat ||
 
459
                                                   (table->open_placeholder && wait_for_placeholders)))
239
460
        {
240
461
          found= true;
 
462
          pthread_cond_wait(&COND_refresh,&LOCK_open);
 
463
          break;
241
464
        }
242
465
      }
243
 
      if (!found)
244
 
        wait_for_refresh= false;                        // Nothing to wait for
245
466
    }
 
467
    /*
 
468
      No other thread has the locked tables open; reopen them and get the
 
469
      old locks. This should always succeed (unless some external process
 
470
      has removed the tables)
 
471
    */
 
472
    result= session->reopen_tables(true, true);
246
473
 
247
 
    if (wait_for_refresh)
 
474
    /* Set version for table */
 
475
    for (Table *table= session->open_tables; table ; table= table->getNext())
248
476
    {
249
477
      /*
250
 
        If there is any table that has a lower refresh_version, wait until
251
 
        this is closed (or this thread is killed) before returning
252
 
      */
253
 
      session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
254
 
      session->mysys_var->current_cond= &COND_refresh;
255
 
      session->set_proc_info("Flushing tables");
256
 
 
257
 
      session->close_old_data_files();
258
 
 
259
 
      bool found= true;
260
 
      /* Wait until all threads has closed all the tables we had locked */
261
 
      while (found && ! session->getKilled())
262
 
      {
263
 
        found= false;
264
 
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
265
 
             iter != table::getCache().end();
266
 
             iter++)
267
 
        {
268
 
          Table *table= (*iter).second;
269
 
          /* Avoid a self-deadlock. */
270
 
          if (table->in_use == session)
271
 
            continue;
272
 
          /*
273
 
            Note that we wait here only for tables which are actually open, and
274
 
            not for placeholders with Table::open_placeholder set. Waiting for
275
 
            latter will cause deadlock in the following scenario, for example:
276
 
 
277
 
            conn1-> lock table t1 write;
278
 
            conn2-> lock table t2 write;
279
 
            conn1-> flush tables;
280
 
            conn2-> flush tables;
281
 
 
282
 
            It also does not make sense to wait for those of placeholders that
283
 
            are employed by CREATE TABLE as in this case table simply does not
284
 
            exist yet.
285
 
          */
286
 
          if (table->needs_reopen_or_name_lock() && (table->db_stat ||
287
 
                                                     (table->open_placeholder && wait_for_placeholders)))
288
 
          {
289
 
            found= true;
290
 
            boost_unique_lock_t scoped(table::Cache::singleton().mutex(), boost::adopt_lock_t());
291
 
            COND_refresh.wait(scoped);
292
 
            scoped.release();
293
 
            break;
294
 
          }
295
 
        }
296
 
      }
297
 
      /*
298
 
        No other thread has the locked tables open; reopen them and get the
299
 
        old locks. This should always succeed (unless some external process
300
 
        has removed the tables)
301
 
      */
302
 
      result= session->reopen_tables(true, true);
303
 
 
304
 
      /* Set version for table */
305
 
      for (Table *table= session->open_tables; table ; table= table->getNext())
306
 
      {
307
 
        /*
308
 
          Preserve the version (0) of write locked tables so that a impending
309
 
          global read lock won't sneak in.
310
 
        */
311
 
        if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
312
 
          table->getMutableShare()->refreshVersion();
313
 
      }
 
478
        Preserve the version (0) of write locked tables so that a impending
 
479
        global read lock won't sneak in.
 
480
      */
 
481
      if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
 
482
        table->getMutableShare()->refreshVersion();
314
483
    }
315
 
 
316
 
    table::Cache::singleton().mutex().unlock();
317
484
  }
318
485
 
 
486
  pthread_mutex_unlock(&LOCK_open);
 
487
 
319
488
  if (wait_for_refresh)
320
489
  {
321
 
    boost_unique_lock_t scopedLock(session->mysys_var->mutex);
 
490
    pthread_mutex_lock(&session->mysys_var->mutex);
322
491
    session->mysys_var->current_mutex= 0;
323
492
    session->mysys_var->current_cond= 0;
324
493
    session->set_proc_info(0);
 
494
    pthread_mutex_unlock(&session->mysys_var->mutex);
325
495
  }
326
496
 
327
497
  return result;
335
505
bool Session::free_cached_table()
336
506
{
337
507
  bool found_old_table= false;
338
 
  table::Concurrent *table= static_cast<table::Concurrent *>(open_tables);
 
508
  Table *table= open_tables;
339
509
 
340
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
510
  safe_mutex_assert_owner(&LOCK_open);
341
511
  assert(table->key_read == 0);
342
512
  assert(!table->cursor || table->cursor->inited == Cursor::NONE);
343
513
 
346
516
  if (table->needs_reopen_or_name_lock() ||
347
517
      version != refresh_version || !table->db_stat)
348
518
  {
349
 
    table::remove_table(table);
 
519
    remove_table(table);
350
520
    found_old_table= true;
351
521
  }
352
522
  else
359
529
 
360
530
    /* Free memory and reset for next loop */
361
531
    table->cursor->ha_reset();
362
 
    table->in_use= NULL;
 
532
    table->in_use= false;
363
533
 
364
 
    table::getUnused().link(table);
 
534
    unused_tables.link(table);
365
535
  }
366
536
 
367
537
  return found_old_table;
380
550
{
381
551
  bool found_old_table= false;
382
552
 
383
 
  safe_mutex_assert_not_owner(table::Cache::singleton().mutex().native_handle());
 
553
  safe_mutex_assert_not_owner(&LOCK_open);
384
554
 
385
 
  boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close all open tables on Session */
 
555
  pthread_mutex_lock(&LOCK_open); /* Close all open tables on Session */
386
556
 
387
557
  while (open_tables)
388
558
  {
393
563
  if (found_old_table)
394
564
  {
395
565
    /* Tell threads waiting for refresh that something has happened */
396
 
    locking::broadcast_refresh();
 
566
    broadcast_refresh();
397
567
  }
 
568
 
 
569
  pthread_mutex_unlock(&LOCK_open);
398
570
}
399
571
 
400
572
/*
423
595
  for (; table; table= table->*link )
424
596
  {
425
597
    if ((table->table == 0 || table->table->getShare()->getType() == message::Table::STANDARD) &&
426
 
        strcasecmp(table->getSchemaName(), db_name) == 0 &&
427
 
        strcasecmp(table->getTableName(), table_name) == 0)
 
598
        strcasecmp(table->db, db_name) == 0 &&
 
599
        strcasecmp(table->table_name, table_name) == 0)
428
600
      break;
429
601
  }
430
602
  return table;
495
667
    */
496
668
    assert(table);
497
669
  }
498
 
  d_name= table->getSchemaName();
499
 
  t_name= table->getTableName();
 
670
  d_name= table->db;
 
671
  t_name= table->table_name;
500
672
  t_alias= table->alias;
501
673
 
502
674
  for (;;)
517
689
}
518
690
 
519
691
 
520
 
void Open_tables_state::doGetTableNames(const SchemaIdentifier &schema_identifier,
521
 
                                        std::set<std::string>& set_of_names)
 
692
void Session::doGetTableNames(const SchemaIdentifier &schema_identifier,
 
693
                              std::set<std::string>& set_of_names)
522
694
{
523
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
695
  for (Table *table= temporary_tables ; table ; table= table->getNext())
524
696
  {
525
697
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
526
698
    {
529
701
  }
530
702
}
531
703
 
532
 
void Open_tables_state::doGetTableNames(CachedDirectory &,
533
 
                                        const SchemaIdentifier &schema_identifier,
534
 
                                        std::set<std::string> &set_of_names)
 
704
void Session::doGetTableNames(CachedDirectory &,
 
705
                              const SchemaIdentifier &schema_identifier,
 
706
                              std::set<std::string> &set_of_names)
535
707
{
536
708
  doGetTableNames(schema_identifier, set_of_names);
537
709
}
538
710
 
539
 
void Open_tables_state::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
540
 
                                              TableIdentifier::vector &set_of_identifiers)
 
711
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
 
712
                                    TableIdentifiers &set_of_identifiers)
541
713
{
542
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
714
  for (Table *table= temporary_tables ; table ; table= table->getNext())
543
715
  {
544
716
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
545
717
    {
550
722
  }
551
723
}
552
724
 
553
 
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
554
 
                                              const SchemaIdentifier &schema_identifier,
555
 
                                              TableIdentifier::vector &set_of_identifiers)
 
725
void Session::doGetTableIdentifiers(CachedDirectory &,
 
726
                                    const SchemaIdentifier &schema_identifier,
 
727
                                    TableIdentifiers &set_of_identifiers)
556
728
{
557
729
  doGetTableIdentifiers(schema_identifier, set_of_identifiers);
558
730
}
559
731
 
560
 
bool Open_tables_state::doDoesTableExist(const TableIdentifier &identifier)
 
732
bool Session::doDoesTableExist(const TableIdentifier &identifier)
561
733
{
562
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
734
  for (Table *table= temporary_tables ; table ; table= table->getNext())
563
735
  {
564
736
    if (table->getShare()->getType() == message::Table::TEMPORARY)
565
737
    {
573
745
  return false;
574
746
}
575
747
 
576
 
int Open_tables_state::doGetTableDefinition(const TableIdentifier &identifier,
 
748
int Session::doGetTableDefinition(const TableIdentifier &identifier,
577
749
                                  message::Table &table_proto)
578
750
{
579
 
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
 
751
  for (Table *table= temporary_tables ; table ; table= table->getNext())
580
752
  {
581
753
    if (table->getShare()->getType() == message::Table::TEMPORARY)
582
754
    {
592
764
  return ENOENT;
593
765
}
594
766
 
595
 
Table *Open_tables_state::find_temporary_table(const TableIdentifier &identifier)
 
767
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
 
768
{
 
769
  char  key[MAX_DBKEY_LENGTH];
 
770
  uint  key_length;
 
771
 
 
772
  key_length= TableIdentifier::createKey(key, new_db, table_name);
 
773
 
 
774
  for (Table *table= temporary_tables ; table ; table= table->getNext())
 
775
  {
 
776
    const TableIdentifier::Key &share_key(table->getShare()->getCacheKey());
 
777
    if (share_key.size() == key_length &&
 
778
        not memcmp(&share_key[0], key, key_length))
 
779
    {
 
780
      return table;
 
781
    }
 
782
  }
 
783
  return NULL;                               // Not a temporary table
 
784
}
 
785
 
 
786
Table *Session::find_temporary_table(TableList *table_list)
 
787
{
 
788
  return find_temporary_table(table_list->db, table_list->table_name);
 
789
}
 
790
 
 
791
Table *Session::find_temporary_table(TableIdentifier &identifier)
596
792
{
597
793
  for (Table *table= temporary_tables ; table ; table= table->getNext())
598
794
  {
630
826
  @retval -1  the table is in use by a outer query
631
827
*/
632
828
 
633
 
int Open_tables_state::drop_temporary_table(const drizzled::TableIdentifier &identifier)
 
829
int Session::drop_temporary_table(TableList *table_list)
634
830
{
635
831
  Table *table;
636
832
 
637
 
  if (not (table= find_temporary_table(identifier)))
 
833
  if (not (table= find_temporary_table(table_list)))
638
834
    return 1;
639
835
 
640
836
  /* Table might be in use by some outer statement. */
641
 
  if (table->query_id && table->query_id != getQueryId())
 
837
  if (table->query_id && table->query_id != query_id)
642
838
  {
643
839
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
644
840
    return -1;
656
852
 
657
853
  @param  session     Thread context
658
854
  @param  find    Table to remove
659
 
 
660
 
  @note because we risk the chance of deleting the share, we can't assume that it will exist past, this should be modified once we can use a TableShare::shared_ptr here.
661
855
*/
662
856
 
663
857
void Session::unlink_open_table(Table *find)
664
858
{
665
 
  const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
666
 
  Table **prev;
667
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
859
  char key[MAX_DBKEY_LENGTH];
 
860
  uint32_t key_length= find->getShare()->getCacheKeySize();
 
861
  Table *list, **prev;
 
862
  safe_mutex_assert_owner(&LOCK_open);
668
863
 
 
864
  memcpy(key, &find->getMutableShare()->getCacheKey()[0], key_length);
669
865
  /*
670
 
    Note that we need to hold table::Cache::singleton().mutex() while changing the
 
866
    Note that we need to hold LOCK_open while changing the
671
867
    open_tables list. Another thread may work on it.
672
 
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
 
868
    (See: remove_table_from_cache(), mysql_wait_completed_table())
673
869
    Closing a MERGE child before the parent would be fatal if the
674
870
    other thread tries to abort the MERGE lock in between.
675
871
  */
676
872
  for (prev= &open_tables; *prev; )
677
873
  {
678
 
    Table *list= *prev;
 
874
    list= *prev;
679
875
 
680
 
    if (list->getShare()->getCacheKey() == find_key)
 
876
    if (list->getShare()->getCacheKeySize() == key_length &&
 
877
        not memcmp(&list->getShare()->getCacheKey()[0], key, key_length))
681
878
    {
682
879
      /* Remove table from open_tables list. */
683
880
      *prev= list->getNext();
684
881
 
685
882
      /* Close table. */
686
 
      table::remove_table(static_cast<table::Concurrent *>(list));
 
883
      remove_table(list);
687
884
    }
688
885
    else
689
886
    {
693
890
  }
694
891
 
695
892
  // Notify any 'refresh' threads
696
 
  locking::broadcast_refresh();
 
893
  broadcast_refresh();
697
894
}
698
895
 
699
896
 
716
913
  table that was locked with LOCK TABLES.
717
914
*/
718
915
 
719
 
void Session::drop_open_table(Table *table, const TableIdentifier &identifier)
 
916
void Session::drop_open_table(Table *table, TableIdentifier &identifier)
720
917
{
721
918
  if (table->getShare()->getType())
722
919
  {
724
921
  }
725
922
  else
726
923
  {
727
 
    boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close and drop a table (AUX routine) */
 
924
    pthread_mutex_lock(&LOCK_open); /* Close and drop a table (AUX routine) */
728
925
    /*
729
926
      unlink_open_table() also tells threads waiting for refresh or close
730
927
      that something has happened.
731
928
    */
732
929
    unlink_open_table(table);
733
 
    plugin::StorageEngine::dropTable(*this, identifier);
 
930
    quick_rm_table(*this, identifier);
 
931
    pthread_mutex_unlock(&LOCK_open);
734
932
  }
735
933
}
736
934
 
746
944
  cond  Condition to wait for
747
945
*/
748
946
 
749
 
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
 
947
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
750
948
{
751
949
  /* Wait until the current table is up to date */
752
950
  const char *saved_proc_info;
753
 
  mysys_var->current_mutex= &mutex;
754
 
  mysys_var->current_cond= &cond;
 
951
  mysys_var->current_mutex= mutex;
 
952
  mysys_var->current_cond= cond;
755
953
  saved_proc_info= get_proc_info();
756
954
  set_proc_info("Waiting for table");
757
 
  {
758
 
    /*
759
 
      We must unlock mutex first to avoid deadlock becasue conditions are
760
 
      sent to this thread by doing locks in the following order:
761
 
      lock(mysys_var->mutex)
762
 
      lock(mysys_var->current_mutex)
763
 
 
764
 
      One by effect of this that one can only use wait_for_condition with
765
 
      condition variables that are guranteed to not disapper (freed) even if this
766
 
      mutex is unlocked
767
 
    */
768
 
    boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
769
 
    if (not getKilled())
770
 
    {
771
 
      cond.wait(scopedLock);
772
 
    }
773
 
  }
774
 
  boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
 
955
  if (!killed)
 
956
    (void) pthread_cond_wait(cond, mutex);
 
957
 
 
958
  /*
 
959
    We must unlock mutex first to avoid deadlock becasue conditions are
 
960
    sent to this thread by doing locks in the following order:
 
961
    lock(mysys_var->mutex)
 
962
    lock(mysys_var->current_mutex)
 
963
 
 
964
    One by effect of this that one can only use wait_for_condition with
 
965
    condition variables that are guranteed to not disapper (freed) even if this
 
966
    mutex is unlocked
 
967
  */
 
968
 
 
969
  pthread_mutex_unlock(mutex);
 
970
  pthread_mutex_lock(&mysys_var->mutex);
775
971
  mysys_var->current_mutex= 0;
776
972
  mysys_var->current_cond= 0;
777
973
  set_proc_info(saved_proc_info);
 
974
  pthread_mutex_unlock(&mysys_var->mutex);
 
975
}
 
976
 
 
977
 
 
978
/*
 
979
  Open table which is already name-locked by this thread.
 
980
 
 
981
  SYNOPSIS
 
982
  reopen_name_locked_table()
 
983
  session         Thread handle
 
984
  table_list  TableList object for table to be open, TableList::table
 
985
  member should point to Table object which was used for
 
986
  name-locking.
 
987
  link_in     true  - if Table object for table to be opened should be
 
988
  linked into Session::open_tables list.
 
989
  false - placeholder used for name-locking is already in
 
990
  this list so we only need to preserve Table::next
 
991
  pointer.
 
992
 
 
993
  NOTE
 
994
  This function assumes that its caller already acquired LOCK_open mutex.
 
995
 
 
996
  RETURN VALUE
 
997
  false - Success
 
998
  true  - Error
 
999
*/
 
1000
 
 
1001
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
 
1002
{
 
1003
  Table *table= table_list->table;
 
1004
  TableShare *share;
 
1005
  char *table_name= table_list->table_name;
 
1006
  Table orig_table;
 
1007
 
 
1008
  safe_mutex_assert_owner(&LOCK_open);
 
1009
 
 
1010
  if (killed || !table)
 
1011
    return true;
 
1012
 
 
1013
  orig_table= *table;
 
1014
 
 
1015
  TableIdentifier identifier(table_list->db, table_list->table_name);
 
1016
  if (open_unireg_entry(this, table, table_name, identifier))
 
1017
  {
 
1018
    table->intern_close_table();
 
1019
    /*
 
1020
      If there was an error during opening of table (for example if it
 
1021
      does not exist) '*table' object can be wiped out. To be able
 
1022
      properly release name-lock in this case we should restore this
 
1023
      object to its original state.
 
1024
    */
 
1025
    *table= orig_table;
 
1026
    return true;
 
1027
  }
 
1028
 
 
1029
  share= table->getMutableShare();
 
1030
  /*
 
1031
    We want to prevent other connections from opening this table until end
 
1032
    of statement as it is likely that modifications of table's metadata are
 
1033
    not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
 
1034
    or we might want to drop table if CREATE TABLE ... SELECT fails).
 
1035
    This also allows us to assume that no other connection will sneak in
 
1036
    before we will get table-level lock on this table.
 
1037
  */
 
1038
  share->resetVersion();
 
1039
  table->in_use = this;
 
1040
 
 
1041
  if (link_in)
 
1042
  {
 
1043
    table->setNext(open_tables);
 
1044
    open_tables= table;
 
1045
  }
 
1046
  else
 
1047
  {
 
1048
    /*
 
1049
      Table object should be already in Session::open_tables list so we just
 
1050
      need to set Table::next correctly.
 
1051
    */
 
1052
    table->setNext(orig_table.getNext());
 
1053
  }
 
1054
 
 
1055
  table->tablenr= current_tablenr++;
 
1056
  table->used_fields= 0;
 
1057
  table->const_table= 0;
 
1058
  table->null_row= false;
 
1059
  table->maybe_null= false;
 
1060
  table->force_index= false;
 
1061
  table->status= STATUS_NO_RECORD;
 
1062
 
 
1063
  return false;
778
1064
}
779
1065
 
780
1066
 
791
1077
  case of failure.
792
1078
*/
793
1079
 
794
 
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
 
1080
Table *Session::table_cache_insert_placeholder(const char *db_name, const char *table_name)
795
1081
{
796
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
1082
  safe_mutex_assert_owner(&LOCK_open);
797
1083
 
798
1084
  /*
799
1085
    Create a table entry with the right key and with an old refresh version
 
1086
    Note that we must use multi_malloc() here as this is freed by the
 
1087
    table cache
800
1088
  */
801
 
  TableIdentifier identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
802
 
  table::Placeholder *table= new table::Placeholder(this, identifier);
 
1089
  TableIdentifier identifier(db_name, table_name, message::Table::INTERNAL);
 
1090
  TablePlaceholder *table= new TablePlaceholder(this, identifier);
803
1091
 
804
 
  if (not table::Cache::singleton().insert(table))
 
1092
  if (not add_table(table))
805
1093
  {
806
1094
    delete table;
807
1095
 
833
1121
  @retval  true   Error occured (OOM)
834
1122
  @retval  false  Success. 'table' parameter set according to above rules.
835
1123
*/
836
 
bool Session::lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table)
 
1124
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
837
1125
{
838
1126
  const TableIdentifier::Key &key(identifier.getKey());
839
1127
 
840
 
  boost_unique_lock_t scope_lock(table::Cache::singleton().mutex()); /* Obtain a name lock even though table is not in cache (like for create table)  */
841
 
 
842
 
  table::CacheMap::iterator iter;
843
 
 
844
 
  iter= table::getCache().find(key);
845
 
 
846
 
  if (iter != table::getCache().end())
 
1128
  pthread_mutex_lock(&LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table)  */
 
1129
 
 
1130
  TableOpenCache::iterator iter;
 
1131
 
 
1132
  iter= get_open_cache().find(key);
 
1133
 
 
1134
  if (iter != get_open_cache().end())
847
1135
  {
 
1136
    pthread_mutex_unlock(&LOCK_open);
848
1137
    *table= 0;
849
1138
    return false;
850
1139
  }
851
1140
 
852
 
  if (not (*table= table_cache_insert_placeholder(identifier)))
 
1141
  if (not (*table= table_cache_insert_placeholder(identifier.getSchemaName().c_str(), identifier.getTableName().c_str())))
853
1142
  {
 
1143
    pthread_mutex_unlock(&LOCK_open);
854
1144
    return true;
855
1145
  }
856
1146
  (*table)->open_placeholder= true;
857
1147
  (*table)->setNext(open_tables);
858
1148
  open_tables= *table;
 
1149
  pthread_mutex_unlock(&LOCK_open);
859
1150
 
860
1151
  return false;
861
1152
}
909
1200
  if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
910
1201
    return NULL;
911
1202
 
912
 
  if (getKilled())
 
1203
  if (killed)
913
1204
    return NULL;
914
1205
 
915
 
  TableIdentifier identifier(table_list->getSchemaName(), table_list->getTableName());
 
1206
  TableIdentifier identifier(table_list->db, table_list->table_name);
916
1207
  const TableIdentifier::Key &key(identifier.getKey());
917
 
  table::CacheRange ppp;
 
1208
  TableOpenCacheRange ppp;
918
1209
 
919
1210
  /*
920
1211
    Unless requested otherwise, try to resolve this table in the list
923
1214
    same name. This block implements the behaviour.
924
1215
    TODO -> move this block into a separate function.
925
1216
  */
926
 
  bool reset= false;
927
 
  for (table= getTemporaryTables(); table ; table=table->getNext())
 
1217
  for (table= temporary_tables; table ; table=table->getNext())
928
1218
  {
929
1219
    if (table->getShare()->getCacheKey() == key)
930
1220
    {
940
1230
        return NULL;
941
1231
      }
942
1232
      table->query_id= getQueryId();
943
 
      reset= true;
 
1233
      goto reset;
 
1234
    }
 
1235
  }
 
1236
 
 
1237
  if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
 
1238
  {
 
1239
    my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
 
1240
    return NULL;
 
1241
  }
 
1242
 
 
1243
  /*
 
1244
    If it's the first table from a list of tables used in a query,
 
1245
    remember refresh_version (the version of open_cache state).
 
1246
    If the version changes while we're opening the remaining tables,
 
1247
    we will have to back off, close all the tables opened-so-far,
 
1248
    and try to reopen them.
 
1249
 
 
1250
    Note-> refresh_version is currently changed only during FLUSH TABLES.
 
1251
  */
 
1252
  if (!open_tables)
 
1253
  {
 
1254
    version= refresh_version;
 
1255
  }
 
1256
  else if ((version != refresh_version) &&
 
1257
           ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
 
1258
  {
 
1259
    /* Someone did a refresh while thread was opening tables */
 
1260
    if (refresh)
 
1261
      *refresh= true;
 
1262
 
 
1263
    return NULL;
 
1264
  }
 
1265
 
 
1266
  /*
 
1267
    Before we test the global cache, we test our local session cache.
 
1268
  */
 
1269
  if (cached_table)
 
1270
  {
 
1271
    assert(false); /* Not implemented yet */
 
1272
  }
 
1273
 
 
1274
  /*
 
1275
    Non pre-locked/LOCK TABLES mode, and the table is not temporary:
 
1276
    this is the normal use case.
 
1277
    Now we should:
 
1278
    - try to find the table in the table cache.
 
1279
    - if one of the discovered Table instances is name-locked
 
1280
    (table->getShare()->version == 0) back off -- we have to wait
 
1281
    until no one holds a name lock on the table.
 
1282
    - if there is no such Table in the name cache, read the table definition
 
1283
    and insert it into the cache.
 
1284
    We perform all of the above under LOCK_open which currently protects
 
1285
    the open cache (also known as table cache) and table definitions stored
 
1286
    on disk.
 
1287
  */
 
1288
 
 
1289
  pthread_mutex_lock(&LOCK_open); /* Lock for FLUSH TABLES for open table */
 
1290
 
 
1291
  /*
 
1292
    Actually try to find the table in the open_cache.
 
1293
    The cache may contain several "Table" instances for the same
 
1294
    physical table. The instances that are currently "in use" by
 
1295
    some thread have their "in_use" member != NULL.
 
1296
    There is no good reason for having more than one entry in the
 
1297
    hash for the same physical table, except that we use this as
 
1298
    an implicit "pending locks queue" - see
 
1299
    wait_for_locked_table_names for details.
 
1300
  */
 
1301
  ppp= get_open_cache().equal_range(key);
 
1302
 
 
1303
  table= NULL;
 
1304
  for (TableOpenCache::const_iterator iter= ppp.first;
 
1305
       iter != ppp.second; ++iter, table= NULL)
 
1306
  {
 
1307
    table= (*iter).second;
 
1308
 
 
1309
    if (not table->in_use)
944
1310
      break;
945
 
    }
946
 
  }
947
 
 
948
 
  if (not reset)
949
 
  {
950
 
    if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
951
 
    {
952
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->getSchemaName(), table_list->getTableName());
953
 
      return NULL;
954
 
    }
955
 
 
956
1311
    /*
957
 
      If it's the first table from a list of tables used in a query,
958
 
      remember refresh_version (the version of open_cache state).
959
 
      If the version changes while we're opening the remaining tables,
960
 
      we will have to back off, close all the tables opened-so-far,
961
 
      and try to reopen them.
962
 
 
963
 
      Note-> refresh_version is currently changed only during FLUSH TABLES.
 
1312
      Here we flush tables marked for flush.
 
1313
      Normally, table->getShare()->version contains the value of
 
1314
      refresh_version from the moment when this table was
 
1315
      (re-)opened and added to the cache.
 
1316
      If since then we did (or just started) FLUSH TABLES
 
1317
      statement, refresh_version has been increased.
 
1318
      For "name-locked" Table instances, table->getShare()->version is set
 
1319
      to 0 (see lock_table_name for details).
 
1320
      In case there is a pending FLUSH TABLES or a name lock, we
 
1321
      need to back off and re-start opening tables.
 
1322
      If we do not back off now, we may dead lock in case of lock
 
1323
      order mismatch with some other thread:
 
1324
      c1-> name lock t1; -- sort of exclusive lock
 
1325
      c2-> open t2;      -- sort of shared lock
 
1326
      c1-> name lock t2; -- blocks
 
1327
      c2-> open t1; -- blocks
964
1328
    */
965
 
    if (!open_tables)
966
 
    {
967
 
      version= refresh_version;
968
 
    }
969
 
    else if ((version != refresh_version) &&
970
 
             ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
971
 
    {
972
 
      /* Someone did a refresh while thread was opening tables */
 
1329
    if (table->needs_reopen_or_name_lock())
 
1330
    {
 
1331
      if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
 
1332
      {
 
1333
        /* Force close at once after usage */
 
1334
        version= table->getShare()->getVersion();
 
1335
        continue;
 
1336
      }
 
1337
 
 
1338
      /* Avoid self-deadlocks by detecting self-dependencies. */
 
1339
      if (table->open_placeholder && table->in_use == this)
 
1340
      {
 
1341
        pthread_mutex_unlock(&LOCK_open);
 
1342
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getMutableShare()->getTableName());
 
1343
        return NULL;
 
1344
      }
 
1345
 
 
1346
      /*
 
1347
        Back off, part 1: mark the table as "unused" for the
 
1348
        purpose of name-locking by setting table->db_stat to 0. Do
 
1349
        that only for the tables in this thread that have an old
 
1350
        table->getShare()->version (this is an optimization (?)).
 
1351
        table->db_stat == 0 signals wait_for_locked_table_names
 
1352
        that the tables in question are not used any more. See
 
1353
        table_is_used call for details.
 
1354
      */
 
1355
      close_old_data_files(false, false);
 
1356
 
 
1357
      /*
 
1358
        Back-off part 2: try to avoid "busy waiting" on the table:
 
1359
        if the table is in use by some other thread, we suspend
 
1360
        and wait till the operation is complete: when any
 
1361
        operation that juggles with table->getShare()->version completes,
 
1362
        it broadcasts COND_refresh condition variable.
 
1363
        If 'old' table we met is in use by current thread we return
 
1364
        without waiting since in this situation it's this thread
 
1365
        which is responsible for broadcasting on COND_refresh
 
1366
        (and this was done already in Session::close_old_data_files()).
 
1367
        Good example of such situation is when we have statement
 
1368
        that needs two instances of table and FLUSH TABLES comes
 
1369
        after we open first instance but before we open second
 
1370
        instance.
 
1371
      */
 
1372
      if (table->in_use != this)
 
1373
      {
 
1374
        /* wait_for_conditionwill unlock LOCK_open for us */
 
1375
        wait_for_condition(&LOCK_open, &COND_refresh);
 
1376
      }
 
1377
      else
 
1378
      {
 
1379
        pthread_mutex_unlock(&LOCK_open);
 
1380
      }
 
1381
      /*
 
1382
        There is a refresh in progress for this table.
 
1383
        Signal the caller that it has to try again.
 
1384
      */
973
1385
      if (refresh)
974
1386
        *refresh= true;
975
 
 
976
1387
      return NULL;
977
1388
    }
978
 
 
979
 
    /*
980
 
      Before we test the global cache, we test our local session cache.
981
 
    */
982
 
    if (cached_table)
983
 
    {
984
 
      assert(false); /* Not implemented yet */
985
 
    }
986
 
 
987
 
    /*
988
 
      Non pre-locked/LOCK TABLES mode, and the table is not temporary:
989
 
      this is the normal use case.
990
 
      Now we should:
991
 
      - try to find the table in the table cache.
992
 
      - if one of the discovered Table instances is name-locked
993
 
      (table->getShare()->version == 0) back off -- we have to wait
994
 
      until no one holds a name lock on the table.
995
 
      - if there is no such Table in the name cache, read the table definition
996
 
      and insert it into the cache.
997
 
      We perform all of the above under table::Cache::singleton().mutex() which currently protects
998
 
      the open cache (also known as table cache) and table definitions stored
999
 
      on disk.
1000
 
    */
1001
 
 
1002
 
    {
1003
 
      table::Cache::singleton().mutex().lock(); /* Lock for FLUSH TABLES for open table */
1004
 
 
1005
 
      /*
1006
 
        Actually try to find the table in the open_cache.
1007
 
        The cache may contain several "Table" instances for the same
1008
 
        physical table. The instances that are currently "in use" by
1009
 
        some thread have their "in_use" member != NULL.
1010
 
        There is no good reason for having more than one entry in the
1011
 
        hash for the same physical table, except that we use this as
1012
 
        an implicit "pending locks queue" - see
1013
 
        wait_for_locked_table_names for details.
1014
 
      */
1015
 
      ppp= table::getCache().equal_range(key);
1016
 
 
1017
 
      table= NULL;
1018
 
      for (table::CacheMap::const_iterator iter= ppp.first;
1019
 
           iter != ppp.second; ++iter, table= NULL)
 
1389
  }
 
1390
  if (table)
 
1391
  {
 
1392
    unused_tables.unlink(table);
 
1393
    table->in_use= this;
 
1394
  }
 
1395
  else
 
1396
  {
 
1397
    /* Insert a new Table instance into the open cache */
 
1398
    int error;
 
1399
    /* Free cache if too big */
 
1400
    unused_tables.cull();
 
1401
 
 
1402
    if (table_list->isCreate())
 
1403
    {
 
1404
      TableIdentifier  lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
 
1405
 
 
1406
      if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1020
1407
      {
1021
 
        table= (*iter).second;
1022
 
 
1023
 
        if (not table->in_use)
1024
 
          break;
1025
1408
        /*
1026
 
          Here we flush tables marked for flush.
1027
 
          Normally, table->getShare()->version contains the value of
1028
 
          refresh_version from the moment when this table was
1029
 
          (re-)opened and added to the cache.
1030
 
          If since then we did (or just started) FLUSH TABLES
1031
 
          statement, refresh_version has been increased.
1032
 
          For "name-locked" Table instances, table->getShare()->version is set
1033
 
          to 0 (see lock_table_name for details).
1034
 
          In case there is a pending FLUSH TABLES or a name lock, we
1035
 
          need to back off and re-start opening tables.
1036
 
          If we do not back off now, we may dead lock in case of lock
1037
 
          order mismatch with some other thread:
1038
 
          c1-> name lock t1; -- sort of exclusive lock
1039
 
          c2-> open t2;      -- sort of shared lock
1040
 
          c1-> name lock t2; -- blocks
1041
 
          c2-> open t1; -- blocks
 
1409
          Table to be created, so we need to create placeholder in table-cache.
1042
1410
        */
1043
 
        if (table->needs_reopen_or_name_lock())
 
1411
        if (!(table= table_cache_insert_placeholder(table_list->db, table_list->table_name)))
1044
1412
        {
1045
 
          if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1046
 
          {
1047
 
            /* Force close at once after usage */
1048
 
            version= table->getShare()->getVersion();
1049
 
            continue;
1050
 
          }
1051
 
 
1052
 
          /* Avoid self-deadlocks by detecting self-dependencies. */
1053
 
          if (table->open_placeholder && table->in_use == this)
1054
 
          {
1055
 
            table::Cache::singleton().mutex().unlock();
1056
 
            my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1057
 
            return NULL;
1058
 
          }
1059
 
 
1060
 
          /*
1061
 
            Back off, part 1: mark the table as "unused" for the
1062
 
            purpose of name-locking by setting table->db_stat to 0. Do
1063
 
            that only for the tables in this thread that have an old
1064
 
            table->getShare()->version (this is an optimization (?)).
1065
 
            table->db_stat == 0 signals wait_for_locked_table_names
1066
 
            that the tables in question are not used any more. See
1067
 
            table_is_used call for details.
1068
 
          */
1069
 
          close_old_data_files(false, false);
1070
 
 
1071
 
          /*
1072
 
            Back-off part 2: try to avoid "busy waiting" on the table:
1073
 
            if the table is in use by some other thread, we suspend
1074
 
            and wait till the operation is complete: when any
1075
 
            operation that juggles with table->getShare()->version completes,
1076
 
            it broadcasts COND_refresh condition variable.
1077
 
            If 'old' table we met is in use by current thread we return
1078
 
            without waiting since in this situation it's this thread
1079
 
            which is responsible for broadcasting on COND_refresh
1080
 
            (and this was done already in Session::close_old_data_files()).
1081
 
            Good example of such situation is when we have statement
1082
 
            that needs two instances of table and FLUSH TABLES comes
1083
 
            after we open first instance but before we open second
1084
 
            instance.
1085
 
          */
1086
 
          if (table->in_use != this)
1087
 
          {
1088
 
            /* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1089
 
            wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1090
 
          }
1091
 
          else
1092
 
          {
1093
 
            table::Cache::singleton().mutex().unlock();
1094
 
          }
1095
 
          /*
1096
 
            There is a refresh in progress for this table.
1097
 
            Signal the caller that it has to try again.
1098
 
          */
1099
 
          if (refresh)
1100
 
            *refresh= true;
 
1413
          pthread_mutex_unlock(&LOCK_open);
1101
1414
          return NULL;
1102
1415
        }
1103
 
      }
1104
 
      if (table)
1105
 
      {
1106
 
        table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1107
 
        table->in_use= this;
1108
 
      }
1109
 
      else
1110
 
      {
1111
 
        /* Insert a new Table instance into the open cache */
1112
 
        int error;
1113
 
        /* Free cache if too big */
1114
 
        table::getUnused().cull();
1115
 
 
1116
 
        if (table_list->isCreate())
1117
 
        {
1118
 
          TableIdentifier  lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1119
 
 
1120
 
          if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1121
 
          {
1122
 
            /*
1123
 
              Table to be created, so we need to create placeholder in table-cache.
1124
 
            */
1125
 
            if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1126
 
            {
1127
 
              table::Cache::singleton().mutex().unlock();
1128
 
              return NULL;
1129
 
            }
1130
 
            /*
1131
 
              Link placeholder to the open tables list so it will be automatically
1132
 
              removed once tables are closed. Also mark it so it won't be ignored
1133
 
              by other trying to take name-lock.
1134
 
            */
1135
 
            table->open_placeholder= true;
1136
 
            table->setNext(open_tables);
1137
 
            open_tables= table;
1138
 
            table::Cache::singleton().mutex().unlock();
1139
 
 
1140
 
            return table ;
1141
 
          }
1142
 
          /* Table exists. Let us try to open it. */
1143
 
        }
1144
 
 
1145
 
        /* make a new table */
1146
 
        {
1147
 
          table::Concurrent *new_table= new table::Concurrent;
1148
 
          table= new_table;
1149
 
          if (new_table == NULL)
1150
 
          {
1151
 
            table::Cache::singleton().mutex().unlock();
1152
 
            return NULL;
1153
 
          }
1154
 
 
1155
 
          error= new_table->open_unireg_entry(this, alias, identifier);
1156
 
          if (error != 0)
1157
 
          {
1158
 
            delete new_table;
1159
 
            table::Cache::singleton().mutex().unlock();
1160
 
            return NULL;
1161
 
          }
1162
 
          (void)table::Cache::singleton().insert(new_table);
1163
 
        }
1164
 
      }
1165
 
 
1166
 
      table::Cache::singleton().mutex().unlock();
1167
 
    }
1168
 
    if (refresh)
1169
 
    {
1170
 
      table->setNext(open_tables); /* Link into simple list */
1171
 
      open_tables= table;
1172
 
    }
1173
 
    table->reginfo.lock_type= TL_READ; /* Assume read */
1174
 
 
1175
 
  }
 
1416
        /*
 
1417
          Link placeholder to the open tables list so it will be automatically
 
1418
          removed once tables are closed. Also mark it so it won't be ignored
 
1419
          by other trying to take name-lock.
 
1420
        */
 
1421
        table->open_placeholder= true;
 
1422
        table->setNext(open_tables);
 
1423
        open_tables= table;
 
1424
        pthread_mutex_unlock(&LOCK_open);
 
1425
 
 
1426
        return table ;
 
1427
      }
 
1428
      /* Table exists. Let us try to open it. */
 
1429
    }
 
1430
 
 
1431
    /* make a new table */
 
1432
    table= new Table;
 
1433
    if (table == NULL)
 
1434
    {
 
1435
      pthread_mutex_unlock(&LOCK_open);
 
1436
      return NULL;
 
1437
    }
 
1438
 
 
1439
    error= open_unireg_entry(this, table, alias, identifier);
 
1440
    if (error != 0)
 
1441
    {
 
1442
      delete table;
 
1443
      pthread_mutex_unlock(&LOCK_open);
 
1444
      return NULL;
 
1445
    }
 
1446
    (void)add_table(table);
 
1447
  }
 
1448
 
 
1449
  pthread_mutex_unlock(&LOCK_open);
 
1450
  if (refresh)
 
1451
  {
 
1452
    table->setNext(open_tables); /* Link into simple list */
 
1453
    open_tables= table;
 
1454
  }
 
1455
  table->reginfo.lock_type= TL_READ; /* Assume read */
 
1456
 
 
1457
reset:
1176
1458
  assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1177
1459
 
 
1460
  if (lex->need_correct_ident())
 
1461
    table->alias_name_used= my_strcasecmp(table_alias_charset,
 
1462
                                          table->getMutableShare()->getTableName(), alias);
1178
1463
  /* Fix alias if table name changes */
1179
1464
  if (strcmp(table->getAlias(), alias))
1180
1465
  {
1181
 
    table->setAlias(alias);
 
1466
    uint32_t length=(uint32_t) strlen(alias)+1;
 
1467
    table->alias= (char*) realloc((char*) table->alias, length);
 
1468
    memcpy((void*) table->alias, alias, length);
1182
1469
  }
1183
1470
 
1184
1471
  /* These variables are also set in reopen_table() */
1189
1476
  table->maybe_null= false;
1190
1477
  table->force_index= false;
1191
1478
  table->status=STATUS_NO_RECORD;
1192
 
  table->insert_values.clear();
 
1479
  table->insert_values= 0;
1193
1480
  /* Catch wrong handling of the auto_increment_field_not_null. */
1194
1481
  assert(!table->auto_increment_field_not_null);
1195
1482
  table->auto_increment_field_not_null= false;
1205
1492
}
1206
1493
 
1207
1494
 
 
1495
#if 0
 
1496
/*
 
1497
  Reopen an table because the definition has changed.
 
1498
 
 
1499
  SYNOPSIS
 
1500
  reopen_table()
 
1501
  table Table object
 
1502
 
 
1503
  NOTES
 
1504
  The data cursor for the table is already closed and the share is released
 
1505
  The table has a 'dummy' share that mainly contains database and table name.
 
1506
 
 
1507
  RETURN
 
1508
  0  ok
 
1509
  1  error. The old table object is not changed.
 
1510
*/
 
1511
 
 
1512
bool reopen_table(Table *table)
 
1513
{
 
1514
  Table tmp;
 
1515
  bool error= 1;
 
1516
  Field **field;
 
1517
  uint32_t key,part;
 
1518
  TableList table_list;
 
1519
  Session *session= table->in_use;
 
1520
 
 
1521
  assert(table->getShare()->ref_count == 0);
 
1522
  assert(!table->sort.io_cache);
 
1523
 
 
1524
#ifdef EXTRA_DEBUG
 
1525
  if (table->db_stat)
 
1526
    errmsg_printf(ERRMSG_LVL_ERROR, _("Table %s had a open data Cursor in reopen_table"),
 
1527
                  table->alias);
 
1528
#endif
 
1529
  table_list.db=         const_cast<char *>(table->getShare()->getSchemaName());
 
1530
  table_list.table_name= table->getShare()->getTableName();
 
1531
  table_list.table=      table;
 
1532
 
 
1533
  if (wait_for_locked_table_names(session, &table_list))
 
1534
    return true;                             // Thread was killed
 
1535
 
 
1536
  if (open_unireg_entry(session, &tmp, &table_list,
 
1537
                        table->alias,
 
1538
                        table->getShare()->getCacheKey(),
 
1539
                        table->getShare()->getCacheKeySize()))
 
1540
    goto end;
 
1541
 
 
1542
  /* This list copies variables set by open_table */
 
1543
  tmp.tablenr=          table->tablenr;
 
1544
  tmp.used_fields=      table->used_fields;
 
1545
  tmp.const_table=      table->const_table;
 
1546
  tmp.null_row=         table->null_row;
 
1547
  tmp.maybe_null=       table->maybe_null;
 
1548
  tmp.status=           table->status;
 
1549
 
 
1550
  /* Get state */
 
1551
  tmp.in_use=           session;
 
1552
  tmp.reginfo.lock_type=table->reginfo.lock_type;
 
1553
 
 
1554
  /* Replace table in open list */
 
1555
  tmp.next=             table->next;
 
1556
  tmp.prev=             table->prev;
 
1557
 
 
1558
  if (table->cursor)
 
1559
    table->delete_table(true);          // close cursor, free everything
 
1560
 
 
1561
  *table= tmp;
 
1562
  table->default_column_bitmaps();
 
1563
  table->cursor->change_table_ptr(table, table->s);
 
1564
 
 
1565
  assert(table->alias != 0);
 
1566
  for (field=table->field ; *field ; field++)
 
1567
  {
 
1568
    (*field)->table= (*field)->orig_table= table;
 
1569
    (*field)->table_name= &table->alias;
 
1570
  }
 
1571
  for (key=0 ; key < table->getShare()->keys ; key++)
 
1572
  {
 
1573
    for (part=0 ; part < table->key_info[key].usable_key_parts ; part++)
 
1574
      table->key_info[key].key_part[part].field->table= table;
 
1575
  }
 
1576
 
 
1577
  broadcast_refresh();
 
1578
  error= false;
 
1579
 
 
1580
end:
 
1581
  return(error);
 
1582
}
 
1583
#endif
 
1584
 
 
1585
 
1208
1586
/**
1209
1587
  Close all instances of a table open by this thread and replace
1210
1588
  them with exclusive name-locks.
1222
1600
  the strings are used in a loop even after the share may be freed.
1223
1601
*/
1224
1602
 
1225
 
void Session::close_data_files_and_morph_locks(const TableIdentifier &identifier)
 
1603
void Session::close_data_files_and_morph_locks(TableIdentifier &identifier)
1226
1604
{
1227
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
 
1605
  safe_mutex_assert_owner(&LOCK_open); /* Adjust locks at the end of ALTER TABLEL */
1228
1606
 
1229
1607
  if (lock)
1230
1608
  {
1232
1610
      If we are not under LOCK TABLES we should have only one table
1233
1611
      open and locked so it makes sense to remove the lock at once.
1234
1612
    */
1235
 
    unlockTables(lock);
 
1613
    mysql_unlock_tables(this, lock);
1236
1614
    lock= 0;
1237
1615
  }
1238
1616
 
1267
1645
  combination when one needs tables to be reopened (for
1268
1646
  example see openTablesLock()).
1269
1647
 
1270
 
  @note One should have lock on table::Cache::singleton().mutex() when calling this.
 
1648
  @note One should have lock on LOCK_open when calling this.
1271
1649
 
1272
1650
  @return false in case of success, true - otherwise.
1273
1651
*/
1284
1662
  if (open_tables == NULL)
1285
1663
    return false;
1286
1664
 
1287
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
1665
  safe_mutex_assert_owner(&LOCK_open);
1288
1666
  if (get_locks)
1289
1667
  {
1290
1668
    /*
1311
1689
    next= table->getNext();
1312
1690
 
1313
1691
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1314
 
    table::remove_table(static_cast<table::Concurrent *>(table));
 
1692
    remove_table(table);
1315
1693
    error= 1;
1316
1694
  }
1317
1695
  *prev=0;
1318
1696
  if (tables != tables_ptr)                     // Should we get back old locks
1319
1697
  {
1320
 
    DrizzleLock *local_lock;
 
1698
    DRIZZLE_LOCK *local_lock;
1321
1699
    /*
1322
1700
      We should always get these locks. Anyway, we must not go into
1323
 
      wait_for_tables() as it tries to acquire table::Cache::singleton().mutex(), which is
 
1701
      wait_for_tables() as it tries to acquire LOCK_open, which is
1324
1702
      already locked.
1325
1703
    */
1326
1704
    some_tables_deleted= false;
1327
1705
 
1328
 
    if ((local_lock= lockTables(tables, (uint32_t) (tables_ptr - tables),
1329
 
                                       flags, &not_used)))
 
1706
    if ((local_lock= mysql_lock_tables(this, tables, (uint32_t) (tables_ptr - tables),
 
1707
                                 flags, &not_used)))
1330
1708
    {
1331
1709
      /* unused */
1332
1710
    }
1345
1723
  if (get_locks && tables)
1346
1724
    delete [] tables;
1347
1725
 
1348
 
  locking::broadcast_refresh();
 
1726
  broadcast_refresh();
1349
1727
 
1350
1728
  return(error);
1351
1729
}
1392
1770
              lock on it. This will also give them a chance to close their
1393
1771
              instances of this table.
1394
1772
            */
1395
 
            abortLock(ulcktbl);
1396
 
            removeLock(ulcktbl);
 
1773
            mysql_lock_abort(this, ulcktbl);
 
1774
            mysql_lock_remove(this, ulcktbl);
1397
1775
            ulcktbl->lock_count= 0;
1398
1776
          }
1399
1777
          if ((ulcktbl != table) && ulcktbl->db_stat)
1433
1811
    }
1434
1812
  }
1435
1813
  if (found)
1436
 
    locking::broadcast_refresh();
 
1814
    broadcast_refresh();
 
1815
}
 
1816
 
 
1817
 
 
1818
/*
 
1819
  Wait until all threads has closed the tables in the list
 
1820
  We have also to wait if there is thread that has a lock on this table even
 
1821
  if the table is closed
 
1822
*/
 
1823
 
 
1824
bool table_is_used(Table *table, bool wait_for_name_lock)
 
1825
{
 
1826
  do
 
1827
  {
 
1828
    const TableIdentifier::Key &key(table->getShare()->getCacheKey());
 
1829
 
 
1830
    TableOpenCacheRange ppp;
 
1831
    ppp= get_open_cache().equal_range(key);
 
1832
 
 
1833
    for (TableOpenCache::const_iterator iter= ppp.first;
 
1834
         iter != ppp.second; ++iter)
 
1835
    {
 
1836
      Table *search= (*iter).second;
 
1837
      if (search->in_use == table->in_use)
 
1838
        continue;                               // Name locked by this thread
 
1839
      /*
 
1840
        We can't use the table under any of the following conditions:
 
1841
        - There is an name lock on it (Table is to be deleted or altered)
 
1842
        - If we are in flush table and we didn't execute the flush
 
1843
        - If the table engine is open and it's an old version
 
1844
        (We must wait until all engines are shut down to use the table)
 
1845
      */
 
1846
      if ( (search->locked_by_name && wait_for_name_lock) ||
 
1847
           (search->is_name_opened() && search->needs_reopen_or_name_lock()))
 
1848
        return 1;
 
1849
    }
 
1850
  } while ((table=table->getNext()));
 
1851
  return 0;
1437
1852
}
1438
1853
 
1439
1854
 
1444
1859
  bool result;
1445
1860
 
1446
1861
  session->set_proc_info("Waiting for tables");
1447
 
  {
1448
 
    boost_unique_lock_t lock(table::Cache::singleton().mutex());
1449
 
    while (not session->getKilled())
1450
 
    {
1451
 
      session->some_tables_deleted= false;
1452
 
      session->close_old_data_files(false, dropping_tables != 0);
1453
 
      if (not table::Cache::singleton().areTablesUsed(session->open_tables, 1))
1454
 
      {
1455
 
        break;
1456
 
      }
1457
 
      COND_refresh.wait(lock);
1458
 
    }
1459
 
    if (session->getKilled())
1460
 
      result= true;                                     // aborted
1461
 
    else
1462
 
    {
1463
 
      /* Now we can open all tables without any interference */
1464
 
      session->set_proc_info("Reopen tables");
1465
 
      session->version= refresh_version;
1466
 
      result= session->reopen_tables(false, false);
1467
 
    }
1468
 
  }
 
1862
  pthread_mutex_lock(&LOCK_open); /* Lock for all tables to be refreshed */
 
1863
  while (!session->killed)
 
1864
  {
 
1865
    session->some_tables_deleted= false;
 
1866
    session->close_old_data_files(false, dropping_tables != 0);
 
1867
    if (!table_is_used(session->open_tables, 1))
 
1868
      break;
 
1869
    (void) pthread_cond_wait(&COND_refresh,&LOCK_open);
 
1870
  }
 
1871
  if (session->killed)
 
1872
    result= true;                                       // aborted
 
1873
  else
 
1874
  {
 
1875
    /* Now we can open all tables without any interference */
 
1876
    session->set_proc_info("Reopen tables");
 
1877
    session->version= refresh_version;
 
1878
    result= session->reopen_tables(false, false);
 
1879
  }
 
1880
  pthread_mutex_unlock(&LOCK_open);
1469
1881
  session->set_proc_info(0);
1470
1882
 
1471
1883
  return result;
1502
1914
  prev= &session->open_tables;
1503
1915
 
1504
1916
  /*
1505
 
    Note that we need to hold table::Cache::singleton().mutex() while changing the
 
1917
    Note that we need to hold LOCK_open while changing the
1506
1918
    open_tables list. Another thread may work on it.
1507
 
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
 
1919
    (See: remove_table_from_cache(), mysql_wait_completed_table())
1508
1920
    Closing a MERGE child before the parent would be fatal if the
1509
1921
    other thread tries to abort the MERGE lock in between.
1510
1922
  */
1513
1925
    next=table->getNext();
1514
1926
    if (table->getShare()->getCacheKey() == identifier.getKey())
1515
1927
    {
1516
 
      session->removeLock(table);
 
1928
      mysql_lock_remove(session, table);
1517
1929
 
1518
1930
      if (!found)
1519
1931
      {
1528
1940
      else
1529
1941
      {
1530
1942
        /* We already have a name lock, remove copy */
1531
 
        table::remove_table(static_cast<table::Concurrent *>(table));
 
1943
        remove_table(table);
1532
1944
      }
1533
1945
    }
1534
1946
    else
1539
1951
  }
1540
1952
  *prev=0;
1541
1953
  if (found)
1542
 
    locking::broadcast_refresh();
 
1954
    broadcast_refresh();
1543
1955
 
1544
1956
  return(found);
1545
1957
}
1559
1971
    if (table->getShare()->getCacheKey() == identifier.getKey())
1560
1972
    {
1561
1973
      /* If MERGE child, forward lock handling to parent. */
1562
 
      session->abortLock(table);
 
1974
      mysql_lock_abort(session, table);
1563
1975
      break;
1564
1976
    }
1565
1977
  }
1566
1978
}
1567
1979
 
 
1980
/*
 
1981
  Load a table definition from cursor and open unireg table
 
1982
 
 
1983
  SYNOPSIS
 
1984
  open_unireg_entry()
 
1985
  session                       Thread handle
 
1986
  entry         Store open table definition here
 
1987
  table_list            TableList with db, table_name
 
1988
  alias         Alias name
 
1989
  cache_key             Key for share_cache
 
1990
  cache_key_length      length of cache_key
 
1991
 
 
1992
  NOTES
 
1993
  Extra argument for open is taken from session->open_options
 
1994
  One must have a lock on LOCK_open when calling this function
 
1995
 
 
1996
  RETURN
 
1997
  0     ok
 
1998
#       Error
 
1999
*/
 
2000
 
 
2001
static int open_unireg_entry(Session *session,
 
2002
                             Table *entry,
 
2003
                             const char *alias,
 
2004
                             TableIdentifier &identifier)
 
2005
{
 
2006
  int error;
 
2007
  TableShare *share;
 
2008
  uint32_t discover_retry_count= 0;
 
2009
 
 
2010
  safe_mutex_assert_owner(&LOCK_open);
 
2011
retry:
 
2012
  if (not (share= TableShare::getShareCreate(session,
 
2013
                                             identifier,
 
2014
                                             &error)))
 
2015
    return 1;
 
2016
 
 
2017
  while ((error= share->open_table_from_share(session,
 
2018
                                              identifier,
 
2019
                                              alias,
 
2020
                                              (uint32_t) (HA_OPEN_KEYFILE |
 
2021
                                                          HA_OPEN_RNDFILE |
 
2022
                                                          HA_GET_INDEX |
 
2023
                                                          HA_TRY_READ_ONLY),
 
2024
                                              session->open_options, *entry)))
 
2025
  {
 
2026
    if (error == 7)                             // Table def changed
 
2027
    {
 
2028
      share->resetVersion();                        // Mark share as old
 
2029
      if (discover_retry_count++)               // Retry once
 
2030
        goto err;
 
2031
 
 
2032
      /*
 
2033
        TODO->
 
2034
        Here we should wait until all threads has released the table.
 
2035
        For now we do one retry. This may cause a deadlock if there
 
2036
        is other threads waiting for other tables used by this thread.
 
2037
 
 
2038
        Proper fix would be to if the second retry failed:
 
2039
        - Mark that table def changed
 
2040
        - Return from open table
 
2041
        - Close all tables used by this thread
 
2042
        - Start waiting that the share is released
 
2043
        - Retry by opening all tables again
 
2044
      */
 
2045
 
 
2046
      /*
 
2047
        TO BE FIXED
 
2048
        To avoid deadlock, only wait for release if no one else is
 
2049
        using the share.
 
2050
      */
 
2051
      if (share->getTableCount() != 1)
 
2052
        goto err;
 
2053
      /* Free share and wait until it's released by all threads */
 
2054
      TableShare::release(share);
 
2055
 
 
2056
      if (!session->killed)
 
2057
      {
 
2058
        drizzle_reset_errors(session, 1);         // Clear warnings
 
2059
        session->clear_error();                 // Clear error message
 
2060
        goto retry;
 
2061
      }
 
2062
      return 1;
 
2063
    }
 
2064
 
 
2065
    goto err;
 
2066
  }
 
2067
 
 
2068
  return 0;
 
2069
 
 
2070
err:
 
2071
  TableShare::release(share);
 
2072
 
 
2073
  return 1;
 
2074
}
 
2075
 
1568
2076
 
1569
2077
/*
1570
2078
  Open all tables in list
1632
2140
     * to see if it exists so that an unauthorized user cannot phish for
1633
2141
     * table/schema information via error messages
1634
2142
     */
1635
 
    TableIdentifier the_table(tables->getSchemaName(), tables->getTableName());
1636
 
    if (not plugin::Authorization::isAuthorized(user(), the_table))
 
2143
    TableIdentifier the_table(tables->db, tables->table_name);
 
2144
    if (not plugin::Authorization::isAuthorized(getSecurityContext(),
 
2145
                                                the_table))
1637
2146
    {
1638
2147
      result= -1;                               // Fatal error
1639
2148
      break;
1741
2250
 
1742
2251
    assert(lock == 0);  // You must lock everything at once
1743
2252
    if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
1744
 
      if (! (lock= lockTables(&table_list->table, 1, 0, &refresh)))
 
2253
      if (! (lock= mysql_lock_tables(this, &table_list->table, 1, 0, &refresh)))
1745
2254
        table= 0;
1746
2255
  }
1747
2256
 
1804
2313
      *(ptr++)= table->table;
1805
2314
  }
1806
2315
 
1807
 
  if (!(session->lock= session->lockTables(start, (uint32_t) (ptr - start), lock_flag, need_reopen)))
 
2316
  if (!(session->lock= mysql_lock_tables(session, start, (uint32_t) (ptr - start),
 
2317
                                         lock_flag, need_reopen)))
1808
2318
  {
1809
2319
    return -1;
1810
2320
  }
1833
2343
#  Table object
1834
2344
*/
1835
2345
 
1836
 
Table *Open_tables_state::open_temporary_table(const TableIdentifier &identifier,
1837
 
                                               bool link_in_list)
 
2346
Table *Session::open_temporary_table(TableIdentifier &identifier,
 
2347
                                     bool link_in_list)
1838
2348
{
 
2349
  TableShare *share;
 
2350
 
1839
2351
  assert(identifier.isTmp());
1840
 
 
1841
 
 
1842
 
  table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
1843
 
                                                        identifier,
1844
 
                                                        const_cast<char *>(const_cast<TableIdentifier&>(identifier).getPath().c_str()),
1845
 
                                                        static_cast<uint32_t>(identifier.getPath().length()));
 
2352
  share= new TableShare(identifier.getType(),
 
2353
                        identifier,
 
2354
                        const_cast<char *>(identifier.getPath().c_str()), static_cast<uint32_t>(identifier.getPath().length()));
 
2355
 
 
2356
 
 
2357
  Table *new_tmp_table= new Table;
1846
2358
  if (not new_tmp_table)
1847
2359
    return NULL;
1848
2360
 
1849
2361
  /*
1850
2362
    First open the share, and then open the table from the share we just opened.
1851
2363
  */
1852
 
  if (new_tmp_table->getMutableShare()->open_table_def(*static_cast<Session *>(this), identifier) ||
1853
 
      new_tmp_table->getMutableShare()->open_table_from_share(static_cast<Session *>(this), identifier, identifier.getTableName().c_str(),
1854
 
                                                              (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
1855
 
                                                                          HA_GET_INDEX),
1856
 
                                                              ha_open_options,
1857
 
                                                              *new_tmp_table))
 
2364
  if (share->open_table_def(*this, identifier) ||
 
2365
      share->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
 
2366
                            (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
 
2367
                                        HA_GET_INDEX),
 
2368
                            ha_open_options,
 
2369
                            *new_tmp_table))
1858
2370
  {
1859
2371
    /* No need to lock share->mutex as this is not needed for tmp tables */
1860
 
    delete new_tmp_table->getMutableShare();
1861
 
    delete new_tmp_table;
 
2372
    delete share;
 
2373
    free((char*) new_tmp_table);
1862
2374
 
1863
2375
    return 0;
1864
2376
  }
1899
2411
{
1900
2412
  if (session->mark_used_columns != MARK_COLUMNS_NONE)
1901
2413
  {
1902
 
    boost::dynamic_bitset<> *current_bitmap= NULL;
 
2414
    MyBitmap *current_bitmap, *other_bitmap;
1903
2415
 
1904
2416
    /*
1905
2417
      We always want to register the used keys, as the column bitmap may have
1912
2424
    if (session->mark_used_columns == MARK_COLUMNS_READ)
1913
2425
    {
1914
2426
      current_bitmap= table->read_set;
 
2427
      other_bitmap=   table->write_set;
1915
2428
    }
1916
2429
    else
1917
2430
    {
1918
2431
      current_bitmap= table->write_set;
 
2432
      other_bitmap=   table->read_set;
1919
2433
    }
1920
2434
 
1921
 
    //if (current_bitmap->testAndSet(field->position()))
1922
 
    if (current_bitmap->test(field->position()))
 
2435
    if (current_bitmap->testAndSet(field->field_index))
1923
2436
    {
1924
2437
      if (session->mark_used_columns == MARK_COLUMNS_WRITE)
1925
2438
        session->dup_field= field;
2153
2666
      */
2154
2667
      table_name && table_name[0] &&
2155
2668
      (my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
2156
 
       (db_name && db_name[0] && table_list->getSchemaName() && table_list->getSchemaName()[0] &&
2157
 
        strcmp(db_name, table_list->getSchemaName()))))
 
2669
       (db_name && db_name[0] && table_list->db && table_list->db[0] &&
 
2670
        strcmp(db_name, table_list->db))))
2158
2671
    return 0;
2159
2672
 
2160
2673
  *actual_table= NULL;
2229
2742
      {
2230
2743
        Table *table= field_to_set->getTable();
2231
2744
        if (session->mark_used_columns == MARK_COLUMNS_READ)
2232
 
          table->setReadSet(field_to_set->position());
 
2745
          table->setReadSet(field_to_set->field_index);
2233
2746
        else
2234
 
          table->setWriteSet(field_to_set->position());
 
2747
          table->setWriteSet(field_to_set->field_index);
2235
2748
      }
2236
2749
    }
2237
2750
  }
2787
3300
    /* true if field_name_1 is a member of using_fields */
2788
3301
    bool is_using_column_1;
2789
3302
    if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
2790
 
      return(result);
 
3303
      goto err;
2791
3304
    field_name_1= nj_col_1->name();
2792
3305
    is_using_column_1= using_fields &&
2793
3306
      test_if_string_in_list(field_name_1, using_fields);
2805
3318
      Natural_join_column *cur_nj_col_2;
2806
3319
      const char *cur_field_name_2;
2807
3320
      if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
2808
 
        return(result);
 
3321
        goto err;
2809
3322
      cur_field_name_2= cur_nj_col_2->name();
2810
3323
 
2811
3324
      /*
2825
3338
            (found && (!using_fields || is_using_column_1)))
2826
3339
        {
2827
3340
          my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where);
2828
 
          return(result);
 
3341
          goto err;
2829
3342
        }
2830
3343
        nj_col_2= cur_nj_col_2;
2831
3344
        found= true;
2858
3371
      Item_func_eq *eq_cond;
2859
3372
 
2860
3373
      if (!item_1 || !item_2)
2861
 
        return(result); // out of memory
 
3374
        goto err;                               // out of memory
2862
3375
 
2863
3376
      /*
2864
3377
        In the case of no_wrap_view_item == 0, the created items must be
2883
3396
      */
2884
3397
      if (set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref) ||
2885
3398
          set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref))
2886
 
        return(result);
 
3399
        goto err;
2887
3400
 
2888
3401
      if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
2889
 
        return(result);                               /* Out of memory. */
 
3402
        goto err;                               /* Out of memory. */
2890
3403
 
2891
3404
      /*
2892
3405
        Add the new equi-join condition to the ON clause. Notice that
2903
3416
      {
2904
3417
        Table *table_1= nj_col_1->table_ref->table;
2905
3418
        /* Mark field_1 used for table cache. */
2906
 
        table_1->setReadSet(field_1->position());
 
3419
        table_1->setReadSet(field_1->field_index);
2907
3420
        table_1->covering_keys&= field_1->part_of_key;
2908
3421
        table_1->merge_keys|= field_1->part_of_key;
2909
3422
      }
2911
3424
      {
2912
3425
        Table *table_2= nj_col_2->table_ref->table;
2913
3426
        /* Mark field_2 used for table cache. */
2914
 
        table_2->setReadSet(field_2->position());
 
3427
        table_2->setReadSet(field_2->field_index);
2915
3428
        table_2->covering_keys&= field_2->part_of_key;
2916
3429
        table_2->merge_keys|= field_2->part_of_key;
2917
3430
      }
2932
3445
  */
2933
3446
  result= false;
2934
3447
 
 
3448
err:
2935
3449
  return(result);
2936
3450
}
2937
3451
 
2989
3503
 
2990
3504
  if (!(non_join_columns= new List<Natural_join_column>) ||
2991
3505
      !(natural_using_join->join_columns= new List<Natural_join_column>))
2992
 
  {
2993
 
    return(result);
2994
 
  }
 
3506
    goto err;
2995
3507
 
2996
3508
  /* Append the columns of the first join operand. */
2997
3509
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
3030
3542
        {
3031
3543
          my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
3032
3544
                   session->where);
3033
 
          return(result);
 
3545
          goto err;
3034
3546
        }
3035
3547
        if (!my_strcasecmp(system_charset_info,
3036
3548
                           common_field->name(), using_field_name_ptr))
3058
3570
 
3059
3571
  result= false;
3060
3572
 
 
3573
err:
3061
3574
  return(result);
3062
3575
}
3063
3576
 
3143
3656
      if (cur_table_ref->getNestedJoin() &&
3144
3657
          store_top_level_join_columns(session, cur_table_ref,
3145
3658
                                       real_left_neighbor, real_right_neighbor))
3146
 
        return(result);
 
3659
        goto err;
3147
3660
      same_level_right_neighbor= cur_table_ref;
3148
3661
    }
3149
3662
  }
3175
3688
      std::swap(table_ref_1, table_ref_2);
3176
3689
    if (mark_common_columns(session, table_ref_1, table_ref_2,
3177
3690
                            using_fields, &found_using_fields))
3178
 
      return(result);
 
3691
      goto err;
3179
3692
 
3180
3693
    /*
3181
3694
      Swap the join operands back, so that we pick the columns of the second
3187
3700
    if (store_natural_using_join_columns(session, table_ref, table_ref_1,
3188
3701
                                         table_ref_2, using_fields,
3189
3702
                                         found_using_fields))
3190
 
      return(result);
 
3703
      goto err;
3191
3704
 
3192
3705
    /*
3193
3706
      Change NATURAL JOIN to JOIN ... ON. We do this for both operands
3220
3733
  }
3221
3734
  result= false; /* All is OK. */
3222
3735
 
 
3736
err:
3223
3737
  return(result);
3224
3738
}
3225
3739
 
3621
4135
    assert(tables->is_leaf_for_name_resolution());
3622
4136
 
3623
4137
    if ((table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias)) ||
3624
 
        (db_name && strcasecmp(tables->getSchemaName(),db_name)))
 
4138
        (db_name && strcasecmp(tables->db,db_name)))
3625
4139
      continue;
3626
4140
 
3627
4141
    /*
3657
4171
      if ((field= field_iterator.field()))
3658
4172
      {
3659
4173
        /* Mark fields as used to allow storage engine to optimze access */
3660
 
        field->getTable()->setReadSet(field->position());
 
4174
        field->getTable()->setReadSet(field->field_index);
3661
4175
        if (table)
3662
4176
        {
3663
4177
          table->covering_keys&= field->part_of_key;
3864
4378
    if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
3865
4379
    {
3866
4380
      my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
3867
 
      if (table)
3868
 
        table->auto_increment_field_not_null= false;
3869
 
 
3870
 
      return true;
 
4381
      goto err;
3871
4382
    }
3872
4383
  }
3873
4384
 
3874
4385
  return session->is_error();
 
4386
 
 
4387
err:
 
4388
  if (table)
 
4389
    table->auto_increment_field_not_null= false;
 
4390
 
 
4391
  return true;
3875
4392
}
3876
4393
 
3877
4394
 
3921
4438
    if (field == table->next_number_field)
3922
4439
      table->auto_increment_field_not_null= true;
3923
4440
    if (value->save_in_field(field, 0) < 0)
3924
 
    {
3925
 
      if (table)
3926
 
        table->auto_increment_field_not_null= false;
3927
 
 
3928
 
      return true;
3929
 
    }
 
4441
      goto err;
3930
4442
  }
3931
4443
 
3932
4444
  return(session->is_error());
 
4445
 
 
4446
err:
 
4447
  if (table)
 
4448
    table->auto_increment_field_not_null= false;
 
4449
 
 
4450
  return true;
3933
4451
}
3934
4452
 
3935
4453
 
3957
4475
  unireg support functions
3958
4476
 *****************************************************************************/
3959
4477
 
3960
 
 
 
4478
/*
 
4479
  Invalidate any cache entries that are for some DB
 
4480
 
 
4481
  SYNOPSIS
 
4482
  remove_db_from_cache()
 
4483
  db            Database name. This will be in lower case if
 
4484
  lower_case_table_name is set
 
4485
 
 
4486
NOTE:
 
4487
We can't use hash_delete when looping hash_elements. We mark them first
 
4488
and afterwards delete those marked unused.
 
4489
*/
 
4490
 
 
4491
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
 
4492
{
 
4493
  safe_mutex_assert_owner(&LOCK_open);
 
4494
 
 
4495
  for (TableOpenCache::const_iterator iter= get_open_cache().begin();
 
4496
       iter != get_open_cache().end();
 
4497
       iter++)
 
4498
  {
 
4499
    Table *table= (*iter).second;
 
4500
 
 
4501
    if (not schema_identifier.getPath().compare(table->getMutableShare()->getSchemaName()))
 
4502
    {
 
4503
      table->getMutableShare()->resetVersion();                 /* Free when thread is ready */
 
4504
      if (not table->in_use)
 
4505
        unused_tables.relink(table);
 
4506
    }
 
4507
  }
 
4508
 
 
4509
  unused_tables.cullByVersion();
 
4510
}
 
4511
 
 
4512
 
 
4513
/*
 
4514
  Mark all entries with the table as deleted to force an reopen of the table
 
4515
 
 
4516
  The table will be closed (not stored in cache) by the current thread when
 
4517
  close_thread_tables() is called.
 
4518
 
 
4519
  PREREQUISITES
 
4520
  Lock on LOCK_open()
 
4521
 
 
4522
  RETURN
 
4523
  0  This thread now have exclusive access to this table and no other thread
 
4524
  can access the table until close_thread_tables() is called.
 
4525
  1  Table is in use by another thread
 
4526
*/
 
4527
 
 
4528
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
 
4529
{
 
4530
  const TableIdentifier::Key &key(identifier.getKey());
 
4531
  bool result= false; 
 
4532
  bool signalled= false;
 
4533
 
 
4534
  for (;;)
 
4535
  {
 
4536
    result= signalled= false;
 
4537
 
 
4538
    TableOpenCacheRange ppp;
 
4539
    ppp= get_open_cache().equal_range(key);
 
4540
 
 
4541
    for (TableOpenCache::const_iterator iter= ppp.first;
 
4542
         iter != ppp.second; ++iter)
 
4543
    {
 
4544
      Table *table= (*iter).second;
 
4545
      Session *in_use;
 
4546
 
 
4547
      table->getMutableShare()->resetVersion();         /* Free when thread is ready */
 
4548
      if (!(in_use=table->in_use))
 
4549
      {
 
4550
        unused_tables.relink(table);
 
4551
      }
 
4552
      else if (in_use != session)
 
4553
      {
 
4554
        /*
 
4555
          Mark that table is going to be deleted from cache. This will
 
4556
          force threads that are in mysql_lock_tables() (but not yet
 
4557
          in thr_multi_lock()) to abort it's locks, close all tables and retry
 
4558
        */
 
4559
        in_use->some_tables_deleted= true;
 
4560
        if (table->is_name_opened())
 
4561
        {
 
4562
          result= true;
 
4563
        }
 
4564
        /*
 
4565
          Now we must abort all tables locks used by this thread
 
4566
          as the thread may be waiting to get a lock for another table.
 
4567
          Note that we need to hold LOCK_open while going through the
 
4568
          list. So that the other thread cannot change it. The other
 
4569
          thread must also hold LOCK_open whenever changing the
 
4570
          open_tables list. Aborting the MERGE lock after a child was
 
4571
          closed and before the parent is closed would be fatal.
 
4572
        */
 
4573
        for (Table *session_table= in_use->open_tables;
 
4574
             session_table ;
 
4575
             session_table= session_table->getNext())
 
4576
        {
 
4577
          /* Do not handle locks of MERGE children. */
 
4578
          if (session_table->db_stat)   // If table is open
 
4579
            signalled|= mysql_lock_abort_for_thread(session, session_table);
 
4580
        }
 
4581
      }
 
4582
      else
 
4583
        result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
 
4584
    }
 
4585
 
 
4586
    unused_tables.cullByVersion();
 
4587
 
 
4588
    /* Remove table from table definition cache if it's not in use */
 
4589
    TableShare::release(identifier);
 
4590
 
 
4591
    if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
 
4592
    {
 
4593
      /*
 
4594
        Signal any thread waiting for tables to be freed to
 
4595
        reopen their tables
 
4596
      */
 
4597
      broadcast_refresh();
 
4598
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
 
4599
      {
 
4600
        dropping_tables++;
 
4601
        if (likely(signalled))
 
4602
          (void) pthread_cond_wait(&COND_refresh, &LOCK_open);
 
4603
        else
 
4604
        {
 
4605
          struct timespec abstime;
 
4606
          /*
 
4607
            It can happen that another thread has opened the
 
4608
            table but has not yet locked any table at all. Since
 
4609
            it can be locked waiting for a table that our thread
 
4610
            has done LOCK Table x WRITE on previously, we need to
 
4611
            ensure that the thread actually hears our signal
 
4612
            before we go to sleep. Thus we wait for a short time
 
4613
            and then we retry another loop in the
 
4614
            remove_table_from_cache routine.
 
4615
          */
 
4616
          set_timespec(abstime, 10);
 
4617
          pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);
 
4618
        }
 
4619
        dropping_tables--;
 
4620
        continue;
 
4621
      }
 
4622
    }
 
4623
    break;
 
4624
  }
 
4625
 
 
4626
  return result;
 
4627
}
3961
4628
 
3962
4629
 
3963
4630
/**