Update.
[glibc.git] / db2 / include / lock.h
blob47a38b8783cca11e794a15458527c3d720343ff0
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
7 * @(#)lock.h 10.15 (Sleepycat) 5/10/98
8 */
10 typedef struct __db_lockobj DB_LOCKOBJ;
12 #define DB_DEFAULT_LOCK_FILE "__db_lock.share"
14 #ifndef DB_LOCK_DEFAULT_N
15 #define DB_LOCK_DEFAULT_N 5000 /* Default # of locks in region. */
16 #endif
19 * The locker id space is divided between the transaction manager and the lock
20 * manager. Lockid's start at 0 and go to DB_LOCK_MAXID. Txn Id's start at
21 * DB_LOCK_MAXID + 1 and go up to TXN_INVALID.
23 #define DB_LOCK_MAXID 0x7fffffff
26 * The lock region consists of:
27 * The DB_LOCKREGION structure (sizeof(DB_LOCKREGION)).
28 * The conflict matrix of nmodes * nmodes bytes (nmodes * nmodes).
29 * The hash table for object lookup (hashsize * sizeof(DB_OBJ *)).
30 * The locks themselves (maxlocks * sizeof(struct __db_lock).
31 * The objects being locked (maxlocks * sizeof(DB_OBJ)).
32 * String space to represent the DBTs that are the objects being locked.
34 struct __db_lockregion {
35 RLAYOUT hdr; /* Shared region header. */
36 u_int32_t magic; /* lock magic number */
37 u_int32_t version; /* version number */
38 u_int32_t id; /* unique id generator */
39 u_int32_t need_dd; /* flag for deadlock detector */
40 u_int32_t detect; /* run dd on every conflict */
41 SH_TAILQ_HEAD(lock_header) free_locks; /* free lock header */
42 SH_TAILQ_HEAD(obj_header) free_objs; /* free obj header */
43 u_int32_t maxlocks; /* maximum number of locks in table */
44 u_int32_t table_size; /* size of hash table */
45 u_int32_t nmodes; /* number of lock modes */
46 u_int32_t numobjs; /* number of objects */
47 u_int32_t nlockers; /* number of lockers */
48 size_t increment; /* how much to grow region */
49 size_t hash_off; /* offset of hash table */
50 size_t mem_off; /* offset of memory region */
51 size_t mem_bytes; /* number of bytes in memory region */
52 u_int32_t nconflicts; /* number of lock conflicts */
53 u_int32_t nrequests; /* number of lock gets */
54 u_int32_t nreleases; /* number of lock puts */
55 u_int32_t ndeadlocks; /* number of deadlocks */
58 /* Macros to lock/unlock the region. */
59 #define LOCK_LOCKREGION(lt) \
60 (void)__db_mutex_lock(&(lt)->region->hdr.lock, (lt)->reginfo.fd)
61 #define UNLOCK_LOCKREGION(lt) \
62 (void)__db_mutex_unlock(&(lt)->region->hdr.lock, (lt)->reginfo.fd)
65 * Since we will be keeping DBTs in shared memory, we need the equivalent
66 * of a DBT that will work in shared memory.
68 typedef struct __sh_dbt {
69 u_int32_t size;
70 ssize_t off;
71 } SH_DBT;
73 #define SH_DBT_PTR(p) ((void *)(((u_int8_t *)(p)) + (p)->off))
75 struct __db_lockobj {
76 SH_DBT lockobj; /* Identifies object locked. */
77 SH_TAILQ_ENTRY links; /* Links for free list. */
78 union {
79 SH_TAILQ_HEAD(_wait) _waiters; /* List of waiting locks. */
80 u_int32_t _dd_id; /* Deadlock detector id. */
81 } wlinks;
82 union {
83 SH_LIST_HEAD(_held) _heldby; /* Locks held by this locker. */
84 SH_TAILQ_HEAD(_hold) _holders; /* List of held locks. */
85 } dlinks;
86 #define DB_LOCK_OBJTYPE 1
87 #define DB_LOCK_LOCKER 2
88 /* Allocate room in the object to
89 * hold typical DB lock structures
90 * so that we do not have to
91 * allocate them from shalloc. */
92 u_int8_t objdata[sizeof(struct __db_ilock)];
93 u_int8_t type; /* Real object or locker id. */
96 #define dd_id wlinks._dd_id
97 #define waiters wlinks._waiters
98 #define holders dlinks._holders
99 #define heldby dlinks._heldby
102 * The lock table is the per-process cookie returned from a lock_open call.
104 struct __db_locktab {
105 DB_ENV *dbenv; /* Environment. */
106 REGINFO reginfo; /* Region information. */
107 DB_LOCKREGION *region; /* Address of shared memory region. */
108 DB_HASHTAB *hashtab; /* Beginning of hash table. */
109 void *mem; /* Beginning of string space. */
110 u_int8_t *conflicts; /* Pointer to conflict matrix. */
113 /* Test for conflicts. */
114 #define CONFLICTS(T, HELD, WANTED) \
115 T->conflicts[HELD * T->region->nmodes + WANTED]
118 * Resources in the lock region. Used to indicate which resource
119 * is running low when we need to grow the region.
121 typedef enum {
122 DB_LOCK_MEM, DB_LOCK_OBJ, DB_LOCK_LOCK
123 } db_resource_t;
125 struct __db_lock {
127 * Wait on mutex to wait on lock. You reference your own mutex with
128 * ID 0 and others reference your mutex with ID 1.
130 db_mutex_t mutex;
132 u_int32_t holder; /* Who holds this lock. */
133 SH_TAILQ_ENTRY links; /* Free or holder/waiter list. */
134 SH_LIST_ENTRY locker_links; /* List of locks held by a locker. */
135 u_int32_t refcount; /* Reference count the lock. */
136 db_lockmode_t mode; /* What sort of lock. */
137 ssize_t obj; /* Relative offset of object struct. */
138 db_status_t status; /* Status of this lock. */
142 * We cannot return pointers to the user (else we cannot easily grow regions),
143 * so we return offsets in the region. These must be converted to and from
144 * regular pointers. Always use the macros below.
146 #define OFFSET_TO_LOCK(lt, off) \
147 ((struct __db_lock *)((u_int8_t *)((lt)->region) + (off)))
148 #define LOCK_TO_OFFSET(lt, lock) \
149 ((size_t)((u_int8_t *)(lock) - (u_int8_t *)lt->region))
150 #define OFFSET_TO_OBJ(lt, off) \
151 ((DB_LOCKOBJ *)((u_int8_t *)((lt)->region) + (off)))
152 #define OBJ_TO_OFFSET(lt, obj) \
153 ((size_t)((u_int8_t *)(obj) - (u_int8_t *)lt->region))
156 * The lock header contains the region structure and the conflict matrix.
157 * Aligned to a large boundary because we don't know what the underlying
158 * type of the hash table elements are.
160 #define LOCK_HASH_ALIGN 8
161 #define LOCK_HEADER_SIZE(M) \
162 ((size_t)(sizeof(DB_LOCKREGION) + ALIGN((M * M), LOCK_HASH_ALIGN)))
165 * For the full region, we need to add the locks, the objects, the hash table
166 * and the string space (which is 16 bytes per lock).
168 #define STRING_SIZE(N) (16 * N)
170 #define LOCK_REGION_SIZE(M, N, H) \
171 (ALIGN(LOCK_HEADER_SIZE(M) + \
172 (H) * sizeof(DB_HASHTAB), MUTEX_ALIGNMENT) + \
173 (N) * ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT) + \
174 ALIGN((N) * sizeof(DB_LOCKOBJ), sizeof(size_t)) + \
175 ALIGN(STRING_SIZE(N), sizeof(size_t)))
177 #include "lock_ext.h"