~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2010-11-23 07:02:38 UTC
  • mfrom: (1945.2.1 quick)
  • Revision ID: brian@tangent.org-20101123070238-1f31pgg7gvmytpr3
Refactor basic lock bits (ie, encapsulate!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
155
155
 
156
156
  @param session Thread context (may be NULL)
157
157
  @param tables List of tables to remove from the cache
158
 
  @param have_lock If LOCK_open is locked
 
158
  @param have_lock If table::Cache::singleton().mutex() is locked
159
159
  @param wait_for_refresh Wait for a impending flush
160
160
  @param wait_for_placeholders Wait for tables being reopened so that the GRL
161
161
  won't proceed while write-locked tables are being reopened by other
171
171
  Session *session= this;
172
172
 
173
173
  {
174
 
    LOCK_open.lock(); /* Optionally lock for remove tables from open_cahe if not in use */
 
174
    table::Cache::singleton().mutex().lock(); /* Optionally lock for remove tables from open_cahe if not in use */
175
175
 
176
176
    if (tables == NULL)
177
177
    {
250
250
        If there is any table that has a lower refresh_version, wait until
251
251
        this is closed (or this thread is killed) before returning
252
252
      */
253
 
      session->mysys_var->current_mutex= &LOCK_open;
 
253
      session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
254
254
      session->mysys_var->current_cond= &COND_refresh;
255
255
      session->set_proc_info("Flushing tables");
256
256
 
287
287
                                                     (table->open_placeholder && wait_for_placeholders)))
288
288
          {
289
289
            found= true;
290
 
            boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
 
290
            boost_unique_lock_t scoped(table::Cache::singleton().mutex(), boost::adopt_lock_t());
291
291
            COND_refresh.wait(scoped);
292
292
            scoped.release();
293
293
            break;
313
313
      }
314
314
    }
315
315
 
316
 
    LOCK_open.unlock();
 
316
    table::Cache::singleton().mutex().unlock();
317
317
  }
318
318
 
319
319
  if (wait_for_refresh)
337
337
  bool found_old_table= false;
338
338
  table::Concurrent *table= static_cast<table::Concurrent *>(open_tables);
339
339
 
340
 
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
340
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
341
341
  assert(table->key_read == 0);
342
342
  assert(!table->cursor || table->cursor->inited == Cursor::NONE);
343
343
 
380
380
{
381
381
  bool found_old_table= false;
382
382
 
383
 
  safe_mutex_assert_not_owner(LOCK_open.native_handle());
 
383
  safe_mutex_assert_not_owner(table::Cache::singleton().mutex().native_handle());
384
384
 
385
 
  boost_unique_lock_t scoped_lock(LOCK_open); /* Close all open tables on Session */
 
385
  boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close all open tables on Session */
386
386
 
387
387
  while (open_tables)
388
388
  {
664
664
{
665
665
  const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
666
666
  Table **prev;
667
 
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
667
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
668
668
 
669
669
  /*
670
 
    Note that we need to hold LOCK_open while changing the
 
670
    Note that we need to hold table::Cache::singleton().mutex() while changing the
671
671
    open_tables list. Another thread may work on it.
672
672
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
673
673
    Closing a MERGE child before the parent would be fatal if the
724
724
  }
725
725
  else
726
726
  {
727
 
    boost_unique_lock_t scoped_lock(LOCK_open); /* Close and drop a table (AUX routine) */
 
727
    boost_unique_lock_t scoped_lock(table::Cache::singleton().mutex()); /* Close and drop a table (AUX routine) */
728
728
    /*
729
729
      unlink_open_table() also tells threads waiting for refresh or close
730
730
      that something has happened.
793
793
 
794
794
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
795
795
{
796
 
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
796
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
797
797
 
798
798
  /*
799
799
    Create a table entry with the right key and with an old refresh version
837
837
{
838
838
  const TableIdentifier::Key &key(identifier.getKey());
839
839
 
840
 
  boost_unique_lock_t scope_lock(LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table)  */
 
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
841
 
842
842
  table::CacheMap::iterator iter;
843
843
 
994
994
      until no one holds a name lock on the table.
995
995
      - if there is no such Table in the name cache, read the table definition
996
996
      and insert it into the cache.
997
 
      We perform all of the above under LOCK_open which currently protects
 
997
      We perform all of the above under table::Cache::singleton().mutex() which currently protects
998
998
      the open cache (also known as table cache) and table definitions stored
999
999
      on disk.
1000
1000
    */
1001
1001
 
1002
1002
    {
1003
 
      LOCK_open.lock(); /* Lock for FLUSH TABLES for open table */
 
1003
      table::Cache::singleton().mutex().lock(); /* Lock for FLUSH TABLES for open table */
1004
1004
 
1005
1005
      /*
1006
1006
        Actually try to find the table in the open_cache.
1052
1052
          /* Avoid self-deadlocks by detecting self-dependencies. */
1053
1053
          if (table->open_placeholder && table->in_use == this)
1054
1054
          {
1055
 
            LOCK_open.unlock();
 
1055
            table::Cache::singleton().mutex().unlock();
1056
1056
            my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1057
1057
            return NULL;
1058
1058
          }
1085
1085
          */
1086
1086
          if (table->in_use != this)
1087
1087
          {
1088
 
            /* wait_for_conditionwill unlock LOCK_open for us */
1089
 
            wait_for_condition(LOCK_open, COND_refresh);
 
1088
            /* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
 
1089
            wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1090
1090
          }
1091
1091
          else
1092
1092
          {
1093
 
            LOCK_open.unlock();
 
1093
            table::Cache::singleton().mutex().unlock();
1094
1094
          }
1095
1095
          /*
1096
1096
            There is a refresh in progress for this table.
1124
1124
            */
1125
1125
            if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1126
1126
            {
1127
 
              LOCK_open.unlock();
 
1127
              table::Cache::singleton().mutex().unlock();
1128
1128
              return NULL;
1129
1129
            }
1130
1130
            /*
1135
1135
            table->open_placeholder= true;
1136
1136
            table->setNext(open_tables);
1137
1137
            open_tables= table;
1138
 
            LOCK_open.unlock();
 
1138
            table::Cache::singleton().mutex().unlock();
1139
1139
 
1140
1140
            return table ;
1141
1141
          }
1148
1148
          table= new_table;
1149
1149
          if (new_table == NULL)
1150
1150
          {
1151
 
            LOCK_open.unlock();
 
1151
            table::Cache::singleton().mutex().unlock();
1152
1152
            return NULL;
1153
1153
          }
1154
1154
 
1156
1156
          if (error != 0)
1157
1157
          {
1158
1158
            delete new_table;
1159
 
            LOCK_open.unlock();
 
1159
            table::Cache::singleton().mutex().unlock();
1160
1160
            return NULL;
1161
1161
          }
1162
1162
          (void)table::Cache::singleton().insert(new_table);
1163
1163
        }
1164
1164
      }
1165
1165
 
1166
 
      LOCK_open.unlock();
 
1166
      table::Cache::singleton().mutex().unlock();
1167
1167
    }
1168
1168
    if (refresh)
1169
1169
    {
1224
1224
 
1225
1225
void Session::close_data_files_and_morph_locks(TableIdentifier &identifier)
1226
1226
{
1227
 
  safe_mutex_assert_owner(LOCK_open.native_handle()); /* Adjust locks at the end of ALTER TABLEL */
 
1227
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
1228
1228
 
1229
1229
  if (lock)
1230
1230
  {
1267
1267
  combination when one needs tables to be reopened (for
1268
1268
  example see openTablesLock()).
1269
1269
 
1270
 
  @note One should have lock on LOCK_open when calling this.
 
1270
  @note One should have lock on table::Cache::singleton().mutex() when calling this.
1271
1271
 
1272
1272
  @return false in case of success, true - otherwise.
1273
1273
*/
1284
1284
  if (open_tables == NULL)
1285
1285
    return false;
1286
1286
 
1287
 
  safe_mutex_assert_owner(LOCK_open.native_handle());
 
1287
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
1288
1288
  if (get_locks)
1289
1289
  {
1290
1290
    /*
1320
1320
    DrizzleLock *local_lock;
1321
1321
    /*
1322
1322
      We should always get these locks. Anyway, we must not go into
1323
 
      wait_for_tables() as it tries to acquire LOCK_open, which is
 
1323
      wait_for_tables() as it tries to acquire table::Cache::singleton().mutex(), which is
1324
1324
      already locked.
1325
1325
    */
1326
1326
    some_tables_deleted= false;
1445
1445
 
1446
1446
  session->set_proc_info("Waiting for tables");
1447
1447
  {
1448
 
    boost_unique_lock_t lock(LOCK_open);
 
1448
    boost_unique_lock_t lock(table::Cache::singleton().mutex());
1449
1449
    while (not session->getKilled())
1450
1450
    {
1451
1451
      session->some_tables_deleted= false;
1502
1502
  prev= &session->open_tables;
1503
1503
 
1504
1504
  /*
1505
 
    Note that we need to hold LOCK_open while changing the
 
1505
    Note that we need to hold table::Cache::singleton().mutex() while changing the
1506
1506
    open_tables list. Another thread may work on it.
1507
1507
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
1508
1508
    Closing a MERGE child before the parent would be fatal if the