~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/include/sync0rw.h

Tags: innodb-plugin-1.0.1
Imported 1.0.1 with clean - with no changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
The read-write lock (for threads, not for database transactions)
 
3
 
 
4
(c) 1995 Innobase Oy
 
5
 
 
6
Created 9/11/1995 Heikki Tuuri
 
7
*******************************************************/
 
8
 
 
9
#ifndef sync0rw_h
 
10
#define sync0rw_h
 
11
 
 
12
#include "univ.i"
 
13
#include "ut0lst.h"
 
14
#include "sync0sync.h"
 
15
#include "os0sync.h"
 
16
 
 
17
/* The following undef is to prevent a name conflict with a macro
 
18
in MySQL: */
 
19
#undef rw_lock_t
 
20
 
 
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! */
 
23
#define RW_S_LATCH      1
 
24
#define RW_X_LATCH      2
 
25
#define RW_NO_LATCH     3
 
26
 
 
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 */
 
31
 
 
32
typedef UT_LIST_BASE_NODE_T(rw_lock_t)  rw_lock_list_t;
 
33
 
 
34
extern rw_lock_list_t   rw_lock_list;
 
35
extern mutex_t          rw_lock_list_mutex;
 
36
 
 
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
 
40
 
 
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 */
 
49
 
 
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;
 
58
 
 
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. */
 
64
#ifdef UNIV_DEBUG
 
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 */
 
76
 
 
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. */
 
82
UNIV_INTERN
 
83
void
 
84
rw_lock_create_func(
 
85
/*================*/
 
86
        rw_lock_t*      lock,           /* in: pointer to memory */
 
87
#ifdef UNIV_DEBUG
 
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. */
 
99
UNIV_INTERN
 
100
void
 
101
rw_lock_free(
 
102
/*=========*/
 
103
        rw_lock_t*      lock);  /* in: rw-lock */
 
104
#ifdef UNIV_DEBUG
 
105
/**********************************************************************
 
106
Checks that the rw-lock has been initialized and that there are no
 
107
simultaneous shared and exclusive locks. */
 
108
UNIV_INTERN
 
109
ibool
 
110
rw_lock_validate(
 
111
/*=============*/
 
112
        rw_lock_t*      lock);
 
113
#endif /* UNIV_DEBUG */
 
114
/******************************************************************
 
115
NOTE! The following macros should be used in rw s-locking, not the
 
116
corresponding function. */
 
117
 
 
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. */
 
123
 
 
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. */
 
129
 
 
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. */
 
139
UNIV_INLINE
 
140
void
 
141
rw_lock_s_lock_func(
 
142
/*================*/
 
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. */
 
152
UNIV_INLINE
 
153
ibool
 
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. */
 
164
UNIV_INLINE
 
165
ibool
 
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. */
 
174
UNIV_INLINE
 
175
void
 
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 */
 
182
#endif
 
183
        );
 
184
/***********************************************************************
 
185
Releases a shared mode lock. */
 
186
 
 
187
#ifdef UNIV_SYNC_DEBUG
 
188
#define rw_lock_s_unlock(L)     rw_lock_s_unlock_func(L, 0)
 
189
#else
 
190
#define rw_lock_s_unlock(L)     rw_lock_s_unlock_func(L)
 
191
#endif
 
192
/***********************************************************************
 
193
Releases a shared mode lock. */
 
194
 
 
195
#ifdef UNIV_SYNC_DEBUG
 
196
#define rw_lock_s_unlock_gen(L, P)      rw_lock_s_unlock_func(L, P)
 
197
#else
 
198
#define rw_lock_s_unlock_gen(L, P)      rw_lock_s_unlock_func(L)
 
199
#endif
 
200
/******************************************************************
 
201
NOTE! The following macro should be used in rw x-locking, not the
 
202
corresponding function. */
 
203
 
 
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. */
 
209
 
 
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. */
 
215
 
 
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! */
 
227
UNIV_INTERN
 
228
void
 
229
rw_lock_x_lock_func(
 
230
/*================*/
 
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. */
 
238
UNIV_INLINE
 
239
void
 
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 */
 
246
#endif
 
247
        );
 
248
/***********************************************************************
 
249
Releases an exclusive mode lock. */
 
250
 
 
251
#ifdef UNIV_SYNC_DEBUG
 
252
#define rw_lock_x_unlock(L)     rw_lock_x_unlock_func(L, 0)
 
253
#else
 
254
#define rw_lock_x_unlock(L)     rw_lock_x_unlock_func(L)
 
255
#endif
 
256
/***********************************************************************
 
257
Releases an exclusive mode lock. */
 
258
 
 
259
#ifdef UNIV_SYNC_DEBUG
 
260
#define rw_lock_x_unlock_gen(L, P)      rw_lock_x_unlock_func(L, P)
 
261
#else
 
262
#define rw_lock_x_unlock_gen(L, P)      rw_lock_x_unlock_func(L)
 
263
#endif
 
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. */
 
268
UNIV_INLINE
 
269
void
 
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 */
 
275
);
 
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. */
 
280
UNIV_INLINE
 
281
void
 
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 */
 
287
);
 
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
 
295
operations. */
 
296
UNIV_INTERN
 
297
void
 
298
rw_lock_x_lock_move_ownership(
 
299
/*==========================*/
 
300
        rw_lock_t*      lock);  /* in: lock which was x-locked in the
 
301
                                buffer read */
 
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. */
 
305
UNIV_INLINE
 
306
void
 
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. */
 
313
UNIV_INLINE
 
314
void
 
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. */
 
321
UNIV_INLINE
 
322
ulint
 
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. */
 
329
UNIV_INLINE
 
330
ulint
 
331
rw_lock_get_waiters(
 
332
/*================*/
 
333
        rw_lock_t*      lock);
 
334
UNIV_INLINE
 
335
ulint
 
336
rw_lock_get_writer(
 
337
/*===============*/
 
338
        rw_lock_t*      lock);
 
339
UNIV_INLINE
 
340
ulint
 
341
rw_lock_get_reader_count(
 
342
/*=====================*/
 
343
        rw_lock_t*      lock);
 
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. */
 
348
UNIV_INTERN
 
349
ibool
 
350
rw_lock_own(
 
351
/*========*/
 
352
        rw_lock_t*      lock,           /* in: rw-lock */
 
353
        ulint           lock_type);     /* in: lock type: RW_LOCK_SHARED,
 
354
                                        RW_LOCK_EX */
 
355
#endif /* UNIV_SYNC_DEBUG */
 
356
/**********************************************************************
 
357
Checks if somebody has locked the rw-lock in the specified mode. */
 
358
UNIV_INTERN
 
359
ibool
 
360
rw_lock_is_locked(
 
361
/*==============*/
 
362
        rw_lock_t*      lock,           /* in: rw-lock */
 
363
        ulint           lock_type);     /* in: lock type: RW_LOCK_SHARED,
 
364
                                        RW_LOCK_EX */
 
365
#ifdef UNIV_SYNC_DEBUG
 
366
/*******************************************************************
 
367
Prints debug info of an rw-lock. */
 
368
UNIV_INTERN
 
369
void
 
370
rw_lock_print(
 
371
/*==========*/
 
372
        rw_lock_t*      lock);  /* in: rw-lock */
 
373
/*******************************************************************
 
374
Prints debug info of currently locked rw-locks. */
 
375
UNIV_INTERN
 
376
void
 
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. */
 
383
UNIV_INTERN
 
384
ulint
 
385
rw_lock_n_locked(void);
 
386
/*==================*/
 
387
 
 
388
/*#####################################################################*/
 
389
 
 
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
 
395
mutex. */
 
396
UNIV_INTERN
 
397
void
 
398
rw_lock_debug_mutex_enter(void);
 
399
/*==========================*/
 
400
/**********************************************************************
 
401
Releases the debug mutex. */
 
402
UNIV_INTERN
 
403
void
 
404
rw_lock_debug_mutex_exit(void);
 
405
/*==========================*/
 
406
/*************************************************************************
 
407
Prints info of a debug struct. */
 
408
UNIV_INTERN
 
409
void
 
410
rw_lock_debug_print(
 
411
/*================*/
 
412
        rw_lock_debug_t*        info);  /* in: debug struct */
 
413
#endif /* UNIV_SYNC_DEBUG */
 
414
 
 
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. */
 
422
 
 
423
struct rw_lock_struct {
 
424
        os_event_t      event;  /* Used by sync0arr.c for thread queueing */
 
425
 
 
426
#ifdef __WIN__
 
427
        os_event_t      wait_ex_event;  /* This windows specific event is
 
428
                                used by the thread which has set the
 
429
                                lock state to RW_LOCK_WAIT_EX. The
 
430
                                rw_lock design guarantees that this
 
431
                                thread will be the next one to proceed
 
432
                                once the current the event gets
 
433
                                signalled. See LEMMA 2 in sync0sync.c */
 
434
#endif
 
435
 
 
436
        ulint   reader_count;   /* Number of readers who have locked this
 
437
                                lock in the shared mode */
 
438
        ulint   writer;         /* This field is set to RW_LOCK_EX if there
 
439
                                is a writer owning the lock (in exclusive
 
440
                                mode), RW_LOCK_WAIT_EX if a writer is
 
441
                                queueing for the lock, and
 
442
                                RW_LOCK_NOT_LOCKED, otherwise. */
 
443
        os_thread_id_t  writer_thread;
 
444
                                /* Thread id of a possible writer thread */
 
445
        ulint   writer_count;   /* Number of times the same thread has
 
446
                                recursively locked the lock in the exclusive
 
447
                                mode */
 
448
        mutex_t mutex;          /* The mutex protecting rw_lock_struct */
 
449
        ulint   pass;           /* Default value 0. This is set to some
 
450
                                value != 0 given by the caller of an x-lock
 
451
                                operation, if the x-lock is to be passed to
 
452
                                another thread to unlock (which happens in
 
453
                                asynchronous i/o). */
 
454
        ulint   waiters;        /* This ulint is set to 1 if there are
 
455
                                waiters (readers or writers) in the global
 
456
                                wait array, waiting for this rw_lock.
 
457
                                Otherwise, == 0. */
 
458
        UT_LIST_NODE_T(rw_lock_t) list;
 
459
                                /* All allocated rw locks are put into a
 
460
                                list */
 
461
#ifdef UNIV_SYNC_DEBUG
 
462
        UT_LIST_BASE_NODE_T(rw_lock_debug_t) debug_list;
 
463
                                /* In the debug version: pointer to the debug
 
464
                                info list of the lock */
 
465
        ulint   level;          /* Level in the global latching order. */
 
466
#endif /* UNIV_SYNC_DEBUG */
 
467
        const char*     cfile_name;/* File name where lock created */
 
468
        const char*     last_s_file_name;/* File name where last s-locked */
 
469
        const char*     last_x_file_name;/* File name where last x-locked */
 
470
        ibool           writer_is_wait_ex;
 
471
                                /* This is TRUE if the writer field is
 
472
                                RW_LOCK_WAIT_EX; this field is located far
 
473
                                from the memory update hotspot fields which
 
474
                                are at the start of this struct, thus we can
 
475
                                peek this field without causing much memory
 
476
                                bus traffic */
 
477
        unsigned        cline:14;       /* Line where created */
 
478
        unsigned        last_s_line:14; /* Line number where last time s-locked */
 
479
        unsigned        last_x_line:14; /* Line number where last time x-locked */
 
480
        ulint   magic_n;
 
481
};
 
482
 
 
483
#define RW_LOCK_MAGIC_N 22643
 
484
 
 
485
#ifdef UNIV_SYNC_DEBUG
 
486
/* The structure for storing debug info of an rw-lock */
 
487
struct  rw_lock_debug_struct {
 
488
 
 
489
        os_thread_id_t thread_id;  /* The thread id of the thread which
 
490
                                locked the rw-lock */
 
491
        ulint   pass;           /* Pass value given in the lock operation */
 
492
        ulint   lock_type;      /* Type of the lock: RW_LOCK_EX,
 
493
                                RW_LOCK_SHARED, RW_LOCK_WAIT_EX */
 
494
        const char*     file_name;/* File name where the lock was obtained */
 
495
        ulint   line;           /* Line where the rw-lock was locked */
 
496
        UT_LIST_NODE_T(rw_lock_debug_t) list;
 
497
                                /* Debug structs are linked in a two-way
 
498
                                list */
 
499
};
 
500
#endif /* UNIV_SYNC_DEBUG */
 
501
 
 
502
#ifndef UNIV_NONINL
 
503
#include "sync0rw.ic"
 
504
#endif
 
505
 
 
506
#endif