---
name: "db_lock"
section: "3db"
platform: "linux"
package: "libdb2-dev"
version: "2.4.14-1"
release: "Debian 2.1 (Slink)"
platforms: ["linux"]
url: "https://new.carta.tech/db_lock"
---

# db\_lock(3db)

lock manager

## NAME

db\_lock - lock manager

## SYNOPSIS

```
#include <db2/db.h>
int
lock_open(const char *dir,
u_int32_t flags, int mode, DB_ENV *dbenv, DB_LOCKTAB **regionp);
int
lock_id(DB_LOCKTAB *lt, u_int32_t *idp);
int
lock_vec(DB_LOCKTAB *lt, u_int32_t locker, u_int32_t flags,
DB_LOCKREQ list[], int nlist, DB_LOCKREQ **elistp);
int
lock_get(DB_LOCKTAB *lt, u_int32_t locker, u_int32_t flags,
const DBT *obj, const db_lockmode_t lock_mode, DB_LOCK *lock);
int
lock_put(DB_LOCKTAB *lt, DB_LOCK lock);
int
lock_close(DB_LOCKTAB *lt);
int
lock_unlink(const char *dir, int force, DB_ENV *dbenv);
int
lock_detect(DB_LOCKTAB *lt, u_int32_t flags, u_int32_t atype);
int
lock_stat(DB_LOCKTAB *lt,
DB_LOCK_STAT **spp, void *(*db_malloc)(size_t));
```

## DESCRIPTION

The DB library is a family of groups of functions that provides a modular programming interface to transactions and record-oriented file access. The library includes support for transactions, locking, logging and file page caching, as well as various indexed access methods. Many of the functional groups (e.g., the file page caching functions) are useful independent of the other DB functions, although some functional groups are explicitly based on other functional groups (e.g., transactions and logging). For a general description of the DB package, see *db\_intro*(3).

This manual page describes the specific details of the locking interface.

The *db\_lock* functions are the library interface intended to provide general-purpose locking. While designed to work with the other DB functions, these functions are also useful for more general locking purposes. Locks can be shared between processes. In most cases, when multiple threads or processes are using locking, the deadlock detector, [*db\_deadlock*(1)](/db_deadlock/1.md), should be run.

*lock\_open* The *lock\_open* function copies a pointer, to the lock table identified by the **directory** *dir*, into the memory location referenced by *regionp*.

If the *dbenv* argument to *lock\_open* was initialized using *db\_appinit*, *dir* is interpreted as described by *db\_appinit*(3).

Otherwise, if *dir* is not NULL, it is interpreted relative to the current working directory of the process. If *dir* is NULL, the following environment variables are checked in order: \`\`TMPDIR'', \`\`TEMP'', and \`\`TMP''. If one of them is set, lock table files are created relative to the directory it specifies. If none of them are set, the first possible one of the following directories is used: */var/tmp*, */usr/tmp*, */temp*, */tmp*, *C:/temp* and *C:/tmp*.

All files associated with the lock table are created in this directory. This directory must already exist when lock table\_open is called. If the lock table already exists, the process must have permission to read and write the existing files. If the lock table does not already exist, it is optionally created and initialized.

The *flags* and *mode* arguments specify how files will be opened and/or created when they don't already exist. The flags value is specified by **or**'ing together one or more of the following values:

- DB\_CREATE

  Create any underlying files, as necessary. If the files do not already exist and the DB\_CREATE flag is not specified, the call will fail.

-

  - DB\_THREAD

  Cause the DB\_LOCKTAB handle returned by the *lock\_open* function to be useable by multiple threads within a single address space, i.e., to be \`\`free-threaded''.

All files created by the lock subsystem are created with mode *mode* (as described in [*chmod*(2)](/chmod/2.md)) and modified by the process' umask value at the time of creation (see [*umask*(2)](/umask/2.md)). The group ownership of created files is based on the system and directory defaults, and is not further specified by DB.

The locking subsystem is configured based on the *dbenv* argument to *lock\_open*, which is a pointer to a structure of type DB\_ENV (typedef'd in \<db2/db.h\>). Applications will normally use the same DB\_ENV structure (initialized by *db\_appinit*(3)), as an argument to all of the subsystems in the DB package.

References to the DB\_ENV structure are maintained by DB, so it may not be discarded until the last close function, corresponding to an open function for which it was an argument, has returned. In order to ensure compatibility with future releases of DB, all fields of the DB\_ENV structure that are not explicitly set should be initialized to 0 before the first time the structure is used. Do this by declaring the structure external or static, or by calling the C library routine [*bzero*(3)](/bzero/3.md) or [*memset*(3)](/memset/3.md).

The fields of the DB\_ENV structure used by *lock\_open* are described below. If *dbenv* is NULL or any of its fields are set to 0, defaults appropriate for the system are used where possible.

The following fields in the DB\_ENV structure may be initialized before calling *lock\_open*:

- void \*(\*db\_errcall)(char \*db\_errpfx, char \*buffer);

  - FILE \*db\_errfile;

  - const char \*db\_errpfx;

  - int db\_verbose;

  The error fields of the DB\_ENV behave as described for *db\_appinit*(3).

- const u\_int8\_t lk\_conflicts\[\]\[\];

  A *lk\_modes* by *lk\_modes* array. A non-0 value for the array element:

  lk\_conflicts\[requested\_mode\]\[held\_mode\]

  indicates that requested\_mode and held\_mode conflict. The \`\`not-granted'' mode must be represented by 0. If *lk\_conflicts* is NULL, the conflicts array *db\_rw\_conflicts* is used; see the section below entitled \`\`STANDARD LOCK MODES'' for a description of that array.

- db\_detect\_t lk\_detect;

  If non-0, specifies that the deadlock detector be run whenever a lock conflict occurs, and specifies which transaction should be aborted in the case of a deadlock. The *lk\_detect* field must be set to one of the following values.

  - DB\_LOCK\_DEFAULT

    Use the default policy as specified in the [*db\_deadlock*(1)](/db_deadlock/1.md) man page.

  - DB\_LOCK\_OLDEST

    Abort the oldest transaction.

  - DB\_LOCK\_RANDOM

    Abort a random transaction involved in the deadlock.

  - DB\_LOCK\_YOUNGEST

    Abort the youngest transaction.

- u\_int32\_t lk\_max;

  The maximum number of locks to be held or requested in the table. This value is used by *lock\_open* to estimate how much space to allocate for various lock-table data structures. If *lk\_max* is 0, a default value is used.

- u\_int32\_t lk\_modes;

  The number of lock modes to be recognized by the lock table (including the \`\`not-granted'' mode). If *lk\_modes* is 0, the value DB\_LOCK\_RW\_N is used; see below for a description of that value.

The *lock\_open* function returns the value of *errno* on failure and 0 on success.

*lock\_id* The *lock\_id* function copies a locker ID, which is guaranteed to be unique in the specified lock table, into the memory location referenced by *idp*.

The access methods (see *db\_open*(3)), generate a unique locker ID for each file that is opened with locking. During DB access method operation, this locker ID will be used for all lock calls unless a transaction identifier was specified for the call, in which case the transaction ID specified is used for locking.

The *lock\_id* function returns the value of *errno* on failure and 0 on success.

*lock\_vec* The *lock\_vec* function atomically obtains and releases one or more locks from the specified table, *lt*. The *lock\_vec* function is intended to support acquisition or trading of multiple locks under one lock table semaphore, as is needed for lock coupling or in multigranularity locking for lock escalation.

The *locker* argument specified to *lock\_vec* is an unsigned 32-bit integer quantity. It represents the entity requesting or releasing the lock.

The *flags* value must be set to 0 or the following value:

- DB\_LOCK\_NOWAIT

  If a lock cannot be granted because the requested lock conflicts with an existing lock, return immediately instead of waiting for the lock to become available.

The *list* array provided to *lock\_vec* is typedef'd in \<db2/db.h\> as DB\_LOCKREQ. A DB\_LOCKREQ structure has at least the following fields, which must be initialized before calling *lock\_vec*:

- lockop\_t op;

  The operation to be performed, which must be set to one of the following values:

  - DB\_LOCK\_GET

    Get a lock, as defined by the values of *locker*, *obj* and *mode*. Upon return from *lock\_vec*, if the *lock* field is non-NULL, a reference to the acquired lock is stored there. (This reference is invalidated by any call to *lock\_vec* or *lock\_put* that releases the lock.)

  - DB\_LOCK\_PUT

    The lock referenced by the contents of the *lock* field is released.

  - DB\_LOCK\_PUT\_ALL

    All locks held by the *locker* are released. (Any locks acquired as a part of the current call to *lock\_vec* that appear after the DB\_LOCK\_PUT\_ALL entry are not considered for this operation).

  - DB\_LOCK\_PUT\_OBJ

    All locks held by the *locker*, on the object *obj*, with the mode specified by *lock\_mode*, are released. A *lock\_mode* of DB\_LOCK\_NG indicates that all locks on the object should be released. Note that any locks acquired as a part of the current call to *lock\_vec* that occur before the DB\_LOCK\_PUT\_OBJ will also be released; those acquired afterwards will not be released.

- const DBT obj;

  An untyped byte string that specifies the object to be locked or released.

- const lockmode\_t mode;

  The lock mode, used as an index into *lt*'s conflict array.

- DB\_LOCK lock;

  A lock reference.

The *nlist* argument specifies the number of elements in the *list* array.

If any of the requested locks cannot be acquired, or any of the locks to be released cannot be released, the operations before the failing operation are guaranteed to have completed successfully, and *lock\_vec* returns a non-zero value. In addition, if *elistp* is not NULL, it is set to point to the DB\_LOCKREQ entry that was being processed when the error occurred.

In the case of an error, *lock\_vec* may return one of the following values:

- DB\_LOCK\_DEADLOCK

  The specified *locker* was selected as a victim in order to resolve a deadlock.

- DB\_LOCK\_NOTHELD

  The lock cannot be released, as it was not held by the *locker*.

- DB\_LOCK\_NOTGRANTED

  A lock was requested that could not be granted and the *flag* parameter was set to DB\_LOCK\_NOWAIT. In this case, if non-NULL, *elistp* identifies the request that was granted.

Otherwise, the *lock\_vec* function returns the value of *errno* on failure and 0 on success.

*lock\_get* The *lock\_get* function is a simple interface to the *lock\_vec* functionality, and is equivalent to calling the *lock\_vec* function with the *lt* and *locker* arguments, *elistp* and *conflict* arguments, and a single element *list* array, for which the *op* field is DB\_LOCK\_GET, and the *obj*, *lock\_mode* and *lock* fields are represented by the arguments of the same name. Note that the type of the *obj* argument to *lock\_get* is different from the *obj* element found in the DB\_LOCKREQ structure. The *lock\_get* function returns success and failure as described for the *lock\_vec* function.

*lock\_put* The *lock\_put* function is a simple interface to the *lock\_vec* functionality, and is equivalent to calling the *lock\_vec* function with a single element *list* array, for which the *op* field is DB\_LOCK\_PUT and the *lock* field is represented by the argument of the same name. The *lock\_put* function returns success and failure as described for the *lock\_vec* function.

*lock\_close* The *lock\_close* function disassociates the calling process from the lock table *lt*. Note that *lock\_close* does not release any locks still held by the closing process. (This provides functionality for long-lived locks.) Processes that wish to have all their locks released can do so by issuing the appropriate *lock\_vec* call.

In addition, if the *dir* argument to *lock\_open* was NULL and *dbenv* was not initialized using *db\_appinit*, all files created for this shared region will be removed, as if *lock\_unlink* were called.

When multiple threads are using the DB\_LOCKTAB handle concurrently, only a single thread may call the *lock\_close* function.

The *lock\_close* function returns the value of *errno* on failure and 0 on success.

*lock\_unlink* The *lock\_unlink* function destroys the lock table identified by the directory *dir*, removing all files used to implement the lock table. (The directory *dir* is not removed.) If there are processes that have called *lock\_open* without calling *lock\_close* (i.e., there are processes currently using the lock table), *lock\_unlink* will fail without further action, unless the force flag is set, in which case *lock\_unlink* will attempt to remove the lock table files regardless of any processes still using the lock table.

The result of attempting to forcibly destroy the region when a process has the region open is unspecified. Processes using a shared memory region maintain an open file descriptor for it. On UNIX systems, the region removal should succeed and processes that have already joined the region should continue to run in the region without change, however processes attempting to join the lock table will either fail or attempt to create a new region. On other systems, e.g., WNT, where the [*unlink*(2)](/unlink/2.md) system call will fail if any process has an open file descriptor for the file, the region removal will fail.

In the case of catastrophic or system failure, database recovery must be performed (see [*db\_recover*(1)](/db_recover/1.md) or the DB\_RECOVER and DB\_RECOVER\_FATAL flags to *db\_appinit*(3)). Alternatively, if recovery is not required because no database state is maintained across failures, it is possible to clean up a lock table by removing all of the files in the directory specified to the *lock\_open* function, as lock table files are never created in any directory other than the one specified to *lock\_open*. Note, however, that this has the potential to remove files created by the other DB subsystems in this database environment.

The *lock\_unlink* function returns the value of *errno* on failure and 0 on success.

*lock\_detect* The *lock\_detect* function runs one iteration of the deadlock detector on the specified table, *lt*. The deadlock detector traverses the lock table, detects deadlocks, and if it finds one, marks one of the participating transactions for abort and then returns.

The flags value is specified by **or**'ing together one or more of the following values:

- DB\_LOCK\_CONFLICT

  Only run the deadlock detector if a lock conflict has occurred since the last time that the deadlock detector was run.

The *atype* parameter specifies which transaction to abort in the case of deadlock. It must be set to one of values described above for the *lk\_detect* field of the *DB\_ENV* structure.

The *lock\_detect* function returns the value of *errno* on failure and 0 on success.

The *lock\_detect* function is the underlying function used by the [*db\_deadlock*(1)](/db_deadlock/1.md) utility. See the source code for the *db\_deadlock* utility for an example of using *lock\_detect* in a UNIX environment.

*lock\_stat* The *lock\_stat* function creates a statistical structure and copies a pointer to it into the user-specified memory location.

Statistical structure are created in allocated memory. If *db\_malloc* is non-NULL, it is called to allocate the memory, otherwise, the library function [*malloc*(3)](/malloc/3.md) is used. The function *db\_malloc* must match the calling conventions of the [*malloc*(3)](/malloc/3.md) library routine. Regardless, the caller is responsible for deallocating the returned memory. To deallocate the returned memory, free each returned memory pointer; pointers inside the memory do not need to be individually freed.

The lock region statistics are stored in a structure of type DB\_LOCK\_STAT (typedef'd in \<db2/db.h\>). The following DB\_LOCK\_STAT fields will be filled in:

-

  - u\_int32\_t st\_magic;

  The magic number that identifies a file as a lock file.\

- u\_int32\_t st\_version;

  The version of the lock file type.\

- u\_int32\_t st\_refcnt;

  The number of references to the region.\

- u\_int32\_t st\_regsize;

  The size of the region.\

- u\_int32\_t st\_maxlocks;

  The maximum number of locks possible.\

- u\_int32\_t st\_nmodes;

  The number of lock modes.\

- u\_int32\_t st\_numobjs;

  The number of unique objects locked.\

- u\_int32\_t st\_nlockers;

  The number of unique lockers.\

- u\_int32\_t st\_nconflicts;

  The total number of locks not immediately available due to conflicts.\

- u\_int32\_t st\_nrequests;

  The total number of locks requested.\

- u\_int32\_t st\_nreleases;

  The total number of locks released.\

- u\_int32\_t st\_ndeadlocks

  The number of deadlocks detected.\

- u\_int32\_t st\_region\_wait;

  The number of times that a thread of control was forced to wait before obtaining the region lock.\

- u\_int32\_t st\_region\_nowait;

  The number of times that a thread of control was able to obtain the region lock without waiting.

## ENVIRONMENT VARIABLES

The following environment variables affect the execution of *db\_lock*:

- DB\_HOME

  If the *dbenv* argument to *lock\_open* was initialized using *db\_appinit*, the environment variable DB\_HOME may be used as the path of the database home for the interpretation of the *dir* argument to *lock\_open*, as described in *db\_appinit*(3).

- TMPDIR

  If the *dbenv* argument to *lock\_open* was NULL or not initialized using *db\_appinit*, the environment variable TMPDIR may be used as the directory in which to create the lock table, as described in the *lock\_open* section above.

## STANDARD LOCK MODES

The include file \<db2/db.h\> declares two commonly used conflict arrays:

- const u\_int8\_t db\_lock\_rw\_conflicts\[\];

  This is a conflict array for a simple scheme using shared and exclusive lock modes.

- const u\_int8\_t db\_lock\_riw\_conflicts\[\];

  This is a conflict array that involves various intent lock modes (e.g., intent shared) that are used for multigranularity locking.

Their associated sizes are DB\_LOCK\_RW\_N and DB\_LOCK\_RIW\_N.

In addition, the include file \<db2/db.h\> 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)

## ERRORS

The *lock\_open* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [close(2)](/close/2.md),  db\_version(3),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_unlink(3),  [lseek(2)](/lseek/2.md),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [open(2)](/open/2.md),  [sigfillset(3)](/sigfillset/3.md),  [sigprocmask(2)](/sigprocmask/2.md),  [stat(2)](/stat/2.md),  [strcpy(3)](/strcpy/3.md),  [strdup(3)](/strdup/3.md),  [strerror(3)](/strerror/3.md),  [strlen(3)](/strlen/3.md),  [unlink(2)](/unlink/2.md),  and [write(2)](/write/2.md).

In addition, the *lock\_open* function may fail and return *errno* for the following conditions:

- \[EAGAIN\]

  The shared memory region was locked and (repeatedly) unavailable.

- \[EINVAL\]

  An invalid flag value or parameter was specified.

  The DB\_THREAD flag was specified and spinlocks are not implemented for this architecture.

The *lock\_vec* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_detect(3),  [lseek(2)](/lseek/2.md),  [memcpy(3)](/memcpy/3.md),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [strerror(3)](/strerror/3.md),  and [write(2)](/write/2.md).

In addition, the *lock\_vec* function may fail and return *errno* for the following conditions:

- \[EACCES\]

  An attempt was made to release lock held by another locker.

- \[EINVAL\]

  An invalid flag value or parameter was specified.

The *lock\_get* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_detect(3),  [lseek(2)](/lseek/2.md),  [memcpy(3)](/memcpy/3.md),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [strerror(3)](/strerror/3.md),  and [write(2)](/write/2.md).

In addition, the *lock\_get* function may fail and return *errno* for the following conditions:

- \[EINVAL\]

  An invalid flag value or parameter was specified.

The *lock\_put* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_detect(3),  [lseek(2)](/lseek/2.md),  [memcpy(3)](/memcpy/3.md),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [strerror(3)](/strerror/3.md),  and [write(2)](/write/2.md).

In addition, the *lock\_put* function may fail and return *errno* for the following conditions:

- \[EACCES\]

  An attempt was made to release lock held by another locker.

- \[EINVAL\]

  An invalid flag value or parameter was specified.

The *lock\_close* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [close(2)](/close/2.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  [munmap(2)](/munmap/2.md),  and [strerror(3)](/strerror/3.md).

The *lock\_unlink* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [close(2)](/close/2.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [open(2)](/open/2.md),  [sigfillset(3)](/sigfillset/3.md),  [sigprocmask(2)](/sigprocmask/2.md),  [stat(2)](/stat/2.md),  [strcpy(3)](/strcpy/3.md),  [strdup(3)](/strdup/3.md),  [strerror(3)](/strerror/3.md),  [strlen(3)](/strlen/3.md),  and [unlink(2)](/unlink/2.md).

In addition, the *lock\_unlink* function may fail and return *errno* for the following conditions:

- \[EBUSY\]

  The shared memory region was in use and the force flag was not set.

The *lock\_detect* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [calloc(3)](/calloc/3.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  [lseek(2)](/lseek/2.md),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [strerror(3)](/strerror/3.md),  and [write(2)](/write/2.md).

## BUGS

If a process dies while holding locks, those locks remain held and are **never** released. In this case, all processes should exit as quickly as possible, so that *db\_recover* can be run.

## SEE ALSO

[*db\_archive*(1)](/db_archive/1.md), [*db\_checkpoint*(1)](/db_checkpoint/1.md), [*db\_deadlock*(1)](/db_deadlock/1.md), [*db\_dump*(1)](/db_dump/1.md), [*db\_load*(1)](/db_load/1.md), [*db\_recover*(1)](/db_recover/1.md), [*db\_stat*(1)](/db_stat/1.md), *db\_intro*(3), *db\_appinit*(3), *db\_cursor*(3), *db\_dbm*(3), *db\_internal*(3), *db\_lock*(3), *db\_log*(3), *db\_mpool*(3), *db\_open*(3), *db\_thread*(3), *db\_txn*(3)
