NNAAMMEE DbLockTab - lock manager SSYYNNOOPPSSIISS ##iinncclluuddee <> ssttaattiicc iinntt DDbbLLoocckkTTaabb::::ooppeenn((ccoonnsstt cchhaarr **ddiirr,, uu__iinntt3322__tt ffllaaggss,, iinntt mmooddee,, DDbbEEnnvv **ddbbeennvv,, DDbbLLoocckkTTaabb ****rreeggiioonnpp));; iinntt DDbbLLoocckkTTaabb::::iidd((uu__iinntt3322__tt **iiddpp));; iinntt DDbbLLoocckkTTaabb::::vveecc((uu__iinntt3322__tt lloocckkeerr,, uu__iinntt3322__tt ffllaaggss,, DDBB__LLOOCCKKRREEQQ lliisstt[[]],, iinntt nnlliisstt,, DDBB__LLOOCCKKRREEQQ ****eelliissttpp));; iinntt DDbbLLoocckkTTaabb::::ggeett((uu__iinntt3322__tt lloocckkeerr,, uu__iinntt3322__tt ffllaaggss,, ccoonnsstt DDbbtt **oobbjj,, ccoonnsstt ddbb__lloocckkmmooddee__tt lloocckk__mmooddee,, DDBB__LLOOCCKK **lloocckk));; iinntt DDbbLLoocckkTTaabb::::cclloossee(());; ssttaattiicc iinntt DDbbLLoocckkTTaabb::::uunnlliinnkk((ccoonnsstt cchhaarr **ddiirr,, iinntt ffoorrccee,, DDbbEEnnvv **ddbbeennvv));; iinntt DDbbLLoocckkTTaabb::::ddeetteecctt((uu__iinntt3322__tt ffllaaggss,, uu__iinntt3322__tt aattyyppee));; DDEESSCCRRIIPPTTIIOONN The DB library is a family of classes that provides a mod- ular programming interface to transactions and record-ori- ented file access. The library includes support for transactions, locking, logging and file page caching, as well as various indexed access methods. Many of the classes (e.g., the file page caching class) are useful independent of the other DB classes, although some classes are explicitly based on other classes (e.g., transactions and logging). For a general description of the DB pack- age, see _d_b___i_n_t_r_o(3). This manual page describes the specific details of the locking interface. The _D_b_L_o_c_k_T_a_b class is intended to provide general-purpose locking. While designed to work with the other Db classes, this class is also useful for more general lock- ing purposes. Locks can be shared between processes. In most cases, when multiple treads or processes are using locking, the deadlock detector, _d_b___d_e_a_d_l_o_c_k(1), should be run. _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n The _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n method copies a pointer, to the lock table identified by the ddiirreeccttoorryy _d_i_r, into the memory location referenced by _r_e_g_i_o_n_p. If the _d_b_e_n_v argument to _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n was initialized using _D_b_E_n_v_:_:_a_p_p_i_n_i_t, _d_i_r is interpreted as described by _D_b_E_n_v(3). Otherwise, if _d_i_r is not NULL, it is interpreted relative to the current working directory of the process. If _d_i_r 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: _/_v_a_r_/_t_m_p, _/_u_s_r_/_t_m_p, _/_t_e_m_p, _/_t_m_p, _C_:_/_t_e_m_p and _C_:_/_t_m_p. All files associated with the lock table are created in this directory. This directory must already exist when DbLockTab::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 _f_l_a_g_s and _m_o_d_e arguments specify how files will be opened and/or created when they don't already exist. The flags value is specified by oorr'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 DbLockTab handle returned by the _D_b_L_o_c_k_- _T_a_b_:_:_o_p_e_n method 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 _m_o_d_e (as described in _c_h_m_o_d(2)) and modified by the process' umask value at the time of creation (see _u_m_a_s_k(2)). 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 which set methods have been used. It is expected that applications will use a single DbEnv object as the argument to all of the subsystems in the DB package. The fields of the DbEnv object used by _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n are described below. As references to the DbEnv object may be maintained by _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n, it is necessary that the DbEnv object and memory it references be valid until the object is destroyed. Any of the DbEnv fields that are not explic- itly set will default to appropriate values. The following fields in the DbEnv object may be initial- ized, using the appropriate set method, before calling _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n: void *(*db_errcall)(char *db_errpfx, char *buffer); FILE *db_errfile; const char *db_errpfx; class ostream *db_error_stream; int db_verbose; The error fields of the DbEnv behave as described for _D_b_E_n_v(3). const u_int8_t lk_conflicts[][]; A _l_k___m_o_d_e_s by _l_k___m_o_d_e_s 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 _l_k___c_o_n_f_l_i_c_t_s is NULL, the conflicts array _d_b___r_w___c_o_n_f_l_i_c_t_s is used; see the section below enti- tled ``STANDARD LOCK MODES'' for a description of that array. u_int32_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 dead- lock. The _l_k___d_e_t_e_c_t field must be set to one of the following values. DB_LOCK_DEFAULT Use the default policy as specified in the _d_b___d_e_a_d_l_o_c_k(1) man page. DB_LOCK_OLDEST Abort the oldest transaction. DB_LOCK_RANDOM Abort a random transaction involved in the dead- lock. 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 _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n to estimate how much space to allocate for various lock-table data structures. If _l_k___m_a_x is not explic- itly set, 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 _l_k___m_o_d_e_s is 0, the value DB_LOCK_RW_N is used; see below for a descrip- tion of that value. The _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n method throws a _D_b_E_x_c_e_p_t_i_o_n(3) or returns the value of _e_r_r_n_o on failure and 0 on success. _D_b_L_o_c_k_T_a_b_:_:_i_d The _D_b_L_o_c_k_T_a_b_:_:_i_d method copies a locker ID, which is guaranteed to be unique in the specified lock table, into the memory location referenced by _i_d_p. The access methods (see _D_b_:_:_o_p_e_n in _D_b(3)), generate a unique locker ID for each file that is opened with lock- ing. During Db access method operation, this locker ID will be used for all lock calls unless a transaction iden- tifier was specified for the call, in which case the transaction ID specified is used for locking. The _D_b_L_o_c_k_T_a_b_:_:_i_d method throws a _D_b_E_x_c_e_p_t_i_o_n(3) or returns the value of _e_r_r_n_o on failure and 0 on success. _D_b_L_o_c_k_T_a_b_:_:_v_e_c The _D_b_L_o_c_k_T_a_b_:_:_v_e_c method atomically obtains and releases one or more locks from the specified table. The _D_b_L_o_c_k_- _T_a_b_:_:_v_e_c method 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 _l_o_c_k_e_r argument specified to _D_b_L_o_c_k_T_a_b_:_:_v_e_c is an unsigned 32-bit integer quantity. It represents the entity requesting or releasing the lock. The _f_l_a_g_s 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 immedi- ately instead of waiting for the lock to become available. The _l_i_s_t array provided to _D_b_L_o_c_k_T_a_b_:_:_v_e_c is typedef'd in as DB_LOCKREQ. A DB_LOCKREQ structure has at least the following fields, which must be initialized before calling _D_b_L_o_c_k_T_a_b_:_:_v_e_c: 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 _l_o_c_k_e_r, _o_b_j and _m_o_d_e. Upon return from _D_b_L_o_c_k_T_a_b_:_:_v_e_c, if the _l_o_c_k field is non-NULL, a reference to the acquired lock is stored there. (This refer- ence is invalidated by any call to _D_b_L_o_c_k_- _T_a_b_:_:_v_e_c or _D_b_L_o_c_k_:_:_p_u_t that releases the lock.) See _D_b_L_o_c_k(3). DB_LOCK_PUT The lock referenced by the contents of the _l_o_c_k field is released. DB_LOCK_PUT_ALL All locks held by the _l_o_c_k_e_r are released. (Any locks acquired as a part of the current call to _D_b_L_o_c_k_T_a_b_:_:_v_e_c that appear after the DB_LOCK_PUT_ALL entry are not considered for this operation). DB_LOCK_PUT_OBJ All locks held by the _l_o_c_k_e_r, on the object _o_b_j, with the mode specified by _l_o_c_k___m_o_d_e, are released. A _l_o_c_k___m_o_d_e 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 _D_b_L_o_c_k_T_a_b_:_:_v_e_c 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 object's con- flict array. DB_LOCK lock; A lock reference. The _n_l_i_s_t argument specifies the number of elements in the _l_i_s_t array. If any of the requested locks cannot be acquired, or any of the locks to be released cannot be released, the opera- tions before the failing operation are guaranteed to have completed successfully, and _D_b_L_o_c_k_T_a_b_:_:_v_e_c returns a non- zero value. In addition, if _e_l_i_s_t_p 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, _D_b_L_o_c_k_T_a_b_:_:_v_e_c may return one of the following values: DB_LOCK_DEADLOCK The specified _l_o_c_k_e_r 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 _l_o_c_k_e_r. DB_LOCK_NOTGRANTED A lock was requested that could not be granted and the _f_l_a_g parameter was set to DB_LOCK_NOWAIT. In this case, if non-NULL, _e_l_i_s_t_p identifies the request that was granted. Otherwise, the _D_b_L_o_c_k_T_a_b_:_:_v_e_c method throws a _D_b_E_x_c_e_p_- _t_i_o_n(3) or returns the value of _e_r_r_n_o on failure and 0 on success. _D_b_L_o_c_k_T_a_b_:_:_g_e_t The _D_b_L_o_c_k_T_a_b_:_:_g_e_t method is a simple interface to the _D_b_L_o_c_k_T_a_b_:_:_v_e_c functionality, and is equivalent to calling the _D_b_L_o_c_k_T_a_b_:_:_v_e_c method with the _l_o_c_k_e_r argument, _e_l_i_s_t_p and _c_o_n_f_l_i_c_t arguments, and a single element _l_i_s_t array, for which the _o_p field is DB_LOCK_GET, and the _o_b_j, _l_o_c_k___m_o_d_e and _l_o_c_k fields are represented by the arguments of the same name. Note that the type of the _o_b_j argument to _D_b_L_o_c_k_T_a_b_:_:_g_e_t is different from the _o_b_j element found in the DB_LOCKREQ structure. The _D_b_L_o_c_k_T_a_b_:_:_g_e_t method returns success and failure as described for the _D_b_L_o_c_k_T_a_b_:_:_v_e_c method. _D_b_L_o_c_k_T_a_b_:_:_c_l_o_s_e The _D_b_L_o_c_k_T_a_b_:_:_c_l_o_s_e method disassociates the calling pro- cess from the lock table. The object should not be used after a call to close. Note that _D_b_L_o_c_k_T_a_b_:_:_c_l_o_s_e 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 _D_b_L_o_c_k_T_a_b_:_:_v_e_c call. In addition, if the _d_i_r argument to _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n was NULL and _d_b_e_n_v was not initialized using _D_b_E_n_v_:_:_a_p_p_i_n_i_t, all files created for this shared region will be removed, as if _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k were called. When multiple threads are using the DbLockTab object con- currently, only a single thread may call the _D_b_L_o_c_k_- _T_a_b_:_:_c_l_o_s_e method. The _D_b_L_o_c_k_T_a_b_:_:_c_l_o_s_e method throws a _D_b_E_x_c_e_p_t_i_o_n(3) or returns the value of _e_r_r_n_o on failure and 0 on success. _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k The _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k method destroys the lock table iden- tified by the directory _d_i_r, removing all files used to implement the lock table. (The directory _d_i_r is not removed.) If there are processes that have called _D_b_L_o_c_k_- _T_a_b_:_:_o_p_e_n without calling _D_b_L_o_c_k_T_a_b_:_:_c_l_o_s_e (i.e., there are processes currently using the lock table), _D_b_L_o_c_k_- _T_a_b_:_:_u_n_l_i_n_k will fail without further action, unless the force flag is set, in which case _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k 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. Pro- cesses 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 _u_n_l_i_n_k(2) 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 _d_b___r_e_c_o_v_e_r(1) or the DB_RECOVER and DB_RECOVER_FATAL flags to _D_b_E_n_v_:_:_a_p_p_i_n_i_t(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 _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n method, as lock table files are never created in any directory other than the one specified to _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n. Note, however, that this has the poten- tial to remove files created by the other DB subsystems in this database environment. The _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k method throws a _D_b_E_x_c_e_p_t_i_o_n(3) or returns the value of _e_r_r_n_o on failure and 0 on success. _D_b_L_o_c_k_T_a_b_:_:_d_e_t_e_c_t The _D_b_L_o_c_k_T_a_b_:_:_d_e_t_e_c_t method runs one iteration of the deadlock detector on the current lock table. The deadlock detector traverses the lock table, detects deadlocks, and if it finds one, marks one of the participating transac- tions for abort and then returns. The flags value is specified by oorr'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 detec- tor was run. The _a_t_y_p_e parameter specifies which transaction to abort in the case of deadlock. It must be set to one of values described above for the _l_k___d_e_t_e_c_t field of the _D_b_E_n_v object. The _D_b_L_o_c_k_T_a_b_:_:_d_e_t_e_c_t method throws a _D_b_E_x_c_e_p_t_i_o_n(3) or returns the value of _e_r_r_n_o on failure and 0 on success. The _D_b_L_o_c_k_T_a_b_:_:_d_e_t_e_c_t method is based on the C _l_o_c_k___d_e_t_e_c_t function, which is the underlying function used by the _d_b___d_e_a_d_l_o_c_k(1) utility. See the source code for the _d_b___d_e_a_d_l_o_c_k utility for an example of using _l_o_c_k___d_e_t_e_c_t in a UNIX environment. EENNVVIIRROONNMMEENNTT VVAARRIIAABBLLEESS The following environment variables affect the execution of _d_b___l_o_c_k: DB_HOME If the _d_b_e_n_v argument to _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n was initial- ized using _d_b___a_p_p_i_n_i_t, the environment variable DB_HOME may be used as the path of the database home for the interpretation of the _d_i_r argument to _D_b_L_o_c_k_- _T_a_b_:_:_o_p_e_n, as described in _d_b___a_p_p_i_n_i_t(3). TMPDIR If the _d_b_e_n_v argument to _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n was NULL or not initialized using _d_b___a_p_p_i_n_i_t, the environment variable TMPDIR may be used as the directory in which to create the lock table, as described in the _D_b_L_o_c_k_- _T_a_b_:_:_o_p_e_n section above. SSTTAANNDDAARRDD LLOOCCKK MMOODDEESS The include file 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 defines the type _d_b___l_o_c_k_m_o_d_e___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) EERRRROORRSS Methods marked as returning _e_r_r_n_o will, by default, throw an exception that encapsulates the error information. The default error behavior can be changed, see _D_b_E_x_c_e_p_t_i_o_n(3). The _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n method may fail and throw a _D_b_E_x_c_e_p_- _t_i_o_n(3) or return _e_r_r_n_o for any of the errors specified for the following DB and library functions: DbLock::unlink(3), close(2), db_version(3), fcntl(2), fflush(3), lseek(2), malloc(3), memcpy(3), memset(3), mmap(2), munmap(2), open(2), sigfillset(3), sigprocmask(2), stat(2), str- cpy(3), strdup(3), strerror(3), strlen(3), unlink(2), and write(2). In addition, the _D_b_L_o_c_k_T_a_b_:_:_o_p_e_n method may fail and throw a _D_b_E_x_c_e_p_t_i_o_n(3) or return _e_r_r_n_o for the following condi- tions: [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 _D_b_L_o_c_k_T_a_b_:_:_v_e_c method may fail and throw a _D_b_E_x_c_e_p_- _t_i_o_n(3) or return _e_r_r_n_o for any of the errors specified for the following DB and library functions: DbLock::detect(3), fcntl(2), fflush(3), lseek(2), memcpy(3), memset(3), mmap(2), munmap(2), strerror(3), and write(2). In addition, the _D_b_L_o_c_k_T_a_b_:_:_v_e_c method may fail and throw a _D_b_E_x_c_e_p_t_i_o_n(3) or return _e_r_r_n_o for the following condi- tions: [EACCES] An attempt was made to release lock held by another locker. [EINVAL] An invalid flag value or parameter was specified. The _D_b_L_o_c_k_T_a_b_:_:_g_e_t method may fail and throw a _D_b_E_x_c_e_p_- _t_i_o_n(3) or return _e_r_r_n_o for any of the errors specified for the following DB and library functions: DbLock::detect(3), fcntl(2), fflush(3), lseek(2), memcpy(3), memset(3), mmap(2), munmap(2), strerror(3), and write(2). In addition, the _D_b_L_o_c_k_T_a_b_:_:_g_e_t method may fail and throw a _D_b_E_x_c_e_p_t_i_o_n(3) or return _e_r_r_n_o for the following condi- tions: [EINVAL] An invalid flag value or parameter was specified. The _D_b_L_o_c_k_T_a_b_:_:_c_l_o_s_e method may fail and throw a _D_b_E_x_c_e_p_- _t_i_o_n(3) or return _e_r_r_n_o for any of the errors specified for the following DB and library functions: close(2), fcntl(2), fflush(3), munmap(2), and strerror(3). The _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k method may fail and throw a _D_b_E_x_c_e_p_- _t_i_o_n(3) or return _e_r_r_n_o for any of the errors specified for the following DB and library functions: close(2), fcntl(2), fflush(3), malloc(3), memcpy(3), memset(3), mmap(2), mun- map(2), open(2), sigfillset(3), sigprocmask(2), stat(2), strcpy(3), strdup(3), strerror(3), strlen(3), and unlink(2). In addition, the _D_b_L_o_c_k_T_a_b_:_:_u_n_l_i_n_k method may fail and throw a _D_b_E_x_c_e_p_t_i_o_n(3) or return _e_r_r_n_o for the following conditions: [EBUSY] The shared memory region was in use and the force flag was not set. The _D_b_L_o_c_k_T_a_b_:_:_d_e_t_e_c_t method may fail and throw a _D_b_E_x_c_e_p_- _t_i_o_n(3) or return _e_r_r_n_o for any of the errors specified for the following DB and library functions: calloc(3), fcntl(2), fflush(3), lseek(2), malloc(3), memcpy(3), memset(3), mmap(2), munmap(2), strerror(3), and write(2). BBUUGGSS If a process dies while holding locks, those locks remain held and are nneevveerr released. In this case, all processes should exit as quickly as possible, so that _d_b___r_e_c_o_v_e_r can be run. SSEEEE AALLSSOO _d_b___a_r_c_h_i_v_e(1), _d_b___c_h_e_c_k_p_o_i_n_t(1), _d_b___d_e_a_d_l_o_c_k(1), _d_b___d_u_m_p(1), _d_b___l_o_a_d(1), _d_b___r_e_c_o_v_e_r(1), _d_b___s_t_a_t(1), _d_b___i_n_t_r_o(3), _d_b___i_n_t_e_r_n_a_l(3), _d_b___t_h_r_e_a_d(3), _D_b(3), _D_b_c(3), _D_b_E_n_v(3), _D_b_E_x_c_e_p_t_i_o_n(3), _D_b_I_n_f_o(3), _D_b_L_o_c_k(3), _D_b_L_o_c_k_T_a_b(3), _D_b_L_o_g(3), _D_b_L_s_n(3), _D_b_M_p_o_o_l(3), _D_b_M_p_o_o_l_F_i_l_e(3), _D_b_t(3), _D_b_T_x_n(3), _D_b_T_x_n_M_g_r(3)