1 /* Copyright (c) 2000, 2010, 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 #ifdef	__cplusplus
28 extern "C" {
29 #endif
30 
31 #include <my_pthread.h>
32 #include <my_list.h>
33 
34 struct st_thr_lock;
35 extern ulong locks_immediate,locks_waited ;
36 
37 /*
38   Important: if a new lock type is added, a matching lock description
39              must be added to sql_test.cc's lock_descriptions array.
40 */
41 enum thr_lock_type { TL_IGNORE=-1,
42 		     TL_UNLOCK,			/* UNLOCK ANY LOCK */
43                      /*
44                        Parser only! At open_tables() becomes TL_READ or
45                        TL_READ_NO_INSERT depending on the binary log format
46                        (SBR/RBR) and on the table category (log table).
47                        Used for tables that are read by statements which
48                        modify tables.
49                      */
50                      TL_READ_DEFAULT,
51 		     TL_READ,			/* Read lock */
52 		     TL_READ_WITH_SHARED_LOCKS,
53 		     /* High prior. than TL_WRITE. Allow concurrent insert */
54 		     TL_READ_HIGH_PRIORITY,
55 		     /* READ, Don't allow concurrent insert */
56 		     TL_READ_NO_INSERT,
57 		     /*
58 			Write lock, but allow other threads to read / write.
59 			Used by BDB tables in MySQL to mark that someone is
60 			reading/writing to the table.
61 		      */
62 		     TL_WRITE_ALLOW_WRITE,
63 		     /*
64 		       WRITE lock used by concurrent insert. Will allow
65 		       READ, if one could use concurrent insert on table.
66 		     */
67 		     TL_WRITE_CONCURRENT_INSERT,
68 		     /* Write used by INSERT DELAYED.  Allows READ locks */
69 		     TL_WRITE_DELAYED,
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 enum_thr_lock_result { THR_LOCK_SUCCESS= 0, THR_LOCK_ABORTED= 1,
83                             THR_LOCK_WAIT_TIMEOUT= 2, THR_LOCK_DEADLOCK= 3 };
84 
85 
86 extern ulong max_write_lock_count;
87 extern my_bool thr_lock_inited;
88 extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
89 
90 /*
91   A description of the thread which owns the lock. The address
92   of an instance of this structure is used to uniquely identify the thread.
93 */
94 
95 typedef struct st_thr_lock_info
96 {
97   pthread_t thread;
98   my_thread_id thread_id;
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   enum thr_lock_type type;
108   void *status_param;			/* Param to status functions */
109   void *debug_print_param;
110   struct PSI_table *m_psi;
111 } THR_LOCK_DATA;
112 
113 struct st_lock_list {
114   THR_LOCK_DATA *data,**last;
115 };
116 
117 typedef struct st_thr_lock {
118   LIST list;
119   mysql_mutex_t mutex;
120   struct st_lock_list read_wait;
121   struct st_lock_list read;
122   struct st_lock_list write_wait;
123   struct st_lock_list write;
124   /* write_lock_count is incremented for write locks and reset on read locks */
125   ulong write_lock_count;
126   uint read_no_write_count;
127   void (*get_status)(void*, int);	/* When one gets a lock */
128   void (*copy_status)(void*,void*);
129   void (*update_status)(void*);		/* Before release of write */
130   void (*restore_status)(void*);         /* Before release of read */
131   my_bool (*check_status)(void *);
132 } THR_LOCK;
133 
134 
135 extern LIST *thr_lock_thread_list;
136 extern mysql_mutex_t THR_LOCK_lock;
137 
138 my_bool init_thr_lock(void);		/* Must be called once/thread */
139 void thr_lock_info_init(THR_LOCK_INFO *info);
140 void thr_lock_init(THR_LOCK *lock);
141 void thr_lock_delete(THR_LOCK *lock);
142 void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data,
143 			void *status_param);
144 enum enum_thr_lock_result thr_lock(THR_LOCK_DATA *data,
145                                    THR_LOCK_INFO *owner,
146                                    enum thr_lock_type lock_type,
147                                    ulong lock_wait_timeout);
148 void thr_unlock(THR_LOCK_DATA *data);
149 enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data,
150                                          uint count, THR_LOCK_INFO *owner,
151                                          ulong lock_wait_timeout);
152 void thr_multi_unlock(THR_LOCK_DATA **data,uint count);
153 void
154 thr_lock_merge_status(THR_LOCK_DATA **data, uint count);
155 void thr_abort_locks(THR_LOCK *lock, my_bool upgrade_lock);
156 my_bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread);
157 void thr_print_locks(void);		/* For debugging */
158 my_bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data,
159                                      enum thr_lock_type new_lock_type,
160                                      ulong lock_wait_timeout);
161 void    thr_downgrade_write_lock(THR_LOCK_DATA *data,
162                                  enum thr_lock_type new_lock_type);
163 my_bool thr_reschedule_write_lock(THR_LOCK_DATA *data,
164                                   ulong lock_wait_timeout);
165 void thr_set_lock_wait_callback(void (*before_wait)(void),
166                                 void (*after_wait)(void));
167 #ifdef	__cplusplus
168 }
169 #endif
170 #endif /* _thr_lock_h */
171