1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
|
/* Copyright 2000-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
@file
Locking functions for mysql.
Because of the new concurrent inserts, we must first get external locks
before getting internal locks. If we do it in the other order, the status
information is not up to date when called from the lock handler.
GENERAL DESCRIPTION OF LOCKING
When not using LOCK TABLES:
- For each SQL statement mysql_lock_tables() is called for all involved
tables.
- mysql_lock_tables() will call
table_handler->external_lock(thd,locktype) for each table.
This is followed by a call to thr_multi_lock() for all tables.
- When statement is done, we call mysql_unlock_tables().
This will call thr_multi_unlock() followed by
table_handler->external_lock(thd, F_UNLCK) for each table.
- Note that mysql_unlock_tables() may be called several times as
MySQL in some cases can free some tables earlier than others.
- The above is true both for normal and temporary tables.
- Temporary non transactional tables are never passed to thr_multi_lock()
and we never call external_lock(thd, F_UNLOCK) on these.
When using LOCK TABLES:
- LOCK TABLE will call mysql_lock_tables() for all tables.
mysql_lock_tables() will call
table_handler->external_lock(thd,locktype) for each table.
This is followed by a call to thr_multi_lock() for all tables.
- For each statement, we will call table_handler->start_stmt(THD)
to inform the table handler that we are using the table.
The tables used can only be tables used in LOCK TABLES or a
temporary table.
- When statement is done, we will call ha_commit_stmt(thd);
- When calling UNLOCK TABLES we call mysql_unlock_tables() for all
tables used in LOCK TABLES
If table_handler->external_lock(thd, locktype) fails, we call
table_handler->external_lock(thd, F_UNLCK) for each table that was locked,
excluding one that caused failure. That means handler must cleanup itself
in case external_lock() fails.
@todo
Change to use my_malloc() ONLY when using LOCK TABLES command or when
we are forced to use mysql_lock_merge.
*/
#include "mysql_priv.h"
#include "debug_sync.h"
#include <hash.h>
#include <assert.h>
/**
@defgroup Locking Locking
@{
*/
extern HASH open_cache;
/* flags for get_lock_data */
#define GET_LOCK_UNLOCK 1
#define GET_LOCK_STORE_LOCKS 2
static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
uint flags);
static int lock_external(THD *thd, TABLE **table,uint count);
static int unlock_external(THD *thd, TABLE **table,uint count);
static void print_lock_error(int error, const char *);
/* Map the return value of thr_lock to an error from errmsg.txt */
static int thr_lock_errno_to_mysql[]=
{ 0, ER_LOCK_ABORTED, ER_LOCK_WAIT_TIMEOUT, ER_LOCK_DEADLOCK };
/**
Perform semantic checks for mysql_lock_tables.
@param thd The current thread
@param tables The tables to lock
@param count The number of tables to lock
@param flags Lock flags
@return 0 if all the check passed, non zero if a check failed.
*/
static int
lock_tables_check(THD *thd, TABLE **tables, uint count, uint flags)
{
uint system_count, i;
bool is_superuser, log_table_write_query;
DBUG_ENTER("lock_tables_check");
system_count= 0;
is_superuser= thd->security_ctx->master_access & SUPER_ACL;
log_table_write_query= (is_log_table_write_query(thd->lex->sql_command)
|| ((flags & MYSQL_LOCK_LOG_TABLE) != 0));
for (i=0 ; i<count; i++)
{
TABLE *t= tables[i];
/* Protect against 'fake' partially initialized TABLE_SHARE */
DBUG_ASSERT(t->s->table_category != TABLE_UNKNOWN_CATEGORY);
/*
Table I/O to performance schema tables is performed
only internally by the server implementation.
When a user is requesting a lock, the following
constraints are enforced:
*/
if (t->s->require_write_privileges() &&
! log_table_write_query)
{
/*
A user should not be able to prevent writes,
or hold any type of lock in a session,
since this would be a DOS attack.
*/
if ((t->reginfo.lock_type >= TL_READ_NO_INSERT)
|| (thd->lex->sql_command == SQLCOM_LOCK_TABLES))
{
my_error(ER_CANT_LOCK_LOG_TABLE, MYF(0));
DBUG_RETURN(1);
}
}
if (t->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE)
{
if (t->s->table_category == TABLE_CATEGORY_SYSTEM)
system_count++;
if (t->db_stat & HA_READ_ONLY)
{
my_error(ER_OPEN_AS_READONLY, MYF(0), t->alias);
DBUG_RETURN(1);
}
}
/*
If we are going to lock a non-temporary table we must own metadata
lock of appropriate type on it (I.e. for table to be locked for
write we must own metadata lock of MDL_SHARED_WRITE or stronger
type. For table to be locked for read we must own metadata lock
of MDL_SHARED_READ or stronger type).
The only exception are HANDLER statements which are allowed to
lock table for read while having only MDL_SHARED lock on it.
*/
DBUG_ASSERT(t->s->tmp_table ||
thd->mdl_context.is_lock_owner(MDL_key::TABLE,
t->s->db.str, t->s->table_name.str,
t->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE ?
MDL_SHARED_WRITE : MDL_SHARED_READ) ||
(t->open_by_handler &&
thd->mdl_context.is_lock_owner(MDL_key::TABLE,
t->s->db.str, t->s->table_name.str,
MDL_SHARED)));
/*
Prevent modifications to base tables if READ_ONLY is activated.
In any case, read only does not apply to temporary tables.
*/
if (!(flags & MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY) && !t->s->tmp_table)
{
if (t->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE &&
!is_superuser && opt_readonly && !thd->slave_thread)
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
DBUG_RETURN(1);
}
}
}
/*
Locking of system tables is restricted:
locking a mix of system and non-system tables in the same lock
is prohibited, to prevent contention.
*/
if ((system_count > 0) && (system_count < count))
{
my_error(ER_WRONG_LOCK_OF_SYSTEM_TABLE, MYF(0));
DBUG_RETURN(1);
}
DBUG_RETURN(0);
}
/**
Reset lock type in lock data
@param mysql_lock Lock structures to reset.
@note After a locking error we want to quit the locking of the table(s).
The test case in the bug report for Bug #18544 has the following
cases: 1. Locking error in lock_external() due to InnoDB timeout.
2. Locking error in get_lock_data() due to missing write permission.
3. Locking error in wait_if_global_read_lock() due to lock conflict.
@note In all these cases we have already set the lock type into the lock
data of the open table(s). If the table(s) are in the open table
cache, they could be reused with the non-zero lock type set. This
could lead to ignoring a different lock type with the next lock.
@note Clear the lock type of all lock data. This ensures that the next
lock request will set its lock type properly.
*/
static void reset_lock_data(MYSQL_LOCK *sql_lock)
{
THR_LOCK_DATA **ldata, **ldata_end;
DBUG_ENTER("reset_lock_data");
/* Clear the lock type of all lock data to avoid reusage. */
for (ldata= sql_lock->locks, ldata_end= ldata + sql_lock->lock_count;
ldata < ldata_end;
ldata++)
{
/* Reset lock type. */
(*ldata)->type= TL_UNLOCK;
}
DBUG_VOID_RETURN;
}
/**
Reset lock type in lock data and free.
@param mysql_lock Lock structures to reset.
*/
static void reset_lock_data_and_free(MYSQL_LOCK **mysql_lock)
{
reset_lock_data(*mysql_lock);
my_free(*mysql_lock, MYF(0));
*mysql_lock= 0;
}
/**
Lock tables.
@param thd The current thread.
@param tables An array of pointers to the tables to lock.
@param count The number of tables to lock.
@param flags Options:
MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY Ignore SET GLOBAL READ_ONLY
MYSQL_LOCK_IGNORE_TIMEOUT Use maximum timeout value.
@retval A lock structure pointer on success.
@retval NULL if an error or if wait on a lock was killed.
*/
MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **tables, uint count, uint flags)
{
int rc;
MYSQL_LOCK *sql_lock;
ulong timeout= (flags & MYSQL_LOCK_IGNORE_TIMEOUT) ?
LONG_TIMEOUT : thd->variables.lock_wait_timeout;
DBUG_ENTER("mysql_lock_tables");
if (lock_tables_check(thd, tables, count, flags))
DBUG_RETURN(NULL);
if (! (sql_lock= get_lock_data(thd, tables, count, GET_LOCK_STORE_LOCKS)))
DBUG_RETURN(NULL);
thd_proc_info(thd, "System lock");
DBUG_PRINT("info", ("thd->proc_info %s", thd->proc_info));
if (sql_lock->table_count && lock_external(thd, sql_lock->table,
sql_lock->table_count))
{
/* Clear the lock type of all lock data to avoid reusage. */
reset_lock_data_and_free(&sql_lock);
goto end;
}
/* Copy the lock data array. thr_multi_lock() reorders its contents. */
memcpy(sql_lock->locks + sql_lock->lock_count, sql_lock->locks,
sql_lock->lock_count * sizeof(*sql_lock->locks));
/* Lock on the copied half of the lock data array. */
rc= thr_lock_errno_to_mysql[(int) thr_multi_lock(sql_lock->locks +
sql_lock->lock_count,
sql_lock->lock_count,
thd->lock_id, timeout)];
if (rc)
{
if (sql_lock->table_count)
(void) unlock_external(thd, sql_lock->table, sql_lock->table_count);
reset_lock_data_and_free(&sql_lock);
if (! thd->killed)
my_error(rc, MYF(0));
}
end:
thd_proc_info(thd, 0);
if (thd->killed)
{
thd->send_kill_message();
if (sql_lock)
{
mysql_unlock_tables(thd, sql_lock);
sql_lock= 0;
}
}
thd->set_time_after_lock();
DBUG_RETURN(sql_lock);
}
static int lock_external(THD *thd, TABLE **tables, uint count)
{
reg1 uint i;
int lock_type,error;
DBUG_ENTER("lock_external");
DBUG_PRINT("info", ("count %d", count));
for (i=1 ; i <= count ; i++, tables++)
{
DBUG_ASSERT((*tables)->reginfo.lock_type >= TL_READ);
lock_type=F_WRLCK; /* Lock exclusive */
if ((*tables)->db_stat & HA_READ_ONLY ||
((*tables)->reginfo.lock_type >= TL_READ &&
(*tables)->reginfo.lock_type <= TL_READ_NO_INSERT))
lock_type=F_RDLCK;
if ((error=(*tables)->file->ha_external_lock(thd,lock_type)))
{
print_lock_error(error, (*tables)->file->table_type());
while (--i)
{
tables--;
(*tables)->file->ha_external_lock(thd, F_UNLCK);
(*tables)->current_lock=F_UNLCK;
}
DBUG_RETURN(error);
}
else
{
(*tables)->db_stat &= ~ HA_BLOCK_LOCK;
(*tables)->current_lock= lock_type;
}
}
DBUG_RETURN(0);
}
void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock)
{
DBUG_ENTER("mysql_unlock_tables");
if (sql_lock->lock_count)
thr_multi_unlock(sql_lock->locks,sql_lock->lock_count);
if (sql_lock->table_count)
(void) unlock_external(thd,sql_lock->table,sql_lock->table_count);
my_free((uchar*) sql_lock,MYF(0));
DBUG_VOID_RETURN;
}
/**
Unlock some of the tables locked by mysql_lock_tables.
This will work even if get_lock_data fails (next unlock will free all)
*/
void mysql_unlock_some_tables(THD *thd, TABLE **table,uint count)
{
MYSQL_LOCK *sql_lock;
if ((sql_lock= get_lock_data(thd, table, count, GET_LOCK_UNLOCK)))
mysql_unlock_tables(thd, sql_lock);
}
/**
unlock all tables locked for read.
*/
void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock)
{
uint i,found;
DBUG_ENTER("mysql_unlock_read_tables");
/* Move all write locks first */
THR_LOCK_DATA **lock=sql_lock->locks;
for (i=found=0 ; i < sql_lock->lock_count ; i++)
{
if (sql_lock->locks[i]->type >= TL_WRITE_ALLOW_READ)
{
swap_variables(THR_LOCK_DATA *, *lock, sql_lock->locks[i]);
lock++;
found++;
}
}
/* unlock the read locked tables */
if (i != found)
{
thr_multi_unlock(lock,i-found);
sql_lock->lock_count= found;
}
/* Then do the same for the external locks */
/* Move all write locked tables first */
TABLE **table=sql_lock->table;
for (i=found=0 ; i < sql_lock->table_count ; i++)
{
DBUG_ASSERT(sql_lock->table[i]->lock_position == i);
if ((uint) sql_lock->table[i]->reginfo.lock_type >= TL_WRITE_ALLOW_READ)
{
swap_variables(TABLE *, *table, sql_lock->table[i]);
table++;
found++;
}
}
/* Unlock all read locked tables */
if (i != found)
{
(void) unlock_external(thd,table,i-found);
sql_lock->table_count=found;
}
/* Fix the lock positions in TABLE */
table= sql_lock->table;
found= 0;
for (i= 0; i < sql_lock->table_count; i++)
{
TABLE *tbl= *table;
tbl->lock_position= (uint) (table - sql_lock->table);
tbl->lock_data_start= found;
found+= tbl->lock_count;
table++;
}
DBUG_VOID_RETURN;
}
/**
Try to find the table in the list of locked tables.
In case of success, unlock the table and remove it from this list.
If a table has more than one lock instance, removes them all.
@param thd thread context
@param locked list of locked tables
@param table the table to unlock
*/
void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table)
{
if (locked)
{
reg1 uint i;
for (i=0; i < locked->table_count; i++)
{
if (locked->table[i] == table)
{
uint j, removed_locks, old_tables;
TABLE *tbl;
uint lock_data_end;
DBUG_ASSERT(table->lock_position == i);
/* Unlock the table. */
mysql_unlock_some_tables(thd, &table, /* table count */ 1);
/* Decrement table_count in advance, making below expressions easier */
old_tables= --locked->table_count;
/* The table has 'removed_locks' lock data elements in locked->locks */
removed_locks= table->lock_count;
/* Move down all table pointers above 'i'. */
bmove((char*) (locked->table+i),
(char*) (locked->table+i+1),
(old_tables - i) * sizeof(TABLE*));
lock_data_end= table->lock_data_start + table->lock_count;
/* Move down all lock data pointers above 'table->lock_data_end-1' */
bmove((char*) (locked->locks + table->lock_data_start),
(char*) (locked->locks + lock_data_end),
(locked->lock_count - lock_data_end) *
sizeof(THR_LOCK_DATA*));
/*
Fix moved table elements.
lock_position is the index in the 'locked->table' array,
it must be fixed by one.
table->lock_data_start is pointer to the lock data for this table
in the 'locked->locks' array, they must be fixed by 'removed_locks',
the lock data count of the removed table.
*/
for (j= i ; j < old_tables; j++)
{
tbl= locked->table[j];
tbl->lock_position--;
DBUG_ASSERT(tbl->lock_position == j);
tbl->lock_data_start-= removed_locks;
}
/* Finally adjust lock_count. */
locked->lock_count-= removed_locks;
break;
}
}
}
}
/* Downgrade all locks on a table to new WRITE level from WRITE_ONLY */
void mysql_lock_downgrade_write(THD *thd, TABLE *table,
thr_lock_type new_lock_type)
{
MYSQL_LOCK *locked;
if ((locked = get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK)))
{
for (uint i=0; i < locked->lock_count; i++)
thr_downgrade_write_lock(locked->locks[i], new_lock_type);
my_free((uchar*) locked,MYF(0));
}
}
/** Abort all other threads waiting to get lock in table. */
void mysql_lock_abort(THD *thd, TABLE *table, bool upgrade_lock)
{
MYSQL_LOCK *locked;
DBUG_ENTER("mysql_lock_abort");
if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK)))
{
for (uint i=0; i < locked->lock_count; i++)
thr_abort_locks(locked->locks[i]->lock, upgrade_lock);
my_free((uchar*) locked,MYF(0));
}
DBUG_VOID_RETURN;
}
/**
Abort one thread / table combination.
@param thd Thread handler
@param table Table that should be removed from lock queue
@retval
0 Table was not locked by another thread
@retval
1 Table was locked by at least one other thread
*/
bool mysql_lock_abort_for_thread(THD *thd, TABLE *table)
{
MYSQL_LOCK *locked;
bool result= FALSE;
DBUG_ENTER("mysql_lock_abort_for_thread");
if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK)))
{
for (uint i=0; i < locked->lock_count; i++)
{
if (thr_abort_locks_for_thread(locked->locks[i]->lock,
table->in_use->thread_id))
result= TRUE;
}
my_free((uchar*) locked,MYF(0));
}
DBUG_RETURN(result);
}
MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b)
{
MYSQL_LOCK *sql_lock;
TABLE **table, **end_table;
DBUG_ENTER("mysql_lock_merge");
if (!(sql_lock= (MYSQL_LOCK*)
my_malloc(sizeof(*sql_lock)+
sizeof(THR_LOCK_DATA*)*(a->lock_count+b->lock_count)+
sizeof(TABLE*)*(a->table_count+b->table_count),MYF(MY_WME))))
DBUG_RETURN(0); // Fatal error
sql_lock->lock_count=a->lock_count+b->lock_count;
sql_lock->table_count=a->table_count+b->table_count;
sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1);
sql_lock->table=(TABLE**) (sql_lock->locks+sql_lock->lock_count);
memcpy(sql_lock->locks,a->locks,a->lock_count*sizeof(*a->locks));
memcpy(sql_lock->locks+a->lock_count,b->locks,
b->lock_count*sizeof(*b->locks));
memcpy(sql_lock->table,a->table,a->table_count*sizeof(*a->table));
memcpy(sql_lock->table+a->table_count,b->table,
b->table_count*sizeof(*b->table));
/*
Now adjust lock_position and lock_data_start for all objects that was
moved in 'b' (as there is now all objects in 'a' before these).
*/
for (table= sql_lock->table + a->table_count,
end_table= table + b->table_count;
table < end_table;
table++)
{
(*table)->lock_position+= a->table_count;
(*table)->lock_data_start+= a->lock_count;
}
/* Delete old, not needed locks */
my_free((uchar*) a,MYF(0));
my_free((uchar*) b,MYF(0));
thr_lock_merge_status(sql_lock->locks, sql_lock->lock_count);
DBUG_RETURN(sql_lock);
}
/**
Find duplicate lock in tables.
Temporary tables are ignored here like they are ignored in
get_lock_data(). If we allow two opens on temporary tables later,
both functions should be checked.
@param thd The current thread.
@param needle The table to check for duplicate lock.
@param haystack The list of tables to search for the dup lock.
@note
This is mainly meant for MERGE tables in INSERT ... SELECT
situations. The 'real', underlying tables can be found only after
the MERGE tables are opened. This function assumes that the tables are
already locked.
@retval
NULL No duplicate lock found.
@retval
!NULL First table from 'haystack' that matches a lock on 'needle'.
*/
TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle,
TABLE_LIST *haystack)
{
MYSQL_LOCK *mylock;
TABLE **lock_tables;
TABLE *table;
TABLE *table2;
THR_LOCK_DATA **lock_locks;
THR_LOCK_DATA **table_lock_data;
THR_LOCK_DATA **end_data;
THR_LOCK_DATA **lock_data2;
THR_LOCK_DATA **end_data2;
DBUG_ENTER("mysql_lock_have_duplicate");
/*
Table may not be defined for derived or view tables.
Table may not be part of a lock for delayed operations.
*/
if (! (table= needle->table) || ! table->lock_count)
goto end;
/* A temporary table does not have locks. */
if (table->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
goto end;
/* Get command lock or LOCK TABLES lock. Maybe empty for INSERT DELAYED. */
if (! (mylock= thd->lock))
goto end;
/* If we have less than two tables, we cannot have duplicates. */
if (mylock->table_count < 2)
goto end;
lock_locks= mylock->locks;
lock_tables= mylock->table;
/* Prepare table related variables that don't change in loop. */
DBUG_ASSERT((table->lock_position < mylock->table_count) &&
(table == lock_tables[table->lock_position]));
table_lock_data= lock_locks + table->lock_data_start;
end_data= table_lock_data + table->lock_count;
for (; haystack; haystack= haystack->next_global)
{
if (haystack->placeholder())
continue;
table2= haystack->table;
if (table2->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
continue;
/* All tables in list must be in lock. */
DBUG_ASSERT((table2->lock_position < mylock->table_count) &&
(table2 == lock_tables[table2->lock_position]));
for (lock_data2= lock_locks + table2->lock_data_start,
end_data2= lock_data2 + table2->lock_count;
lock_data2 < end_data2;
lock_data2++)
{
THR_LOCK_DATA **lock_data;
THR_LOCK *lock2= (*lock_data2)->lock;
for (lock_data= table_lock_data;
lock_data < end_data;
lock_data++)
{
if ((*lock_data)->lock == lock2)
{
DBUG_PRINT("info", ("haystack match: '%s'", haystack->table_name));
DBUG_RETURN(haystack);
}
}
}
}
end:
DBUG_PRINT("info", ("no duplicate found"));
DBUG_RETURN(NULL);
}
/** Unlock a set of external. */
static int unlock_external(THD *thd, TABLE **table,uint count)
{
int error,error_code;
DBUG_ENTER("unlock_external");
error_code=0;
do
{
if ((*table)->current_lock != F_UNLCK)
{
(*table)->current_lock = F_UNLCK;
if ((error=(*table)->file->ha_external_lock(thd, F_UNLCK)))
{
error_code=error;
print_lock_error(error_code, (*table)->file->table_type());
}
}
table++;
} while (--count);
DBUG_RETURN(error_code);
}
/**
Get lock structures from table structs and initialize locks.
@param thd Thread handler
@param table_ptr Pointer to tables that should be locks
@param flags One of:
- GET_LOCK_UNLOCK : If we should send TL_IGNORE to store lock
- GET_LOCK_STORE_LOCKS : Store lock info in TABLE
*/
static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
uint flags)
{
uint i,tables,lock_count;
MYSQL_LOCK *sql_lock;
THR_LOCK_DATA **locks, **locks_buf, **locks_start;
TABLE **to, **table_buf;
DBUG_ENTER("get_lock_data");
DBUG_ASSERT((flags == GET_LOCK_UNLOCK) || (flags == GET_LOCK_STORE_LOCKS));
DBUG_PRINT("info", ("count %d", count));
for (i=tables=lock_count=0 ; i < count ; i++)
{
TABLE *t= table_ptr[i];
if (t->s->tmp_table != NON_TRANSACTIONAL_TMP_TABLE)
{
tables+= t->file->lock_count();
lock_count++;
}
}
/*
Allocating twice the number of pointers for lock data for use in
thr_mulit_lock(). This function reorders the lock data, but cannot
update the table values. So the second part of the array is copied
from the first part immediately before calling thr_multi_lock().
*/
if (!(sql_lock= (MYSQL_LOCK*)
my_malloc(sizeof(*sql_lock) +
sizeof(THR_LOCK_DATA*) * tables * 2 +
sizeof(table_ptr) * lock_count,
MYF(0))))
DBUG_RETURN(0);
locks= locks_buf= sql_lock->locks= (THR_LOCK_DATA**) (sql_lock + 1);
to= table_buf= sql_lock->table= (TABLE**) (locks + tables * 2);
sql_lock->table_count=lock_count;
for (i=0 ; i < count ; i++)
{
TABLE *table;
enum thr_lock_type lock_type;
THR_LOCK_DATA **org_locks = locks;
if ((table=table_ptr[i])->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
continue;
lock_type= table->reginfo.lock_type;
DBUG_ASSERT(lock_type != TL_WRITE_DEFAULT && lock_type != TL_READ_DEFAULT);
locks_start= locks;
locks= table->file->store_lock(thd, locks,
(flags & GET_LOCK_UNLOCK) ? TL_IGNORE :
lock_type);
if (flags & GET_LOCK_STORE_LOCKS)
{
table->lock_position= (uint) (to - table_buf);
table->lock_data_start= (uint) (locks_start - locks_buf);
table->lock_count= (uint) (locks - locks_start);
}
*to++= table;
if (locks)
for ( ; org_locks != locks ; org_locks++)
(*org_locks)->debug_print_param= (void *) table;
}
/*
We do not use 'tables', because there are cases where store_lock()
returns less locks than lock_count() claimed. This can happen when
a FLUSH TABLES tries to abort locks from a MERGE table of another
thread. When that thread has just opened the table, but not yet
attached its children, it cannot return the locks. lock_count()
always returns the number of locks that an attached table has.
This is done to avoid the reverse situation: If lock_count() would
return 0 for a non-attached MERGE table, and that table becomes
attached between the calls to lock_count() and store_lock(), then
we would have allocated too little memory for the lock data. Now
we may allocate too much, but better safe than memory overrun.
And in the FLUSH case, the memory is released quickly anyway.
*/
sql_lock->lock_count= locks - locks_buf;
DBUG_PRINT("info", ("sql_lock->table_count %d sql_lock->lock_count %d",
sql_lock->table_count, sql_lock->lock_count));
DBUG_RETURN(sql_lock);
}
/*****************************************************************************
Lock table based on the name.
This is used when we need total access to a closed, not open table
*****************************************************************************/
/**
Obtain exclusive metadata locks on the list of tables.
@param thd Thread handle
@param table_list List of tables to lock
@note This function assumes that no metadata locks were acquired
before calling it. Also it cannot be called while holding
LOCK_open mutex. Both these invariants are enforced by asserts
in MDL_context::acquire_locks().
@retval FALSE Success.
@retval TRUE Failure (OOM or thread was killed).
*/
bool lock_table_names(THD *thd, TABLE_LIST *table_list)
{
MDL_request_list mdl_requests;
MDL_request global_request;
TABLE_LIST *lock_table;
global_request.init(MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE);
for (lock_table= table_list; lock_table; lock_table= lock_table->next_local)
{
lock_table->mdl_request.init(MDL_key::TABLE,
lock_table->db, lock_table->table_name,
MDL_EXCLUSIVE);
mdl_requests.push_front(&lock_table->mdl_request);
}
mdl_requests.push_front(&global_request);
if (thd->mdl_context.acquire_locks(&mdl_requests,
thd->variables.lock_wait_timeout))
return 1;
return 0;
}
/**
Release all metadata locks previously obtained by lock_table_names().
@param thd Thread handle.
@note Cannot be called while holding LOCK_open mutex.
*/
void unlock_table_names(THD *thd)
{
DBUG_ENTER("unlock_table_names");
thd->mdl_context.release_transactional_locks();
DBUG_VOID_RETURN;
}
/**
Obtain an exclusive metadata lock on the stored routine name.
@param thd Thread handle.
@param is_function Stored routine type (only functions or procedures
are name-locked.
@param db The schema the routine belongs to.
@param name Routine name.
This function assumes that no metadata locks were acquired
before calling it. Additionally, it cannot be called while
holding LOCK_open mutex. Both these invariants are enforced by
asserts in MDL_context::acquire_locks().
To avoid deadlocks, we do not try to obtain exclusive metadata
locks in LOCK TABLES mode, since in this mode there may be
other metadata locks already taken by the current connection,
and we must not wait for MDL locks while holding locks.
@retval FALSE Success.
@retval TRUE Failure: we're in LOCK TABLES mode, or out of memory,
or this connection was killed.
*/
bool lock_routine_name(THD *thd, bool is_function,
const char *db, const char *name)
{
MDL_key::enum_mdl_namespace mdl_type= (is_function ?
MDL_key::FUNCTION :
MDL_key::PROCEDURE);
MDL_request_list mdl_requests;
MDL_request global_request;
MDL_request mdl_request;
if (thd->locked_tables_mode)
{
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
return TRUE;
}
DBUG_ASSERT(name);
DEBUG_SYNC(thd, "before_wait_locked_pname");
global_request.init(MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE);
mdl_request.init(mdl_type, db, name, MDL_EXCLUSIVE);
mdl_requests.push_front(&mdl_request);
mdl_requests.push_front(&global_request);
if (thd->mdl_context.acquire_locks(&mdl_requests,
thd->variables.lock_wait_timeout))
return TRUE;
DEBUG_SYNC(thd, "after_wait_locked_pname");
return FALSE;
}
static void print_lock_error(int error, const char *table)
{
int textno;
DBUG_ENTER("print_lock_error");
switch (error) {
case HA_ERR_LOCK_WAIT_TIMEOUT:
textno=ER_LOCK_WAIT_TIMEOUT;
break;
case HA_ERR_READ_ONLY_TRANSACTION:
textno=ER_READ_ONLY_TRANSACTION;
break;
case HA_ERR_LOCK_DEADLOCK:
textno=ER_LOCK_DEADLOCK;
break;
case HA_ERR_WRONG_COMMAND:
textno=ER_ILLEGAL_HA;
break;
default:
textno=ER_CANT_LOCK;
break;
}
if ( textno == ER_ILLEGAL_HA )
my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), table);
else
my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), error);
DBUG_VOID_RETURN;
}
/****************************************************************************
Handling of global read locks
Taking the global read lock is TWO steps (2nd step is optional; without
it, COMMIT of existing transactions will be allowed):
lock_global_read_lock() THEN make_global_read_lock_block_commit().
The global locks are handled through the global variables:
global_read_lock
count of threads which have the global read lock (i.e. have completed at
least the first step above)
global_read_lock_blocks_commit
count of threads which have the global read lock and block
commits (i.e. are in or have completed the second step above)
waiting_for_read_lock
count of threads which want to take a global read lock but cannot
protect_against_global_read_lock
count of threads which have set protection against global read lock.
access to them is protected with a mutex LOCK_global_read_lock
(XXX: one should never take LOCK_open if LOCK_global_read_lock is
taken, otherwise a deadlock may occur. Other mutexes could be a
problem too - grep the code for global_read_lock if you want to use
any other mutex here) Also one must not hold LOCK_open when calling
wait_if_global_read_lock(). When the thread with the global read lock
tries to close its tables, it needs to take LOCK_open in
close_thread_table().
How blocking of threads by global read lock is achieved: that's
advisory. Any piece of code which should be blocked by global read lock must
be designed like this:
- call to wait_if_global_read_lock(). When this returns 0, no global read
lock is owned; if argument abort_on_refresh was 0, none can be obtained.
- job
- if abort_on_refresh was 0, call to start_waiting_global_read_lock() to
allow other threads to get the global read lock. I.e. removal of the
protection.
(Note: it's a bit like an implementation of rwlock).
[ I am sorry to mention some SQL syntaxes below I know I shouldn't but found
no better descriptive way ]
Why does FLUSH TABLES WITH READ LOCK need to block COMMIT: because it's used
to read a non-moving SHOW MASTER STATUS, and a COMMIT writes to the binary
log.
Why getting the global read lock is two steps and not one. Because FLUSH
TABLES WITH READ LOCK needs to insert one other step between the two:
flushing tables. So the order is
1) lock_global_read_lock() (prevents any new table write locks, i.e. stalls
all new updates)
2) close_cached_tables() (the FLUSH TABLES), which will wait for tables
currently opened and being updated to close (so it's possible that there is
a moment where all new updates of server are stalled *and* FLUSH TABLES WITH
READ LOCK is, too).
3) make_global_read_lock_block_commit().
If we have merged 1) and 3) into 1), we would have had this deadlock:
imagine thread 1 and 2, in non-autocommit mode, thread 3, and an InnoDB
table t.
thd1: SELECT * FROM t FOR UPDATE;
thd2: UPDATE t SET a=1; # blocked by row-level locks of thd1
thd3: FLUSH TABLES WITH READ LOCK; # blocked in close_cached_tables() by the
table instance of thd2
thd1: COMMIT; # blocked by thd3.
thd1 blocks thd2 which blocks thd3 which blocks thd1: deadlock.
Note that we need to support that one thread does
FLUSH TABLES WITH READ LOCK; and then COMMIT;
(that's what innobackup does, for some good reason).
So in this exceptional case the COMMIT should not be blocked by the FLUSH
TABLES WITH READ LOCK.
****************************************************************************/
volatile uint global_read_lock=0;
volatile uint global_read_lock_blocks_commit=0;
static volatile uint protect_against_global_read_lock=0;
static volatile uint waiting_for_read_lock=0;
/**
Take global read lock, wait if there is protection against lock.
If the global read lock is already taken by this thread, then nothing is done.
See also "Handling of global read locks" above.
@param thd Reference to thread.
@retval False Success, global read lock set, commits are NOT blocked.
@retval True Failure, thread was killed.
*/
bool Global_read_lock::lock_global_read_lock(THD *thd)
{
DBUG_ENTER("lock_global_read_lock");
if (!m_state)
{
MDL_request mdl_request;
const char *old_message;
const char *new_message= "Waiting to get readlock";
(void) mysql_mutex_lock(&LOCK_global_read_lock);
#if defined(ENABLED_DEBUG_SYNC)
/*
The below sync point fires if we have to wait for
protect_against_global_read_lock.
WARNING: Beware to use WAIT_FOR with this sync point. We hold
LOCK_global_read_lock here.
Call the sync point before calling enter_cond() as it does use
enter_cond() and exit_cond() itself if a WAIT_FOR action is
executed in spite of the above warning.
Pre-set proc_info so that it is available immediately after the
sync point sends a SIGNAL. This makes tests more reliable.
*/
if (protect_against_global_read_lock)
{
thd_proc_info(thd, new_message);
DEBUG_SYNC(thd, "wait_lock_global_read_lock");
}
#endif /* defined(ENABLED_DEBUG_SYNC) */
old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
new_message);
DBUG_PRINT("info",
("waiting_for: %d protect_against: %d",
waiting_for_read_lock, protect_against_global_read_lock));
waiting_for_read_lock++;
while (protect_against_global_read_lock && !thd->killed)
mysql_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
waiting_for_read_lock--;
if (thd->killed)
{
thd->exit_cond(old_message);
DBUG_RETURN(1);
}
m_state= GRL_ACQUIRED;
global_read_lock++;
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
/*
When we perform FLUSH TABLES or ALTER TABLE under LOCK TABLES,
tables being reopened are protected only by meta-data locks at
some point. To avoid sneaking in with our global read lock at
this moment we have to take global shared meta data lock.
TODO: We should change this code to acquire global shared metadata
lock before acquiring global read lock. But in order to do
this we have to get rid of all those places in which
wait_if_global_read_lock() is called before acquiring
metadata locks first. Also long-term we should get rid of
redundancy between metadata locks, global read lock and DDL
blocker (see WL#4399 and WL#4400).
*/
DBUG_ASSERT(! thd->mdl_context.is_lock_owner(MDL_key::GLOBAL, "", "",
MDL_SHARED));
mdl_request.init(MDL_key::GLOBAL, "", "", MDL_SHARED);
if (thd->mdl_context.acquire_lock(&mdl_request,
thd->variables.lock_wait_timeout))
{
/* Our thread was killed -- return back to initial state. */
mysql_mutex_lock(&LOCK_global_read_lock);
if (!(--global_read_lock))
{
DBUG_PRINT("signal", ("Broadcasting COND_global_read_lock"));
mysql_cond_broadcast(&COND_global_read_lock);
}
mysql_mutex_unlock(&LOCK_global_read_lock);
m_state= GRL_NONE;
DBUG_RETURN(1);
}
thd->mdl_context.move_ticket_after_trans_sentinel(mdl_request.ticket);
m_mdl_global_shared_lock= mdl_request.ticket;
}
/*
We DON'T set global_read_lock_blocks_commit now, it will be set after
tables are flushed (as the present function serves for FLUSH TABLES WITH
READ LOCK only). Doing things in this order is necessary to avoid
deadlocks (we must allow COMMIT until all tables are closed; we should not
forbid it before, or we can have a 3-thread deadlock if 2 do SELECT FOR
UPDATE and one does FLUSH TABLES WITH READ LOCK).
*/
DBUG_RETURN(0);
}
/**
Unlock global read lock.
Commits may or may not be blocked when this function is called.
See also "Handling of global read locks" above.
@param thd Reference to thread.
*/
void Global_read_lock::unlock_global_read_lock(THD *thd)
{
uint tmp;
DBUG_ENTER("unlock_global_read_lock");
DBUG_PRINT("info",
("global_read_lock: %u global_read_lock_blocks_commit: %u",
global_read_lock, global_read_lock_blocks_commit));
DBUG_ASSERT(m_mdl_global_shared_lock && m_state);
thd->mdl_context.release_lock(m_mdl_global_shared_lock);
m_mdl_global_shared_lock= NULL;
mysql_mutex_lock(&LOCK_global_read_lock);
tmp= --global_read_lock;
if (m_state == GRL_ACQUIRED_AND_BLOCKS_COMMIT)
--global_read_lock_blocks_commit;
mysql_mutex_unlock(&LOCK_global_read_lock);
/* Send the signal outside the mutex to avoid a context switch */
if (!tmp)
{
DBUG_PRINT("signal", ("Broadcasting COND_global_read_lock"));
mysql_cond_broadcast(&COND_global_read_lock);
}
m_state= GRL_NONE;
DBUG_VOID_RETURN;
}
/**
Wait if the global read lock is set, and optionally seek protection against
global read lock.
See also "Handling of global read locks" above.
@param thd Reference to thread.
@param abort_on_refresh If True, abort waiting if a refresh occurs,
do NOT seek protection against GRL.
If False, wait until the GRL is released and seek
protection against GRL.
@param is_not_commit If False, called from a commit operation,
wait only if commit blocking is also enabled.
@retval False Success, protection against global read lock is set
(if !abort_on_refresh)
@retval True Failure, wait was aborted or thread was killed.
*/
#define must_wait (global_read_lock && \
(is_not_commit || \
global_read_lock_blocks_commit))
bool Global_read_lock::
wait_if_global_read_lock(THD *thd, bool abort_on_refresh,
bool is_not_commit)
{
const char *UNINIT_VAR(old_message);
bool result= 0, need_exit_cond;
DBUG_ENTER("wait_if_global_read_lock");
/*
If we already have protection against global read lock,
just increment the counter.
*/
if (unlikely(m_protection_count > 0))
{
if (!abort_on_refresh)
m_protection_count++;
DBUG_RETURN(FALSE);
}
/*
Assert that we do not own LOCK_open. If we would own it, other
threads could not close their tables. This would make a pretty
deadlock.
*/
mysql_mutex_assert_not_owner(&LOCK_open);
mysql_mutex_lock(&LOCK_global_read_lock);
if ((need_exit_cond= must_wait))
{
if (m_state) // This thread had the read locks
{
if (is_not_commit)
my_message(ER_CANT_UPDATE_WITH_READLOCK,
ER(ER_CANT_UPDATE_WITH_READLOCK), MYF(0));
mysql_mutex_unlock(&LOCK_global_read_lock);
/*
We allow FLUSHer to COMMIT; we assume FLUSHer knows what it does.
This allowance is needed to not break existing versions of innobackup
which do a BEGIN; INSERT; FLUSH TABLES WITH READ LOCK; COMMIT.
*/
DBUG_RETURN(is_not_commit);
}
old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
"Waiting for release of readlock");
while (must_wait && ! thd->killed &&
(!abort_on_refresh || thd->version == refresh_version))
{
DBUG_PRINT("signal", ("Waiting for COND_global_read_lock"));
mysql_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
DBUG_PRINT("signal", ("Got COND_global_read_lock"));
}
if (thd->killed)
result=1;
}
if (!abort_on_refresh && !result)
{
m_protection_count++;
protect_against_global_read_lock++;
DBUG_PRINT("sql_lock", ("protect_against_global_read_lock incr: %u",
protect_against_global_read_lock));
}
/*
The following is only true in case of a global read locks (which is rare)
and if old_message is set
*/
if (unlikely(need_exit_cond))
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
else
mysql_mutex_unlock(&LOCK_global_read_lock);
DBUG_RETURN(result);
}
/**
Release protection against global read lock and restart
global read lock waiters.
Should only be called if we have protection against global read lock.
See also "Handling of global read locks" above.
@param thd Reference to thread.
*/
void Global_read_lock::start_waiting_global_read_lock(THD *thd)
{
bool tmp;
DBUG_ENTER("start_waiting_global_read_lock");
/*
Ignore request if we do not have protection against global read lock.
(Note that this is a violation of the interface contract, hence the assert).
*/
DBUG_ASSERT(m_protection_count > 0);
if (unlikely(m_protection_count == 0))
DBUG_VOID_RETURN;
/* Decrement local read lock protection counter, return if we still have it */
if (unlikely(--m_protection_count > 0))
DBUG_VOID_RETURN;
if (unlikely(m_state))
DBUG_VOID_RETURN;
mysql_mutex_lock(&LOCK_global_read_lock);
DBUG_ASSERT(protect_against_global_read_lock);
tmp= (!--protect_against_global_read_lock &&
(waiting_for_read_lock || global_read_lock_blocks_commit));
mysql_mutex_unlock(&LOCK_global_read_lock);
if (tmp)
mysql_cond_broadcast(&COND_global_read_lock);
DBUG_VOID_RETURN;
}
/**
Make global read lock also block commits.
The scenario is:
- This thread has the global read lock.
- Global read lock blocking of commits is not set.
See also "Handling of global read locks" above.
@param thd Reference to thread.
@retval False Success, global read lock set, commits are blocked.
@retval True Failure, thread was killed.
*/
bool Global_read_lock::make_global_read_lock_block_commit(THD *thd)
{
bool error;
const char *old_message;
DBUG_ENTER("make_global_read_lock_block_commit");
/*
If we didn't succeed lock_global_read_lock(), or if we already suceeded
make_global_read_lock_block_commit(), do nothing.
*/
if (m_state != GRL_ACQUIRED)
DBUG_RETURN(0);
mysql_mutex_lock(&LOCK_global_read_lock);
/* increment this BEFORE waiting on cond (otherwise race cond) */
global_read_lock_blocks_commit++;
/* For testing we set up some blocking, to see if we can be killed */
DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
protect_against_global_read_lock++;);
old_message= thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
"Waiting for all running commits to finish");
while (protect_against_global_read_lock && !thd->killed)
mysql_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
protect_against_global_read_lock--;);
if ((error= test(thd->killed)))
global_read_lock_blocks_commit--; // undo what we did
else
m_state= GRL_ACQUIRED_AND_BLOCKS_COMMIT;
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
DBUG_RETURN(error);
}
/**
Broadcast COND_refresh and COND_global_read_lock.
Due to a bug in a threading library it could happen that a signal
did not reach its target. A condition for this was that the same
condition variable was used with different mutexes in
mysql_cond_wait(). Some time ago we changed LOCK_open to
LOCK_global_read_lock in global read lock handling. So COND_refresh
was used with LOCK_open and LOCK_global_read_lock.
We did now also change from COND_refresh to COND_global_read_lock
in global read lock handling. But now it is necessary to signal
both conditions at the same time.
@note
When signalling COND_global_read_lock within the global read lock
handling, it is not necessary to also signal COND_refresh.
*/
void broadcast_refresh(void)
{
mysql_cond_broadcast(&COND_refresh);
mysql_cond_broadcast(&COND_global_read_lock);
}
/**
@} (end of group Locking)
*/
|