CUBRID Engine  latest
lock_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * Overview: LOCK MANAGMENT MODULE (AT THE SERVER) -- Interface --
22  *
23  */
24 
25 #ifndef _LOCK_MANAGER_H_
26 #define _LOCK_MANAGER_H_
27 
28 #ident "$Id$"
29 
30 #include "config.h"
31 #if defined(SERVER_MODE)
32 #include "connection_error.h"
33 #endif /* SERVER_MODE */
34 #include "error_manager.h"
35 #include "locator.h"
36 #include "log_comm.h"
37 #include "oid.h"
38 #include "storage_common.h"
39 #include "thread_compat.hpp"
40 
41 #include <time.h>
42 #include <stdio.h>
43 
44 enum
45 {
53 };
54 
55 enum
56 {
57 /* Value to timeout immediately.. not wait */
59 /* Value to wait forever */
61 /* Value to force a timeout without setting errors */
63 };
64 
65 enum
67 
68 typedef enum
69 {
74 
75 /*****************************/
76 /* Lock Heap Entry Structure */
77 /*****************************/
78 typedef struct lk_entry LK_ENTRY;
79 struct lk_entry
80 {
81 #if defined(SERVER_MODE)
82  struct lk_res *res_head; /* back to resource entry */
83  THREAD_ENTRY *thrd_entry; /* thread entry pointer */
84  int tran_index; /* transaction table index */
85  LOCK granted_mode; /* granted lock mode */
86  LOCK blocked_mode; /* blocked lock mode */
87  int count; /* number of lock requests */
88  UINT64 del_id; /* delete transaction ID (for latch free) */
89  LK_ENTRY *stack; /* pointer to retired stack */
90  LK_ENTRY *next; /* next entry */
91  LK_ENTRY *tran_next; /* list of locks that trans. holds */
92  LK_ENTRY *tran_prev; /* list of locks that trans. holds */
93  LK_ENTRY *class_entry; /* ptr. to class lk_entry */
94  int ngranules; /* number of finer granules */
95  int instant_lock_count; /* number of instant lock requests */
96  int bind_index_in_tran;
98 #else /* not SERVER_MODE */
99  int dummy;
100 #endif /* not SERVER_MODE */
101 };
102 
105 {
106  OID oid; /* lock resource object identifier */
107  OID class_oid; /* only needed in case of instance lock */
108  LOCK lock; /* lock mode */
109 };
110 
113 {
114  LK_ACQOBJ_LOCK *obj; /* The list of acquired object locks */
115  unsigned int nobj_locks; /* Number of actual object locks */
116 };
117 
118 /* During delete and update operation,
119  * if the number of objects to be deleted or updated is larger than
120  * lock escalation threshold, we should acquire a lock on the class
121  * instead of acquiring a lock on each instance.
122  */
123 
124 /* composite locking for delete and update operation */
127 {
134 };
135 
136 typedef struct lk_lockcomp LK_LOCKCOMP;
138 {
143 };
144 
147 {
149 };
150 
151 /* type of locking resource */
152 typedef enum
153 {
154  LOCK_RESOURCE_INSTANCE, /* An instance resource */
155  LOCK_RESOURCE_CLASS, /* A class resource */
156  LOCK_RESOURCE_ROOT_CLASS, /* A root class resource */
157  LOCK_RESOURCE_OBJECT /* An object resource */
159 
160 /*
161  * Lock Resource key structure
162  */
163 typedef struct lk_res_key LK_RES_KEY;
165 {
166  LOCK_RESOURCE_TYPE type; /* type of resource: class,instance */
169 };
170 
171 /*
172  * Lock Resource Entry Structure
173  */
174 typedef struct lk_res LK_RES;
175 struct lk_res
176 {
177  LK_RES_KEY key; /* lock resource's hash key */
178  LOCK total_holders_mode; /* total mode of the holders */
179  LOCK total_waiters_mode; /* total mode of the waiters */
180  LK_ENTRY *holder; /* lock holder list */
181  LK_ENTRY *waiter; /* lock waiter list */
182  LK_ENTRY *non2pl; /* non2pl list */
183  pthread_mutex_t res_mutex; /* resource mutex */
184  LK_RES *hash_next; /* for hash chain */
185  LK_RES *stack; /* for freelist */
186  UINT64 del_id; /* delete transaction ID (for latch free) */
187 
188  // *INDENT-OFF*
189  lk_res ();
190  ~lk_res ();
191  // *INDENT-ON*
192 };
193 
194 #if defined(SERVER_MODE)
195 extern void lock_remove_all_inst_locks (THREAD_ENTRY * thread_p, int tran_index, const OID * class_oid, LOCK lock);
196 #endif /* SERVER_MODE */
197 extern int lock_initialize (void);
198 extern void lock_finalize (void);
199 extern int lock_hold_object_instant (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid, LOCK lock);
200 extern int lock_object_wait_msecs (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid, LOCK lock,
201  int cond_flag, int wait_msecs);
202 extern int lock_object (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid, LOCK lock, int cond_flag);
203 extern int lock_subclass (THREAD_ENTRY * thread_p, const OID * subclass_oid, const OID * superclass_oid, LOCK lock,
204  int cond_flag);
205 extern int lock_object_with_btid (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid, const BTID * btid,
206  LOCK lock, int cond_flag);
207 extern int lock_scan (THREAD_ENTRY * thread_p, const OID * class_oid, int cond_flag, LOCK class_lock);
208 extern int lock_classes_lock_hint (THREAD_ENTRY * thread_p, LC_LOCKHINT * lockhint);
209 extern void lock_remove_object_lock (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid, LOCK lock);
210 extern void lock_unlock_object_donot_move_to_non2pl (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid,
211  LOCK lock);
212 extern void lock_unlock_object (THREAD_ENTRY * thread_p, const OID * oid, const OID * class_oid, LOCK lock, bool force);
213 extern void lock_unlock_objects_lock_set (THREAD_ENTRY * thread_p, LC_LOCKSET * lockset);
214 extern void lock_unlock_classes_lock_hint (THREAD_ENTRY * thread_p, LC_LOCKHINT * lockhint);
215 extern void lock_unlock_all (THREAD_ENTRY * thread_p);
216 extern LOCK lock_get_object_lock (const OID * oid, const OID * class_oid);
217 extern bool lock_has_xlock (THREAD_ENTRY * thread_p);
218 #if defined (ENABLE_UNUSED_FUNCTION)
219 extern bool lock_has_lock_transaction (int tran_index);
220 #endif
221 extern bool lock_is_waiting_transaction (int tran_index);
222 extern LK_ENTRY *lock_get_class_lock (THREAD_ENTRY * thread_p, const OID * class_oid);
223 extern void lock_notify_isolation_incons (THREAD_ENTRY * thread_p,
224  bool (*fun) (const OID * class_oid, const OID * oid, void *args), void *args);
225 extern int lock_reacquire_crash_locks (THREAD_ENTRY * thread_p, LK_ACQUIRED_LOCKS * acqlocks, int tran_index);
226 extern void lock_unlock_all_shared_get_all_exclusive (THREAD_ENTRY * thread_p, LK_ACQUIRED_LOCKS * acqlocks);
227 extern void lock_dump_acquired (FILE * fp, LK_ACQUIRED_LOCKS * acqlocks);
228 extern void lock_start_instant_lock_mode (int tran_index);
229 extern void lock_stop_instant_lock_mode (THREAD_ENTRY * thread_p, int tran_index, bool need_unlock);
230 extern bool lock_is_instant_lock_mode (int tran_index);
231 extern void lock_clear_deadlock_victim (int tran_index);
232 extern unsigned int lock_get_number_object_locks (void);
233 extern int lock_initialize_composite_lock (THREAD_ENTRY * thread_p, LK_COMPOSITE_LOCK * comp_lock);
234 extern int lock_add_composite_lock (THREAD_ENTRY * thread_p, LK_COMPOSITE_LOCK * comp_lock, const OID * oid,
235  const OID * class_oid);
236 extern int lock_finalize_composite_lock (THREAD_ENTRY * thread_p, LK_COMPOSITE_LOCK * comp_lock);
237 extern void lock_abort_composite_lock (LK_COMPOSITE_LOCK * comp_lock);
238 extern int lock_get_lock_holder_tran_index (THREAD_ENTRY * thread_p, char **out_buf, int waiter_index, LK_RES * res);
239 extern int lock_has_lock_on_object (const OID * oid, const OID * class_oid, LOCK lock);
240 extern int lock_rep_read_tran (THREAD_ENTRY * thread_p, LOCK lock, int cond_flag);
241 extern int lock_demote_class_lock (THREAD_ENTRY * thread_p, const OID * oid, LOCK lock, LOCK * ex_lock);
242 extern void lock_demote_read_class_lock_for_checksumdb (THREAD_ENTRY * thread_p, int tran_index, const OID * class_oid);
243 extern const char *lock_wait_state_to_string (int state);
244 extern void lock_force_thread_timeout_lock (THREAD_ENTRY * thread_p);
245 
246 #if defined (SERVER_MODE)
247 extern void lock_deadlock_detect_daemon_get_stats (UINT64 * statsp);
248 #endif // SERVER_MODE
249 #endif /* _LOCK_MANAGER_H_ */
LOCK_RESOURCE_TYPE type
Definition: lock_manager.h:166
LK_LOCKCOMP lockcomp
Definition: lock_manager.h:148
LK_ENTRY * class_lock_ptr
Definition: lock_manager.h:129
void lock_unlock_object_donot_move_to_non2pl(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, LOCK lock)
int lock_has_lock_on_object(const OID *oid, const OID *class_oid, LOCK lock)
void lock_unlock_object(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, LOCK lock, bool force)
void lock_unlock_objects_lock_set(THREAD_ENTRY *thread_p, LC_LOCKSET *lockset)
LOCK total_waiters_mode
Definition: lock_manager.h:179
KEY_LOCK_ESCALATION
Definition: lock_manager.h:68
int lock_add_composite_lock(THREAD_ENTRY *thread_p, LK_COMPOSITE_LOCK *comp_lock, const OID *oid, const OID *class_oid)
bool lock_is_waiting_transaction(int tran_index)
void lock_clear_deadlock_victim(int tran_index)
LK_ENTRY * lock_get_class_lock(THREAD_ENTRY *thread_p, const OID *class_oid)
void lock_start_instant_lock_mode(int tran_index)
int lock_get_lock_holder_tran_index(THREAD_ENTRY *thread_p, char **out_buf, int waiter_index, LK_RES *res)
void THREAD_ENTRY
int lock_classes_lock_hint(THREAD_ENTRY *thread_p, LC_LOCKHINT *lockhint)
LOCK
void lock_unlock_all_shared_get_all_exclusive(THREAD_ENTRY *thread_p, LK_ACQUIRED_LOCKS *acqlocks)
LOCK total_holders_mode
Definition: lock_manager.h:178
int lock_reacquire_crash_locks(THREAD_ENTRY *thread_p, LK_ACQUIRED_LOCKS *acqlocks, int tran_index)
int lock_object_with_btid(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, const BTID *btid, LOCK lock, int cond_flag)
int dummy
Definition: lock_manager.h:99
int lock_demote_class_lock(THREAD_ENTRY *thread_p, const OID *oid, LOCK lock, LOCK *ex_lock)
LOCK_RESOURCE_TYPE
Definition: lock_manager.h:152
LK_ENTRY * waiter
Definition: lock_manager.h:181
LK_RES * stack
Definition: lock_manager.h:185
void lock_unlock_classes_lock_hint(THREAD_ENTRY *thread_p, LC_LOCKHINT *lockhint)
int lock_scan(THREAD_ENTRY *thread_p, const OID *class_oid, int cond_flag, LOCK class_lock)
int lock_finalize_composite_lock(THREAD_ENTRY *thread_p, LK_COMPOSITE_LOCK *comp_lock)
LK_ENTRY * non2pl
Definition: lock_manager.h:182
int lock_object_wait_msecs(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, LOCK lock, int cond_flag, int wait_msecs)
bool lock_is_instant_lock_mode(int tran_index)
LOCK lock_get_object_lock(const OID *oid, const OID *class_oid)
const char * lock_wait_state_to_string(int state)
void lock_remove_object_lock(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, LOCK lock)
LK_LOCKCOMP_CLASS * next
Definition: lock_manager.h:133
unsigned int lock_get_number_object_locks(void)
int lock_object(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, LOCK lock, int cond_flag)
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
LK_ACQOBJ_LOCK * obj
Definition: lock_manager.h:114
int lock_rep_read_tran(THREAD_ENTRY *thread_p, LOCK lock, int cond_flag)
void lock_finalize(void)
void lock_force_thread_timeout_lock(THREAD_ENTRY *thread_p)
UINT64 del_id
Definition: lock_manager.h:186
void lock_demote_read_class_lock_for_checksumdb(THREAD_ENTRY *thread_p, int tran_index, const OID *class_oid)
bool lock_has_xlock(THREAD_ENTRY *thread_p)
void lock_notify_isolation_incons(THREAD_ENTRY *thread_p, bool(*fun)(const OID *class_oid, const OID *oid, void *args), void *args)
unsigned int nobj_locks
Definition: lock_manager.h:115
int lock_subclass(THREAD_ENTRY *thread_p, const OID *subclass_oid, const OID *superclass_oid, LOCK lock, int cond_flag)
void lock_unlock_all(THREAD_ENTRY *thread_p)
void lock_dump_acquired(FILE *fp, LK_ACQUIRED_LOCKS *acqlocks)
LK_ENTRY * holder
Definition: lock_manager.h:180
LK_LOCKCOMP_CLASS * class_list
Definition: lock_manager.h:142
LK_ENTRY * root_class_ptr
Definition: lock_manager.h:141
int lock_initialize_composite_lock(THREAD_ENTRY *thread_p, LK_COMPOSITE_LOCK *comp_lock)
int lock_hold_object_instant(THREAD_ENTRY *thread_p, const OID *oid, const OID *class_oid, LOCK lock)
int lock_initialize(void)
Definition: lock_manager.h:79
LK_RES_KEY key
Definition: lock_manager.h:177
void lock_stop_instant_lock_mode(THREAD_ENTRY *thread_p, int tran_index, bool need_unlock)
pthread_mutex_t res_mutex
Definition: lock_manager.h:183
LK_RES * hash_next
Definition: lock_manager.h:184
void lock_abort_composite_lock(LK_COMPOSITE_LOCK *comp_lock)