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.

576 lines
15 KiB

9 years ago
MDEV-12674 Innodb_row_lock_current_waits has overflow There is a race condition related to the variable srv_stats.n_lock_wait_current_count, which is only incremented and decremented by the function lock_wait_suspend_thread(), The incrementing is protected by lock_sys->wait_mutex, but the decrementing does not appear to be protected by anything. This mismatch could allow the counter to be corrupted when a transactional InnoDB table or record lock wait is terminating roughly at the same time with the start of a wait on a (possibly different) lock. ib_counter_t: Remove some unused methods. Prevent instantiation for N=1. Add an inc() method that takes a slot index as a parameter. single_indexer_t: Remove. simple_counter<typename Type, bool atomic=false>: A new counter wrapper. Optionally use atomic memory operations for modifying the counter. Aligned to the cache line size. lsn_ctr_1_t, ulint_ctr_1_t, int64_ctr_1_t: Define as simple_counter<Type>. These counters are either only incremented (and we do not care about losing some increment operations), or the increment/decrement operations are protected by some mutex. srv_stats_t::os_log_pending_writes: Document that the number is protected by log_sys->mutex. srv_stats_t::n_lock_wait_current_count: Use simple_counter<ulint, true>, that is, atomic inc() and dec() operations. lock_wait_suspend_thread(): Release the mutexes before incrementing the counters. Avoid acquiring the lock mutex if the lock wait has already been resolved. Atomically increment and decrement srv_stats.n_lock_wait_current_count. row_insert_for_mysql(), row_update_for_mysql(), row_update_cascade_for_mysql(): Use the inc() method with the trx->id as the slot index. This is a non-functional change, just using inc() instead of add(1). buf_LRU_get_free_block(): Replace the method add(index, n) with inc(). There is no slot index in the simple_counter.
9 years ago
MDEV-12674 Innodb_row_lock_current_waits has overflow There is a race condition related to the variable srv_stats.n_lock_wait_current_count, which is only incremented and decremented by the function lock_wait_suspend_thread(), The incrementing is protected by lock_sys->wait_mutex, but the decrementing does not appear to be protected by anything. This mismatch could allow the counter to be corrupted when a transactional InnoDB table or record lock wait is terminating roughly at the same time with the start of a wait on a (possibly different) lock. ib_counter_t: Remove some unused methods. Prevent instantiation for N=1. Add an inc() method that takes a slot index as a parameter. single_indexer_t: Remove. simple_counter<typename Type, bool atomic=false>: A new counter wrapper. Optionally use atomic memory operations for modifying the counter. Aligned to the cache line size. lsn_ctr_1_t, ulint_ctr_1_t, int64_ctr_1_t: Define as simple_counter<Type>. These counters are either only incremented (and we do not care about losing some increment operations), or the increment/decrement operations are protected by some mutex. srv_stats_t::os_log_pending_writes: Document that the number is protected by log_sys->mutex. srv_stats_t::n_lock_wait_current_count: Use simple_counter<ulint, true>, that is, atomic inc() and dec() operations. lock_wait_suspend_thread(): Release the mutexes before incrementing the counters. Avoid acquiring the lock mutex if the lock wait has already been resolved. Atomically increment and decrement srv_stats.n_lock_wait_current_count. row_insert_for_mysql(), row_update_for_mysql(), row_update_cascade_for_mysql(): Use the inc() method with the trx->id as the slot index. This is a non-functional change, just using inc() instead of add(1). buf_LRU_get_free_block(): Replace the method add(index, n) with inc(). There is no slot index in the simple_counter.
9 years ago
MDEV-12674 Innodb_row_lock_current_waits has overflow There is a race condition related to the variable srv_stats.n_lock_wait_current_count, which is only incremented and decremented by the function lock_wait_suspend_thread(), The incrementing is protected by lock_sys->wait_mutex, but the decrementing does not appear to be protected by anything. This mismatch could allow the counter to be corrupted when a transactional InnoDB table or record lock wait is terminating roughly at the same time with the start of a wait on a (possibly different) lock. ib_counter_t: Remove some unused methods. Prevent instantiation for N=1. Add an inc() method that takes a slot index as a parameter. single_indexer_t: Remove. simple_counter<typename Type, bool atomic=false>: A new counter wrapper. Optionally use atomic memory operations for modifying the counter. Aligned to the cache line size. lsn_ctr_1_t, ulint_ctr_1_t, int64_ctr_1_t: Define as simple_counter<Type>. These counters are either only incremented (and we do not care about losing some increment operations), or the increment/decrement operations are protected by some mutex. srv_stats_t::os_log_pending_writes: Document that the number is protected by log_sys->mutex. srv_stats_t::n_lock_wait_current_count: Use simple_counter<ulint, true>, that is, atomic inc() and dec() operations. lock_wait_suspend_thread(): Release the mutexes before incrementing the counters. Avoid acquiring the lock mutex if the lock wait has already been resolved. Atomically increment and decrement srv_stats.n_lock_wait_current_count. row_insert_for_mysql(), row_update_for_mysql(), row_update_cascade_for_mysql(): Use the inc() method with the trx->id as the slot index. This is a non-functional change, just using inc() instead of add(1). buf_LRU_get_free_block(): Replace the method add(index, n) with inc(). There is no slot index in the simple_counter.
9 years ago
MDEV-12674 Innodb_row_lock_current_waits has overflow There is a race condition related to the variable srv_stats.n_lock_wait_current_count, which is only incremented and decremented by the function lock_wait_suspend_thread(), The incrementing is protected by lock_sys->wait_mutex, but the decrementing does not appear to be protected by anything. This mismatch could allow the counter to be corrupted when a transactional InnoDB table or record lock wait is terminating roughly at the same time with the start of a wait on a (possibly different) lock. ib_counter_t: Remove some unused methods. Prevent instantiation for N=1. Add an inc() method that takes a slot index as a parameter. single_indexer_t: Remove. simple_counter<typename Type, bool atomic=false>: A new counter wrapper. Optionally use atomic memory operations for modifying the counter. Aligned to the cache line size. lsn_ctr_1_t, ulint_ctr_1_t, int64_ctr_1_t: Define as simple_counter<Type>. These counters are either only incremented (and we do not care about losing some increment operations), or the increment/decrement operations are protected by some mutex. srv_stats_t::os_log_pending_writes: Document that the number is protected by log_sys->mutex. srv_stats_t::n_lock_wait_current_count: Use simple_counter<ulint, true>, that is, atomic inc() and dec() operations. lock_wait_suspend_thread(): Release the mutexes before incrementing the counters. Avoid acquiring the lock mutex if the lock wait has already been resolved. Atomically increment and decrement srv_stats.n_lock_wait_current_count. row_insert_for_mysql(), row_update_for_mysql(), row_update_cascade_for_mysql(): Use the inc() method with the trx->id as the slot index. This is a non-functional change, just using inc() instead of add(1). buf_LRU_get_free_block(): Replace the method add(index, n) with inc(). There is no slot index in the simple_counter.
9 years ago
MDEV-11638 Encryption causes race conditions in InnoDB shutdown InnoDB shutdown failed to properly take fil_crypt_thread() into account. The encryption threads were signalled to shut down together with other non-critical tasks. This could be much too early in case of slow shutdown, which could need minutes to complete the purge. Furthermore, InnoDB failed to wait for the fil_crypt_thread() to actually exit before proceeding to the final steps of shutdown, causing the race conditions. Furthermore, the log_scrub_thread() was shut down way too early. Also it should remain until the SRV_SHUTDOWN_FLUSH_PHASE. fil_crypt_threads_end(): Remove. This would cause the threads to be terminated way too early. srv_buf_dump_thread_active, srv_dict_stats_thread_active, lock_sys->timeout_thread_active, log_scrub_thread_active, srv_monitor_active, srv_error_monitor_active: Remove a race condition between startup and shutdown, by setting these in the startup thread that creates threads, not in each created thread. In this way, once the flag is cleared, it will remain cleared during shutdown. srv_n_fil_crypt_threads_started, fil_crypt_threads_event: Declare in global rather than static scope. log_scrub_event, srv_log_scrub_thread_active, log_scrub_thread(): Declare in static rather than global scope. Let these be created by log_init() and freed by log_shutdown(). rotate_thread_t::should_shutdown(): Do not shut down before the SRV_SHUTDOWN_FLUSH_PHASE. srv_any_background_threads_are_active(): Remove. These checks now exist in logs_empty_and_mark_files_at_shutdown(). logs_empty_and_mark_files_at_shutdown(): Shut down the threads in the proper order. Keep fil_crypt_thread() and log_scrub_thread() alive until SRV_SHUTDOWN_FLUSH_PHASE, and check that they actually terminate.
9 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2014, 2017, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file lock/lock0wait.cc
  16. The transaction lock system
  17. Created 25/5/2010 Sunny Bains
  18. *******************************************************/
  19. #define LOCK_MODULE_IMPLEMENTATION
  20. #include "srv0mon.h"
  21. #include "que0que.h"
  22. #include "lock0lock.h"
  23. #include "row0mysql.h"
  24. #include "srv0start.h"
  25. #include "ha_prototypes.h"
  26. #include "lock0priv.h"
  27. #include <mysql/service_wsrep.h>
  28. /*********************************************************************//**
  29. Print the contents of the lock_sys_t::waiting_threads array. */
  30. static
  31. void
  32. lock_wait_table_print(void)
  33. /*=======================*/
  34. {
  35. ulint i;
  36. const srv_slot_t* slot;
  37. ut_ad(lock_wait_mutex_own());
  38. slot = lock_sys->waiting_threads;
  39. for (i = 0; i < OS_THREAD_MAX_N; i++, ++slot) {
  40. fprintf(stderr,
  41. "Slot %lu: thread type %lu,"
  42. " in use %lu, susp %lu, timeout %lu, time %lu\n",
  43. (ulong) i,
  44. (ulong) slot->type,
  45. (ulong) slot->in_use,
  46. (ulong) slot->suspended,
  47. slot->wait_timeout,
  48. (ulong) difftime(ut_time(), slot->suspend_time));
  49. }
  50. }
  51. /*********************************************************************//**
  52. Release a slot in the lock_sys_t::waiting_threads. Adjust the array last pointer
  53. if there are empty slots towards the end of the table. */
  54. static
  55. void
  56. lock_wait_table_release_slot(
  57. /*=========================*/
  58. srv_slot_t* slot) /*!< in: slot to release */
  59. {
  60. #ifdef UNIV_DEBUG
  61. srv_slot_t* upper = lock_sys->waiting_threads + OS_THREAD_MAX_N;
  62. #endif /* UNIV_DEBUG */
  63. lock_wait_mutex_enter();
  64. ut_ad(slot->in_use);
  65. ut_ad(slot->thr != NULL);
  66. ut_ad(slot->thr->slot != NULL);
  67. ut_ad(slot->thr->slot == slot);
  68. /* Must be within the array boundaries. */
  69. ut_ad(slot >= lock_sys->waiting_threads);
  70. ut_ad(slot < upper);
  71. /* Note: When we reserve the slot we use the trx_t::mutex to update
  72. the slot values to change the state to reserved. Here we are using the
  73. lock mutex to change the state of the slot to free. This is by design,
  74. because when we query the slot state we always hold both the lock and
  75. trx_t::mutex. To reduce contention on the lock mutex when reserving the
  76. slot we avoid acquiring the lock mutex. */
  77. lock_mutex_enter();
  78. slot->thr->slot = NULL;
  79. slot->thr = NULL;
  80. slot->in_use = FALSE;
  81. lock_mutex_exit();
  82. /* Scan backwards and adjust the last free slot pointer. */
  83. for (slot = lock_sys->last_slot;
  84. slot > lock_sys->waiting_threads && !slot->in_use;
  85. --slot) {
  86. /* No op */
  87. }
  88. /* Either the array is empty or the last scanned slot is in use. */
  89. ut_ad(slot->in_use || slot == lock_sys->waiting_threads);
  90. lock_sys->last_slot = slot + 1;
  91. /* The last slot is either outside of the array boundary or it's
  92. on an empty slot. */
  93. ut_ad(lock_sys->last_slot == upper || !lock_sys->last_slot->in_use);
  94. ut_ad(lock_sys->last_slot >= lock_sys->waiting_threads);
  95. ut_ad(lock_sys->last_slot <= upper);
  96. lock_wait_mutex_exit();
  97. }
  98. /*********************************************************************//**
  99. Reserves a slot in the thread table for the current user OS thread.
  100. @return reserved slot */
  101. static
  102. srv_slot_t*
  103. lock_wait_table_reserve_slot(
  104. /*=========================*/
  105. que_thr_t* thr, /*!< in: query thread associated
  106. with the user OS thread */
  107. ulong wait_timeout) /*!< in: lock wait timeout value */
  108. {
  109. ulint i;
  110. srv_slot_t* slot;
  111. ut_ad(lock_wait_mutex_own());
  112. ut_ad(trx_mutex_own(thr_get_trx(thr)));
  113. slot = lock_sys->waiting_threads;
  114. for (i = OS_THREAD_MAX_N; i--; ++slot) {
  115. if (!slot->in_use) {
  116. slot->in_use = TRUE;
  117. slot->thr = thr;
  118. slot->thr->slot = slot;
  119. if (slot->event == NULL) {
  120. slot->event = os_event_create();
  121. ut_a(slot->event);
  122. }
  123. os_event_reset(slot->event);
  124. slot->suspended = TRUE;
  125. slot->suspend_time = ut_time();
  126. slot->wait_timeout = wait_timeout;
  127. if (slot == lock_sys->last_slot) {
  128. ++lock_sys->last_slot;
  129. }
  130. ut_ad(lock_sys->last_slot
  131. <= lock_sys->waiting_threads + OS_THREAD_MAX_N);
  132. return(slot);
  133. }
  134. }
  135. ut_print_timestamp(stderr);
  136. fprintf(stderr,
  137. " InnoDB: There appear to be %lu user"
  138. " threads currently waiting\n"
  139. "InnoDB: inside InnoDB, which is the"
  140. " upper limit. Cannot continue operation.\n"
  141. "InnoDB: As a last thing, we print"
  142. " a list of waiting threads.\n", (ulong) OS_THREAD_MAX_N);
  143. lock_wait_table_print();
  144. ut_error;
  145. return(NULL);
  146. }
  147. #ifdef WITH_WSREP
  148. /*********************************************************************//**
  149. check if lock timeout was for priority thread,
  150. as a side effect trigger lock monitor
  151. @return false for regular lock timeout */
  152. static ibool
  153. wsrep_is_BF_lock_timeout(
  154. /*====================*/
  155. trx_t* trx) /* in: trx to check for lock priority */
  156. {
  157. if (wsrep_on(trx->mysql_thd) &&
  158. wsrep_thd_is_BF(trx->mysql_thd, FALSE)) {
  159. fprintf(stderr, "WSREP: BF lock wait long\n");
  160. srv_print_innodb_monitor = TRUE;
  161. srv_print_innodb_lock_monitor = TRUE;
  162. os_event_set(srv_monitor_event);
  163. return TRUE;
  164. }
  165. return FALSE;
  166. }
  167. #endif /* WITH_WSREP */
  168. /***************************************************************//**
  169. Puts a user OS thread to wait for a lock to be released. If an error
  170. occurs during the wait trx->error_state associated with thr is
  171. != DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
  172. are possible errors. DB_DEADLOCK is returned if selective deadlock
  173. resolution chose this transaction as a victim. */
  174. UNIV_INTERN
  175. void
  176. lock_wait_suspend_thread(
  177. /*=====================*/
  178. que_thr_t* thr) /*!< in: query thread associated with the
  179. user OS thread */
  180. {
  181. srv_slot_t* slot;
  182. double wait_time;
  183. trx_t* trx;
  184. ulint had_dict_lock;
  185. ibool was_declared_inside_innodb;
  186. ib_int64_t start_time = 0;
  187. ib_int64_t finish_time;
  188. ulint sec;
  189. ulint ms;
  190. ulong lock_wait_timeout;
  191. trx = thr_get_trx(thr);
  192. if (trx->mysql_thd != 0) {
  193. DEBUG_SYNC_C("lock_wait_suspend_thread_enter");
  194. }
  195. /* InnoDB system transactions (such as the purge, and
  196. incomplete transactions that are being rolled back after crash
  197. recovery) will use the global value of
  198. innodb_lock_wait_timeout, because trx->mysql_thd == NULL. */
  199. lock_wait_timeout = trx_lock_wait_timeout_get(trx);
  200. lock_wait_mutex_enter();
  201. trx_mutex_enter(trx);
  202. trx->error_state = DB_SUCCESS;
  203. if (thr->state == QUE_THR_RUNNING) {
  204. ut_ad(thr->is_active);
  205. /* The lock has already been released or this transaction
  206. was chosen as a deadlock victim: no need to suspend */
  207. if (trx->lock.was_chosen_as_deadlock_victim) {
  208. trx->error_state = DB_DEADLOCK;
  209. trx->lock.was_chosen_as_deadlock_victim = FALSE;
  210. }
  211. lock_wait_mutex_exit();
  212. trx_mutex_exit(trx);
  213. return;
  214. }
  215. ut_ad(!thr->is_active);
  216. slot = lock_wait_table_reserve_slot(thr, lock_wait_timeout);
  217. lock_wait_mutex_exit();
  218. trx_mutex_exit(trx);
  219. if (thr->lock_state == QUE_THR_LOCK_ROW) {
  220. srv_stats.n_lock_wait_count.inc();
  221. srv_stats.n_lock_wait_current_count.inc();
  222. if (ut_usectime(&sec, &ms) == -1) {
  223. start_time = -1;
  224. } else {
  225. start_time = (ib_int64_t) sec * 1000000 + ms;
  226. }
  227. }
  228. ulint lock_type = ULINT_UNDEFINED;
  229. /* The wait_lock can be cleared by another thread when the
  230. lock is released. But the wait can only be initiated by the
  231. current thread which owns the transaction. Only acquire the
  232. mutex if the wait_lock is still active. */
  233. if (const lock_t* wait_lock = trx->lock.wait_lock) {
  234. lock_mutex_enter();
  235. wait_lock = trx->lock.wait_lock;
  236. if (wait_lock) {
  237. lock_type = lock_get_type_low(wait_lock);
  238. }
  239. lock_mutex_exit();
  240. }
  241. had_dict_lock = trx->dict_operation_lock_mode;
  242. switch (had_dict_lock) {
  243. case 0:
  244. break;
  245. case RW_S_LATCH:
  246. /* Release foreign key check latch */
  247. row_mysql_unfreeze_data_dictionary(trx);
  248. DEBUG_SYNC_C("lock_wait_release_s_latch_before_sleep");
  249. break;
  250. default:
  251. /* There should never be a lock wait when the
  252. dictionary latch is reserved in X mode. Dictionary
  253. transactions should only acquire locks on dictionary
  254. tables, not other tables. All access to dictionary
  255. tables should be covered by dictionary
  256. transactions. */
  257. ut_error;
  258. }
  259. ut_a(trx->dict_operation_lock_mode == 0);
  260. /* Suspend this thread and wait for the event. */
  261. was_declared_inside_innodb = trx->declared_to_be_inside_innodb;
  262. if (was_declared_inside_innodb) {
  263. /* We must declare this OS thread to exit InnoDB, since a
  264. possible other thread holding a lock which this thread waits
  265. for must be allowed to enter, sooner or later */
  266. srv_conc_force_exit_innodb(trx);
  267. }
  268. /* Unknown is also treated like a record lock */
  269. if (lock_type == ULINT_UNDEFINED || lock_type == LOCK_REC) {
  270. thd_wait_begin(trx->mysql_thd, THD_WAIT_ROW_LOCK);
  271. } else {
  272. ut_ad(lock_type == LOCK_TABLE);
  273. thd_wait_begin(trx->mysql_thd, THD_WAIT_TABLE_LOCK);
  274. }
  275. os_event_wait(slot->event);
  276. thd_wait_end(trx->mysql_thd);
  277. /* After resuming, reacquire the data dictionary latch if
  278. necessary. */
  279. if (was_declared_inside_innodb) {
  280. /* Return back inside InnoDB */
  281. srv_conc_force_enter_innodb(trx);
  282. }
  283. if (had_dict_lock) {
  284. row_mysql_freeze_data_dictionary(trx);
  285. }
  286. wait_time = ut_difftime(ut_time(), slot->suspend_time);
  287. /* Release the slot for others to use */
  288. lock_wait_table_release_slot(slot);
  289. if (thr->lock_state == QUE_THR_LOCK_ROW) {
  290. ulint diff_time;
  291. if (ut_usectime(&sec, &ms) == -1) {
  292. finish_time = -1;
  293. } else {
  294. finish_time = (ib_int64_t) sec * 1000000 + ms;
  295. }
  296. diff_time = (finish_time > start_time) ?
  297. (ulint) (finish_time - start_time) : 0;
  298. srv_stats.n_lock_wait_current_count.dec();
  299. srv_stats.n_lock_wait_time.add(diff_time);
  300. /* Only update the variable if we successfully
  301. retrieved the start and finish times. See Bug#36819. */
  302. if (diff_time > lock_sys->n_lock_max_wait_time
  303. && start_time != -1
  304. && finish_time != -1) {
  305. lock_sys->n_lock_max_wait_time = diff_time;
  306. }
  307. /* Record the lock wait time for this thread */
  308. thd_set_lock_wait_time(trx->mysql_thd, diff_time);
  309. }
  310. if (lock_wait_timeout < 100000000
  311. && wait_time > (double) lock_wait_timeout) {
  312. #ifdef WITH_WSREP
  313. if (!wsrep_on(trx->mysql_thd) ||
  314. (!wsrep_is_BF_lock_timeout(trx) &&
  315. trx->error_state != DB_DEADLOCK)) {
  316. #endif /* WITH_WSREP */
  317. trx->error_state = DB_LOCK_WAIT_TIMEOUT;
  318. #ifdef WITH_WSREP
  319. }
  320. #endif /* WITH_WSREP */
  321. MONITOR_INC(MONITOR_TIMEOUT);
  322. }
  323. if (trx_is_interrupted(trx)) {
  324. trx->error_state = DB_INTERRUPTED;
  325. }
  326. }
  327. /********************************************************************//**
  328. Releases a user OS thread waiting for a lock to be released, if the
  329. thread is already suspended. */
  330. UNIV_INTERN
  331. void
  332. lock_wait_release_thread_if_suspended(
  333. /*==================================*/
  334. que_thr_t* thr) /*!< in: query thread associated with the
  335. user OS thread */
  336. {
  337. ut_ad(lock_mutex_own());
  338. ut_ad(trx_mutex_own(thr_get_trx(thr)));
  339. /* We own both the lock mutex and the trx_t::mutex but not the
  340. lock wait mutex. This is OK because other threads will see the state
  341. of this slot as being in use and no other thread can change the state
  342. of the slot to free unless that thread also owns the lock mutex. */
  343. if (thr->slot != NULL && thr->slot->in_use && thr->slot->thr == thr) {
  344. trx_t* trx = thr_get_trx(thr);
  345. if (trx->lock.was_chosen_as_deadlock_victim) {
  346. trx->error_state = DB_DEADLOCK;
  347. trx->lock.was_chosen_as_deadlock_victim = FALSE;
  348. }
  349. os_event_set(thr->slot->event);
  350. }
  351. }
  352. /*********************************************************************//**
  353. Check if the thread lock wait has timed out. Release its locks if the
  354. wait has actually timed out. */
  355. static
  356. void
  357. lock_wait_check_and_cancel(
  358. /*=======================*/
  359. const srv_slot_t* slot) /*!< in: slot reserved by a user
  360. thread when the wait started */
  361. {
  362. trx_t* trx;
  363. double wait_time;
  364. ib_time_t suspend_time = slot->suspend_time;
  365. ut_ad(lock_wait_mutex_own());
  366. ut_ad(slot->in_use);
  367. ut_ad(slot->suspended);
  368. wait_time = ut_difftime(ut_time(), suspend_time);
  369. trx = thr_get_trx(slot->thr);
  370. if (trx_is_interrupted(trx)
  371. || (slot->wait_timeout < 100000000
  372. && (wait_time > (double) slot->wait_timeout
  373. || wait_time < 0))) {
  374. /* Timeout exceeded or a wrap-around in system
  375. time counter: cancel the lock request queued
  376. by the transaction and release possible
  377. other transactions waiting behind; it is
  378. possible that the lock has already been
  379. granted: in that case do nothing */
  380. lock_mutex_enter();
  381. trx_mutex_enter(trx);
  382. if (trx->lock.wait_lock) {
  383. ut_a(trx->lock.que_state == TRX_QUE_LOCK_WAIT);
  384. #ifdef WITH_WSREP
  385. if (!wsrep_is_BF_lock_timeout(trx)) {
  386. #endif /* WITH_WSREP */
  387. lock_cancel_waiting_and_release(trx->lock.wait_lock);
  388. #ifdef WITH_WSREP
  389. }
  390. #endif /* WITH_WSREP */
  391. }
  392. lock_mutex_exit();
  393. trx_mutex_exit(trx);
  394. }
  395. }
  396. /*********************************************************************//**
  397. A thread which wakes up threads whose lock wait may have lasted too long.
  398. @return a dummy parameter */
  399. extern "C" UNIV_INTERN
  400. os_thread_ret_t
  401. DECLARE_THREAD(lock_wait_timeout_thread)(void*)
  402. {
  403. ib_int64_t sig_count = 0;
  404. os_event_t event = lock_sys->timeout_event;
  405. ut_ad(!srv_read_only_mode);
  406. #ifdef UNIV_PFS_THREAD
  407. pfs_register_thread(srv_lock_timeout_thread_key);
  408. #endif /* UNIV_PFS_THREAD */
  409. do {
  410. srv_slot_t* slot;
  411. /* When someone is waiting for a lock, we wake up every second
  412. and check if a timeout has passed for a lock wait */
  413. os_event_wait_time_low(event, 1000000, sig_count);
  414. sig_count = os_event_reset(event);
  415. if (srv_shutdown_state >= SRV_SHUTDOWN_CLEANUP) {
  416. break;
  417. }
  418. lock_wait_mutex_enter();
  419. /* Check all slots for user threads that are waiting
  420. on locks, and if they have exceeded the time limit. */
  421. for (slot = lock_sys->waiting_threads;
  422. slot < lock_sys->last_slot;
  423. ++slot) {
  424. /* We are doing a read without the lock mutex
  425. and/or the trx mutex. This is OK because a slot
  426. can't be freed or reserved without the lock wait
  427. mutex. */
  428. if (slot->in_use) {
  429. lock_wait_check_and_cancel(slot);
  430. }
  431. }
  432. sig_count = os_event_reset(event);
  433. lock_wait_mutex_exit();
  434. } while (srv_shutdown_state < SRV_SHUTDOWN_CLEANUP);
  435. lock_sys->timeout_thread_active = false;
  436. /* We count the number of threads in os_thread_exit(). A created
  437. thread should always use that to exit and not use return() to exit. */
  438. os_thread_exit(NULL);
  439. OS_THREAD_DUMMY_RETURN;
  440. }