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