147
145
TABLE *t= tables[i];
149
147
/* Protect against 'fake' partially initialized TABLE_SHARE */
150
DBUG_ASSERT(t->s->table_category != TABLE_UNKNOWN_CATEGORY);
148
assert(t->s->table_category != TABLE_UNKNOWN_CATEGORY);
152
150
if ((t->s->table_category == TABLE_CATEGORY_SYSTEM) &&
153
151
(t->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE))
267
263
thd_proc_info(thd, "System lock");
268
DBUG_PRINT("info", ("thd->proc_info %s", thd->proc_info));
269
264
if (sql_lock->table_count && lock_external(thd, sql_lock->table,
270
265
sql_lock->table_count))
276
271
thd_proc_info(thd, "Table lock");
277
DBUG_PRINT("info", ("thd->proc_info %s", thd->proc_info));
278
272
/* Copy the lock data array. thr_multi_lock() reorders its contens. */
279
273
memcpy(sql_lock->locks + sql_lock->lock_count, sql_lock->locks,
280
274
sql_lock->lock_count * sizeof(*sql_lock->locks));
357
351
int lock_type,error;
358
DBUG_ENTER("lock_external");
360
DBUG_PRINT("info", ("count %d", count));
361
352
for (i=1 ; i <= count ; i++, tables++)
363
DBUG_ASSERT((*tables)->reginfo.lock_type >= TL_READ);
354
assert((*tables)->reginfo.lock_type >= TL_READ);
364
355
lock_type=F_WRLCK; /* Lock exclusive */
365
356
if ((*tables)->db_stat & HA_READ_ONLY ||
366
357
((*tables)->reginfo.lock_type >= TL_READ &&
384
375
(*tables)->current_lock= lock_type;
391
382
void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock)
393
DBUG_ENTER("mysql_unlock_tables");
394
384
if (sql_lock->lock_count)
395
385
thr_multi_unlock(sql_lock->locks,sql_lock->lock_count);
396
386
if (sql_lock->table_count)
397
387
VOID(unlock_external(thd,sql_lock->table,sql_lock->table_count));
398
388
my_free((uchar*) sql_lock,MYF(0));
422
412
void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock)
425
DBUG_ENTER("mysql_unlock_read_tables");
427
416
/* Move all write locks first */
428
417
THR_LOCK_DATA **lock=sql_lock->locks;
447
436
TABLE **table=sql_lock->table;
448
437
for (i=found=0 ; i < sql_lock->table_count ; i++)
450
DBUG_ASSERT(sql_lock->table[i]->lock_position == i);
439
assert(sql_lock->table[i]->lock_position == i);
451
440
if ((uint) sql_lock->table[i]->reginfo.lock_type >= TL_WRITE_ALLOW_READ)
453
442
swap_variables(TABLE *, *table, sql_lock->table[i]);
499
488
void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table,
500
489
bool always_unlock)
502
if (always_unlock == TRUE)
491
if (always_unlock == true)
503
492
mysql_unlock_some_tables(thd, &table, /* table count */ 1);
513
502
uint lock_data_end;
515
DBUG_ASSERT(table->lock_position == i);
504
assert(table->lock_position == i);
517
506
/* Unlock if not yet unlocked */
518
if (always_unlock == FALSE)
507
if (always_unlock == false)
519
508
mysql_unlock_some_tables(thd, &table, /* table count */ 1);
521
510
/* Decrement table_count in advance, making below expressions easier */
635
622
MYSQL_LOCK *sql_lock;
636
623
TABLE **table, **end_table;
637
DBUG_ENTER("mysql_lock_merge");
639
625
if (!(sql_lock= (MYSQL_LOCK*)
640
626
my_malloc(sizeof(*sql_lock)+
641
627
sizeof(THR_LOCK_DATA*)*(a->lock_count+b->lock_count)+
642
628
sizeof(TABLE*)*(a->table_count+b->table_count),MYF(MY_WME))))
643
DBUG_RETURN(0); // Fatal error
629
return(0); // Fatal error
644
630
sql_lock->lock_count=a->lock_count+b->lock_count;
645
631
sql_lock->table_count=a->table_count+b->table_count;
646
632
sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1);
707
693
THR_LOCK_DATA **end_data;
708
694
THR_LOCK_DATA **lock_data2;
709
695
THR_LOCK_DATA **end_data2;
710
DBUG_ENTER("mysql_lock_have_duplicate");
713
698
Table may not be defined for derived or view tables.
732
717
lock_tables= mylock->table;
734
719
/* Prepare table related variables that don't change in loop. */
735
DBUG_ASSERT((table->lock_position < mylock->table_count) &&
720
assert((table->lock_position < mylock->table_count) &&
736
721
(table == lock_tables[table->lock_position]));
737
722
table_lock_data= lock_locks + table->lock_data_start;
738
723
end_data= table_lock_data + table->lock_count;
748
733
/* All tables in list must be in lock. */
749
DBUG_ASSERT((table2->lock_position < mylock->table_count) &&
734
assert((table2->lock_position < mylock->table_count) &&
750
735
(table2 == lock_tables[table2->lock_position]));
752
737
for (lock_data2= lock_locks + table2->lock_data_start,
764
749
if ((*lock_data)->lock == lock2)
766
DBUG_PRINT("info", ("haystack match: '%s'", haystack->table_name));
767
DBUG_RETURN(haystack);
774
DBUG_PRINT("info", ("no duplicate found"));
819
801
MYSQL_LOCK *sql_lock;
820
802
THR_LOCK_DATA **locks, **locks_buf, **locks_start;
821
803
TABLE **to, **table_buf;
822
DBUG_ENTER("get_lock_data");
824
DBUG_ASSERT((flags == GET_LOCK_UNLOCK) || (flags == GET_LOCK_STORE_LOCKS));
826
DBUG_PRINT("info", ("count %d", count));
805
assert((flags == GET_LOCK_UNLOCK) || (flags == GET_LOCK_STORE_LOCKS));
827
807
*write_lock_used=0;
828
808
for (i=tables=lock_count=0 ; i < count ; i++)
847
827
sizeof(THR_LOCK_DATA*) * tables * 2 +
848
828
sizeof(table_ptr) * lock_count,
851
831
locks= locks_buf= sql_lock->locks= (THR_LOCK_DATA**) (sql_lock + 1);
852
832
to= table_buf= sql_lock->table= (TABLE**) (locks + tables * 2);
853
833
sql_lock->table_count=lock_count;
860
840
if ((table=table_ptr[i])->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
862
842
lock_type= table->reginfo.lock_type;
863
DBUG_ASSERT (lock_type != TL_WRITE_DEFAULT);
843
assert (lock_type != TL_WRITE_DEFAULT);
864
844
if (lock_type >= TL_WRITE_ALLOW_WRITE)
866
846
*write_lock_used=table;
904
884
And in the FLUSH case, the memory is released quickly anyway.
906
886
sql_lock->lock_count= locks - locks_buf;
907
DBUG_PRINT("info", ("sql_lock->table_count %d sql_lock->lock_count %d",
908
sql_lock->table_count, sql_lock->lock_count));
909
DBUG_RETURN(sql_lock);
936
914
int lock_retcode;
938
DBUG_ENTER("lock_and_wait_for_table_name");
940
917
if (wait_if_global_read_lock(thd, 0, 1))
942
919
VOID(pthread_mutex_lock(&LOCK_open));
943
if ((lock_retcode = lock_table_name(thd, table_list, TRUE)) < 0)
920
if ((lock_retcode = lock_table_name(thd, table_list, true)) < 0)
945
922
if (lock_retcode && wait_for_locked_table_names(thd, table_list))
989
966
char key[MAX_DBKEY_LENGTH];
990
967
char *db= table_list->db;
992
bool found_locked_table= FALSE;
969
bool found_locked_table= false;
993
970
HASH_SEARCH_STATE state;
994
DBUG_ENTER("lock_table_name");
995
DBUG_PRINT("enter",("db: %s name: %s", db, table_list->table_name));
997
972
key_length= create_table_def_key(thd, key, table_list, 0);
1008
983
if (table->reginfo.lock_type < TL_WRITE)
1010
985
if (table->in_use == thd)
1011
found_locked_table= TRUE;
986
found_locked_table= true;
1015
990
if (table->in_use == thd)
1017
DBUG_PRINT("info", ("Table is in use"));
1018
992
table->s->version= 0; // Ensure no one can use this
1019
993
table->locked_by_name= 1;
1031
1005
my_error(ER_TABLE_NOT_LOCKED, MYF(0), table_list->alias);
1036
1010
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
1039
1013
table_list->table=table;
1041
1015
/* Return 1 if table is in use */
1042
DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
1016
return(test(remove_table_from_cache(thd, db, table_list->table_name,
1043
1017
check_in_use ? RTFC_NO_FLAG : RTFC_WAIT_OTHER_THREAD_FLAG)));
1124
1097
for (lock_table= table_list; lock_table; lock_table= lock_table->next_local)
1127
if ((got_lock=lock_table_name(thd,lock_table, TRUE)) < 0)
1100
if ((got_lock=lock_table_name(thd,lock_table, true)) < 0)
1128
1101
goto end; // Fatal error
1130
1103
got_all_locks=0; // Someone is using table
1267
1240
void unlock_table_names(THD *thd, TABLE_LIST *table_list,
1268
1241
TABLE_LIST *last_table)
1270
DBUG_ENTER("unlock_table_names");
1271
1243
for (TABLE_LIST *table= table_list;
1272
1244
table != last_table;
1273
1245
table= table->next_local)
1274
1246
unlock_table_name(thd,table);
1275
1247
broadcast_refresh();
1280
1252
static void print_lock_error(int error, const char *table)
1283
DBUG_ENTER("print_lock_error");
1285
1256
switch (error) {
1286
1257
case HA_ERR_LOCK_WAIT_TIMEOUT:
1395
1366
bool lock_global_read_lock(THD *thd)
1397
DBUG_ENTER("lock_global_read_lock");
1399
1368
if (!thd->global_read_lock)
1401
1370
const char *old_message;
1402
1371
(void) pthread_mutex_lock(&LOCK_global_read_lock);
1403
1372
old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
1404
1373
"Waiting to get readlock");
1406
("waiting_for: %d protect_against: %d",
1407
waiting_for_read_lock, protect_against_global_read_lock));
1409
1375
waiting_for_read_lock++;
1410
1376
while (protect_against_global_read_lock && !thd->killed)
1427
1393
forbid it before, or we can have a 3-thread deadlock if 2 do SELECT FOR
1428
1394
UPDATE and one does FLUSH TABLES WITH READ LOCK).
1434
1400
void unlock_global_read_lock(THD *thd)
1437
DBUG_ENTER("unlock_global_read_lock");
1439
("global_read_lock: %u global_read_lock_blocks_commit: %u",
1440
global_read_lock, global_read_lock_blocks_commit));
1442
1404
pthread_mutex_lock(&LOCK_global_read_lock);
1443
1405
tmp= --global_read_lock;
1447
1409
/* Send the signal outside the mutex to avoid a context switch */
1450
DBUG_PRINT("signal", ("Broadcasting COND_global_read_lock"));
1451
1412
pthread_cond_broadcast(&COND_global_read_lock);
1453
1414
thd->global_read_lock= 0;
1458
1419
#define must_wait (global_read_lock && \
1465
1426
const char *old_message= NULL;
1466
1427
bool result= 0, need_exit_cond;
1467
DBUG_ENTER("wait_if_global_read_lock");
1470
1430
Assert that we do not own LOCK_open. If we would own it, other
1487
1447
This allowance is needed to not break existing versions of innobackup
1488
1448
which do a BEGIN; INSERT; FLUSH TABLES WITH READ LOCK; COMMIT.
1490
DBUG_RETURN(is_not_commit);
1450
return(is_not_commit);
1492
1452
old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
1493
1453
"Waiting for release of readlock");
1494
1454
while (must_wait && ! thd->killed &&
1495
1455
(!abort_on_refresh || thd->version == refresh_version))
1497
DBUG_PRINT("signal", ("Waiting for COND_global_read_lock"));
1498
1457
(void) pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
1499
DBUG_PRINT("signal", ("Got COND_global_read_lock"));
1501
1459
if (thd->killed)
1511
1469
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
1513
1471
pthread_mutex_unlock(&LOCK_global_read_lock);
1514
DBUG_RETURN(result);
1518
1476
void start_waiting_global_read_lock(THD *thd)
1521
DBUG_ENTER("start_waiting_global_read_lock");
1522
1479
if (unlikely(thd->global_read_lock))
1524
1481
(void) pthread_mutex_lock(&LOCK_global_read_lock);
1525
1482
tmp= (!--protect_against_global_read_lock &&
1526
1483
(waiting_for_read_lock || global_read_lock_blocks_commit));
1527
1484
(void) pthread_mutex_unlock(&LOCK_global_read_lock);
1529
1486
pthread_cond_broadcast(&COND_global_read_lock);
1537
1494
const char *old_message;
1538
DBUG_ENTER("make_global_read_lock_block_commit");
1540
1496
If we didn't succeed lock_global_read_lock(), or if we already suceeded
1541
1497
make_global_read_lock_block_commit(), do nothing.
1543
1499
if (thd->global_read_lock != GOT_GLOBAL_READ_LOCK)
1545
1501
pthread_mutex_lock(&LOCK_global_read_lock);
1546
1502
/* increment this BEFORE waiting on cond (otherwise race cond) */
1547
1503
global_read_lock_blocks_commit++;
1548
1504
/* For testing we set up some blocking, to see if we can be killed */
1549
DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
1550
protect_against_global_read_lock++;);
1505
protect_against_global_read_lock++;
1551
1506
old_message= thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
1552
1507
"Waiting for all running commits to finish");
1553
1508
while (protect_against_global_read_lock && !thd->killed)
1554
1509
pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
1555
DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
1556
protect_against_global_read_lock--;);
1510
protect_against_global_read_lock--;
1557
1511
if ((error= test(thd->killed)))
1558
1512
global_read_lock_blocks_commit--; // undo what we did
1560
1514
thd->global_read_lock= MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT;
1561
1515
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
1612
1566
uint dummy_counter;
1615
DBUG_ENTER("try_transactional_lock");
1617
1570
/* Need to open the tables to be able to access engine methods. */
1618
1571
if (open_tables(thd, &table_list, &dummy_counter, 0))
1620
1573
/* purecov: begin tested */
1621
DBUG_PRINT("lock_info", ("aborting, open_tables failed"));
1623
1575
/* purecov: end */
1626
1578
/* Required by InnoDB. */
1627
thd->in_lock_tables= TRUE;
1579
thd->in_lock_tables= true;
1629
if ((error= set_handler_table_locks(thd, table_list, TRUE)))
1581
if ((error= set_handler_table_locks(thd, table_list, true)))
1632
1584
Not all transactional locks could be taken. If the error was
1654
1604
(void) ha_autocommit_or_rollback(thd, 0);
1655
1605
/* Close the tables. The locks (if taken) persist in the storage engines. */
1656
1606
close_tables_for_reopen(thd, &table_list);
1657
thd->in_lock_tables= FALSE;
1658
DBUG_PRINT("lock_info", ("result: %d", result));
1659
DBUG_RETURN(result);
1607
thd->in_lock_tables= false;
1690
1639
TABLE_LIST *tlist;
1692
1641
char warn_buff[MYSQL_ERRMSG_SIZE];
1693
DBUG_ENTER("check_transactional_lock");
1695
1643
for (tlist= table_list; tlist; tlist= tlist->next_global)
1697
DBUG_PRINT("lock_info", ("checking table: '%s'", tlist->table_name));
1700
1647
Unfortunately we cannot use tlist->placeholder() here. This method
1769
1710
if (tlist->placeholder())
1772
DBUG_ASSERT((tlist->lock_type == TL_READ) ||
1713
assert((tlist->lock_type == TL_READ) ||
1773
1714
(tlist->lock_type == TL_READ_NO_INSERT) ||
1774
1715
(tlist->lock_type == TL_WRITE_DEFAULT) ||
1775
1716
(tlist->lock_type == TL_WRITE) ||
1792
1733
Non-transactional locks do not support a lock_timeout.
1794
1735
lock_timeout= tlist->top_table()->lock_timeout;
1795
DBUG_PRINT("lock_info",
1796
("table: '%s' tlist==top_table: %d lock_timeout: %d",
1797
tlist->table_name, tlist==tlist->top_table(), lock_timeout));
1800
1738
For warning/error reporting we need to set the intended lock