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