### Berkeley DB Reference Guide: Locking Subsystem |

# Standard lock modes

The Berkeley DB locking protocol is described by a conflict matrix. A conflict matrix is an n x n array where n is the number of different lock modes supported, and the (i, j)th entry of the array indicates whether a lock of mode i conflicts with a lock of mode j.

The Berkeley DB include files declare two commonly used conflict arrays:

const u_int8_t db_rw_conflicts[ ];
  This is a conflict matrix for a simple scheme using shared and exclusive lock modes.

const u_int8_t db_riw_conflicts[ ];
  This is a conflict matrix that involves various intent lock modes (e.g., intent shared) that are used for multigranularity locking.

The number of modes associated with each matrix are DB_LOCK_RW_N and DB_LOCK_RIW_N, respectively.

In addition, the Berkeley DB include file defines the type db_lockmode_t, which is the type of the lock modes used with the standard tables above:

DB_LOCK_NG
  not granted (always 0)

DB_LOCK_READ
  read (shared)

DB_LOCK_WRITE
  write (exclusive)

As an example, consider the basic multiple-reader/single writer conflict matrix described by db_rw_conflicts. In the following example (and in the appropriate file), a 1 represents a conflict (i.e., do not grant the lock if the indicated lock is held) and a 0 indicates that it is OK to grant the lock.

The rows indicate the lock that is held and the columns indicate the lock that is requested.

>     		Notheld	Read    Write
>     Notheld		0	0	0
>     Read*		0	0	1
>     Write**		0	1	1

*
  In this case, suppose that there is a read lock held on an object. A new request for a read lock would be granted, but a request for a write lock would not.

**
  In this case, suppose that there is a write lock held on an object. A new request for either a read or write lock would be denied.

Copyright Sleepycat Software