summaryrefslogtreecommitdiff
path: root/db/mp/Design
blob: 1b26aae6cba616c0ba8671bd7c0c02a16bb29d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$Id: Design,v 11.2 1999/11/21 23:08:27 bostic Exp $

There are three ways we do locking in the mpool code:

Locking a handle mutex to provide concurrency for DB_THREAD operations.
Locking the region mutex to provide mutual exclusion while reading and
    writing structures in the shared region.
Locking buffer header mutexes during I/O.

The first will not be further described here.  We use the shared mpool
region lock to provide mutual exclusion while reading/modifying all of
the data structures, including the buffer headers.  We use a per-buffer
header lock to wait on buffer I/O.  The order of locking is as follows:

Searching for a buffer:
    Acquire the region lock.
    Find the buffer header.
    Increment the reference count (guarantee the buffer stays).
    While the BH_LOCKED flag is set (I/O is going on) {
	Release the region lock.
	    Explicitly yield the processor if it's not the first pass
	    through this loop, otherwise, we can simply spin because
	    we'll be simply switching between the two locks.
	Request the buffer lock.
	The I/O will complete...
	Acquire the buffer lock.
	Release the buffer lock.
	Acquire the region lock.
    }
    Return the buffer.

Reading/writing a buffer:
    Acquire the region lock.
    Find/create the buffer header.
    If reading, increment the reference count (guarantee the buffer stays).
    Set the BH_LOCKED flag.
    Acquire the buffer lock (guaranteed not to block).
    Release the region lock.
    Do the I/O and/or initialize the buffer contents.
    Release the buffer lock.
	At this point, the buffer lock is available, but the logical
	operation (flagged by BH_LOCKED) is not yet completed.  For
	this reason, among others, threads checking the BH_LOCKED flag
	must loop around their test.
    Acquire the region lock.
    Clear the BH_LOCKED flag.
    Release the region lock.
    Return/discard the buffer.

Pointers to DB_MPOOL, MPOOL, DB_MPOOLFILE and MPOOLFILE structures are
not reacquired when a region lock is reacquired because they couldn't
have been closed/discarded and because they never move in memory.