thr_lock.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (c) 2000, 2010, 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 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  12. /* For use with thr_lock:s */
  13. #ifndef _thr_lock_h
  14. #define _thr_lock_h
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <my_pthread.h>
  19. #include <my_list.h>
  20. struct st_thr_lock;
  21. extern ulong locks_immediate,locks_waited ;
  22. /*
  23. Important: if a new lock type is added, a matching lock description
  24. must be added to sql_test.cc's lock_descriptions array.
  25. */
  26. enum thr_lock_type { TL_IGNORE=-1,
  27. TL_UNLOCK, /* UNLOCK ANY LOCK */
  28. /*
  29. Parser only! At open_tables() becomes TL_READ or
  30. TL_READ_NO_INSERT depending on the binary log format
  31. (SBR/RBR) and on the table category (log table).
  32. Used for tables that are read by statements which
  33. modify tables.
  34. */
  35. TL_READ_DEFAULT,
  36. TL_READ, /* Read lock */
  37. TL_READ_WITH_SHARED_LOCKS,
  38. /* High prior. than TL_WRITE. Allow concurrent insert */
  39. TL_READ_HIGH_PRIORITY,
  40. /* READ, Don't allow concurrent insert */
  41. TL_READ_NO_INSERT,
  42. /*
  43. Write lock, but allow other threads to read / write.
  44. Used by BDB tables in MySQL to mark that someone is
  45. reading/writing to the table.
  46. */
  47. TL_WRITE_ALLOW_WRITE,
  48. /*
  49. WRITE lock used by concurrent insert. Will allow
  50. READ, if one could use concurrent insert on table.
  51. */
  52. TL_WRITE_CONCURRENT_INSERT,
  53. /* Write used by INSERT DELAYED. Allows READ locks */
  54. TL_WRITE_DELAYED,
  55. /*
  56. parser only! Late bound low_priority flag.
  57. At open_tables() becomes thd->update_lock_default.
  58. */
  59. TL_WRITE_DEFAULT,
  60. /* WRITE lock that has lower priority than TL_READ */
  61. TL_WRITE_LOW_PRIORITY,
  62. /* Normal WRITE lock */
  63. TL_WRITE,
  64. /* Abort new lock request with an error */
  65. TL_WRITE_ONLY};
  66. enum enum_thr_lock_result { THR_LOCK_SUCCESS= 0, THR_LOCK_ABORTED= 1,
  67. THR_LOCK_WAIT_TIMEOUT= 2, THR_LOCK_DEADLOCK= 3 };
  68. /* Priority for locks */
  69. #define THR_LOCK_LATE_PRIV 1 /* For locks to be merged with org lock */
  70. #define THR_LOCK_MERGE_PRIV 2 /* For merge tables */
  71. #define THR_UNLOCK_UPDATE_STATUS 1
  72. extern ulong max_write_lock_count;
  73. extern my_bool thr_lock_inited;
  74. extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
  75. /*
  76. A description of the thread which owns the lock. The address
  77. of an instance of this structure is used to uniquely identify the thread.
  78. */
  79. typedef struct st_thr_lock_info
  80. {
  81. pthread_t thread;
  82. my_thread_id thread_id;
  83. void *mysql_thd; // THD pointer
  84. } THR_LOCK_INFO;
  85. typedef struct st_thr_lock_data {
  86. THR_LOCK_INFO *owner;
  87. struct st_thr_lock_data *next,**prev;
  88. struct st_thr_lock *lock;
  89. mysql_cond_t *cond;
  90. void *status_param; /* Param to status functions */
  91. void *debug_print_param; /* For error messages */
  92. struct PSI_table *m_psi;
  93. enum thr_lock_type type;
  94. enum thr_lock_type org_type; /* Cache for MariaDB */
  95. uint priority;
  96. } THR_LOCK_DATA;
  97. struct st_lock_list {
  98. THR_LOCK_DATA *data,**last;
  99. };
  100. typedef struct st_thr_lock {
  101. LIST list;
  102. mysql_mutex_t mutex;
  103. struct st_lock_list read_wait;
  104. struct st_lock_list read;
  105. struct st_lock_list write_wait;
  106. struct st_lock_list write;
  107. /* write_lock_count is incremented for write locks and reset on read locks */
  108. ulong write_lock_count;
  109. uint read_no_write_count;
  110. void (*get_status)(void*, my_bool); /* When one gets a lock */
  111. void (*copy_status)(void*,void*);
  112. void (*update_status)(void*); /* Before release of write */
  113. void (*restore_status)(void*); /* Before release of read */
  114. my_bool (*start_trans)(void*); /* When all locks are taken */
  115. my_bool (*check_status)(void *);
  116. void (*fix_status)(void *, void *);/* For thr_merge_locks() */
  117. const char *name; /* Used for error reporting */
  118. my_bool allow_multiple_concurrent_insert;
  119. } THR_LOCK;
  120. extern LIST *thr_lock_thread_list;
  121. extern mysql_mutex_t THR_LOCK_lock;
  122. my_bool init_thr_lock(void); /* Must be called once/thread */
  123. void thr_lock_info_init(THR_LOCK_INFO *info);
  124. void thr_lock_init(THR_LOCK *lock);
  125. void thr_lock_delete(THR_LOCK *lock);
  126. void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data,
  127. void *status_param);
  128. void thr_unlock(THR_LOCK_DATA *data, uint unlock_flags);
  129. enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data,
  130. uint count, THR_LOCK_INFO *owner,
  131. ulong lock_wait_timeout);
  132. void thr_multi_unlock(THR_LOCK_DATA **data,uint count, uint unlock_flags);
  133. void thr_merge_locks(THR_LOCK_DATA **data, uint org_count, uint new_count);
  134. void thr_abort_locks(THR_LOCK *lock, my_bool upgrade_lock);
  135. my_bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread);
  136. void thr_print_locks(void); /* For debugging */
  137. my_bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data,
  138. enum thr_lock_type new_lock_type,
  139. ulong lock_wait_timeout);
  140. void thr_downgrade_write_lock(THR_LOCK_DATA *data,
  141. enum thr_lock_type new_lock_type);
  142. my_bool thr_reschedule_write_lock(THR_LOCK_DATA *data,
  143. ulong lock_wait_timeout);
  144. void thr_set_lock_wait_callback(void (*before_wait)(void),
  145. void (*after_wait)(void));
  146. #ifdef WITH_WSREP
  147. typedef my_bool (* wsrep_thd_is_brute_force_fun)(void *, my_bool);
  148. typedef int (* wsrep_abort_thd_fun)(void *, void *, my_bool);
  149. typedef int (* wsrep_on_fun)(void *);
  150. void wsrep_thr_lock_init(
  151. wsrep_thd_is_brute_force_fun bf_fun, wsrep_abort_thd_fun abort_fun,
  152. my_bool debug, my_bool convert_LOCK_to_trx, wsrep_on_fun on_fun);
  153. #endif
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif /* _thr_lock_h */