~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/lock.cc

  • Committer: Monty Taylor
  • Date: 2008-10-05 01:41:06 UTC
  • Revision ID: monty@inaugust.com-20081005014106-bulqe4kp7i6ipts1
Moved qsort declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
#define GET_LOCK_UNLOCK         1
87
87
#define GET_LOCK_STORE_LOCKS    2
88
88
 
89
 
static DRIZZLE_LOCK *get_lock_data(THD *thd, Table **table,uint32_t count,
90
 
                                 uint32_t flags, Table **write_locked);
91
 
static int lock_external(THD *thd, Table **table,uint32_t count);
92
 
static int unlock_external(THD *thd, Table **table,uint32_t count);
 
89
static DRIZZLE_LOCK *get_lock_data(THD *thd, Table **table,uint count,
 
90
                                 uint flags, Table **write_locked);
 
91
static int lock_external(THD *thd, Table **table,uint count);
 
92
static int unlock_external(THD *thd, Table **table,uint count);
93
93
static void print_lock_error(int error, const char *);
94
94
 
95
95
/*
155
155
    /* Reset lock type. */
156
156
    (*ldata)->type= TL_UNLOCK;
157
157
  }
158
 
  free((unsigned char*) sql_lock);
 
158
  my_free((uchar*) sql_lock, MYF(0));
159
159
  *mysql_lock= 0;
160
160
}
161
161
 
162
162
 
163
 
DRIZZLE_LOCK *mysql_lock_tables(THD *thd, Table **tables, uint32_t count,
164
 
                              uint32_t flags, bool *need_reopen)
 
163
DRIZZLE_LOCK *mysql_lock_tables(THD *thd, Table **tables, uint count,
 
164
                              uint flags, bool *need_reopen)
165
165
{
166
166
  DRIZZLE_LOCK *sql_lock;
167
167
  Table *write_lock_used;
210
210
      break;
211
211
    }
212
212
 
213
 
    thd->set_proc_info("System lock");
 
213
    thd_proc_info(thd, "System lock");
214
214
    if (sql_lock->table_count && lock_external(thd, sql_lock->table,
215
215
                                               sql_lock->table_count))
216
216
    {
218
218
      reset_lock_data_and_free(&sql_lock);
219
219
      break;
220
220
    }
221
 
    thd->set_proc_info("Table lock");
 
221
    thd_proc_info(thd, "Table lock");
222
222
    /* Copy the lock data array. thr_multi_lock() reorders its contens. */
223
223
    memcpy(sql_lock->locks + sql_lock->lock_count, sql_lock->locks,
224
224
           sql_lock->lock_count * sizeof(*sql_lock->locks));
255
255
      thd->some_tables_deleted=0;
256
256
      break;
257
257
    }
258
 
    thd->set_proc_info(0);
 
258
    thd_proc_info(thd, 0);
259
259
 
260
260
    /* going to retry, unlock all tables */
261
261
    if (sql_lock->lock_count)
279
279
    if (wait_for_tables(thd))
280
280
      break;                                    // Couldn't open tables
281
281
  }
282
 
  thd->set_proc_info(0);
 
282
  thd_proc_info(thd, 0);
283
283
  if (thd->killed)
284
284
  {
285
285
    thd->send_kill_message();
295
295
}
296
296
 
297
297
 
298
 
static int lock_external(THD *thd, Table **tables, uint32_t count)
 
298
static int lock_external(THD *thd, Table **tables, uint count)
299
299
{
300
 
  register uint32_t i;
 
300
  register uint i;
301
301
  int lock_type,error;
302
302
  for (i=1 ; i <= count ; i++, tables++)
303
303
  {
335
335
    thr_multi_unlock(sql_lock->locks,sql_lock->lock_count);
336
336
  if (sql_lock->table_count)
337
337
    unlock_external(thd,sql_lock->table,sql_lock->table_count);
338
 
  free((unsigned char*) sql_lock);
 
338
  my_free((uchar*) sql_lock,MYF(0));
339
339
  return;
340
340
}
341
341
 
345
345
  This will work even if get_lock_data fails (next unlock will free all)
346
346
*/
347
347
 
348
 
void mysql_unlock_some_tables(THD *thd, Table **table,uint32_t count)
 
348
void mysql_unlock_some_tables(THD *thd, Table **table,uint count)
349
349
{
350
350
  DRIZZLE_LOCK *sql_lock;
351
351
  Table *write_lock_used;
361
361
 
362
362
void mysql_unlock_read_tables(THD *thd, DRIZZLE_LOCK *sql_lock)
363
363
{
364
 
  uint32_t i,found;
 
364
  uint i,found;
365
365
 
366
366
  /* Move all write locks first */
367
367
  THR_LOCK_DATA **lock=sql_lock->locks;
442
442
    mysql_unlock_some_tables(thd, &table, /* table count */ 1);
443
443
  if (locked)
444
444
  {
445
 
    register uint32_t i;
 
445
    register uint i;
446
446
    for (i=0; i < locked->table_count; i++)
447
447
    {
448
448
      if (locked->table[i] == table)
449
449
      {
450
 
        uint32_t  j, removed_locks, old_tables;
 
450
        uint  j, removed_locks, old_tables;
451
451
        Table *tbl;
452
 
        uint32_t lock_data_end;
 
452
        uint lock_data_end;
453
453
 
454
454
        assert(table->lock_position == i);
455
455
 
508
508
  if ((locked = get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
509
509
                              &write_lock_used)))
510
510
  {
511
 
    for (uint32_t i=0; i < locked->lock_count; i++)
 
511
    for (uint i=0; i < locked->lock_count; i++)
512
512
      thr_downgrade_write_lock(locked->locks[i], new_lock_type);
513
 
    free((unsigned char*) locked);
 
513
    my_free((uchar*) locked,MYF(0));
514
514
  }
515
515
}
516
516
 
525
525
  if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
526
526
                             &write_lock_used)))
527
527
  {
528
 
    for (uint32_t i=0; i < locked->lock_count; i++)
 
528
    for (uint i=0; i < locked->lock_count; i++)
529
529
      thr_abort_locks(locked->locks[i]->lock, upgrade_lock);
530
 
    free((unsigned char*) locked);
 
530
    my_free((uchar*) locked,MYF(0));
531
531
  }
532
532
  return;
533
533
}
554
554
  if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
555
555
                             &write_lock_used)))
556
556
  {
557
 
    for (uint32_t i=0; i < locked->lock_count; i++)
 
557
    for (uint i=0; i < locked->lock_count; i++)
558
558
    {
559
559
      if (thr_abort_locks_for_thread(locked->locks[i]->lock,
560
560
                                     table->in_use->thread_id))
561
561
        result= true;
562
562
    }
563
 
    free((unsigned char*) locked);
 
563
    my_free((uchar*) locked,MYF(0));
564
564
  }
565
565
  return(result);
566
566
}
601
601
  }
602
602
 
603
603
  /* Delete old, not needed locks */
604
 
  free((unsigned char*) a);
605
 
  free((unsigned char*) b);
 
604
  my_free((uchar*) a,MYF(0));
 
605
  my_free((uchar*) b,MYF(0));
606
606
  return(sql_lock);
607
607
}
608
608
 
710
710
 
711
711
/** Unlock a set of external. */
712
712
 
713
 
static int unlock_external(THD *thd, Table **table,uint32_t count)
 
713
static int unlock_external(THD *thd, Table **table,uint count)
714
714
{
715
715
  int error,error_code;
716
716
 
743
743
  @param write_lock_used   Store pointer to last table with WRITE_ALLOW_WRITE
744
744
*/
745
745
 
746
 
static DRIZZLE_LOCK *get_lock_data(THD *thd, Table **table_ptr, uint32_t count,
747
 
                                 uint32_t flags, Table **write_lock_used)
 
746
static DRIZZLE_LOCK *get_lock_data(THD *thd, Table **table_ptr, uint count,
 
747
                                 uint flags, Table **write_lock_used)
748
748
{
749
 
  uint32_t i,tables,lock_count;
 
749
  uint i,tables,lock_count;
750
750
  DRIZZLE_LOCK *sql_lock;
751
751
  THR_LOCK_DATA **locks, **locks_buf, **locks_start;
752
752
  Table **to, **table_buf;
910
910
  Table *table;
911
911
  char  key[MAX_DBKEY_LENGTH];
912
912
  char *db= table_list->db;
913
 
  uint32_t  key_length;
 
913
  uint  key_length;
914
914
  bool  found_locked_table= false;
915
915
  HASH_SEARCH_STATE state;
916
916
 
919
919
  if (check_in_use)
920
920
  {
921
921
    /* Only insert the table if we haven't insert it already */
922
 
    for (table=(Table*) hash_first(&open_cache, (unsigned char*)key,
 
922
    for (table=(Table*) hash_first(&open_cache, (uchar*)key,
923
923
                                   key_length, &state);
924
924
         table ;
925
 
         table = (Table*) hash_next(&open_cache,(unsigned char*) key,
 
925
         table = (Table*) hash_next(&open_cache,(uchar*) key,
926
926
                                    key_length, &state))
927
927
    {
928
928
      if (table->reginfo.lock_type < TL_WRITE)
968
968
{
969
969
  if (table_list->table)
970
970
  {
971
 
    hash_delete(&open_cache, (unsigned char*) table_list->table);
 
971
    hash_delete(&open_cache, (uchar*) table_list->table);
972
972
    broadcast_refresh();
973
973
  }
974
974
}
1114
1114
                                                TableList *table_list)
1115
1115
{
1116
1116
  char  key[MAX_DBKEY_LENGTH];
1117
 
  uint32_t  key_length;
 
1117
  uint  key_length;
1118
1118
 
1119
1119
  key_length= create_table_def_key(thd, key, table_list, 0);
1120
1120
 
1121
 
  return is_table_name_exclusively_locked_by_this_thread(thd, (unsigned char *)key,
 
1121
  return is_table_name_exclusively_locked_by_this_thread(thd, (uchar *)key,
1122
1122
                                                         key_length);
1123
1123
}
1124
1124
 
1137
1137
 */
1138
1138
 
1139
1139
bool
1140
 
is_table_name_exclusively_locked_by_this_thread(THD *thd, unsigned char *key,
 
1140
is_table_name_exclusively_locked_by_this_thread(THD *thd, uchar *key,
1141
1141
                                                int key_length)
1142
1142
{
1143
1143
  HASH_SEARCH_STATE state;
1300
1300
 
1301
1301
****************************************************************************/
1302
1302
 
1303
 
volatile uint32_t global_read_lock=0;
1304
 
volatile uint32_t global_read_lock_blocks_commit=0;
1305
 
static volatile uint32_t protect_against_global_read_lock=0;
1306
 
static volatile uint32_t waiting_for_read_lock=0;
 
1303
volatile uint global_read_lock=0;
 
1304
volatile uint global_read_lock_blocks_commit=0;
 
1305
static volatile uint protect_against_global_read_lock=0;
 
1306
static volatile uint waiting_for_read_lock=0;
1307
1307
 
1308
1308
#define GOT_GLOBAL_READ_LOCK               1
1309
1309
#define MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT 2
1344
1344
 
1345
1345
void unlock_global_read_lock(THD *thd)
1346
1346
{
1347
 
  uint32_t tmp;
 
1347
  uint tmp;
1348
1348
 
1349
1349
  pthread_mutex_lock(&LOCK_global_read_lock);
1350
1350
  tmp= --global_read_lock;
1508
1508
 
1509
1509
int try_transactional_lock(THD *thd, TableList *table_list)
1510
1510
{
1511
 
  uint32_t          dummy_counter;
 
1511
  uint          dummy_counter;
1512
1512
  int           error;
1513
1513
  int           result= 0;
1514
1514