2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)db_thread.c 8.13 (Sleepycat) 10/25/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
27 static int __db_getlockid
__P((DB
*, DB
*));
31 * Called by db access method routines when the DB_THREAD flag is set.
32 * This routine returns a handle, either an existing handle from the
33 * chain of handles, or creating one if necessary.
35 * PUBLIC: int __db_gethandle __P((DB *, int (*)(DB *, DB *), DB **));
38 __db_gethandle(dbp
, am_func
, dbpp
)
40 int (*am_func
) __P((DB
*, DB
*));
45 if ((ret
= __db_mutex_lock((db_mutex_t
*)dbp
->mutexp
, -1)) != 0)
48 if ((ret_dbp
= LIST_FIRST(&dbp
->handleq
)) != NULL
)
49 /* Simply take one off the list. */
50 LIST_REMOVE(ret_dbp
, links
);
52 /* Allocate a new handle. */
53 if ((ret_dbp
= (DB
*)__db_malloc(sizeof(*dbp
))) == NULL
) {
57 memcpy(ret_dbp
, dbp
, sizeof(*dbp
));
58 ret_dbp
->internal
= NULL
;
59 TAILQ_INIT(&ret_dbp
->curs_queue
);
61 /* Set the locker, the lock structure and the lock DBT. */
62 if ((ret
= __db_getlockid(dbp
, ret_dbp
)) != 0)
65 /* Finally, call the access method specific dup function. */
66 if ((ret
= am_func(dbp
, ret_dbp
)) != 0)
73 err
: if (ret_dbp
!= NULL
)
74 FREE(ret_dbp
, sizeof(*ret_dbp
));
77 __db_mutex_unlock((db_mutex_t
*)dbp
->mutexp
, -1)) != 0 && ret
== 0)
84 * Return a DB handle to the pool for later use.
86 * PUBLIC: int __db_puthandle __P((DB *));
96 if ((ret
= __db_mutex_lock((db_mutex_t
*)master
->mutexp
, -1)) != 0)
99 LIST_INSERT_HEAD(&master
->handleq
, dbp
, links
);
101 return (__db_mutex_unlock((db_mutex_t
*)master
->mutexp
, -1));
106 * Create a new locker ID and copy the file lock information from
107 * the old DB into the new one.
110 __db_getlockid(dbp
, new_dbp
)
115 if (F_ISSET(dbp
, DB_AM_LOCKING
)) {
116 if ((ret
= lock_id(dbp
->dbenv
->lk_info
, &new_dbp
->locker
)) != 0)
118 memcpy(new_dbp
->lock
.fileid
, dbp
->lock
.fileid
, DB_FILE_ID_LEN
);
119 new_dbp
->lock_dbt
.size
= sizeof(new_dbp
->lock
);
120 new_dbp
->lock_dbt
.data
= &new_dbp
->lock
;