You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1342 lines
40 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
Bug#8407 (Stored functions/triggers ignore exception handler) Bug 18914 (Calling certain SPs from triggers fail) Bug 20713 (Functions will not not continue for SQLSTATE VALUE '42S02') Bug 21825 (Incorrect message error deleting records in a table with a trigger for inserting) Bug 22580 (DROP TABLE in nested stored procedure causes strange dependency error) Bug 25345 (Cursors from Functions) This fix resolves a long standing issue originally reported with bug 8407, which affect the behavior of Stored Procedures, Stored Functions and Trigger in many different ways, causing symptoms reported by all the bugs listed. In all cases, the root cause of the problem traces back to 8407 and how the server locks tables involved with sub statements. Prior to this fix, the implementation of stored routines would: - compute the transitive closure of all the tables referenced by a top level statement - open and lock all the tables involved - execute the top level statement "transitive closure of tables" means collecting: - all the tables, - all the stored functions, - all the views, - all the table triggers - all the stored procedures involved, and recursively inspect these objects definition to find more references to more objects, until the list of every object referenced does not grow any more. This mechanism is known as "pre-locking" tables before execution. The motivation for locking all the tables (possibly) used at once is to prevent dead locks. One problem with this approach is that, if the execution path the code really takes during runtime does not use a given table, and if the table is missing, the server would not execute the statement. This in particular has a major impact on triggers, since a missing table referenced by an update/delete trigger would prevent an insert trigger to run. Another problem is that stored routines might define SQL exception handlers to deal with missing tables, but the server implementation would never give user code a chance to execute this logic, since the routine is never executed when a missing table cause the pre-locking code to fail. With this fix, the internal implementation of the pre-locking code has been relaxed of some constraints, so that failure to open a table does not necessarily prevent execution of a stored routine. In particular, the pre-locking mechanism is now behaving as follows: 1) the first step, to compute the transitive closure of all the tables possibly referenced by a statement, is unchanged. 2) the next step, which is to open all the tables involved, only attempts to open the tables added by the pre-locking code, but silently fails without reporting any error or invoking any exception handler is the table is not present. This is achieved by trapping internal errors with Prelock_error_handler 3) the locking step only locks tables that were successfully opened. 4) when executing sub statements, the list of tables used by each statements is evaluated as before. The tables needed by the sub statement are expected to be already opened and locked. Statement referencing tables that were not opened in step 2) will fail to find the table in the open list, and only at this point will execution of the user code fail. 5) when a runtime exception is raised at 4), the instruction continuation destination (the next instruction to execute in case of SQL continue handlers) is evaluated. This is achieved with sp_instr::exec_open_and_lock_tables() 6) if a user exception handler is present in the stored routine, that handler is invoked as usual, so that ER_NO_SUCH_TABLE exceptions can be trapped by stored routines. If no handler exists, then the runtime execution will fail as expected. With all these changes, a side effect is that view security is impacted, in two different ways. First, a view defined as "select stored_function()", where the stored function references a table that may not exist, is considered valid. The rationale is that, because the stored function might trap exceptions during execution and still return a valid result, there is no way to decide when the view is created if a missing table really cause the view to be invalid. Secondly, testing for existence of tables is now done later during execution. View security, which consist of trapping errors and return a generic ER_VIEW_INVALID (to prevent disclosing information) was only implemented at very specific phases covering *opening* tables, but not covering the runtime execution. Because of this existing limitation, errors that were previously trapped and converted into ER_VIEW_INVALID are not trapped, causing table names to be reported to the user. This change is exposing an existing problem, which is independent and will be resolved separately.
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
21 years ago
21 years ago
  1. /* Copyright (C) 2000-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* locking functions for mysql */
  13. /*
  14. Because of the new concurrent inserts, we must first get external locks
  15. before getting internal locks. If we do it in the other order, the status
  16. information is not up to date when called from the lock handler.
  17. GENERAL DESCRIPTION OF LOCKING
  18. When not using LOCK TABLES:
  19. - For each SQL statement mysql_lock_tables() is called for all involved
  20. tables.
  21. - mysql_lock_tables() will call
  22. table_handler->external_lock(thd,locktype) for each table.
  23. This is followed by a call to thr_multi_lock() for all tables.
  24. - When statement is done, we call mysql_unlock_tables().
  25. This will call thr_multi_unlock() followed by
  26. table_handler->external_lock(thd, F_UNLCK) for each table.
  27. - Note that mysql_unlock_tables() may be called several times as
  28. MySQL in some cases can free some tables earlier than others.
  29. - The above is true both for normal and temporary tables.
  30. - Temporary non transactional tables are never passed to thr_multi_lock()
  31. and we never call external_lock(thd, F_UNLOCK) on these.
  32. When using LOCK TABLES:
  33. - LOCK TABLE will call mysql_lock_tables() for all tables.
  34. mysql_lock_tables() will call
  35. table_handler->external_lock(thd,locktype) for each table.
  36. This is followed by a call to thr_multi_lock() for all tables.
  37. - For each statement, we will call table_handler->start_stmt(THD)
  38. to inform the table handler that we are using the table.
  39. The tables used can only be tables used in LOCK TABLES or a
  40. temporary table.
  41. - When statement is done, we will call ha_commit_stmt(thd);
  42. - When calling UNLOCK TABLES we call mysql_unlock_tables() for all
  43. tables used in LOCK TABLES
  44. TODO:
  45. Change to use my_malloc() ONLY when using LOCK TABLES command or when
  46. we are forced to use mysql_lock_merge.
  47. */
  48. #include "mysql_priv.h"
  49. #include <hash.h>
  50. #include <assert.h>
  51. extern HASH open_cache;
  52. /* flags for get_lock_data */
  53. #define GET_LOCK_UNLOCK 1
  54. #define GET_LOCK_STORE_LOCKS 2
  55. static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table,uint count,
  56. uint flags, TABLE **write_locked);
  57. static void reset_lock_data(MYSQL_LOCK *sql_lock);
  58. static int lock_external(THD *thd, TABLE **table,uint count);
  59. static int unlock_external(THD *thd, TABLE **table,uint count);
  60. static void print_lock_error(int error, const char *);
  61. /*
  62. Lock tables.
  63. SYNOPSIS
  64. mysql_lock_tables()
  65. thd The current thread.
  66. tables An array of pointers to the tables to lock.
  67. count The number of tables to lock.
  68. flags Options:
  69. MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK Ignore a global read lock
  70. MYSQL_LOCK_IGNORE_FLUSH Ignore a flush tables.
  71. MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN Instead of reopening altered
  72. or dropped tables by itself,
  73. mysql_lock_tables() should
  74. notify upper level and rely
  75. on caller doing this.
  76. need_reopen Out parameter, TRUE if some tables were altered
  77. or deleted and should be reopened by caller.
  78. RETURN
  79. A lock structure pointer on success.
  80. NULL on error or if some tables should be reopen.
  81. */
  82. /* Map the return value of thr_lock to an error from errmsg.txt */
  83. static int thr_lock_errno_to_mysql[]=
  84. { 0, 1, ER_LOCK_WAIT_TIMEOUT, ER_LOCK_DEADLOCK };
  85. MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **tables, uint count,
  86. uint flags, bool *need_reopen)
  87. {
  88. MYSQL_LOCK *sql_lock;
  89. TABLE *write_lock_used;
  90. int rc;
  91. DBUG_ENTER("mysql_lock_tables");
  92. *need_reopen= FALSE;
  93. for (;;)
  94. {
  95. if (! (sql_lock= get_lock_data(thd, tables, count, GET_LOCK_STORE_LOCKS,
  96. &write_lock_used)))
  97. break;
  98. if (global_read_lock && write_lock_used &&
  99. ! (flags & MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK))
  100. {
  101. /*
  102. Someone has issued LOCK ALL TABLES FOR READ and we want a write lock
  103. Wait until the lock is gone
  104. */
  105. if (wait_if_global_read_lock(thd, 1, 1))
  106. {
  107. /* Clear the lock type of all lock data to avoid reusage. */
  108. reset_lock_data(sql_lock);
  109. my_free((gptr) sql_lock,MYF(0));
  110. sql_lock=0;
  111. break;
  112. }
  113. if (thd->version != refresh_version)
  114. {
  115. /* Clear the lock type of all lock data to avoid reusage. */
  116. reset_lock_data(sql_lock);
  117. my_free((gptr) sql_lock,MYF(0));
  118. goto retry;
  119. }
  120. }
  121. thd->proc_info="System lock";
  122. if (lock_external(thd, tables, count))
  123. {
  124. /* Clear the lock type of all lock data to avoid reusage. */
  125. reset_lock_data(sql_lock);
  126. my_free((gptr) sql_lock,MYF(0));
  127. sql_lock=0;
  128. break;
  129. }
  130. thd->proc_info="Table lock";
  131. thd->locked=1;
  132. /* Copy the lock data array. thr_multi_lock() reorders its contens. */
  133. memcpy(sql_lock->locks + sql_lock->lock_count, sql_lock->locks,
  134. sql_lock->lock_count * sizeof(*sql_lock->locks));
  135. /* Lock on the copied half of the lock data array. */
  136. rc= thr_lock_errno_to_mysql[(int) thr_multi_lock(sql_lock->locks +
  137. sql_lock->lock_count,
  138. sql_lock->lock_count,
  139. thd->lock_id)];
  140. if (rc > 1) /* a timeout or a deadlock */
  141. {
  142. my_error(rc, MYF(0));
  143. my_free((gptr) sql_lock,MYF(0));
  144. sql_lock= 0;
  145. break;
  146. }
  147. else if (rc == 1) /* aborted */
  148. {
  149. thd->some_tables_deleted=1; // Try again
  150. sql_lock->lock_count= 0; // Locks are already freed
  151. }
  152. else if (!thd->some_tables_deleted || (flags & MYSQL_LOCK_IGNORE_FLUSH))
  153. {
  154. thd->locked=0;
  155. break;
  156. }
  157. else if (!thd->open_tables)
  158. {
  159. // Only using temporary tables, no need to unlock
  160. thd->some_tables_deleted=0;
  161. thd->locked=0;
  162. break;
  163. }
  164. thd->proc_info=0;
  165. /* some table was altered or deleted. reopen tables marked deleted */
  166. mysql_unlock_tables(thd,sql_lock);
  167. thd->locked=0;
  168. retry:
  169. sql_lock=0;
  170. if (flags & MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN)
  171. {
  172. *need_reopen= TRUE;
  173. break;
  174. }
  175. if (wait_for_tables(thd))
  176. break; // Couldn't open tables
  177. }
  178. thd->proc_info=0;
  179. if (thd->killed)
  180. {
  181. thd->send_kill_message();
  182. if (sql_lock)
  183. {
  184. mysql_unlock_tables(thd,sql_lock);
  185. sql_lock=0;
  186. }
  187. }
  188. thd->lock_time();
  189. DBUG_RETURN (sql_lock);
  190. }
  191. static int lock_external(THD *thd, TABLE **tables, uint count)
  192. {
  193. reg1 uint i;
  194. int lock_type,error;
  195. DBUG_ENTER("lock_external");
  196. for (i=1 ; i <= count ; i++, tables++)
  197. {
  198. DBUG_ASSERT((*tables)->reginfo.lock_type >= TL_READ);
  199. lock_type=F_WRLCK; /* Lock exclusive */
  200. if ((*tables)->db_stat & HA_READ_ONLY ||
  201. ((*tables)->reginfo.lock_type >= TL_READ &&
  202. (*tables)->reginfo.lock_type <= TL_READ_NO_INSERT))
  203. lock_type=F_RDLCK;
  204. if ((error=(*tables)->file->external_lock(thd,lock_type)))
  205. {
  206. print_lock_error(error, (*tables)->file->table_type());
  207. for (; i-- ; tables--)
  208. {
  209. (*tables)->file->external_lock(thd, F_UNLCK);
  210. (*tables)->current_lock=F_UNLCK;
  211. }
  212. DBUG_RETURN(error);
  213. }
  214. else
  215. {
  216. (*tables)->db_stat &= ~ HA_BLOCK_LOCK;
  217. (*tables)->current_lock= lock_type;
  218. }
  219. }
  220. DBUG_RETURN(0);
  221. }
  222. void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock)
  223. {
  224. DBUG_ENTER("mysql_unlock_tables");
  225. if (sql_lock->lock_count)
  226. thr_multi_unlock(sql_lock->locks,sql_lock->lock_count);
  227. if (sql_lock->table_count)
  228. VOID(unlock_external(thd,sql_lock->table,sql_lock->table_count));
  229. my_free((gptr) sql_lock,MYF(0));
  230. DBUG_VOID_RETURN;
  231. }
  232. /*
  233. Unlock some of the tables locked by mysql_lock_tables
  234. This will work even if get_lock_data fails (next unlock will free all)
  235. */
  236. void mysql_unlock_some_tables(THD *thd, TABLE **table,uint count)
  237. {
  238. MYSQL_LOCK *sql_lock;
  239. TABLE *write_lock_used;
  240. if ((sql_lock= get_lock_data(thd, table, count, GET_LOCK_UNLOCK,
  241. &write_lock_used)))
  242. mysql_unlock_tables(thd, sql_lock);
  243. }
  244. /*
  245. ** unlock all tables locked for read.
  246. */
  247. void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock)
  248. {
  249. uint i,found;
  250. DBUG_ENTER("mysql_unlock_read_tables");
  251. /* Move all write locks first */
  252. THR_LOCK_DATA **lock=sql_lock->locks;
  253. for (i=found=0 ; i < sql_lock->lock_count ; i++)
  254. {
  255. if (sql_lock->locks[i]->type >= TL_WRITE_ALLOW_READ)
  256. {
  257. swap_variables(THR_LOCK_DATA *, *lock, sql_lock->locks[i]);
  258. lock++;
  259. found++;
  260. }
  261. }
  262. /* unlock the read locked tables */
  263. if (i != found)
  264. {
  265. thr_multi_unlock(lock,i-found);
  266. sql_lock->lock_count= found;
  267. }
  268. /* Then do the same for the external locks */
  269. /* Move all write locked tables first */
  270. TABLE **table=sql_lock->table;
  271. for (i=found=0 ; i < sql_lock->table_count ; i++)
  272. {
  273. DBUG_ASSERT(sql_lock->table[i]->lock_position == i);
  274. if ((uint) sql_lock->table[i]->reginfo.lock_type >= TL_WRITE_ALLOW_READ)
  275. {
  276. swap_variables(TABLE *, *table, sql_lock->table[i]);
  277. table++;
  278. found++;
  279. }
  280. }
  281. /* Unlock all read locked tables */
  282. if (i != found)
  283. {
  284. VOID(unlock_external(thd,table,i-found));
  285. sql_lock->table_count=found;
  286. }
  287. /* Fix the lock positions in TABLE */
  288. table= sql_lock->table;
  289. found= 0;
  290. for (i= 0; i < sql_lock->table_count; i++)
  291. {
  292. TABLE *tbl= *table;
  293. tbl->lock_position= table - sql_lock->table;
  294. tbl->lock_data_start= found;
  295. found+= tbl->lock_count;
  296. table++;
  297. }
  298. DBUG_VOID_RETURN;
  299. }
  300. void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table)
  301. {
  302. mysql_unlock_some_tables(thd, &table,1);
  303. if (locked)
  304. {
  305. reg1 uint i;
  306. for (i=0; i < locked->table_count; i++)
  307. {
  308. if (locked->table[i] == table)
  309. {
  310. uint j, removed_locks, old_tables;
  311. TABLE *tbl;
  312. uint lock_data_end;
  313. DBUG_ASSERT(table->lock_position == i);
  314. /* Decrement table_count in advance, making below expressions easier */
  315. old_tables= --locked->table_count;
  316. /* The table has 'removed_locks' lock data elements in locked->locks */
  317. removed_locks= table->lock_count;
  318. /* Move down all table pointers above 'i'. */
  319. bmove((char*) (locked->table+i),
  320. (char*) (locked->table+i+1),
  321. (old_tables - i) * sizeof(TABLE*));
  322. lock_data_end= table->lock_data_start + table->lock_count;
  323. /* Move down all lock data pointers above 'table->lock_data_end-1' */
  324. bmove((char*) (locked->locks + table->lock_data_start),
  325. (char*) (locked->locks + lock_data_end),
  326. (locked->lock_count - lock_data_end) *
  327. sizeof(THR_LOCK_DATA*));
  328. /*
  329. Fix moved table elements.
  330. lock_position is the index in the 'locked->table' array,
  331. it must be fixed by one.
  332. table->lock_data_start is pointer to the lock data for this table
  333. in the 'locked->locks' array, they must be fixed by 'removed_locks',
  334. the lock data count of the removed table.
  335. */
  336. for (j= i ; j < old_tables; j++)
  337. {
  338. tbl= locked->table[j];
  339. tbl->lock_position--;
  340. DBUG_ASSERT(tbl->lock_position == j);
  341. tbl->lock_data_start-= removed_locks;
  342. }
  343. /* Finally adjust lock_count. */
  344. locked->lock_count-= removed_locks;
  345. break;
  346. }
  347. }
  348. }
  349. }
  350. /* abort all other threads waiting to get lock in table */
  351. void mysql_lock_abort(THD *thd, TABLE *table)
  352. {
  353. MYSQL_LOCK *locked;
  354. TABLE *write_lock_used;
  355. if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
  356. &write_lock_used)))
  357. {
  358. for (uint i=0; i < locked->lock_count; i++)
  359. thr_abort_locks(locked->locks[i]->lock);
  360. my_free((gptr) locked,MYF(0));
  361. }
  362. }
  363. /*
  364. Abort one thread / table combination
  365. SYNOPSIS
  366. mysql_lock_abort_for_thread()
  367. thd Thread handler
  368. table Table that should be removed from lock queue
  369. RETURN
  370. 0 Table was not locked by another thread
  371. 1 Table was locked by at least one other thread
  372. */
  373. bool mysql_lock_abort_for_thread(THD *thd, TABLE *table)
  374. {
  375. MYSQL_LOCK *locked;
  376. TABLE *write_lock_used;
  377. bool result= FALSE;
  378. DBUG_ENTER("mysql_lock_abort_for_thread");
  379. if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
  380. &write_lock_used)))
  381. {
  382. for (uint i=0; i < locked->lock_count; i++)
  383. {
  384. if (thr_abort_locks_for_thread(locked->locks[i]->lock,
  385. table->in_use->real_id))
  386. result= TRUE;
  387. }
  388. my_free((gptr) locked,MYF(0));
  389. }
  390. DBUG_RETURN(result);
  391. }
  392. MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b)
  393. {
  394. MYSQL_LOCK *sql_lock;
  395. TABLE **table, **end_table;
  396. DBUG_ENTER("mysql_lock_merge");
  397. if (!(sql_lock= (MYSQL_LOCK*)
  398. my_malloc(sizeof(*sql_lock)+
  399. sizeof(THR_LOCK_DATA*)*(a->lock_count+b->lock_count)+
  400. sizeof(TABLE*)*(a->table_count+b->table_count),MYF(MY_WME))))
  401. DBUG_RETURN(0); // Fatal error
  402. sql_lock->lock_count=a->lock_count+b->lock_count;
  403. sql_lock->table_count=a->table_count+b->table_count;
  404. sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1);
  405. sql_lock->table=(TABLE**) (sql_lock->locks+sql_lock->lock_count);
  406. memcpy(sql_lock->locks,a->locks,a->lock_count*sizeof(*a->locks));
  407. memcpy(sql_lock->locks+a->lock_count,b->locks,
  408. b->lock_count*sizeof(*b->locks));
  409. memcpy(sql_lock->table,a->table,a->table_count*sizeof(*a->table));
  410. memcpy(sql_lock->table+a->table_count,b->table,
  411. b->table_count*sizeof(*b->table));
  412. /*
  413. Now adjust lock_position and lock_data_start for all objects that was
  414. moved in 'b' (as there is now all objects in 'a' before these).
  415. */
  416. for (table= sql_lock->table + a->table_count,
  417. end_table= table + b->table_count;
  418. table < end_table;
  419. table++)
  420. {
  421. (*table)->lock_position+= a->table_count;
  422. (*table)->lock_data_start+= a->lock_count;
  423. }
  424. /* Delete old, not needed locks */
  425. my_free((gptr) a,MYF(0));
  426. my_free((gptr) b,MYF(0));
  427. DBUG_RETURN(sql_lock);
  428. }
  429. /*
  430. Find duplicate lock in tables.
  431. SYNOPSIS
  432. mysql_lock_have_duplicate()
  433. thd The current thread.
  434. needle The table to check for duplicate lock.
  435. haystack The list of tables to search for the dup lock.
  436. NOTE
  437. This is mainly meant for MERGE tables in INSERT ... SELECT
  438. situations. The 'real', underlying tables can be found only after
  439. the MERGE tables are opened. This function assumes that the tables are
  440. already locked.
  441. Temporary tables are ignored here like they are ignored in
  442. get_lock_data(). If we allow two opens on temporary tables later,
  443. both functions should be checked.
  444. RETURN
  445. NULL No duplicate lock found.
  446. ! NULL First table from 'haystack' that matches a lock on 'needle'.
  447. */
  448. TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle,
  449. TABLE_LIST *haystack)
  450. {
  451. MYSQL_LOCK *mylock;
  452. TABLE **lock_tables;
  453. TABLE *table;
  454. TABLE *table2;
  455. THR_LOCK_DATA **lock_locks;
  456. THR_LOCK_DATA **table_lock_data;
  457. THR_LOCK_DATA **end_data;
  458. THR_LOCK_DATA **lock_data2;
  459. THR_LOCK_DATA **end_data2;
  460. DBUG_ENTER("mysql_lock_have_duplicate");
  461. /*
  462. Table may not be defined for derived or view tables.
  463. Table may not be part of a lock for delayed operations.
  464. */
  465. if (! (table= needle->table) || ! table->lock_count)
  466. goto end;
  467. /* A temporary table does not have locks. */
  468. if (table->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
  469. goto end;
  470. /* Get command lock or LOCK TABLES lock. Maybe empty for INSERT DELAYED. */
  471. if (! (mylock= thd->lock ? thd->lock : thd->locked_tables))
  472. goto end;
  473. /* If we have less than two tables, we cannot have duplicates. */
  474. if (mylock->table_count < 2)
  475. goto end;
  476. lock_locks= mylock->locks;
  477. lock_tables= mylock->table;
  478. /* Prepare table related variables that don't change in loop. */
  479. DBUG_ASSERT((table->lock_position < mylock->table_count) &&
  480. (table == lock_tables[table->lock_position]));
  481. table_lock_data= lock_locks + table->lock_data_start;
  482. end_data= table_lock_data + table->lock_count;
  483. for (; haystack; haystack= haystack->next_global)
  484. {
  485. if (haystack->placeholder())
  486. continue;
  487. table2= haystack->table;
  488. if (table2->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
  489. continue;
  490. /* All tables in list must be in lock. */
  491. DBUG_ASSERT((table2->lock_position < mylock->table_count) &&
  492. (table2 == lock_tables[table2->lock_position]));
  493. for (lock_data2= lock_locks + table2->lock_data_start,
  494. end_data2= lock_data2 + table2->lock_count;
  495. lock_data2 < end_data2;
  496. lock_data2++)
  497. {
  498. THR_LOCK_DATA **lock_data;
  499. THR_LOCK *lock2= (*lock_data2)->lock;
  500. for (lock_data= table_lock_data;
  501. lock_data < end_data;
  502. lock_data++)
  503. {
  504. if ((*lock_data)->lock == lock2)
  505. {
  506. DBUG_PRINT("info", ("haystack match: '%s'", haystack->table_name));
  507. DBUG_RETURN(haystack);
  508. }
  509. }
  510. }
  511. }
  512. end:
  513. DBUG_PRINT("info", ("no duplicate found"));
  514. DBUG_RETURN(NULL);
  515. }
  516. /* unlock a set of external */
  517. static int unlock_external(THD *thd, TABLE **table,uint count)
  518. {
  519. int error,error_code;
  520. DBUG_ENTER("unlock_external");
  521. error_code=0;
  522. do
  523. {
  524. if ((*table)->current_lock != F_UNLCK)
  525. {
  526. (*table)->current_lock = F_UNLCK;
  527. if ((error=(*table)->file->external_lock(thd, F_UNLCK)))
  528. {
  529. error_code=error;
  530. print_lock_error(error_code, (*table)->file->table_type());
  531. }
  532. }
  533. table++;
  534. } while (--count);
  535. DBUG_RETURN(error_code);
  536. }
  537. /*
  538. Get lock structures from table structs and initialize locks
  539. SYNOPSIS
  540. get_lock_data()
  541. thd Thread handler
  542. table_ptr Pointer to tables that should be locks
  543. flags One of:
  544. GET_LOCK_UNLOCK: If we should send TL_IGNORE to
  545. store lock
  546. GET_LOCK_STORE_LOCKS: Store lock info in TABLE
  547. write_lock_used Store pointer to last table with WRITE_ALLOW_WRITE
  548. */
  549. static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
  550. uint flags, TABLE **write_lock_used)
  551. {
  552. uint i,tables,lock_count;
  553. MYSQL_LOCK *sql_lock;
  554. THR_LOCK_DATA **locks, **locks_buf, **locks_start;
  555. TABLE **to, **table_buf;
  556. DBUG_ENTER("get_lock_data");
  557. *write_lock_used=0;
  558. for (i=tables=lock_count=0 ; i < count ; i++)
  559. {
  560. if (table_ptr[i]->s->tmp_table != NON_TRANSACTIONAL_TMP_TABLE)
  561. {
  562. tables+=table_ptr[i]->file->lock_count();
  563. lock_count++;
  564. }
  565. /*
  566. To be able to open and lock for reading system tables like 'mysql.proc',
  567. when we already have some tables opened and locked, and avoid deadlocks
  568. we have to disallow write-locking of these tables with any other tables.
  569. */
  570. if (table_ptr[i]->s->system_table &&
  571. table_ptr[i]->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE &&
  572. count != 1)
  573. {
  574. my_error(ER_WRONG_LOCK_OF_SYSTEM_TABLE, MYF(0), table_ptr[i]->s->db,
  575. table_ptr[i]->s->table_name);
  576. return 0;
  577. }
  578. }
  579. /*
  580. Allocating twice the number of pointers for lock data for use in
  581. thr_mulit_lock(). This function reorders the lock data, but cannot
  582. update the table values. So the second part of the array is copied
  583. from the first part immediately before calling thr_multi_lock().
  584. */
  585. if (!(sql_lock= (MYSQL_LOCK*)
  586. my_malloc(sizeof(*sql_lock) +
  587. sizeof(THR_LOCK_DATA*) * tables * 2 +
  588. sizeof(table_ptr) * lock_count,
  589. MYF(0))))
  590. DBUG_RETURN(0);
  591. locks= locks_buf= sql_lock->locks= (THR_LOCK_DATA**) (sql_lock + 1);
  592. to= table_buf= sql_lock->table= (TABLE**) (locks + tables * 2);
  593. sql_lock->table_count=lock_count;
  594. sql_lock->lock_count=tables;
  595. for (i=0 ; i < count ; i++)
  596. {
  597. TABLE *table;
  598. enum thr_lock_type lock_type;
  599. if ((table=table_ptr[i])->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
  600. continue;
  601. lock_type= table->reginfo.lock_type;
  602. if (lock_type >= TL_WRITE_ALLOW_WRITE)
  603. {
  604. *write_lock_used=table;
  605. if (table->db_stat & HA_READ_ONLY)
  606. {
  607. my_error(ER_OPEN_AS_READONLY,MYF(0),table->alias);
  608. /* Clear the lock type of the lock data that are stored already. */
  609. sql_lock->lock_count= locks - sql_lock->locks;
  610. reset_lock_data(sql_lock);
  611. my_free((gptr) sql_lock,MYF(0));
  612. DBUG_RETURN(0);
  613. }
  614. }
  615. THR_LOCK_DATA **org_locks = locks;
  616. locks_start= locks;
  617. locks= table->file->store_lock(thd, locks,
  618. (flags & GET_LOCK_UNLOCK) ? TL_IGNORE :
  619. lock_type);
  620. if (flags & GET_LOCK_STORE_LOCKS)
  621. {
  622. table->lock_position= (uint) (to - table_buf);
  623. table->lock_data_start= (uint) (locks_start - locks_buf);
  624. table->lock_count= (uint) (locks - locks_start);
  625. }
  626. *to++= table;
  627. if (locks)
  628. for ( ; org_locks != locks ; org_locks++)
  629. (*org_locks)->debug_print_param= (void *) table;
  630. }
  631. DBUG_RETURN(sql_lock);
  632. }
  633. /*
  634. Reset lock type in lock data.
  635. SYNOPSIS
  636. reset_lock_data()
  637. sql_lock The MySQL lock.
  638. DESCRIPTION
  639. After a locking error we want to quit the locking of the table(s).
  640. The test case in the bug report for Bug #18544 has the following
  641. cases: 1. Locking error in lock_external() due to InnoDB timeout.
  642. 2. Locking error in get_lock_data() due to missing write permission.
  643. 3. Locking error in wait_if_global_read_lock() due to lock conflict.
  644. In all these cases we have already set the lock type into the lock
  645. data of the open table(s). If the table(s) are in the open table
  646. cache, they could be reused with the non-zero lock type set. This
  647. could lead to ignoring a different lock type with the next lock.
  648. Clear the lock type of all lock data. This ensures that the next
  649. lock request will set its lock type properly.
  650. RETURN
  651. void
  652. */
  653. static void reset_lock_data(MYSQL_LOCK *sql_lock)
  654. {
  655. THR_LOCK_DATA **ldata;
  656. THR_LOCK_DATA **ldata_end;
  657. for (ldata= sql_lock->locks, ldata_end= ldata + sql_lock->lock_count;
  658. ldata < ldata_end;
  659. ldata++)
  660. {
  661. /* Reset lock type. */
  662. (*ldata)->type= TL_UNLOCK;
  663. }
  664. }
  665. /*****************************************************************************
  666. Lock table based on the name.
  667. This is used when we need total access to a closed, not open table
  668. *****************************************************************************/
  669. /*
  670. Lock and wait for the named lock.
  671. SYNOPSIS
  672. lock_and_wait_for_table_name()
  673. thd Thread handler
  674. table_list Lock first table in this list
  675. NOTES
  676. Works together with global read lock.
  677. RETURN
  678. 0 ok
  679. 1 error
  680. */
  681. int lock_and_wait_for_table_name(THD *thd, TABLE_LIST *table_list)
  682. {
  683. int lock_retcode;
  684. int error= -1;
  685. DBUG_ENTER("lock_and_wait_for_table_name");
  686. if (wait_if_global_read_lock(thd, 0, 1))
  687. DBUG_RETURN(1);
  688. VOID(pthread_mutex_lock(&LOCK_open));
  689. if ((lock_retcode = lock_table_name(thd, table_list)) < 0)
  690. goto end;
  691. if (lock_retcode && wait_for_locked_table_names(thd, table_list))
  692. {
  693. unlock_table_name(thd, table_list);
  694. goto end;
  695. }
  696. error=0;
  697. end:
  698. pthread_mutex_unlock(&LOCK_open);
  699. start_waiting_global_read_lock(thd);
  700. DBUG_RETURN(error);
  701. }
  702. /*
  703. Put a not open table with an old refresh version in the table cache.
  704. SYNPOSIS
  705. lock_table_name()
  706. thd Thread handler
  707. table_list Lock first table in this list
  708. WARNING
  709. If you are going to update the table, you should use
  710. lock_and_wait_for_table_name instead of this function as this works
  711. together with 'FLUSH TABLES WITH READ LOCK'
  712. NOTES
  713. This will force any other threads that uses the table to release it
  714. as soon as possible.
  715. REQUIREMENTS
  716. One must have a lock on LOCK_open !
  717. RETURN:
  718. < 0 error
  719. == 0 table locked
  720. > 0 table locked, but someone is using it
  721. */
  722. int lock_table_name(THD *thd, TABLE_LIST *table_list)
  723. {
  724. TABLE *table;
  725. char key[MAX_DBKEY_LENGTH];
  726. char *db= table_list->db;
  727. int table_in_key_offset;
  728. uint key_length;
  729. HASH_SEARCH_STATE state;
  730. DBUG_ENTER("lock_table_name");
  731. DBUG_PRINT("enter",("db: %s name: %s", db, table_list->table_name));
  732. safe_mutex_assert_owner(&LOCK_open);
  733. table_in_key_offset= strmov(key, db) - key + 1;
  734. key_length= (uint)(strmov(key + table_in_key_offset, table_list->table_name)
  735. - key) + 1;
  736. /* Only insert the table if we haven't insert it already */
  737. for (table=(TABLE*) hash_first(&open_cache, (byte*)key, key_length, &state);
  738. table ;
  739. table = (TABLE*) hash_next(&open_cache, (byte*)key, key_length, &state))
  740. if (table->in_use == thd)
  741. DBUG_RETURN(0);
  742. /*
  743. Create a table entry with the right key and with an old refresh version
  744. Note that we must use my_malloc() here as this is freed by the table
  745. cache
  746. */
  747. if (!(table= (TABLE*) my_malloc(sizeof(*table)+key_length,
  748. MYF(MY_WME | MY_ZEROFILL))))
  749. DBUG_RETURN(-1);
  750. table->s= &table->share_not_to_be_used;
  751. memcpy((table->s->table_cache_key= (char*) (table+1)), key, key_length);
  752. table->s->db= table->s->table_cache_key;
  753. table->s->table_name= table->s->table_cache_key + table_in_key_offset;
  754. table->s->key_length=key_length;
  755. table->in_use=thd;
  756. table->locked_by_name=1;
  757. table_list->table=table;
  758. if (my_hash_insert(&open_cache, (byte*) table))
  759. {
  760. my_free((gptr) table,MYF(0));
  761. DBUG_RETURN(-1);
  762. }
  763. /* Return 1 if table is in use */
  764. DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
  765. RTFC_NO_FLAG)));
  766. }
  767. void unlock_table_name(THD *thd, TABLE_LIST *table_list)
  768. {
  769. if (table_list->table)
  770. {
  771. hash_delete(&open_cache, (byte*) table_list->table);
  772. broadcast_refresh();
  773. }
  774. }
  775. static bool locked_named_table(THD *thd, TABLE_LIST *table_list)
  776. {
  777. for (; table_list ; table_list=table_list->next_local)
  778. {
  779. if (table_list->table && table_is_used(table_list->table,0))
  780. return 1;
  781. }
  782. return 0; // All tables are locked
  783. }
  784. bool wait_for_locked_table_names(THD *thd, TABLE_LIST *table_list)
  785. {
  786. bool result=0;
  787. DBUG_ENTER("wait_for_locked_table_names");
  788. safe_mutex_assert_owner(&LOCK_open);
  789. while (locked_named_table(thd,table_list))
  790. {
  791. if (thd->killed)
  792. {
  793. result=1;
  794. break;
  795. }
  796. wait_for_refresh(thd);
  797. pthread_mutex_lock(&LOCK_open);
  798. }
  799. DBUG_RETURN(result);
  800. }
  801. /*
  802. Lock all tables in list with a name lock
  803. SYNOPSIS
  804. lock_table_names()
  805. thd Thread handle
  806. table_list Names of tables to lock
  807. NOTES
  808. If you are just locking one table, you should use
  809. lock_and_wait_for_table_name().
  810. REQUIREMENTS
  811. One must have a lock on LOCK_open when calling this
  812. RETURN
  813. 0 ok
  814. 1 Fatal error (end of memory ?)
  815. */
  816. bool lock_table_names(THD *thd, TABLE_LIST *table_list)
  817. {
  818. bool got_all_locks=1;
  819. TABLE_LIST *lock_table;
  820. for (lock_table= table_list; lock_table; lock_table= lock_table->next_local)
  821. {
  822. int got_lock;
  823. if ((got_lock=lock_table_name(thd,lock_table)) < 0)
  824. goto end; // Fatal error
  825. if (got_lock)
  826. got_all_locks=0; // Someone is using table
  827. }
  828. /* If some table was in use, wait until we got the lock */
  829. if (!got_all_locks && wait_for_locked_table_names(thd, table_list))
  830. goto end;
  831. return 0;
  832. end:
  833. unlock_table_names(thd, table_list, lock_table);
  834. return 1;
  835. }
  836. /*
  837. Unlock all tables in list with a name lock
  838. SYNOPSIS
  839. unlock_table_names()
  840. thd Thread handle
  841. table_list Names of tables to unlock
  842. last_table Don't unlock any tables after this one.
  843. (default 0, which will unlock all tables)
  844. NOTES
  845. One must have a lock on LOCK_open when calling this.
  846. This function will broadcast refresh signals to inform other threads
  847. that the name locks are removed.
  848. RETURN
  849. 0 ok
  850. 1 Fatal error (end of memory ?)
  851. */
  852. void unlock_table_names(THD *thd, TABLE_LIST *table_list,
  853. TABLE_LIST *last_table)
  854. {
  855. for (TABLE_LIST *table= table_list;
  856. table != last_table;
  857. table= table->next_local)
  858. unlock_table_name(thd,table);
  859. broadcast_refresh();
  860. }
  861. static void print_lock_error(int error, const char *table)
  862. {
  863. int textno;
  864. DBUG_ENTER("print_lock_error");
  865. switch (error) {
  866. case HA_ERR_LOCK_WAIT_TIMEOUT:
  867. textno=ER_LOCK_WAIT_TIMEOUT;
  868. break;
  869. case HA_ERR_READ_ONLY_TRANSACTION:
  870. textno=ER_READ_ONLY_TRANSACTION;
  871. break;
  872. case HA_ERR_LOCK_DEADLOCK:
  873. textno=ER_LOCK_DEADLOCK;
  874. break;
  875. case HA_ERR_WRONG_COMMAND:
  876. textno=ER_ILLEGAL_HA;
  877. break;
  878. default:
  879. textno=ER_CANT_LOCK;
  880. break;
  881. }
  882. if ( textno == ER_ILLEGAL_HA )
  883. my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), table);
  884. else
  885. my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), error);
  886. DBUG_VOID_RETURN;
  887. }
  888. /****************************************************************************
  889. Handling of global read locks
  890. Taking the global read lock is TWO steps (2nd step is optional; without
  891. it, COMMIT of existing transactions will be allowed):
  892. lock_global_read_lock() THEN make_global_read_lock_block_commit().
  893. The global locks are handled through the global variables:
  894. global_read_lock
  895. count of threads which have the global read lock (i.e. have completed at
  896. least the first step above)
  897. global_read_lock_blocks_commit
  898. count of threads which have the global read lock and block
  899. commits (i.e. are in or have completed the second step above)
  900. waiting_for_read_lock
  901. count of threads which want to take a global read lock but cannot
  902. protect_against_global_read_lock
  903. count of threads which have set protection against global read lock.
  904. access to them is protected with a mutex LOCK_global_read_lock
  905. (XXX: one should never take LOCK_open if LOCK_global_read_lock is
  906. taken, otherwise a deadlock may occur. Other mutexes could be a
  907. problem too - grep the code for global_read_lock if you want to use
  908. any other mutex here) Also one must not hold LOCK_open when calling
  909. wait_if_global_read_lock(). When the thread with the global read lock
  910. tries to close its tables, it needs to take LOCK_open in
  911. close_thread_table().
  912. How blocking of threads by global read lock is achieved: that's
  913. advisory. Any piece of code which should be blocked by global read lock must
  914. be designed like this:
  915. - call to wait_if_global_read_lock(). When this returns 0, no global read
  916. lock is owned; if argument abort_on_refresh was 0, none can be obtained.
  917. - job
  918. - if abort_on_refresh was 0, call to start_waiting_global_read_lock() to
  919. allow other threads to get the global read lock. I.e. removal of the
  920. protection.
  921. (Note: it's a bit like an implementation of rwlock).
  922. [ I am sorry to mention some SQL syntaxes below I know I shouldn't but found
  923. no better descriptive way ]
  924. Why does FLUSH TABLES WITH READ LOCK need to block COMMIT: because it's used
  925. to read a non-moving SHOW MASTER STATUS, and a COMMIT writes to the binary
  926. log.
  927. Why getting the global read lock is two steps and not one. Because FLUSH
  928. TABLES WITH READ LOCK needs to insert one other step between the two:
  929. flushing tables. So the order is
  930. 1) lock_global_read_lock() (prevents any new table write locks, i.e. stalls
  931. all new updates)
  932. 2) close_cached_tables() (the FLUSH TABLES), which will wait for tables
  933. currently opened and being updated to close (so it's possible that there is
  934. a moment where all new updates of server are stalled *and* FLUSH TABLES WITH
  935. READ LOCK is, too).
  936. 3) make_global_read_lock_block_commit().
  937. If we have merged 1) and 3) into 1), we would have had this deadlock:
  938. imagine thread 1 and 2, in non-autocommit mode, thread 3, and an InnoDB
  939. table t.
  940. thd1: SELECT * FROM t FOR UPDATE;
  941. thd2: UPDATE t SET a=1; # blocked by row-level locks of thd1
  942. thd3: FLUSH TABLES WITH READ LOCK; # blocked in close_cached_tables() by the
  943. table instance of thd2
  944. thd1: COMMIT; # blocked by thd3.
  945. thd1 blocks thd2 which blocks thd3 which blocks thd1: deadlock.
  946. Note that we need to support that one thread does
  947. FLUSH TABLES WITH READ LOCK; and then COMMIT;
  948. (that's what innobackup does, for some good reason).
  949. So in this exceptional case the COMMIT should not be blocked by the FLUSH
  950. TABLES WITH READ LOCK.
  951. ****************************************************************************/
  952. volatile uint global_read_lock=0;
  953. volatile uint global_read_lock_blocks_commit=0;
  954. static volatile uint protect_against_global_read_lock=0;
  955. static volatile uint waiting_for_read_lock=0;
  956. #define GOT_GLOBAL_READ_LOCK 1
  957. #define MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT 2
  958. bool lock_global_read_lock(THD *thd)
  959. {
  960. DBUG_ENTER("lock_global_read_lock");
  961. if (!thd->global_read_lock)
  962. {
  963. const char *old_message;
  964. (void) pthread_mutex_lock(&LOCK_global_read_lock);
  965. old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
  966. "Waiting to get readlock");
  967. DBUG_PRINT("info",
  968. ("waiting_for: %d protect_against: %d",
  969. waiting_for_read_lock, protect_against_global_read_lock));
  970. waiting_for_read_lock++;
  971. while (protect_against_global_read_lock && !thd->killed)
  972. pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
  973. waiting_for_read_lock--;
  974. if (thd->killed)
  975. {
  976. thd->exit_cond(old_message);
  977. DBUG_RETURN(1);
  978. }
  979. thd->global_read_lock= GOT_GLOBAL_READ_LOCK;
  980. global_read_lock++;
  981. thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
  982. }
  983. /*
  984. We DON'T set global_read_lock_blocks_commit now, it will be set after
  985. tables are flushed (as the present function serves for FLUSH TABLES WITH
  986. READ LOCK only). Doing things in this order is necessary to avoid
  987. deadlocks (we must allow COMMIT until all tables are closed; we should not
  988. forbid it before, or we can have a 3-thread deadlock if 2 do SELECT FOR
  989. UPDATE and one does FLUSH TABLES WITH READ LOCK).
  990. */
  991. DBUG_RETURN(0);
  992. }
  993. void unlock_global_read_lock(THD *thd)
  994. {
  995. uint tmp;
  996. DBUG_ENTER("unlock_global_read_lock");
  997. DBUG_PRINT("info",
  998. ("global_read_lock: %u global_read_lock_blocks_commit: %u",
  999. global_read_lock, global_read_lock_blocks_commit));
  1000. pthread_mutex_lock(&LOCK_global_read_lock);
  1001. tmp= --global_read_lock;
  1002. if (thd->global_read_lock == MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT)
  1003. --global_read_lock_blocks_commit;
  1004. pthread_mutex_unlock(&LOCK_global_read_lock);
  1005. /* Send the signal outside the mutex to avoid a context switch */
  1006. if (!tmp)
  1007. {
  1008. DBUG_PRINT("signal", ("Broadcasting COND_global_read_lock"));
  1009. pthread_cond_broadcast(&COND_global_read_lock);
  1010. }
  1011. thd->global_read_lock= 0;
  1012. DBUG_VOID_RETURN;
  1013. }
  1014. #define must_wait (global_read_lock && \
  1015. (is_not_commit || \
  1016. global_read_lock_blocks_commit))
  1017. bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh,
  1018. bool is_not_commit)
  1019. {
  1020. const char *old_message;
  1021. bool result= 0, need_exit_cond;
  1022. DBUG_ENTER("wait_if_global_read_lock");
  1023. LINT_INIT(old_message);
  1024. /*
  1025. Assert that we do not own LOCK_open. If we would own it, other
  1026. threads could not close their tables. This would make a pretty
  1027. deadlock.
  1028. */
  1029. safe_mutex_assert_not_owner(&LOCK_open);
  1030. (void) pthread_mutex_lock(&LOCK_global_read_lock);
  1031. if ((need_exit_cond= must_wait))
  1032. {
  1033. if (thd->global_read_lock) // This thread had the read locks
  1034. {
  1035. if (is_not_commit)
  1036. my_message(ER_CANT_UPDATE_WITH_READLOCK,
  1037. ER(ER_CANT_UPDATE_WITH_READLOCK), MYF(0));
  1038. (void) pthread_mutex_unlock(&LOCK_global_read_lock);
  1039. /*
  1040. We allow FLUSHer to COMMIT; we assume FLUSHer knows what it does.
  1041. This allowance is needed to not break existing versions of innobackup
  1042. which do a BEGIN; INSERT; FLUSH TABLES WITH READ LOCK; COMMIT.
  1043. */
  1044. DBUG_RETURN(is_not_commit);
  1045. }
  1046. old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
  1047. "Waiting for release of readlock");
  1048. while (must_wait && ! thd->killed &&
  1049. (!abort_on_refresh || thd->version == refresh_version))
  1050. {
  1051. DBUG_PRINT("signal", ("Waiting for COND_global_read_lock"));
  1052. (void) pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
  1053. DBUG_PRINT("signal", ("Got COND_global_read_lock"));
  1054. }
  1055. if (thd->killed)
  1056. result=1;
  1057. }
  1058. if (!abort_on_refresh && !result)
  1059. protect_against_global_read_lock++;
  1060. /*
  1061. The following is only true in case of a global read locks (which is rare)
  1062. and if old_message is set
  1063. */
  1064. if (unlikely(need_exit_cond))
  1065. thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
  1066. else
  1067. pthread_mutex_unlock(&LOCK_global_read_lock);
  1068. DBUG_RETURN(result);
  1069. }
  1070. void start_waiting_global_read_lock(THD *thd)
  1071. {
  1072. bool tmp;
  1073. DBUG_ENTER("start_waiting_global_read_lock");
  1074. if (unlikely(thd->global_read_lock))
  1075. DBUG_VOID_RETURN;
  1076. (void) pthread_mutex_lock(&LOCK_global_read_lock);
  1077. tmp= (!--protect_against_global_read_lock &&
  1078. (waiting_for_read_lock || global_read_lock_blocks_commit));
  1079. (void) pthread_mutex_unlock(&LOCK_global_read_lock);
  1080. if (tmp)
  1081. pthread_cond_broadcast(&COND_global_read_lock);
  1082. DBUG_VOID_RETURN;
  1083. }
  1084. bool make_global_read_lock_block_commit(THD *thd)
  1085. {
  1086. bool error;
  1087. const char *old_message;
  1088. DBUG_ENTER("make_global_read_lock_block_commit");
  1089. /*
  1090. If we didn't succeed lock_global_read_lock(), or if we already suceeded
  1091. make_global_read_lock_block_commit(), do nothing.
  1092. */
  1093. if (thd->global_read_lock != GOT_GLOBAL_READ_LOCK)
  1094. DBUG_RETURN(0);
  1095. pthread_mutex_lock(&LOCK_global_read_lock);
  1096. /* increment this BEFORE waiting on cond (otherwise race cond) */
  1097. global_read_lock_blocks_commit++;
  1098. /* For testing we set up some blocking, to see if we can be killed */
  1099. DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
  1100. protect_against_global_read_lock++;);
  1101. old_message= thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
  1102. "Waiting for all running commits to finish");
  1103. while (protect_against_global_read_lock && !thd->killed)
  1104. pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
  1105. DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
  1106. protect_against_global_read_lock--;);
  1107. if ((error= test(thd->killed)))
  1108. global_read_lock_blocks_commit--; // undo what we did
  1109. else
  1110. thd->global_read_lock= MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT;
  1111. thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
  1112. DBUG_RETURN(error);
  1113. }
  1114. /*
  1115. Broadcast COND_refresh and COND_global_read_lock.
  1116. SYNOPSIS
  1117. broadcast_refresh()
  1118. void No parameters.
  1119. DESCRIPTION
  1120. Due to a bug in a threading library it could happen that a signal
  1121. did not reach its target. A condition for this was that the same
  1122. condition variable was used with different mutexes in
  1123. pthread_cond_wait(). Some time ago we changed LOCK_open to
  1124. LOCK_global_read_lock in global read lock handling. So COND_refresh
  1125. was used with LOCK_open and LOCK_global_read_lock.
  1126. We did now also change from COND_refresh to COND_global_read_lock
  1127. in global read lock handling. But now it is necessary to signal
  1128. both conditions at the same time.
  1129. NOTE
  1130. When signalling COND_global_read_lock within the global read lock
  1131. handling, it is not necessary to also signal COND_refresh.
  1132. RETURN
  1133. void
  1134. */
  1135. void broadcast_refresh(void)
  1136. {
  1137. VOID(pthread_cond_broadcast(&COND_refresh));
  1138. VOID(pthread_cond_broadcast(&COND_global_read_lock));
  1139. }