Add db 2.3.4 sources with minor changes.
[kopensolaris-gnu/glibc.git] / db2 / lock / lock.c
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997
5  *      Sleepycat Software.  All rights reserved.
6  */
7
8 #include "config.h"
9
10 #ifndef lint
11 static const char sccsid[] = "@(#)lock.c        10.31 (Sleepycat) 8/17/97";
12 #endif /* not lint */
13
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #endif
27
28 #include "db_int.h"
29 #include "shqueue.h"
30 #include "db_page.h"
31 #include "db_shash.h"
32 #include "lock.h"
33 #include "common_ext.h"
34 #include "db_am.h"
35
36 static void __lock_checklocker __P((DB_LOCKTAB *, struct __db_lock *, int));
37 static int  __lock_count_locks __P((DB_LOCKREGION *));
38 static int  __lock_count_objs __P((DB_LOCKREGION *));
39 static int  __lock_create __P((const char *, int, DB_ENV *));
40 static void __lock_freeobj __P((DB_LOCKTAB *, DB_LOCKOBJ *));
41 static int  __lock_get_internal __P((DB_LOCKTAB *, u_int32_t, int, const DBT *,
42     db_lockmode_t, struct __db_lock **));
43 static int  __lock_grow_region __P((DB_LOCKTAB *, int, size_t));
44 static int  __lock_put_internal __P((DB_LOCKTAB *, struct __db_lock *, int));
45 static void __lock_remove_waiter
46     __P((DB_LOCKTAB *, DB_LOCKOBJ *, struct __db_lock *, db_status_t));
47 static void __lock_reset_region __P((DB_LOCKTAB *));
48 static int  __lock_validate_region __P((DB_LOCKTAB *));
49 #ifdef DEBUG
50 static void __lock_dump_locker __P((DB_LOCKTAB *, DB_LOCKOBJ *));
51 static void __lock_dump_object __P((DB_LOCKTAB *, DB_LOCKOBJ *));
52 static void __lock_printlock __P((DB_LOCKTAB *, struct __db_lock *, int));
53 #endif
54
55 /*
56  * Create and initialize a lock region in shared memory.
57  */
58
59 /*
60  * __lock_create --
61  *      Create the lock region.  Returns an errno.  In most cases,
62  * the errno should be that returned by __db_ropen, in which case
63  * an EAGAIN means that we should retry, and an EEXIST means that
64  * the region exists and we didn't need to create it.  Any other
65  * sort of errno should be treated as a system error, leading to a
66  * failure of the original interface call.
67  */
68 static int
69 __lock_create(path, mode, dbenv)
70         const char *path;
71         int mode;
72         DB_ENV *dbenv;
73 {
74         struct __db_lock *lp;
75         struct lock_header *tq_head;
76         struct obj_header *obj_head;
77         DB_LOCKOBJ *op;
78         DB_LOCKREGION *lrp;
79         u_int maxlocks;
80         u_int32_t i;
81         int fd, lock_modes, nelements, ret;
82         u_int8_t *conflicts, *curaddr;
83
84         maxlocks = dbenv == NULL || dbenv->lk_max == 0 ?
85             DB_LOCK_DEFAULT_N : dbenv->lk_max;
86         lock_modes = dbenv == NULL || dbenv->lk_modes == 0 ?
87             DB_LOCK_RW_N : dbenv->lk_modes;
88         conflicts = dbenv == NULL || dbenv->lk_conflicts == NULL ?
89             (u_int8_t *)db_rw_conflicts : dbenv->lk_conflicts;
90
91         if ((ret =
92             __db_rcreate(dbenv, DB_APP_NONE, path, DB_DEFAULT_LOCK_FILE, mode,
93             LOCK_REGION_SIZE(lock_modes, maxlocks, __db_tablesize(maxlocks)),
94             &fd, &lrp)) != 0)
95                 return (ret);
96
97         /* Region exists; now initialize it. */
98         lrp->table_size = __db_tablesize(maxlocks);
99         lrp->magic = DB_LOCKMAGIC;
100         lrp->version = DB_LOCKVERSION;
101         lrp->id = 0;
102         lrp->maxlocks = maxlocks;
103         lrp->need_dd = 0;
104         lrp->detect = DB_LOCK_NORUN;
105         lrp->numobjs = maxlocks;
106         lrp->nlockers = 0;
107         lrp->mem_bytes = ALIGN(STRING_SIZE(maxlocks), sizeof(size_t));
108         lrp->increment = lrp->hdr.size / 2;
109         lrp->nmodes = lock_modes;
110         lrp->nconflicts = 0;
111         lrp->nrequests = 0;
112         lrp->nreleases = 0;
113         lrp->ndeadlocks = 0;
114
115         /*
116          * As we write the region, we've got to maintain the alignment
117          * for the structures that follow each chunk.  This information
118          * ends up being encapsulated both in here as well as in the
119          * lock.h file for the XXX_SIZE macros.
120          */
121         /* Initialize conflict matrix. */
122         curaddr = (u_int8_t *)lrp + sizeof(DB_LOCKREGION);
123         memcpy(curaddr, conflicts, lock_modes * lock_modes);
124         curaddr += lock_modes * lock_modes;
125
126         /*
127          * Initialize hash table.
128          */
129         curaddr = (u_int8_t *)ALIGNP(curaddr, LOCK_HASH_ALIGN);
130         lrp->hash_off = curaddr - (u_int8_t *)lrp;
131         nelements = lrp->table_size;
132         __db_hashinit(curaddr, nelements);
133         curaddr += nelements * sizeof(DB_HASHTAB);
134
135         /*
136          * Initialize locks onto a free list. Since locks contains mutexes,
137          * we need to make sure that each lock is aligned on a MUTEX_ALIGNMENT
138          * boundary.
139          */
140         curaddr = (u_int8_t *)ALIGNP(curaddr, MUTEX_ALIGNMENT);
141         tq_head = &lrp->free_locks;
142         SH_TAILQ_INIT(tq_head);
143
144         for (i = 0; i++ < maxlocks;
145             curaddr += ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT)) {
146                 lp = (struct __db_lock *)curaddr;
147                 lp->status = DB_LSTAT_FREE;
148                 SH_TAILQ_INSERT_HEAD(tq_head, lp, links, __db_lock);
149         }
150
151         /* Initialize objects onto a free list.  */
152         obj_head = &lrp->free_objs;
153         SH_TAILQ_INIT(obj_head);
154
155         for (i = 0; i++ < maxlocks; curaddr += sizeof(DB_LOCKOBJ)) {
156                 op = (DB_LOCKOBJ *)curaddr;
157                 SH_TAILQ_INSERT_HEAD(obj_head, op, links, __db_lockobj);
158         }
159
160         /*
161          * Initialize the string space; as for all shared memory allocation
162          * regions, this requires size_t alignment, since we store the
163          * lengths of malloc'd areas in the area..
164          */
165         curaddr = (u_int8_t *)ALIGNP(curaddr, sizeof(size_t));
166         lrp->mem_off = curaddr - (u_int8_t *)lrp;
167         __db_shalloc_init(curaddr, lrp->mem_bytes);
168
169         /* Release the lock. */
170         (void)__db_mutex_unlock(&lrp->hdr.lock, fd);
171
172         /* Now unmap the region. */
173         if ((ret = __db_rclose(dbenv, fd, lrp)) != 0) {
174                 (void)lock_unlink(path, 1 /* force */, dbenv);
175                 return (ret);
176         }
177
178         return (0);
179 }
180
181 int
182 lock_open(path, flags, mode, dbenv, ltp)
183         const char *path;
184         int flags, mode;
185         DB_ENV *dbenv;
186         DB_LOCKTAB **ltp;
187 {
188         DB_LOCKTAB *lt;
189         int ret, retry_cnt;
190
191         /* Validate arguments. */
192 #ifdef HAVE_SPINLOCKS
193 #define OKFLAGS (DB_CREATE | DB_THREAD)
194 #else
195 #define OKFLAGS (DB_CREATE)
196 #endif
197         if ((ret = __db_fchk(dbenv, "lock_open", flags, OKFLAGS)) != 0)
198                 return (ret);
199
200         /*
201          * Create the lock table structure.
202          */
203         if ((lt = (DB_LOCKTAB *)calloc(1, sizeof(DB_LOCKTAB))) == NULL) {
204                 __db_err(dbenv, "%s", strerror(errno));
205                 return (ENOMEM);
206         }
207         lt->dbenv = dbenv;
208
209         /*
210          * Now, create the lock region if it doesn't already exist.
211          */
212         retry_cnt = 0;
213 retry:  if (LF_ISSET(DB_CREATE) &&
214             (ret = __lock_create(path, mode, dbenv)) != 0)
215                 if (ret == EAGAIN && ++retry_cnt < 3) {
216                         (void)__db_sleep(1, 0);
217                         goto retry;
218                 } else if (ret == EEXIST) /* We did not create the region */
219                         LF_CLR(DB_CREATE);
220                 else
221                         goto out;
222
223         /*
224          * Finally, open the region, map it in, and increment the
225          * reference count.
226          */
227         retry_cnt = 0;
228 retry1: if ((ret = __db_ropen(dbenv, DB_APP_NONE, path, DB_DEFAULT_LOCK_FILE,
229             LF_ISSET(~(DB_CREATE | DB_THREAD)), &lt->fd, &lt->region)) != 0) {
230                 if (ret == EAGAIN && ++retry_cnt < 3) {
231                         (void)__db_sleep(1, 0);
232                         goto retry1;
233                 }
234                 goto out;
235          }
236
237         if (lt->region->magic != DB_LOCKMAGIC) {
238                 __db_err(dbenv, "lock_open: Bad magic number");
239                 ret = EINVAL;
240                 goto out;
241         }
242
243         /* Check for automatic deadlock detection. */
244         if (dbenv->lk_detect != DB_LOCK_NORUN) {
245                 if (lt->region->detect != DB_LOCK_NORUN &&
246                     dbenv->lk_detect != DB_LOCK_DEFAULT &&
247                     lt->region->detect != dbenv->lk_detect) {
248                         __db_err(dbenv,
249                             "lock_open: incompatible deadlock detector mode");
250                         ret = EINVAL;
251                         goto out;
252                 }
253                 if (lt->region->detect == DB_LOCK_NORUN)
254                         lt->region->detect = dbenv->lk_detect;
255         }
256
257         /* Set up remaining pointers into region. */
258         lt->conflicts = (u_int8_t *)lt->region + sizeof(DB_LOCKREGION);
259         lt->hashtab =
260             (DB_HASHTAB *)((u_int8_t *)lt->region + lt->region->hash_off);
261         lt->mem = (void *)((u_int8_t *)lt->region + lt->region->mem_off);
262         lt->reg_size = lt->region->hdr.size;
263
264         *ltp = lt;
265         return (0);
266
267 /* Error handling. */
268 out:    if (lt->region != NULL)
269                 (void)__db_rclose(lt->dbenv, lt->fd, lt->region);
270         if (LF_ISSET(DB_CREATE))
271                 (void)lock_unlink(path, 1, lt->dbenv);
272         free(lt);
273         return (ret);
274 }
275
276 int
277 lock_id (lt, idp)
278         DB_LOCKTAB *lt;
279         u_int32_t *idp;
280 {
281         u_int32_t id;
282
283         LOCK_LOCKREGION(lt);
284         if (lt->region->id >= DB_LOCK_MAXID)
285                 lt->region->id = 0;
286         id = ++lt->region->id;
287         UNLOCK_LOCKREGION(lt);
288
289         *idp = id;
290         return (0);
291 }
292
293 int
294 lock_vec(lt, locker, flags, list, nlist, elistp)
295         DB_LOCKTAB *lt;
296         u_int32_t locker;
297         int flags, nlist;
298         DB_LOCKREQ *list, **elistp;
299 {
300         struct __db_lock *lp;
301         DB_LOCKOBJ *sh_obj, *sh_locker;
302         int i, ret, run_dd;
303
304         /* Validate arguments. */
305         if ((ret =
306             __db_fchk(lt->dbenv, "lock_vec", flags, DB_LOCK_NOWAIT)) != 0)
307                 return (ret);
308
309         LOCK_LOCKREGION(lt);
310
311         if ((ret = __lock_validate_region(lt)) != 0) {
312                 UNLOCK_LOCKREGION(lt);
313                 return (ret);
314         }
315
316         ret = 0;
317         for (i = 0; i < nlist && ret == 0; i++) {
318                 switch (list[i].op) {
319                 case DB_LOCK_GET:
320                         ret = __lock_get_internal(lt, locker, flags,
321                             list[i].obj, list[i].mode, &lp);
322                         if (ret == 0)
323                                 list[i].lock = LOCK_TO_OFFSET(lt, lp);
324                         break;
325                 case DB_LOCK_PUT:
326                         lp = OFFSET_TO_LOCK(lt, list[i].lock);
327                         if (lp->holder != locker) {
328                                 ret = DB_LOCK_NOTHELD;
329                                 break;
330                         }
331                         list[i].mode = lp->mode;
332
333                         /* XXX Need to copy the object. ??? */
334                         ret = __lock_put_internal(lt, lp, 0);
335                         break;
336                 case DB_LOCK_PUT_ALL:
337                         /* Find the locker. */
338                         if ((ret = __lock_getobj(lt, locker,
339                             NULL, DB_LOCK_LOCKER, &sh_locker)) != 0)
340                                 break;
341
342                         for (lp = SH_LIST_FIRST(&sh_locker->heldby, __db_lock);
343                             lp != NULL;
344                             lp = SH_LIST_FIRST(&sh_locker->heldby, __db_lock)) {
345                                 if ((ret = __lock_put_internal(lt, lp, 0)) != 0)
346                                         break;
347                         }
348                         __lock_freeobj(lt, sh_locker);
349                         lt->region->nlockers--;
350                         break;
351                 case DB_LOCK_PUT_OBJ:
352
353                         /* Look up the object in the hash table. */
354                         __db_hashlookup(lt->hashtab, __db_lockobj, links,
355                             list[i].obj, sh_obj, lt->region->table_size,
356                             __lock_ohash, __lock_cmp);
357                         if (sh_obj == NULL) {
358                                 ret = EINVAL;
359                                 break;
360                         }
361                         /*
362                          * Release waiters first, because they won't cause
363                          * anyone else to be awakened.  If we release the
364                          * lockers first, all the waiters get awakened
365                          * needlessly.
366                          */
367                         for (lp = SH_TAILQ_FIRST(&sh_obj->waiters, __db_lock);
368                             lp != NULL;
369                             lp = SH_TAILQ_FIRST(&sh_obj->waiters, __db_lock)) {
370                                 lt->region->nreleases += lp->refcount;
371                                 __lock_remove_waiter(lt, sh_obj, lp,
372                                     DB_LSTAT_NOGRANT);
373                                 __lock_checklocker(lt, lp, 1);
374                         }
375
376                         for (lp = SH_TAILQ_FIRST(&sh_obj->holders, __db_lock);
377                             lp != NULL;
378                             lp = SH_TAILQ_FIRST(&sh_obj->holders, __db_lock)) {
379
380                                 lt->region->nreleases += lp->refcount;
381                                 SH_LIST_REMOVE(lp, locker_links, __db_lock);
382                                 SH_TAILQ_REMOVE(&sh_obj->holders, lp, links,
383                                     __db_lock);
384                                 lp->status = DB_LSTAT_FREE;
385                                 SH_TAILQ_INSERT_HEAD(&lt->region->free_locks,
386                                     lp, links, __db_lock);
387                         }
388
389                         /* Now free the object. */
390                         __lock_freeobj(lt, sh_obj);
391                         break;
392 #ifdef DEBUG
393                 case DB_LOCK_DUMP:
394                         /* Find the locker. */
395                         if ((ret = __lock_getobj(lt, locker,
396                             NULL, DB_LOCK_LOCKER, &sh_locker)) != 0)
397                                 break;
398
399                         for (lp = SH_LIST_FIRST(&sh_locker->heldby, __db_lock);
400                             lp != NULL;
401                             lp = SH_LIST_NEXT(lp, locker_links, __db_lock)) {
402                                 __lock_printlock(lt, lp, 1);
403                                 ret = EINVAL;
404                         }
405                         if (ret == 0) {
406                                 __lock_freeobj(lt, sh_locker);
407                                 lt->region->nlockers--;
408                         }
409                         break;
410 #endif
411                 default:
412                         ret = EINVAL;
413                         break;
414                 }
415         }
416
417         if (lt->region->need_dd && lt->region->detect != DB_LOCK_NORUN) {
418                 run_dd = 1;
419                 lt->region->need_dd = 0;
420         } else
421                 run_dd = 0;
422
423         UNLOCK_LOCKREGION(lt);
424
425         if (ret == 0 && run_dd)
426                 lock_detect(lt, 0, lt->region->detect);
427
428         if (elistp && ret != 0)
429                 *elistp = &list[i - 1];
430         return (ret);
431 }
432
433 int
434 lock_get(lt, locker, flags, obj, lock_mode, lock)
435         DB_LOCKTAB *lt;
436         u_int32_t locker;
437         int flags;
438         const DBT *obj;
439         db_lockmode_t lock_mode;
440         DB_LOCK *lock;
441 {
442         struct __db_lock *lockp;
443         int ret;
444
445         /* Validate arguments. */
446         if ((ret =
447             __db_fchk(lt->dbenv, "lock_get", flags, DB_LOCK_NOWAIT)) != 0)
448                 return (ret);
449
450         LOCK_LOCKREGION(lt);
451
452         ret = __lock_validate_region(lt);
453         if (ret == 0 && (ret = __lock_get_internal(lt,
454             locker, flags, obj, lock_mode, &lockp)) == 0) {
455                 *lock = LOCK_TO_OFFSET(lt, lockp);
456                 lt->region->nrequests++;
457         }
458
459         UNLOCK_LOCKREGION(lt);
460         return (ret);
461 }
462
463 int
464 lock_put(lt, lock)
465         DB_LOCKTAB *lt;
466         DB_LOCK lock;
467 {
468         struct __db_lock *lockp;
469         int ret, run_dd;
470
471         LOCK_LOCKREGION(lt);
472
473         if ((ret = __lock_validate_region(lt)) != 0)
474                 return (ret);
475         else {
476                 lockp = OFFSET_TO_LOCK(lt, lock);
477                 ret = __lock_put_internal(lt, lockp, 0);
478         }
479
480         __lock_checklocker(lt, lockp, 0);
481
482         if (lt->region->need_dd && lt->region->detect != DB_LOCK_NORUN) {
483                 run_dd = 1;
484                 lt->region->need_dd = 0;
485         } else
486                 run_dd = 0;
487
488         UNLOCK_LOCKREGION(lt);
489
490         if (ret == 0 && run_dd)
491                 lock_detect(lt, 0, lt->region->detect);
492
493         return (ret);
494 }
495
496 int
497 lock_close(lt)
498         DB_LOCKTAB *lt;
499 {
500         int ret;
501
502         if ((ret = __db_rclose(lt->dbenv, lt->fd, lt->region)) != 0)
503                 return (ret);
504
505         /* Free lock table. */
506         free(lt);
507         return (0);
508 }
509
510 int
511 lock_unlink(path, force, dbenv)
512         const char *path;
513         int force;
514         DB_ENV *dbenv;
515 {
516         return (__db_runlink(dbenv,
517             DB_APP_NONE, path, DB_DEFAULT_LOCK_FILE, force));
518 }
519
520 /*
521  * XXX This looks like it could be void, but I'm leaving it returning
522  * an int because I think it will have to when we go through and add
523  * the appropriate error checking for the EINTR on mutexes.
524  */
525 static int
526 __lock_put_internal(lt, lockp, do_all)
527         DB_LOCKTAB *lt;
528         struct __db_lock *lockp;
529         int do_all;
530 {
531         struct __db_lock *lp_w, *lp_h, *next_waiter;
532         DB_LOCKOBJ *sh_obj;
533         int state_changed;
534
535         if (lockp->refcount == 0 || (lockp->status != DB_LSTAT_HELD &&
536             lockp->status != DB_LSTAT_WAITING) || lockp->obj == 0) {
537                 __db_err(lt->dbenv, "lock_put: invalid lock %lu",
538                     (u_long)((u_int8_t *)lockp - (u_int8_t *)lt->region));
539                 return (EINVAL);
540         }
541
542         if (do_all)
543                 lt->region->nreleases += lockp->refcount;
544         else
545                 lt->region->nreleases++;
546         if (do_all == 0 && lockp->refcount > 1) {
547                 lockp->refcount--;
548                 return (0);
549         }
550
551         /* Get the object associated with this lock. */
552         sh_obj = (DB_LOCKOBJ *)((u_int8_t *)lockp + lockp->obj);
553
554         /* Remove lock from locker list. */
555         SH_LIST_REMOVE(lockp, locker_links, __db_lock);
556
557         /* Remove this lock from its holders/waitlist. */
558         if (lockp->status != DB_LSTAT_HELD)
559                 __lock_remove_waiter(lt, sh_obj, lockp, DB_LSTAT_FREE);
560         else
561                 SH_TAILQ_REMOVE(&sh_obj->holders, lockp, links, __db_lock);
562
563         /*
564          * We need to do lock promotion.  We also need to determine if
565          * we're going to need to run the deadlock detector again.  If
566          * we release locks, and there are waiters, but no one gets promoted,
567          * then we haven't fundamentally changed the lockmgr state, so
568          * we may still have a deadlock and we have to run again.  However,
569          * if there were no waiters, or we actually promoted someone, then
570          * we are OK and we don't have to run it immediately.
571          */
572         for (lp_w = SH_TAILQ_FIRST(&sh_obj->waiters, __db_lock),
573             state_changed = lp_w == NULL;
574             lp_w != NULL;
575             lp_w = next_waiter) {
576                 next_waiter = SH_TAILQ_NEXT(lp_w, links, __db_lock);
577                 for (lp_h = SH_TAILQ_FIRST(&sh_obj->holders, __db_lock);
578                     lp_h != NULL;
579                     lp_h = SH_TAILQ_NEXT(lp_h, links, __db_lock)) {
580                         if (CONFLICTS(lt, lp_h->mode, lp_w->mode) &&
581                             lp_h->holder != lp_w->holder)
582                                 break;
583                 }
584                 if (lp_h != NULL)       /* Found a conflict. */
585                         break;
586
587                 /* No conflict, promote the waiting lock. */
588                 SH_TAILQ_REMOVE(&sh_obj->waiters, lp_w, links, __db_lock);
589                 lp_w->status = DB_LSTAT_PENDING;
590                 SH_TAILQ_INSERT_TAIL(&sh_obj->holders, lp_w, links);
591
592                 /* Wake up waiter. */
593                 (void)__db_mutex_unlock(&lp_w->mutex, lt->fd);
594                 state_changed = 1;
595         }
596
597         /* Check if object should be reclaimed. */
598         if (SH_TAILQ_FIRST(&sh_obj->holders, __db_lock) == NULL) {
599                 __db_hashremove_el(lt->hashtab, __db_lockobj, links, sh_obj,
600                     lt->region->table_size, __lock_lhash);
601                 __db_shalloc_free(lt->mem, SH_DBT_PTR(&sh_obj->lockobj));
602                 SH_TAILQ_INSERT_HEAD(&lt->region->free_objs, sh_obj, links,
603                     __db_lockobj);
604                 state_changed = 1;
605         }
606
607         /* Free lock. */
608         lockp->status = DB_LSTAT_FREE;
609         SH_TAILQ_INSERT_HEAD(&lt->region->free_locks, lockp, links, __db_lock);
610
611         /*
612          * If we did not promote anyone; we need to run the deadlock
613          * detector again.
614          */
615         if (state_changed == 0)
616                 lt->region->need_dd = 1;
617
618         return (0);
619 }
620
621 static int
622 __lock_get_internal(lt, locker, flags, obj, lock_mode, lockp)
623         DB_LOCKTAB *lt;
624         u_int32_t locker;
625         int flags;
626         const DBT *obj;
627         db_lockmode_t lock_mode;
628         struct __db_lock **lockp;
629 {
630         struct __db_lock *newl, *lp;
631         DB_LOCKOBJ *sh_obj, *sh_locker;
632         DB_LOCKREGION *lrp;
633         size_t newl_off;
634         int ret;
635
636         ret = 0;
637         /*
638          * Check that lock mode is valid.
639          */
640
641         lrp = lt->region;
642         if ((u_int32_t)lock_mode >= lrp->nmodes) {
643                 __db_err(lt->dbenv,
644                     "lock_get: invalid lock mode %lu\n", (u_long)lock_mode);
645                 return (EINVAL);
646         }
647
648         /* Allocate a new lock.  Optimize for the common case of a grant. */
649         if ((newl = SH_TAILQ_FIRST(&lrp->free_locks, __db_lock)) == NULL) {
650                 if ((ret = __lock_grow_region(lt, DB_LOCK_LOCK, 0)) != 0)
651                         return (ret);
652                 lrp = lt->region;
653                 newl = SH_TAILQ_FIRST(&lrp->free_locks, __db_lock);
654         }
655         newl_off = LOCK_TO_OFFSET(lt, newl);
656
657         /* Optimize for common case of granting a lock. */
658         SH_TAILQ_REMOVE(&lrp->free_locks, newl, links, __db_lock);
659
660         newl->mode = lock_mode;
661         newl->status = DB_LSTAT_HELD;
662         newl->holder = locker;
663         newl->refcount = 1;
664
665         if ((ret =
666             __lock_getobj(lt, 0, (DBT *)obj, DB_LOCK_OBJTYPE, &sh_obj)) != 0)
667                 return (ret);
668
669         lrp = lt->region;                       /* getobj might have grown */
670         newl = OFFSET_TO_LOCK(lt, newl_off);
671
672         /* Now make new lock point to object */
673         newl->obj = SH_PTR_TO_OFF(newl, sh_obj);
674
675         /*
676          * Now we have a lock and an object and we need to see if we should
677          * grant the lock.  We use a FIFO ordering so we can only grant a
678          * new lock if it does not conflict with anyone on the holders list
679          * OR anyone on the waiters list.  In case of conflict, we put the
680          * new lock on the end of the waiters list.
681          */
682         for (lp = SH_TAILQ_FIRST(&sh_obj->holders, __db_lock);
683             lp != NULL;
684             lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
685                 if (CONFLICTS(lt, lp->mode, lock_mode) &&
686                     locker != lp->holder)
687                         break;
688                 else if (lp->holder == locker && lp->mode == lock_mode &&
689                     lp->status == DB_LSTAT_HELD) {
690                         /* Lock is already held, just inc the ref count. */
691                         lp->refcount++;
692                         SH_TAILQ_INSERT_HEAD(&lrp->free_locks, newl, links,
693                             __db_lock);
694                         *lockp = lp;
695                         return (0);
696                 }
697         }
698
699         if (lp == NULL)
700                 for (lp = SH_TAILQ_FIRST(&sh_obj->waiters, __db_lock);
701                     lp != NULL;
702                     lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
703                         if (CONFLICTS(lt, lp->mode, lock_mode) &&
704                             locker != lp->holder)
705                                 break;
706                 }
707         if (lp == NULL)
708                 SH_TAILQ_INSERT_TAIL(&sh_obj->holders, newl, links);
709         else if (!(flags & DB_LOCK_NOWAIT))
710                 SH_TAILQ_INSERT_TAIL(&sh_obj->waiters, newl, links);
711         else {
712                 /* Free the lock and return an error. */
713                 newl->status = DB_LSTAT_FREE;
714                 SH_TAILQ_INSERT_HEAD(&lrp->free_locks, newl, links, __db_lock);
715                 return (DB_LOCK_NOTGRANTED);
716         }
717
718         /*
719          * This is really a blocker for the process, so initialize it
720          * set.  That way the current process will block when it tries
721          * to get it and the waking process will release it.
722          */
723         (void)__db_mutex_init(&newl->mutex,
724             MUTEX_LOCK_OFFSET(lt->region, &newl->mutex));
725         (void)__db_mutex_lock(&newl->mutex, lt->fd,
726             lt->dbenv == NULL ? NULL : lt->dbenv->db_yield);
727
728         /*
729          * Now, insert the lock onto its locker's list.
730          */
731         if ((ret =
732             __lock_getobj(lt, locker, NULL, DB_LOCK_LOCKER, &sh_locker)) != 0)
733                 return (ret);
734
735         lrp = lt->region;
736         SH_LIST_INSERT_HEAD(&sh_locker->heldby, newl, locker_links, __db_lock);
737
738         if (lp != NULL) {
739                 newl->status = DB_LSTAT_WAITING;
740                 lrp->nconflicts++;
741                 /*
742                  * We are about to wait; must release the region mutex.
743                  * Then, when we wakeup, we need to reacquire the region
744                  * mutex before continuing.
745                  */
746                 if (lrp->detect == DB_LOCK_NORUN)
747                         lt->region->need_dd = 1;
748                 UNLOCK_LOCKREGION(lt);
749
750                 /*
751                  * We are about to wait; before waiting, see if the deadlock
752                  * detector should be run.
753                  */
754                 if (lrp->detect != DB_LOCK_NORUN)
755                         ret = lock_detect(lt, 0, lrp->detect);
756
757                 (void)__db_mutex_lock(&newl->mutex,
758                     lt->fd, lt->dbenv == NULL ? NULL : lt->dbenv->db_yield);
759
760                 LOCK_LOCKREGION(lt);
761                 if (newl->status != DB_LSTAT_PENDING) {
762                         /* Return to free list. */
763                         __lock_checklocker(lt, newl, 0);
764                         SH_TAILQ_INSERT_HEAD(&lrp->free_locks, newl, links,
765                             __db_lock);
766                         switch (newl->status) {
767                                 case DB_LSTAT_ABORTED:
768                                         ret = DB_LOCK_DEADLOCK;
769                                         break;
770                                 case DB_LSTAT_NOGRANT:
771                                         ret = DB_LOCK_NOTGRANTED;
772                                         break;
773                                 default:
774                                         ret = EINVAL;
775                                         break;
776                         }
777                         newl->status = DB_LSTAT_FREE;
778                         newl = NULL;
779                 } else
780                         newl->status = DB_LSTAT_HELD;
781         }
782
783         *lockp = newl;
784         return (ret);
785 }
786
787 /*
788  * This is called at every interface to verify if the region
789  * has changed size, and if so, to remap the region in and
790  * reset the process pointers.
791  */
792 static int
793 __lock_validate_region(lt)
794         DB_LOCKTAB *lt;
795 {
796         int ret;
797
798         if (lt->reg_size == lt->region->hdr.size)
799                 return (0);
800
801         /* Grow the region. */
802         if ((ret = __db_rremap(lt->dbenv, lt->region,
803             lt->reg_size, lt->region->hdr.size, lt->fd, &lt->region)) != 0)
804                 return (ret);
805
806         __lock_reset_region(lt);
807
808         return (0);
809 }
810
811 /*
812  * We have run out of space; time to grow the region.
813  */
814 static int
815 __lock_grow_region(lt, which, howmuch)
816         DB_LOCKTAB *lt;
817         int which;
818         size_t howmuch;
819 {
820         struct __db_lock *newl;
821         struct lock_header *lock_head;
822         struct obj_header *obj_head;
823         DB_LOCKOBJ *op;
824         DB_LOCKREGION *lrp;
825         float lock_ratio, obj_ratio;
826         size_t incr, oldsize, used;
827         u_int32_t i, newlocks, newmem, newobjs;
828         int ret, usedlocks, usedmem, usedobjs;
829         u_int8_t *curaddr;
830
831         lrp = lt->region;
832         oldsize = lrp->hdr.size;
833         incr = lrp->increment;
834
835         /* Figure out how much of each sort of space we have. */
836         usedmem = lrp->mem_bytes - __db_shalloc_count(lt->mem);
837         usedobjs = lrp->numobjs - __lock_count_objs(lrp);
838         usedlocks = lrp->maxlocks - __lock_count_locks(lrp);
839
840         /*
841          * Figure out what fraction of the used space belongs to each
842          * different type of "thing" in the region.  Then partition the
843          * new space up according to this ratio.
844          */
845         used = usedmem +
846             usedlocks * ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT) +
847             usedobjs * sizeof(DB_LOCKOBJ);
848
849         lock_ratio = usedlocks *
850             ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT) / (float)used;
851         obj_ratio = usedobjs * sizeof(DB_LOCKOBJ) / (float)used;
852
853         newlocks = (u_int32_t)(lock_ratio *
854             incr / ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT));
855         newobjs = (u_int32_t)(obj_ratio * incr / sizeof(DB_LOCKOBJ));
856         newmem = incr -
857             (newobjs * sizeof(DB_LOCKOBJ) +
858             newlocks * ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT));
859
860         /*
861          * Make sure we allocate enough memory for the object being
862          * requested.
863          */
864         switch (which) {
865                 case DB_LOCK_LOCK:
866                         if (newlocks == 0) {
867                                 newlocks = 10;
868                                 incr += newlocks * sizeof(struct __db_lock);
869                         }
870                         break;
871                 case DB_LOCK_OBJ:
872                         if (newobjs == 0) {
873                                 newobjs = 10;
874                                 incr += newobjs * sizeof(DB_LOCKOBJ);
875                         }
876                         break;
877                 case DB_LOCK_MEM:
878                         if (newmem < howmuch * 2) {
879                                 incr += howmuch * 2 - newmem;
880                                 newmem = howmuch * 2;
881                         }
882                         break;
883         }
884
885         newmem += ALIGN(incr, sizeof(size_t)) - incr;
886         incr = ALIGN(incr, sizeof(size_t));
887
888         /*
889          * Since we are going to be allocating locks at the beginning of the
890          * new chunk, we need to make sure that the chunk is MUTEX_ALIGNMENT
891          * aligned.  We did not guarantee this when we created the region, so
892          * we may need to pad the old region by extra bytes to ensure this
893          * alignment.
894          */
895         incr += ALIGN(oldsize, MUTEX_ALIGNMENT) - oldsize;
896
897         __db_err(lt->dbenv,
898             "Growing lock region: %lu locks %lu objs %lu bytes",
899             (u_long)newlocks, (u_long)newobjs, (u_long)newmem);
900
901         if ((ret = __db_rgrow(lt->dbenv, lt->fd, incr)) != 0)
902                 return (ret);
903         if ((ret = __db_rremap(lt->dbenv,
904             lt->region, oldsize, oldsize + incr, lt->fd, &lt->region)) != 0)
905                 return (ret);
906         __lock_reset_region(lt);
907
908         /* Update region parameters. */
909         lrp = lt->region;
910         lrp->increment = incr << 1;
911         lrp->maxlocks += newlocks;
912         lrp->numobjs += newobjs;
913         lrp->mem_bytes += newmem;
914
915         curaddr = (u_int8_t *)lrp + oldsize;
916         curaddr = (u_int8_t *)ALIGNP(curaddr, MUTEX_ALIGNMENT);
917
918         /* Put new locks onto the free list. */
919         lock_head = &lrp->free_locks;
920         for (i = 0; i++ < newlocks;
921             curaddr += ALIGN(sizeof(struct __db_lock), MUTEX_ALIGNMENT)) {
922                 newl = (struct __db_lock *)curaddr;
923                 SH_TAILQ_INSERT_HEAD(lock_head, newl, links, __db_lock);
924         }
925
926         /* Put new objects onto the free list.  */
927         obj_head = &lrp->free_objs;
928         for (i = 0; i++ < newobjs; curaddr += sizeof(DB_LOCKOBJ)) {
929                 op = (DB_LOCKOBJ *)curaddr;
930                 SH_TAILQ_INSERT_HEAD(obj_head, op, links, __db_lockobj);
931         }
932
933         *((size_t *)curaddr) = newmem - sizeof(size_t);
934         curaddr += sizeof(size_t);
935         __db_shalloc_free(lt->mem, curaddr);
936
937         return (0);
938 }
939
940 #ifdef DEBUG
941 void
942 __lock_dump_region(lt, flags)
943         DB_LOCKTAB *lt;
944         unsigned long flags;
945 {
946         struct __db_lock *lp;
947         DB_LOCKOBJ *op;
948         DB_LOCKREGION *lrp;
949         u_int32_t i, j;
950
951         lrp = lt->region;
952
953         printf("Lock region parameters\n");
954         printf("%s:0x%x\t%s:%lu\t%s:%lu\t%s:%lu\n%s:%lu\t%s:%lu\t%s:%lu\t\n",
955             "magic      ", lrp->magic,
956             "version    ", (u_long)lrp->version,
957             "processes  ", (u_long)lrp->hdr.refcnt,
958             "maxlocks   ", (u_long)lrp->maxlocks,
959             "table size ", (u_long)lrp->table_size,
960             "nmodes     ", (u_long)lrp->nmodes,
961             "numobjs    ", (u_long)lrp->numobjs);
962         printf("%s:%lu\t%s:%lu\t%s:%lu\n%s:%lu\t%s:%lu\t%s:%lu\n",
963             "size       ", (u_long)lrp->hdr.size,
964             "nlockers   ", (u_long)lrp->nlockers,
965             "hash_off   ", (u_long)lrp->hash_off,
966             "increment  ", (u_long)lrp->increment,
967             "mem_off    ", (u_long)lrp->mem_off,
968             "mem_bytes  ", (u_long)lrp->mem_bytes);
969 #ifndef HAVE_SPINLOCKS
970         printf("Mutex: off %lu", (u_long)lrp->hdr.lock.off);
971 #endif
972 #ifdef MUTEX_STATISTICS
973         printf(" waits %lu nowaits %lu",
974             (u_long)lrp->hdr.lock.mutex_set_wait,
975             (u_long)lrp->hdr.lock.mutex_set_nowait);
976 #endif
977         printf("\n%s:%lu\t%s:%lu\t%s:%lu\t%s:%lu\n",
978             "nconflicts ", (u_long)lrp->nconflicts,
979             "nrequests  ", (u_long)lrp->nrequests,
980             "nreleases  ", (u_long)lrp->nreleases,
981             "ndeadlocks ", (u_long)lrp->ndeadlocks);
982         printf("need_dd    %lu\n", (u_long)lrp->need_dd);
983         if (flags & LOCK_DEBUG_CONF) {
984                 printf("\nConflict matrix\n");
985
986                 for (i = 0; i < lrp->nmodes; i++) {
987                         for (j = 0; j < lrp->nmodes; j++)
988                                 printf("%lu\t",
989                                     (u_long)lt->conflicts[i * lrp->nmodes + j]);
990                         printf("\n");
991                 }
992         }
993
994         for (i = 0; i < lrp->table_size; i++) {
995                 op = SH_TAILQ_FIRST(&lt->hashtab[i], __db_lockobj);
996                 if (op != NULL && flags & LOCK_DEBUG_BUCKET)
997                         printf("Bucket %lu:\n", (unsigned long)i);
998                 while (op != NULL) {
999                         if (op->type == DB_LOCK_LOCKER &&
1000                             flags & LOCK_DEBUG_LOCKERS)
1001                                 __lock_dump_locker(lt, op);
1002                         else if (flags & LOCK_DEBUG_OBJECTS &&
1003                             op->type == DB_LOCK_OBJTYPE)
1004                                 __lock_dump_object(lt, op);
1005                         op = SH_TAILQ_NEXT(op, links, __db_lockobj);
1006                 }
1007         }
1008
1009         if (flags & LOCK_DEBUG_LOCK) {
1010                 printf("\nLock Free List\n");
1011                 for (lp = SH_TAILQ_FIRST(&lrp->free_locks, __db_lock);
1012                     lp != NULL;
1013                     lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
1014                         printf("0x%x: %lu\t%lu\t%lu\t0x%x\n", (u_int)lp,
1015                             (u_long)lp->holder, (u_long)lp->mode,
1016                             (u_long)lp->status, (u_int)lp->obj);
1017                 }
1018         }
1019
1020         if (flags & LOCK_DEBUG_LOCK) {
1021                 printf("\nObject Free List\n");
1022                 for (op = SH_TAILQ_FIRST(&lrp->free_objs, __db_lockobj);
1023                     op != NULL;
1024                     op = SH_TAILQ_NEXT(op, links, __db_lockobj))
1025                         printf("0x%x\n", (u_int)op);
1026         }
1027
1028         if (flags & LOCK_DEBUG_MEM) {
1029                 printf("\nMemory Free List\n");
1030                 __db_shalloc_dump(stdout, lt->mem);
1031         }
1032 }
1033
1034 static void
1035 __lock_dump_locker(lt, op)
1036         DB_LOCKTAB *lt;
1037         DB_LOCKOBJ *op;
1038 {
1039         struct __db_lock *lp;
1040         u_int32_t locker;
1041         void *ptr;
1042
1043         ptr = SH_DBT_PTR(&op->lockobj);
1044         memcpy(&locker, ptr, sizeof(u_int32_t));
1045         printf("L %lu", (u_long)locker);
1046
1047         lp = SH_LIST_FIRST(&op->heldby, __db_lock);
1048         if (lp == NULL) {
1049                 printf("\n");
1050                 return;
1051         }
1052         for (; lp != NULL; lp = SH_LIST_NEXT(lp, locker_links, __db_lock))
1053                 __lock_printlock(lt, lp, 0);
1054 }
1055
1056 static void
1057 __lock_dump_object(lt, op)
1058         DB_LOCKTAB *lt;
1059         DB_LOCKOBJ *op;
1060 {
1061         struct __db_lock *lp;
1062         u_int32_t j;
1063         char *ptr;
1064
1065         ptr = SH_DBT_PTR(&op->lockobj);
1066         for (j = 0; j < op->lockobj.size; ptr++, j++)
1067                 printf("%c", (int)*ptr);
1068         printf("\n");
1069
1070         printf("H:");
1071         for (lp =
1072             SH_TAILQ_FIRST(&op->holders, __db_lock);
1073             lp != NULL;
1074             lp = SH_TAILQ_NEXT(lp, links, __db_lock))
1075                 __lock_printlock(lt, lp, 0);
1076         lp = SH_TAILQ_FIRST(&op->waiters, __db_lock);
1077         if (lp != NULL) {
1078                 printf("\nW:");
1079                 for (; lp != NULL; lp = SH_TAILQ_NEXT(lp, links, __db_lock))
1080                         __lock_printlock(lt, lp, 0);
1081         }
1082 }
1083
1084 int
1085 __lock_is_locked(lt, locker, dbt, mode)
1086         DB_LOCKTAB *lt;
1087         u_int32_t locker;
1088         DBT *dbt;
1089         db_lockmode_t mode;
1090 {
1091         struct __db_lock *lp;
1092         DB_LOCKOBJ *sh_obj;
1093         DB_LOCKREGION *lrp;
1094
1095         lrp = lt->region;
1096
1097         /* Look up the object in the hash table. */
1098         __db_hashlookup(lt->hashtab, __db_lockobj, links,
1099             dbt, sh_obj, lrp->table_size, __lock_ohash, __lock_cmp);
1100         if (sh_obj == NULL)
1101                 return (0);
1102
1103         for (lp = SH_TAILQ_FIRST(&sh_obj->holders, __db_lock);
1104             lp != NULL;
1105             lp = SH_TAILQ_FIRST(&sh_obj->holders, __db_lock)) {
1106                 if (lp->holder == locker && lp->mode == mode)
1107                         return (1);
1108         }
1109
1110         return (0);
1111 }
1112
1113 static void
1114 __lock_printlock(lt, lp, ispgno)
1115         DB_LOCKTAB *lt;
1116         struct __db_lock *lp;
1117         int ispgno;
1118 {
1119         DB_LOCKOBJ *lockobj;
1120         db_pgno_t pgno;
1121         size_t obj;
1122         u_int8_t *ptr;
1123         char *mode, *stat;
1124
1125         switch (lp->mode) {
1126         case DB_LOCK_IREAD:
1127                 mode = "IREAD";
1128                 break;
1129         case DB_LOCK_IWR:
1130                 mode = "IWR";
1131                 break;
1132         case DB_LOCK_IWRITE:
1133                 mode = "IWRITE";
1134                 break;
1135         case DB_LOCK_NG:
1136                 mode = "NG";
1137                 break;
1138         case DB_LOCK_READ:
1139                 mode = "READ";
1140                 break;
1141         case DB_LOCK_WRITE:
1142                 mode = "WRITE";
1143                 break;
1144         default:
1145                 mode = "UNKNOWN";
1146                 break;
1147         }
1148         switch (lp->status) {
1149         case DB_LSTAT_ABORTED:
1150                 stat = "ABORT";
1151                 break;
1152         case DB_LSTAT_ERR:
1153                 stat = "ERROR";
1154                 break;
1155         case DB_LSTAT_FREE:
1156                 stat = "FREE";
1157                 break;
1158         case DB_LSTAT_HELD:
1159                 stat = "HELD";
1160                 break;
1161         case DB_LSTAT_NOGRANT:
1162                 stat = "NONE";
1163                 break;
1164         case DB_LSTAT_WAITING:
1165                 stat = "WAIT";
1166                 break;
1167         case DB_LSTAT_PENDING:
1168                 stat = "PENDING";
1169                 break;
1170         default:
1171                 stat = "UNKNOWN";
1172                 break;
1173         }
1174         printf("\t%lu\t%s\t%lu\t%s\t",
1175             (u_long)lp->holder, mode, (u_long)lp->refcount, stat);
1176
1177         lockobj = (DB_LOCKOBJ *)((u_int8_t *)lp + lp->obj);
1178         ptr = SH_DBT_PTR(&lockobj->lockobj);
1179         if (ispgno) {
1180                 /* Assume this is a DBT lock. */
1181                 memcpy(&pgno, ptr, sizeof(db_pgno_t));
1182                 printf("page %lu\n", (u_long)pgno);
1183         } else {
1184                 obj = (u_int8_t *)lp + lp->obj - (u_int8_t *)lt->region;
1185                 printf("0x%lx ", (u_long)obj);
1186                 __db_pr(ptr, lockobj->lockobj.size);
1187                 printf("\n");
1188         }
1189 }
1190
1191 #endif
1192
1193 static int
1194 __lock_count_locks(lrp)
1195         DB_LOCKREGION *lrp;
1196 {
1197         struct __db_lock *newl;
1198         int count;
1199
1200         count = 0;
1201         for (newl = SH_TAILQ_FIRST(&lrp->free_locks, __db_lock);
1202             newl != NULL;
1203             newl = SH_TAILQ_NEXT(newl, links, __db_lock))
1204                 count++;
1205
1206         return (count);
1207 }
1208
1209 static int
1210 __lock_count_objs(lrp)
1211         DB_LOCKREGION *lrp;
1212 {
1213         DB_LOCKOBJ *obj;
1214         int count;
1215
1216         count = 0;
1217         for (obj = SH_TAILQ_FIRST(&lrp->free_objs, __db_lockobj);
1218             obj != NULL;
1219             obj = SH_TAILQ_NEXT(obj, links, __db_lockobj))
1220                 count++;
1221
1222         return (count);
1223 }
1224
1225 /*
1226  * PUBLIC: int __lock_getobj  __P((DB_LOCKTAB *,
1227  * PUBLIC:     u_int32_t, DBT *, u_int32_t type, DB_LOCKOBJ **));
1228  */
1229 int
1230 __lock_getobj(lt, locker, dbt, type, objp)
1231         DB_LOCKTAB *lt;
1232         u_int32_t locker, type;
1233         DBT *dbt;
1234         DB_LOCKOBJ **objp;
1235 {
1236         DB_LOCKREGION *lrp;
1237         DB_LOCKOBJ *sh_obj;
1238         u_int32_t obj_size;
1239         int ret;
1240         void *p, *src;
1241
1242         lrp = lt->region;
1243
1244         /* Look up the object in the hash table. */
1245         if (type == DB_LOCK_OBJTYPE) {
1246                 __db_hashlookup(lt->hashtab, __db_lockobj, links, dbt, sh_obj,
1247                     lrp->table_size, __lock_ohash, __lock_cmp);
1248                 obj_size = dbt->size;
1249         } else {
1250                 __db_hashlookup(lt->hashtab, __db_lockobj, links, locker,
1251                     sh_obj, lrp->table_size, __lock_locker_hash,
1252                     __lock_locker_cmp);
1253                 obj_size = sizeof(locker);
1254         }
1255
1256         /*
1257          * If we found the object, then we can just return it.  If
1258          * we didn't find the object, then we need to create it.
1259          */
1260         if (sh_obj == NULL) {
1261                 /* Create new object and then insert it into hash table. */
1262                 if ((sh_obj = SH_TAILQ_FIRST(&lrp->free_objs, __db_lockobj))
1263                     == NULL) {
1264                         if ((ret = __lock_grow_region(lt, DB_LOCK_OBJ, 0)) != 0)
1265                                 return (ret);
1266                         lrp = lt->region;
1267                         sh_obj = SH_TAILQ_FIRST(&lrp->free_objs, __db_lockobj);
1268                 }
1269                 if ((ret = __db_shalloc(lt->mem, obj_size, 0, &p)) != 0) {
1270                         if ((ret = __lock_grow_region(lt,
1271                             DB_LOCK_MEM, obj_size)) != 0)
1272                                 return (ret);
1273                         lrp = lt->region;
1274                         /* Reacquire the head of the list. */
1275                         sh_obj = SH_TAILQ_FIRST(&lrp->free_objs, __db_lockobj);
1276                         (void)__db_shalloc(lt->mem, obj_size, 0, &p);
1277                 }
1278                 sh_obj->type = type;
1279                 src = type == DB_LOCK_OBJTYPE ? dbt->data : (void *)&locker;
1280                 memcpy(p, src, obj_size);
1281                 SH_TAILQ_REMOVE(&lrp->free_objs, sh_obj, links, __db_lockobj);
1282
1283                 SH_TAILQ_INIT(&sh_obj->waiters);
1284                 if (type == DB_LOCK_LOCKER)
1285                         SH_LIST_INIT(&sh_obj->heldby);
1286                 else
1287                         SH_TAILQ_INIT(&sh_obj->holders);
1288                 sh_obj->lockobj.size = obj_size;
1289                 sh_obj->lockobj.off = SH_PTR_TO_OFF(&sh_obj->lockobj, p);
1290
1291                 __db_hashinsert(lt->hashtab, __db_lockobj, links, sh_obj,
1292                     lrp->table_size, __lock_lhash);
1293
1294                 if (type == DB_LOCK_LOCKER)
1295                         lrp->nlockers++;
1296         }
1297
1298         *objp = sh_obj;
1299         return (0);
1300 }
1301
1302 /*
1303  * Any lock on the waitlist has a process waiting for it.  Therefore, we
1304  * can't return the lock to the freelist immediately.  Instead, we can
1305  * remove the lock from the list of waiters, set the status field of the
1306  * lock, and then let the process waking up return the lock to the
1307  * free list.
1308  */
1309 static void
1310 __lock_remove_waiter(lt, sh_obj, lockp, status)
1311         DB_LOCKTAB *lt;
1312         DB_LOCKOBJ *sh_obj;
1313         struct __db_lock *lockp;
1314         db_status_t status;
1315 {
1316         SH_TAILQ_REMOVE(&sh_obj->waiters, lockp, links, __db_lock);
1317         lockp->status = status;
1318
1319         /* Wake whoever is waiting on this lock. */
1320         (void)__db_mutex_unlock(&lockp->mutex, lt->fd);
1321 }
1322
1323 static void
1324 __lock_freeobj(lt, obj)
1325         DB_LOCKTAB *lt;
1326         DB_LOCKOBJ *obj;
1327 {
1328         __db_hashremove_el(lt->hashtab, __db_lockobj, links,
1329             obj, lt->region->table_size, __lock_lhash);
1330         __db_shalloc_free(lt->mem, SH_DBT_PTR(&obj->lockobj));
1331         SH_TAILQ_INSERT_HEAD(&lt->region->free_objs, obj, links, __db_lockobj);
1332 }
1333
1334 static void
1335 __lock_checklocker(lt, lockp, do_remove)
1336         DB_LOCKTAB *lt;
1337         struct __db_lock *lockp;
1338         int do_remove;
1339 {
1340         DB_LOCKOBJ *sh_locker;
1341
1342         if (do_remove)
1343                 SH_LIST_REMOVE(lockp, locker_links, __db_lock);
1344
1345         /* if the locker list is NULL, free up the object. */
1346         if (__lock_getobj(lt, lockp->holder, NULL, DB_LOCK_LOCKER, &sh_locker)
1347             == 0 && SH_LIST_FIRST(&sh_locker->heldby, __db_lock) == NULL) {
1348                 __lock_freeobj(lt, sh_locker);
1349                 lt->region->nlockers--;
1350         }
1351 }
1352
1353 static void
1354 __lock_reset_region(lt)
1355         DB_LOCKTAB *lt;
1356 {
1357         lt->conflicts = (u_int8_t *)lt->region + sizeof(DB_LOCKREGION);
1358         lt->hashtab =
1359             (DB_HASHTAB *)((u_int8_t *)lt->region + lt->region->hash_off);
1360         lt->mem = (void *)((u_int8_t *)lt->region + lt->region->mem_off);
1361         lt->reg_size = lt->region->hdr.size;
1362 }