2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)db_thread.c 8.15 (Sleepycat) 4/26/98";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
25 static int __db_getlockid
__P((DB
*, DB
*));
29 * Called by db access method routines when the DB_THREAD flag is set.
30 * This routine returns a handle, either an existing handle from the
31 * chain of handles, or creating one if necessary.
33 * PUBLIC: int __db_gethandle __P((DB *, int (*)(DB *, DB *), DB **));
36 __db_gethandle(dbp
, am_func
, dbpp
)
38 int (*am_func
) __P((DB
*, DB
*));
43 if ((ret
= __db_mutex_lock((db_mutex_t
*)dbp
->mutexp
, -1)) != 0)
46 if ((ret_dbp
= LIST_FIRST(&dbp
->handleq
)) != NULL
)
47 /* Simply take one off the list. */
48 LIST_REMOVE(ret_dbp
, links
);
50 /* Allocate a new handle. */
51 if ((ret_dbp
= (DB
*)__db_malloc(sizeof(*dbp
))) == NULL
) {
55 memcpy(ret_dbp
, dbp
, sizeof(*dbp
));
56 ret_dbp
->internal
= NULL
;
57 TAILQ_INIT(&ret_dbp
->curs_queue
);
59 /* Set the locker, the lock structure and the lock DBT. */
60 if ((ret
= __db_getlockid(dbp
, ret_dbp
)) != 0)
63 /* Finally, call the access method specific dup function. */
64 if ((ret
= am_func(dbp
, ret_dbp
)) != 0)
71 err
: if (ret_dbp
!= NULL
)
72 FREE(ret_dbp
, sizeof(*ret_dbp
));
75 __db_mutex_unlock((db_mutex_t
*)dbp
->mutexp
, -1)) != 0 && ret
== 0)
82 * Return a DB handle to the pool for later use.
84 * PUBLIC: int __db_puthandle __P((DB *));
94 if ((ret
= __db_mutex_lock((db_mutex_t
*)master
->mutexp
, -1)) != 0)
97 LIST_INSERT_HEAD(&master
->handleq
, dbp
, links
);
99 return (__db_mutex_unlock((db_mutex_t
*)master
->mutexp
, -1));
104 * Create a new locker ID and copy the file lock information from
105 * the old DB into the new one.
108 __db_getlockid(dbp
, new_dbp
)
113 if (F_ISSET(dbp
, DB_AM_LOCKING
)) {
114 if ((ret
= lock_id(dbp
->dbenv
->lk_info
, &new_dbp
->locker
)) != 0)
116 memcpy(new_dbp
->lock
.fileid
, dbp
->lock
.fileid
, DB_FILE_ID_LEN
);
117 new_dbp
->lock_dbt
.size
= sizeof(new_dbp
->lock
);
118 new_dbp
->lock_dbt
.data
= &new_dbp
->lock
;