Reader Lock Table
[LMDB Internals]

Data Structures

struct  MDB_rxbody
struct  MDB_reader
struct  MDB_txbody
struct  MDB_txninfo

Defines

#define DEFAULT_READERS   126
#define CACHELINE   64
#define MDB_LOCK_FORMAT

Detailed Description

Readers don't acquire any locks for their data access. Instead, they simply record their transaction ID in the reader table. The reader mutex is needed just to find an empty slot in the reader table. The slot's address is saved in thread-specific data so that subsequent read transactions started by the same thread need no further locking to proceed.

If MDB_NOTLS is set, the slot address is not saved in thread-specific data.

No reader table is used if the database is on a read-only filesystem, or if MDB_NOLOCK is set.

Since the database uses multi-version concurrency control, readers don't actually need any locking. This table is used to keep track of which readers are using data from which old transactions, so that we'll know when a particular old transaction is no longer in use. Old transactions that have discarded any data pages can then have those pages reclaimed for use by a later write transaction.

The lock table is constructed such that reader slots are aligned with the processor's cache line size. Any slot is only ever used by one thread. This alignment guarantees that there will be no contention or cache thrashing as threads update their own slot info, and also eliminates any need for locking when accessing a slot.

A writer thread will scan every slot in the table to determine the oldest outstanding reader transaction. Any freed pages older than this will be reclaimed by the writer. The writer doesn't use any locks when scanning this table. This means that there's no guarantee that the writer will see the most up-to-date reader info, but that's not required for correct operation - all we need is to know the upper bound on the oldest reader, we don't care at all about the newest reader. So the only consequence of reading stale information here is that old pages might hang around a while longer before being reclaimed. That's actually good anyway, because the longer we delay reclaiming old pages, the more likely it is that a string of contiguous pages can be found after coalescing old pages from many old transactions together.


Define Documentation

#define DEFAULT_READERS   126

Number of slots in the reader table. This value was chosen somewhat arbitrarily. 126 readers plus a couple mutexes fit exactly into 8KB on my development machine. Applications should set the table size using mdb_env_set_maxreaders().

#define CACHELINE   64

The size of a CPU cache line in bytes. We want our lock structures aligned to this size to avoid false cache line sharing in the lock table. This value works for most CPUs. For Itanium this should be 128.

#define MDB_LOCK_FORMAT
Value:
((uint32_t) \
     ((MDB_LOCK_VERSION) \
      /* Flags which describe functionality */ \
      + (((MDB_PIDLOCK) != 0) << 16)))

Lockfile format signature: version, features and field layout

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 15 Feb 2016 for LMDB by  doxygen 1.6.1