thr_lock.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. /* For use with thr_lock:s */
  19. /**
  20. @file include/thr_lock.h
  21. */
  22. #ifndef _thr_lock_h
  23. #define _thr_lock_h
  24. #include <sys/types.h>
  25. #include "my_inttypes.h"
  26. #include "my_list.h"
  27. #include "my_thread_local.h"
  28. #include "mysql/psi/mysql_cond.h"
  29. #include "mysql/psi/mysql_mutex.h"
  30. extern mysql_mutex_t THR_LOCK_lock;
  31. struct THR_LOCK;
  32. extern ulong locks_immediate, locks_waited;
  33. /*
  34. Important: if a new lock type is added, a matching lock description
  35. must be added to sql_test.cc's lock_descriptions array.
  36. */
  37. enum thr_lock_type {
  38. TL_IGNORE = -1,
  39. TL_UNLOCK, /* UNLOCK ANY LOCK */
  40. /*
  41. Parser only! At open_tables() becomes TL_READ or
  42. TL_READ_NO_INSERT depending on the binary log format
  43. (SBR/RBR) and on the table category (log table).
  44. Used for tables that are read by statements which
  45. modify tables.
  46. */
  47. TL_READ_DEFAULT,
  48. TL_READ, /* Read lock */
  49. TL_READ_WITH_SHARED_LOCKS,
  50. /* High prior. than TL_WRITE. Allow concurrent insert */
  51. TL_READ_HIGH_PRIORITY,
  52. /* READ, Don't allow concurrent insert */
  53. TL_READ_NO_INSERT,
  54. /*
  55. Write lock, but allow other threads to read / write.
  56. Used by BDB tables in MySQL to mark that someone is
  57. reading/writing to the table.
  58. */
  59. TL_WRITE_ALLOW_WRITE,
  60. /*
  61. parser only! Late bound low_priority_flag.
  62. At open_tables() becomes thd->insert_lock_default.
  63. */
  64. TL_WRITE_CONCURRENT_DEFAULT,
  65. /*
  66. WRITE lock used by concurrent insert. Will allow
  67. READ, if one could use concurrent insert on table.
  68. */
  69. TL_WRITE_CONCURRENT_INSERT,
  70. /*
  71. parser only! Late bound low_priority flag.
  72. At open_tables() becomes thd->update_lock_default.
  73. */
  74. TL_WRITE_DEFAULT,
  75. /* WRITE lock that has lower priority than TL_READ */
  76. TL_WRITE_LOW_PRIORITY,
  77. /* Normal WRITE lock */
  78. TL_WRITE,
  79. /* Abort new lock request with an error */
  80. TL_WRITE_ONLY
  81. };
  82. enum thr_locked_row_action { THR_DEFAULT, THR_WAIT, THR_NOWAIT, THR_SKIP };
  83. struct Lock_descriptor {
  84. thr_lock_type type{TL_UNLOCK};
  85. thr_locked_row_action action{THR_DEFAULT};
  86. };
  87. enum enum_thr_lock_result {
  88. THR_LOCK_SUCCESS = 0,
  89. THR_LOCK_ABORTED = 1,
  90. THR_LOCK_WAIT_TIMEOUT = 2,
  91. THR_LOCK_DEADLOCK = 3
  92. };
  93. extern ulong max_write_lock_count;
  94. extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
  95. /*
  96. A description of the thread which owns the lock. The address
  97. of an instance of this structure is used to uniquely identify the thread.
  98. */
  99. struct THR_LOCK_INFO {
  100. my_thread_id thread_id;
  101. mysql_cond_t *suspend;
  102. };
  103. struct THR_LOCK_DATA {
  104. THR_LOCK_INFO *owner{nullptr};
  105. THR_LOCK_DATA *next{nullptr}, **prev{nullptr};
  106. THR_LOCK *lock{nullptr};
  107. mysql_cond_t *cond{nullptr};
  108. thr_lock_type type{TL_IGNORE};
  109. void *status_param{nullptr}; /* Param to status functions */
  110. void *debug_print_param{nullptr};
  111. struct PSI_table *m_psi{nullptr};
  112. };
  113. struct st_lock_list {
  114. THR_LOCK_DATA *data{nullptr}, **last{nullptr};
  115. };
  116. struct THR_LOCK {
  117. LIST list;
  118. mysql_mutex_t mutex;
  119. struct st_lock_list read_wait;
  120. struct st_lock_list read;
  121. struct st_lock_list write_wait;
  122. struct st_lock_list write;
  123. /* write_lock_count is incremented for write locks and reset on read locks */
  124. ulong write_lock_count{0};
  125. uint read_no_write_count{0};
  126. void (*get_status)(void *, int){0}; /* When one gets a lock */
  127. void (*copy_status)(void *, void *){0};
  128. void (*update_status)(void *){0}; /* Before release of write */
  129. void (*restore_status)(void *){0}; /* Before release of read */
  130. bool (*check_status)(void *){0};
  131. };
  132. extern LIST *thr_lock_thread_list;
  133. void thr_lock_info_init(THR_LOCK_INFO *info, my_thread_id thread_id,
  134. mysql_cond_t *suspend);
  135. void thr_lock_init(THR_LOCK *lock);
  136. void thr_lock_delete(THR_LOCK *lock);
  137. void thr_lock_data_init(THR_LOCK *lock, THR_LOCK_DATA *data,
  138. void *status_param);
  139. enum enum_thr_lock_result thr_lock(THR_LOCK_DATA *data, THR_LOCK_INFO *owner,
  140. enum thr_lock_type lock_type,
  141. ulong lock_wait_timeout);
  142. void thr_unlock(THR_LOCK_DATA *data);
  143. enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data, uint count,
  144. THR_LOCK_INFO *owner,
  145. ulong lock_wait_timeout);
  146. void thr_multi_unlock(THR_LOCK_DATA **data, uint count);
  147. void thr_lock_merge_status(THR_LOCK_DATA **data, uint count);
  148. void thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread);
  149. void thr_print_locks(void); /* For debugging */
  150. void thr_downgrade_write_lock(THR_LOCK_DATA *data,
  151. enum thr_lock_type new_lock_type);
  152. void thr_set_lock_wait_callback(void (*before_wait)(void),
  153. void (*after_wait)(void));
  154. #endif /* _thr_lock_h */