~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/include/sync0sync.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
Mutex, the basic synchronization primitive
 
3
 
 
4
(c) 1995 Innobase Oy
 
5
 
 
6
Created 9/5/1995 Heikki Tuuri
 
7
*******************************************************/
 
8
 
 
9
#ifndef sync0sync_h
 
10
#define sync0sync_h
 
11
 
 
12
#include "univ.i"
 
13
#include "sync0types.h"
 
14
#include "ut0lst.h"
 
15
#include "ut0mem.h"
 
16
#include "os0thread.h"
 
17
#include "os0sync.h"
 
18
#include "sync0arr.h"
 
19
 
 
20
#ifndef UNIV_HOTBACKUP
 
21
extern my_bool  timed_mutexes;
 
22
#endif /* UNIV_HOTBACKUP */
 
23
 
 
24
/**********************************************************************
 
25
Initializes the synchronization data structures. */
 
26
UNIV_INTERN
 
27
void
 
28
sync_init(void);
 
29
/*===========*/
 
30
/**********************************************************************
 
31
Frees the resources in synchronization data structures. */
 
32
UNIV_INTERN
 
33
void
 
34
sync_close(void);
 
35
/*===========*/
 
36
/**********************************************************************
 
37
Creates, or rather, initializes a mutex object to a specified memory
 
38
location (which must be appropriately aligned). The mutex is initialized
 
39
in the reset state. Explicit freeing of the mutex with mutex_free is
 
40
necessary only if the memory block containing it is freed. */
 
41
 
 
42
#ifdef UNIV_DEBUG
 
43
# ifdef UNIV_SYNC_DEBUG
 
44
#  define mutex_create(M, level)                                        \
 
45
        mutex_create_func((M), #M, (level), __FILE__, __LINE__)
 
46
# else
 
47
#  define mutex_create(M, level)                                        \
 
48
        mutex_create_func((M), #M, __FILE__, __LINE__)
 
49
# endif
 
50
#else
 
51
# define mutex_create(M, level)                                 \
 
52
        mutex_create_func((M), __FILE__, __LINE__)
 
53
#endif
 
54
 
 
55
/**********************************************************************
 
56
Creates, or rather, initializes a mutex object in a specified memory
 
57
location (which must be appropriately aligned). The mutex is initialized
 
58
in the reset state. Explicit freeing of the mutex with mutex_free is
 
59
necessary only if the memory block containing it is freed. */
 
60
UNIV_INTERN
 
61
void
 
62
mutex_create_func(
 
63
/*==============*/
 
64
        mutex_t*        mutex,          /* in: pointer to memory */
 
65
#ifdef UNIV_DEBUG
 
66
        const char*     cmutex_name,    /* in: mutex name */
 
67
# ifdef UNIV_SYNC_DEBUG
 
68
        ulint           level,          /* in: level */
 
69
# endif /* UNIV_SYNC_DEBUG */
 
70
#endif /* UNIV_DEBUG */
 
71
        const char*     cfile_name,     /* in: file name where created */
 
72
        ulint           cline);         /* in: file line where created */
 
73
 
 
74
#undef mutex_free                       /* Fix for MacOS X */
 
75
 
 
76
/**********************************************************************
 
77
Calling this function is obligatory only if the memory buffer containing
 
78
the mutex is freed. Removes a mutex object from the mutex list. The mutex
 
79
is checked to be in the reset state. */
 
80
UNIV_INTERN
 
81
void
 
82
mutex_free(
 
83
/*=======*/
 
84
        mutex_t*        mutex); /* in: mutex */
 
85
/******************************************************************
 
86
NOTE! The following macro should be used in mutex locking, not the
 
87
corresponding function. */
 
88
 
 
89
#define mutex_enter(M)    mutex_enter_func((M), __FILE__, __LINE__)
 
90
/******************************************************************
 
91
NOTE! The following macro should be used in mutex locking, not the
 
92
corresponding function. */
 
93
 
 
94
/* NOTE! currently same as mutex_enter! */
 
95
 
 
96
#define mutex_enter_fast(M)     mutex_enter_func((M), __FILE__, __LINE__)
 
97
/**********************************************************************
 
98
NOTE! Use the corresponding macro in the header file, not this function
 
99
directly. Locks a mutex for the current thread. If the mutex is reserved
 
100
the function spins a preset time (controlled by SYNC_SPIN_ROUNDS) waiting
 
101
for the mutex before suspending the thread. */
 
102
UNIV_INLINE
 
103
void
 
104
mutex_enter_func(
 
105
/*=============*/
 
106
        mutex_t*        mutex,          /* in: pointer to mutex */
 
107
        const char*     file_name,      /* in: file name where locked */
 
108
        ulint           line);          /* in: line where locked */
 
109
/******************************************************************
 
110
NOTE! The following macro should be used in mutex locking, not the
 
111
corresponding function. */
 
112
 
 
113
#define mutex_enter_nowait(M)   \
 
114
        mutex_enter_nowait_func((M), __FILE__, __LINE__)
 
115
/************************************************************************
 
116
NOTE! Use the corresponding macro in the header file, not this function
 
117
directly. Tries to lock the mutex for the current thread. If the lock is not
 
118
acquired immediately, returns with return value 1. */
 
119
UNIV_INTERN
 
120
ulint
 
121
mutex_enter_nowait_func(
 
122
/*====================*/
 
123
                                        /* out: 0 if succeed, 1 if not */
 
124
        mutex_t*        mutex,          /* in: pointer to mutex */
 
125
        const char*     file_name,      /* in: file name where mutex
 
126
                                        requested */
 
127
        ulint           line);          /* in: line where requested */
 
128
/**********************************************************************
 
129
Unlocks a mutex owned by the current thread. */
 
130
UNIV_INLINE
 
131
void
 
132
mutex_exit(
 
133
/*=======*/
 
134
        mutex_t*        mutex); /* in: pointer to mutex */
 
135
/**********************************************************************
 
136
Returns TRUE if no mutex or rw-lock is currently locked.
 
137
Works only in the debug version. */
 
138
UNIV_INTERN
 
139
ibool
 
140
sync_all_freed(void);
 
141
/*================*/
 
142
/*#####################################################################
 
143
FUNCTION PROTOTYPES FOR DEBUGGING */
 
144
/***********************************************************************
 
145
Prints wait info of the sync system. */
 
146
UNIV_INTERN
 
147
void
 
148
sync_print_wait_info(
 
149
/*=================*/
 
150
        FILE*   file);          /* in: file where to print */
 
151
/***********************************************************************
 
152
Prints info of the sync system. */
 
153
UNIV_INTERN
 
154
void
 
155
sync_print(
 
156
/*=======*/
 
157
        FILE*   file);          /* in: file where to print */
 
158
#ifdef UNIV_DEBUG
 
159
/**********************************************************************
 
160
Checks that the mutex has been initialized. */
 
161
UNIV_INTERN
 
162
ibool
 
163
mutex_validate(
 
164
/*===========*/
 
165
        const mutex_t*  mutex);
 
166
/**********************************************************************
 
167
Checks that the current thread owns the mutex. Works only
 
168
in the debug version. */
 
169
UNIV_INTERN
 
170
ibool
 
171
mutex_own(
 
172
/*======*/
 
173
                                /* out: TRUE if owns */
 
174
        const mutex_t*  mutex); /* in: mutex */
 
175
#endif /* UNIV_DEBUG */
 
176
#ifdef UNIV_SYNC_DEBUG
 
177
/**********************************************************************
 
178
Adds a latch and its level in the thread level array. Allocates the memory
 
179
for the array if called first time for this OS thread. Makes the checks
 
180
against other latch levels stored in the array for this thread. */
 
181
UNIV_INTERN
 
182
void
 
183
sync_thread_add_level(
 
184
/*==================*/
 
185
        void*   latch,  /* in: pointer to a mutex or an rw-lock */
 
186
        ulint   level); /* in: level in the latching order; if
 
187
                        SYNC_LEVEL_VARYING, nothing is done */
 
188
/**********************************************************************
 
189
Removes a latch from the thread level array if it is found there. */
 
190
UNIV_INTERN
 
191
ibool
 
192
sync_thread_reset_level(
 
193
/*====================*/
 
194
                        /* out: TRUE if found from the array; it is no error
 
195
                        if the latch is not found, as we presently are not
 
196
                        able to determine the level for every latch
 
197
                        reservation the program does */
 
198
        void*   latch); /* in: pointer to a mutex or an rw-lock */
 
199
/**********************************************************************
 
200
Checks that the level array for the current thread is empty. */
 
201
UNIV_INTERN
 
202
ibool
 
203
sync_thread_levels_empty(void);
 
204
/*==========================*/
 
205
                        /* out: TRUE if empty */
 
206
/**********************************************************************
 
207
Checks that the level array for the current thread is empty. */
 
208
UNIV_INTERN
 
209
ibool
 
210
sync_thread_levels_empty_gen(
 
211
/*=========================*/
 
212
                                        /* out: TRUE if empty except the
 
213
                                        exceptions specified below */
 
214
        ibool   dict_mutex_allowed);    /* in: TRUE if dictionary mutex is
 
215
                                        allowed to be owned by the thread,
 
216
                                        also purge_is_running mutex is
 
217
                                        allowed */
 
218
/**********************************************************************
 
219
Gets the debug information for a reserved mutex. */
 
220
UNIV_INTERN
 
221
void
 
222
mutex_get_debug_info(
 
223
/*=================*/
 
224
        mutex_t*        mutex,          /* in: mutex */
 
225
        const char**    file_name,      /* out: file where requested */
 
226
        ulint*          line,           /* out: line where requested */
 
227
        os_thread_id_t* thread_id);     /* out: id of the thread which owns
 
228
                                        the mutex */
 
229
/**********************************************************************
 
230
Counts currently reserved mutexes. Works only in the debug version. */
 
231
UNIV_INTERN
 
232
ulint
 
233
mutex_n_reserved(void);
 
234
/*==================*/
 
235
#endif /* UNIV_SYNC_DEBUG */
 
236
/**********************************************************************
 
237
NOT to be used outside this module except in debugging! Gets the value
 
238
of the lock word. */
 
239
UNIV_INLINE
 
240
ulint
 
241
mutex_get_lock_word(
 
242
/*================*/
 
243
        const mutex_t*  mutex); /* in: mutex */
 
244
#ifdef UNIV_SYNC_DEBUG
 
245
/**********************************************************************
 
246
NOT to be used outside this module except in debugging! Gets the waiters
 
247
field in a mutex. */
 
248
UNIV_INLINE
 
249
ulint
 
250
mutex_get_waiters(
 
251
/*==============*/
 
252
                                /* out: value to set */
 
253
        const mutex_t*  mutex); /* in: mutex */
 
254
#endif /* UNIV_SYNC_DEBUG */
 
255
 
 
256
/*
 
257
                LATCHING ORDER WITHIN THE DATABASE
 
258
                ==================================
 
259
 
 
260
The mutex or latch in the central memory object, for instance, a rollback
 
261
segment object, must be acquired before acquiring the latch or latches to
 
262
the corresponding file data structure. In the latching order below, these
 
263
file page object latches are placed immediately below the corresponding
 
264
central memory object latch or mutex.
 
265
 
 
266
Synchronization object                  Notes
 
267
----------------------                  -----
 
268
 
 
269
Dictionary mutex                        If we have a pointer to a dictionary
 
270
|                                       object, e.g., a table, it can be
 
271
|                                       accessed without reserving the
 
272
|                                       dictionary mutex. We must have a
 
273
|                                       reservation, a memoryfix, to the
 
274
|                                       appropriate table object in this case,
 
275
|                                       and the table must be explicitly
 
276
|                                       released later.
 
277
V
 
278
Dictionary header
 
279
|
 
280
V
 
281
Secondary index tree latch              The tree latch protects also all
 
282
|                                       the B-tree non-leaf pages. These
 
283
V                                       can be read with the page only
 
284
Secondary index non-leaf                bufferfixed to save CPU time,
 
285
|                                       no s-latch is needed on the page.
 
286
|                                       Modification of a page requires an
 
287
|                                       x-latch on the page, however. If a
 
288
|                                       thread owns an x-latch to the tree,
 
289
|                                       it is allowed to latch non-leaf pages
 
290
|                                       even after it has acquired the fsp
 
291
|                                       latch.
 
292
V
 
293
Secondary index leaf                    The latch on the secondary index leaf
 
294
|                                       can be kept while accessing the
 
295
|                                       clustered index, to save CPU time.
 
296
V
 
297
Clustered index tree latch              To increase concurrency, the tree
 
298
|                                       latch is usually released when the
 
299
|                                       leaf page latch has been acquired.
 
300
V
 
301
Clustered index non-leaf
 
302
|
 
303
V
 
304
Clustered index leaf
 
305
|
 
306
V
 
307
Transaction system header
 
308
|
 
309
V
 
310
Transaction undo mutex                  The undo log entry must be written
 
311
|                                       before any index page is modified.
 
312
|                                       Transaction undo mutex is for the undo
 
313
|                                       logs the analogue of the tree latch
 
314
|                                       for a B-tree. If a thread has the
 
315
|                                       trx undo mutex reserved, it is allowed
 
316
|                                       to latch the undo log pages in any
 
317
|                                       order, and also after it has acquired
 
318
|                                       the fsp latch.
 
319
V
 
320
Rollback segment mutex                  The rollback segment mutex must be
 
321
|                                       reserved, if, e.g., a new page must
 
322
|                                       be added to an undo log. The rollback
 
323
|                                       segment and the undo logs in its
 
324
|                                       history list can be seen as an
 
325
|                                       analogue of a B-tree, and the latches
 
326
|                                       reserved similarly, using a version of
 
327
|                                       lock-coupling. If an undo log must be
 
328
|                                       extended by a page when inserting an
 
329
|                                       undo log record, this corresponds to
 
330
|                                       a pessimistic insert in a B-tree.
 
331
V
 
332
Rollback segment header
 
333
|
 
334
V
 
335
Purge system latch
 
336
|
 
337
V
 
338
Undo log pages                          If a thread owns the trx undo mutex,
 
339
|                                       or for a log in the history list, the
 
340
|                                       rseg mutex, it is allowed to latch
 
341
|                                       undo log pages in any order, and even
 
342
|                                       after it has acquired the fsp latch.
 
343
|                                       If a thread does not have the
 
344
|                                       appropriate mutex, it is allowed to
 
345
|                                       latch only a single undo log page in
 
346
|                                       a mini-transaction.
 
347
V
 
348
File space management latch             If a mini-transaction must allocate
 
349
|                                       several file pages, it can do that,
 
350
|                                       because it keeps the x-latch to the
 
351
|                                       file space management in its memo.
 
352
V
 
353
File system pages
 
354
|
 
355
V
 
356
Kernel mutex                            If a kernel operation needs a file
 
357
|                                       page allocation, it must reserve the
 
358
|                                       fsp x-latch before acquiring the kernel
 
359
|                                       mutex.
 
360
V
 
361
Search system mutex
 
362
|
 
363
V
 
364
Buffer pool mutex
 
365
|
 
366
V
 
367
Log mutex
 
368
|
 
369
Any other latch
 
370
|
 
371
V
 
372
Memory pool mutex */
 
373
 
 
374
/* Latching order levels */
 
375
 
 
376
/* User transaction locks are higher than any of the latch levels below:
 
377
no latches are allowed when a thread goes to wait for a normal table
 
378
or row lock! */
 
379
#define SYNC_USER_TRX_LOCK      9999
 
380
#define SYNC_NO_ORDER_CHECK     3000    /* this can be used to suppress
 
381
                                        latching order checking */
 
382
#define SYNC_LEVEL_VARYING      2000    /* Level is varying. Only used with
 
383
                                        buffer pool page locks, which do not
 
384
                                        have a fixed level, but instead have
 
385
                                        their level set after the page is
 
386
                                        locked; see e.g.
 
387
                                        ibuf_bitmap_get_map_page(). */
 
388
#define SYNC_TRX_I_S_RWLOCK     1910    /* Used for
 
389
                                        trx_i_s_cache_t::rw_lock */
 
390
#define SYNC_TRX_I_S_LAST_READ  1900    /* Used for
 
391
                                        trx_i_s_cache_t::last_read_mutex */
 
392
#define SYNC_FILE_FORMAT_TAG    1200    /* Used to serialize access to the
 
393
                                        file format tag */
 
394
#define SYNC_DICT_OPERATION     1001    /* table create, drop, etc. reserve
 
395
                                        this in X-mode, implicit or backround
 
396
                                        operations purge, rollback, foreign
 
397
                                        key checks reserve this in S-mode */
 
398
#define SYNC_DICT               1000
 
399
#define SYNC_DICT_AUTOINC_MUTEX 999
 
400
#define SYNC_DICT_HEADER        995
 
401
#define SYNC_IBUF_HEADER        914
 
402
#define SYNC_IBUF_PESS_INSERT_MUTEX 912
 
403
#define SYNC_IBUF_MUTEX         910     /* ibuf mutex is really below
 
404
                                        SYNC_FSP_PAGE: we assign a value this
 
405
                                        high only to make the program to pass
 
406
                                        the debug checks */
 
407
/*-------------------------------*/
 
408
#define SYNC_INDEX_TREE         900
 
409
#define SYNC_TREE_NODE_NEW      892
 
410
#define SYNC_TREE_NODE_FROM_HASH 891
 
411
#define SYNC_TREE_NODE          890
 
412
#define SYNC_PURGE_SYS          810
 
413
#define SYNC_PURGE_LATCH        800
 
414
#define SYNC_TRX_UNDO           700
 
415
#define SYNC_RSEG               600
 
416
#define SYNC_RSEG_HEADER_NEW    591
 
417
#define SYNC_RSEG_HEADER        590
 
418
#define SYNC_TRX_UNDO_PAGE      570
 
419
#define SYNC_EXTERN_STORAGE     500
 
420
#define SYNC_FSP                400
 
421
#define SYNC_FSP_PAGE           395
 
422
/*------------------------------------- Insert buffer headers */
 
423
/*------------------------------------- ibuf_mutex */
 
424
/*------------------------------------- Insert buffer tree */
 
425
#define SYNC_IBUF_BITMAP_MUTEX  351
 
426
#define SYNC_IBUF_BITMAP        350
 
427
/*------------------------------------- MySQL query cache mutex */
 
428
/*------------------------------------- MySQL binlog mutex */
 
429
/*-------------------------------*/
 
430
#define SYNC_KERNEL             300
 
431
#define SYNC_REC_LOCK           299
 
432
#define SYNC_TRX_LOCK_HEAP      298
 
433
#define SYNC_TRX_SYS_HEADER     290
 
434
#define SYNC_LOG                170
 
435
#define SYNC_RECV               168
 
436
#define SYNC_WORK_QUEUE         161
 
437
#define SYNC_SEARCH_SYS         160     /* NOTE that if we have a memory
 
438
                                        heap that can be extended to the
 
439
                                        buffer pool, its logical level is
 
440
                                        SYNC_SEARCH_SYS, as memory allocation
 
441
                                        can call routines there! Otherwise
 
442
                                        the level is SYNC_MEM_HASH. */
 
443
#define SYNC_BUF_POOL           150
 
444
#define SYNC_BUF_BLOCK          149
 
445
#define SYNC_DOUBLEWRITE        140
 
446
#define SYNC_ANY_LATCH          135
 
447
#define SYNC_THR_LOCAL          133
 
448
#define SYNC_MEM_HASH           131
 
449
#define SYNC_MEM_POOL           130
 
450
 
 
451
/* Codes used to designate lock operations */
 
452
#define RW_LOCK_NOT_LOCKED      350
 
453
#define RW_LOCK_EX              351
 
454
#define RW_LOCK_EXCLUSIVE       351
 
455
#define RW_LOCK_SHARED          352
 
456
#define RW_LOCK_WAIT_EX         353
 
457
#define SYNC_MUTEX              354
 
458
 
 
459
/* NOTE! The structure appears here only for the compiler to know its size.
 
460
Do not use its fields directly! The structure used in the spin lock
 
461
implementation of a mutual exclusion semaphore. */
 
462
 
 
463
struct mutex_struct {
 
464
        os_event_t      event;  /* Used by sync0arr.c for the wait queue */
 
465
        ulint   lock_word;      /* This ulint is the target of the atomic
 
466
                                test-and-set instruction in Win32 */
 
467
#if defined WIN32 && defined UNIV_CAN_USE_X86_ASSEMBLER
 
468
#else
 
469
        os_fast_mutex_t
 
470
                os_fast_mutex;  /* In other systems we use this OS mutex
 
471
                                in place of lock_word */
 
472
#endif
 
473
        ulint   waiters;        /* This ulint is set to 1 if there are (or
 
474
                                may be) threads waiting in the global wait
 
475
                                array for this mutex to be released.
 
476
                                Otherwise, this is 0. */
 
477
        UT_LIST_NODE_T(mutex_t) list; /* All allocated mutexes are put into
 
478
                                a list. Pointers to the next and prev. */
 
479
#ifdef UNIV_SYNC_DEBUG
 
480
        const char*     file_name;      /* File where the mutex was locked */
 
481
        ulint   line;           /* Line where the mutex was locked */
 
482
        ulint   level;          /* Level in the global latching order */
 
483
#endif /* UNIV_SYNC_DEBUG */
 
484
        const char*     cfile_name;/* File name where mutex created */
 
485
        ulint           cline;  /* Line where created */
 
486
#ifdef UNIV_DEBUG
 
487
        os_thread_id_t thread_id; /* The thread id of the thread
 
488
                                which locked the mutex. */
 
489
        ulint           magic_n;
 
490
# define MUTEX_MAGIC_N  (ulint)979585
 
491
#endif /* UNIV_DEBUG */
 
492
#ifndef UNIV_HOTBACKUP
 
493
        ulong           count_os_wait; /* count of os_wait */
 
494
# ifdef UNIV_DEBUG
 
495
        ulong           count_using; /* count of times mutex used */
 
496
        ulong           count_spin_loop; /* count of spin loops */
 
497
        ulong           count_spin_rounds; /* count of spin rounds */
 
498
        ulong           count_os_yield; /* count of os_wait */
 
499
        ulonglong       lspent_time; /* mutex os_wait timer msec */
 
500
        ulonglong       lmax_spent_time; /* mutex os_wait timer msec */
 
501
        const char*     cmutex_name;/* mutex name */
 
502
        ulint           mutex_type;/* 0 - usual mutex 1 - rw_lock mutex  */
 
503
# endif /* UNIV_DEBUG */
 
504
#endif /* !UNIV_HOTBACKUP */
 
505
};
 
506
 
 
507
/* The global array of wait cells for implementation of the databases own
 
508
mutexes and read-write locks. Appears here for debugging purposes only! */
 
509
 
 
510
extern sync_array_t*    sync_primary_wait_array;
 
511
 
 
512
/* Constant determining how long spin wait is continued before suspending
 
513
the thread. A value 600 rounds on a 1995 100 MHz Pentium seems to correspond
 
514
to 20 microseconds. */
 
515
 
 
516
#define SYNC_SPIN_ROUNDS        srv_n_spin_wait_rounds
 
517
 
 
518
/* The number of system calls made in this module. Intended for performance
 
519
monitoring. */
 
520
 
 
521
extern  ulint   mutex_system_call_count;
 
522
extern  ulint   mutex_exit_count;
 
523
 
 
524
#ifdef UNIV_SYNC_DEBUG
 
525
/* Latching order checks start when this is set TRUE */
 
526
extern ibool    sync_order_checks_on;
 
527
#endif /* UNIV_SYNC_DEBUG */
 
528
 
 
529
/* This variable is set to TRUE when sync_init is called */
 
530
extern ibool    sync_initialized;
 
531
 
 
532
/* Global list of database mutexes (not OS mutexes) created. */
 
533
typedef UT_LIST_BASE_NODE_T(mutex_t)  ut_list_base_node_t;
 
534
extern ut_list_base_node_t  mutex_list;
 
535
 
 
536
/* Mutex protecting the mutex_list variable */
 
537
extern mutex_t mutex_list_mutex;
 
538
 
 
539
 
 
540
#ifndef UNIV_NONINL
 
541
#include "sync0sync.ic"
 
542
#endif
 
543
 
 
544
#endif