1
/******************************************************
2
The read-write lock (for threads, not for database transactions)
6
Created 9/11/1995 Heikki Tuuri
7
*******************************************************/
14
#include "sync0sync.h"
17
/* The following undef is to prevent a name conflict with a macro
21
/* Latch types; these are used also in btr0btr.h: keep the numerical values
22
smaller than 30 and the order of the numerical values like below! */
27
typedef struct rw_lock_struct rw_lock_t;
28
#ifdef UNIV_SYNC_DEBUG
29
typedef struct rw_lock_debug_struct rw_lock_debug_t;
30
#endif /* UNIV_SYNC_DEBUG */
32
typedef UT_LIST_BASE_NODE_T(rw_lock_t) rw_lock_list_t;
34
extern rw_lock_list_t rw_lock_list;
35
extern mutex_t rw_lock_list_mutex;
37
#ifdef UNIV_SYNC_DEBUG
38
/* The global mutex which protects debug info lists of all rw-locks.
39
To modify the debug info list of an rw-lock, this mutex has to be
41
acquired in addition to the mutex protecting the lock. */
42
extern mutex_t rw_lock_debug_mutex;
43
extern os_event_t rw_lock_debug_event; /* If deadlock detection does
44
not get immediately the mutex it
45
may wait for this event */
46
extern ibool rw_lock_debug_waiters; /* This is set to TRUE, if
47
there may be waiters for the event */
48
#endif /* UNIV_SYNC_DEBUG */
50
extern ulint rw_s_system_call_count;
51
extern ulint rw_s_spin_wait_count;
52
extern ulint rw_s_exit_count;
53
extern ulint rw_s_os_wait_count;
54
extern ulint rw_x_system_call_count;
55
extern ulint rw_x_spin_wait_count;
56
extern ulint rw_x_os_wait_count;
57
extern ulint rw_x_exit_count;
59
/**********************************************************************
60
Creates, or rather, initializes an rw-lock object in a specified memory
61
location (which must be appropriately aligned). The rw-lock is initialized
62
to the non-locked state. Explicit freeing of the rw-lock with rw_lock_free
63
is necessary only if the memory block containing it is freed. */
65
# ifdef UNIV_SYNC_DEBUG
66
# define rw_lock_create(L, level) \
67
rw_lock_create_func((L), (level), #L, __FILE__, __LINE__)
68
# else /* UNIV_SYNC_DEBUG */
69
# define rw_lock_create(L, level) \
70
rw_lock_create_func((L), #L, __FILE__, __LINE__)
71
# endif /* UNIV_SYNC_DEBUG */
72
#else /* UNIV_DEBUG */
73
# define rw_lock_create(L, level) \
74
rw_lock_create_func((L), __FILE__, __LINE__)
75
#endif /* UNIV_DEBUG */
77
/**********************************************************************
78
Creates, or rather, initializes an rw-lock object in a specified memory
79
location (which must be appropriately aligned). The rw-lock is initialized
80
to the non-locked state. Explicit freeing of the rw-lock with rw_lock_free
81
is necessary only if the memory block containing it is freed. */
86
rw_lock_t* lock, /* in: pointer to memory */
88
# ifdef UNIV_SYNC_DEBUG
89
ulint level, /* in: level */
90
# endif /* UNIV_SYNC_DEBUG */
91
const char* cmutex_name, /* in: mutex name */
92
#endif /* UNIV_DEBUG */
93
const char* cfile_name, /* in: file name where created */
94
ulint cline); /* in: file line where created */
95
/**********************************************************************
96
Calling this function is obligatory only if the memory buffer containing
97
the rw-lock is freed. Removes an rw-lock object from the global list. The
98
rw-lock is checked to be in the non-locked state. */
103
rw_lock_t* lock); /* in: rw-lock */
105
/**********************************************************************
106
Checks that the rw-lock has been initialized and that there are no
107
simultaneous shared and exclusive locks. */
113
#endif /* UNIV_DEBUG */
114
/******************************************************************
115
NOTE! The following macros should be used in rw s-locking, not the
116
corresponding function. */
118
#define rw_lock_s_lock(M) rw_lock_s_lock_func(\
119
(M), 0, __FILE__, __LINE__)
120
/******************************************************************
121
NOTE! The following macros should be used in rw s-locking, not the
122
corresponding function. */
124
#define rw_lock_s_lock_gen(M, P) rw_lock_s_lock_func(\
125
(M), (P), __FILE__, __LINE__)
126
/******************************************************************
127
NOTE! The following macros should be used in rw s-locking, not the
128
corresponding function. */
130
#define rw_lock_s_lock_nowait(M) rw_lock_s_lock_func_nowait(\
131
(M), __FILE__, __LINE__)
132
/**********************************************************************
133
NOTE! Use the corresponding macro, not directly this function, except if
134
you supply the file name and line number. Lock an rw-lock in shared mode
135
for the current thread. If the rw-lock is locked in exclusive mode, or
136
there is an exclusive lock request waiting, the function spins a preset
137
time (controlled by SYNC_SPIN_ROUNDS), waiting for the lock, before
138
suspending the thread. */
143
rw_lock_t* lock, /* in: pointer to rw-lock */
144
ulint pass, /* in: pass value; != 0, if the lock will
145
be passed to another thread to unlock */
146
const char* file_name,/* in: file name where lock requested */
147
ulint line); /* in: line where requested */
148
/**********************************************************************
149
NOTE! Use the corresponding macro, not directly this function, except if
150
you supply the file name and line number. Lock an rw-lock in shared mode
151
for the current thread if the lock can be acquired immediately. */
154
rw_lock_s_lock_func_nowait(
155
/*=======================*/
156
/* out: TRUE if success */
157
rw_lock_t* lock, /* in: pointer to rw-lock */
158
const char* file_name,/* in: file name where lock requested */
159
ulint line); /* in: line where requested */
160
/**********************************************************************
161
NOTE! Use the corresponding macro, not directly this function! Lock an
162
rw-lock in exclusive mode for the current thread if the lock can be
163
obtained immediately. */
166
rw_lock_x_lock_func_nowait(
167
/*=======================*/
168
/* out: TRUE if success */
169
rw_lock_t* lock, /* in: pointer to rw-lock */
170
const char* file_name,/* in: file name where lock requested */
171
ulint line); /* in: line where requested */
172
/**********************************************************************
173
Releases a shared mode lock. */
176
rw_lock_s_unlock_func(
177
/*==================*/
178
rw_lock_t* lock /* in: rw-lock */
179
#ifdef UNIV_SYNC_DEBUG
180
,ulint pass /* in: pass value; != 0, if the lock may have
181
been passed to another thread to unlock */
184
/***********************************************************************
185
Releases a shared mode lock. */
187
#ifdef UNIV_SYNC_DEBUG
188
#define rw_lock_s_unlock(L) rw_lock_s_unlock_func(L, 0)
190
#define rw_lock_s_unlock(L) rw_lock_s_unlock_func(L)
192
/***********************************************************************
193
Releases a shared mode lock. */
195
#ifdef UNIV_SYNC_DEBUG
196
#define rw_lock_s_unlock_gen(L, P) rw_lock_s_unlock_func(L, P)
198
#define rw_lock_s_unlock_gen(L, P) rw_lock_s_unlock_func(L)
200
/******************************************************************
201
NOTE! The following macro should be used in rw x-locking, not the
202
corresponding function. */
204
#define rw_lock_x_lock(M) rw_lock_x_lock_func(\
205
(M), 0, __FILE__, __LINE__)
206
/******************************************************************
207
NOTE! The following macro should be used in rw x-locking, not the
208
corresponding function. */
210
#define rw_lock_x_lock_gen(M, P) rw_lock_x_lock_func(\
211
(M), (P), __FILE__, __LINE__)
212
/******************************************************************
213
NOTE! The following macros should be used in rw x-locking, not the
214
corresponding function. */
216
#define rw_lock_x_lock_nowait(M) rw_lock_x_lock_func_nowait(\
217
(M), __FILE__, __LINE__)
218
/**********************************************************************
219
NOTE! Use the corresponding macro, not directly this function! Lock an
220
rw-lock in exclusive mode for the current thread. If the rw-lock is locked
221
in shared or exclusive mode, or there is an exclusive lock request waiting,
222
the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting
223
for the lock, before suspending the thread. If the same thread has an x-lock
224
on the rw-lock, locking succeed, with the following exception: if pass != 0,
225
only a single x-lock may be taken on the lock. NOTE: If the same thread has
226
an s-lock, locking does not succeed! */
231
rw_lock_t* lock, /* in: pointer to rw-lock */
232
ulint pass, /* in: pass value; != 0, if the lock will
233
be passed to another thread to unlock */
234
const char* file_name,/* in: file name where lock requested */
235
ulint line); /* in: line where requested */
236
/**********************************************************************
237
Releases an exclusive mode lock. */
240
rw_lock_x_unlock_func(
241
/*==================*/
242
rw_lock_t* lock /* in: rw-lock */
243
#ifdef UNIV_SYNC_DEBUG
244
,ulint pass /* in: pass value; != 0, if the lock may have
245
been passed to another thread to unlock */
248
/***********************************************************************
249
Releases an exclusive mode lock. */
251
#ifdef UNIV_SYNC_DEBUG
252
#define rw_lock_x_unlock(L) rw_lock_x_unlock_func(L, 0)
254
#define rw_lock_x_unlock(L) rw_lock_x_unlock_func(L)
256
/***********************************************************************
257
Releases an exclusive mode lock. */
259
#ifdef UNIV_SYNC_DEBUG
260
#define rw_lock_x_unlock_gen(L, P) rw_lock_x_unlock_func(L, P)
262
#define rw_lock_x_unlock_gen(L, P) rw_lock_x_unlock_func(L)
264
/**********************************************************************
265
Low-level function which locks an rw-lock in s-mode when we know that it
266
is possible and none else is currently accessing the rw-lock structure.
267
Then we can do the locking without reserving the mutex. */
270
rw_lock_s_lock_direct(
271
/*==================*/
272
rw_lock_t* lock, /* in: pointer to rw-lock */
273
const char* file_name, /* in: file name where requested */
274
ulint line /* in: line where lock requested */
276
/**********************************************************************
277
Low-level function which locks an rw-lock in x-mode when we know that it
278
is not locked and none else is currently accessing the rw-lock structure.
279
Then we can do the locking without reserving the mutex. */
282
rw_lock_x_lock_direct(
283
/*==================*/
284
rw_lock_t* lock, /* in: pointer to rw-lock */
285
const char* file_name, /* in: file name where requested */
286
ulint line /* in: line where lock requested */
288
/**********************************************************************
289
This function is used in the insert buffer to move the ownership of an
290
x-latch on a buffer frame to the current thread. The x-latch was set by
291
the buffer read operation and it protected the buffer frame while the
292
read was done. The ownership is moved because we want that the current
293
thread is able to acquire a second x-latch which is stored in an mtr.
294
This, in turn, is needed to pass the debug checks of index page
298
rw_lock_x_lock_move_ownership(
299
/*==========================*/
300
rw_lock_t* lock); /* in: lock which was x-locked in the
302
/**********************************************************************
303
Releases a shared mode lock when we know there are no waiters and none
304
else will access the lock during the time this function is executed. */
307
rw_lock_s_unlock_direct(
308
/*====================*/
309
rw_lock_t* lock); /* in: rw-lock */
310
/**********************************************************************
311
Releases an exclusive mode lock when we know there are no waiters, and
312
none else will access the lock durint the time this function is executed. */
315
rw_lock_x_unlock_direct(
316
/*====================*/
317
rw_lock_t* lock); /* in: rw-lock */
318
/**********************************************************************
319
Returns the value of writer_count for the lock. Does not reserve the lock
320
mutex, so the caller must be sure it is not changed during the call. */
323
rw_lock_get_x_lock_count(
324
/*=====================*/
325
/* out: value of writer_count */
326
rw_lock_t* lock); /* in: rw-lock */
327
/************************************************************************
328
Accessor functions for rw lock. */
341
rw_lock_get_reader_count(
342
/*=====================*/
344
#ifdef UNIV_SYNC_DEBUG
345
/**********************************************************************
346
Checks if the thread has locked the rw-lock in the specified mode, with
347
the pass value == 0. */
352
rw_lock_t* lock, /* in: rw-lock */
353
ulint lock_type); /* in: lock type: RW_LOCK_SHARED,
355
#endif /* UNIV_SYNC_DEBUG */
356
/**********************************************************************
357
Checks if somebody has locked the rw-lock in the specified mode. */
362
rw_lock_t* lock, /* in: rw-lock */
363
ulint lock_type); /* in: lock type: RW_LOCK_SHARED,
365
#ifdef UNIV_SYNC_DEBUG
366
/*******************************************************************
367
Prints debug info of an rw-lock. */
372
rw_lock_t* lock); /* in: rw-lock */
373
/*******************************************************************
374
Prints debug info of currently locked rw-locks. */
377
rw_lock_list_print_info(
378
/*====================*/
379
FILE* file); /* in: file where to print */
380
/*******************************************************************
381
Returns the number of currently locked rw-locks.
382
Works only in the debug version. */
385
rw_lock_n_locked(void);
386
/*==================*/
388
/*#####################################################################*/
390
/**********************************************************************
391
Acquires the debug mutex. We cannot use the mutex defined in sync0sync,
392
because the debug mutex is also acquired in sync0arr while holding the OS
393
mutex protecting the sync array, and the ordinary mutex_enter might
394
recursively call routines in sync0arr, leading to a deadlock on the OS
398
rw_lock_debug_mutex_enter(void);
399
/*==========================*/
400
/**********************************************************************
401
Releases the debug mutex. */
404
rw_lock_debug_mutex_exit(void);
405
/*==========================*/
406
/*************************************************************************
407
Prints info of a debug struct. */
412
rw_lock_debug_t* info); /* in: debug struct */
413
#endif /* UNIV_SYNC_DEBUG */
415
/* NOTE! The structure appears here only for the compiler to know its size.
416
Do not use its fields directly! The structure used in the spin lock
417
implementation of a read-write lock. Several threads may have a shared lock
418
simultaneously in this lock, but only one writer may have an exclusive lock,
419
in which case no shared locks are allowed. To prevent starving of a writer
420
blocked by readers, a writer may queue for the lock by setting the writer
421
field. Then no new readers are allowed in. */
423
struct rw_lock_struct {
424
ulint reader_count; /* Number of readers who have locked this
425
lock in the shared mode */
426
ulint writer; /* This field is set to RW_LOCK_EX if there
427
is a writer owning the lock (in exclusive
428
mode), RW_LOCK_WAIT_EX if a writer is
429
queueing for the lock, and
430
RW_LOCK_NOT_LOCKED, otherwise. */
431
os_thread_id_t writer_thread;
432
/* Thread id of a possible writer thread */
433
ulint writer_count; /* Number of times the same thread has
434
recursively locked the lock in the exclusive
436
mutex_t mutex; /* The mutex protecting rw_lock_struct */
437
ulint pass; /* Default value 0. This is set to some
438
value != 0 given by the caller of an x-lock
439
operation, if the x-lock is to be passed to
440
another thread to unlock (which happens in
441
asynchronous i/o). */
442
ulint waiters; /* This ulint is set to 1 if there are
443
waiters (readers or writers) in the global
444
wait array, waiting for this rw_lock.
446
UT_LIST_NODE_T(rw_lock_t) list;
447
/* All allocated rw locks are put into a
449
#ifdef UNIV_SYNC_DEBUG
450
UT_LIST_BASE_NODE_T(rw_lock_debug_t) debug_list;
451
/* In the debug version: pointer to the debug
452
info list of the lock */
453
ulint level; /* Level in the global latching order. */
454
#endif /* UNIV_SYNC_DEBUG */
455
const char* cfile_name;/* File name where lock created */
456
const char* last_s_file_name;/* File name where last s-locked */
457
const char* last_x_file_name;/* File name where last x-locked */
458
ibool writer_is_wait_ex;
459
/* This is TRUE if the writer field is
460
RW_LOCK_WAIT_EX; this field is located far
461
from the memory update hotspot fields which
462
are at the start of this struct, thus we can
463
peek this field without causing much memory
465
unsigned cline:14; /* Line where created */
466
unsigned last_s_line:14; /* Line number where last time s-locked */
467
unsigned last_x_line:14; /* Line number where last time x-locked */
471
#define RW_LOCK_MAGIC_N 22643
473
#ifdef UNIV_SYNC_DEBUG
474
/* The structure for storing debug info of an rw-lock */
475
struct rw_lock_debug_struct {
477
os_thread_id_t thread_id; /* The thread id of the thread which
478
locked the rw-lock */
479
ulint pass; /* Pass value given in the lock operation */
480
ulint lock_type; /* Type of the lock: RW_LOCK_EX,
481
RW_LOCK_SHARED, RW_LOCK_WAIT_EX */
482
const char* file_name;/* File name where the lock was obtained */
483
ulint line; /* Line where the rw-lock was locked */
484
UT_LIST_NODE_T(rw_lock_debug_t) list;
485
/* Debug structs are linked in a two-way
488
#endif /* UNIV_SYNC_DEBUG */
491
#include "sync0rw.ic"