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.11 (Sleepycat) 8/18/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->mutex, -1,
46 dbp->dbenv == NULL ? NULL : dbp->dbenv->db_yield)) != 0)
49 if ((ret_dbp = LIST_FIRST(&dbp->handleq)) != NULL)
50 /* Simply take one off the list. */
51 LIST_REMOVE(ret_dbp, links);
53 /* Allocate a new handle. */
54 if ((ret_dbp = (DB *)malloc(sizeof(*dbp))) == NULL) {
58 memcpy(ret_dbp, dbp, sizeof(*dbp));
59 ret_dbp->internal = NULL;
60 TAILQ_INIT(&ret_dbp->curs_queue);
62 /* Set the locker, the lock structure and the lock DBT. */
63 if ((ret = __db_getlockid(dbp, ret_dbp)) != 0)
66 /* Finally, call the access method specific dup function. */
67 if ((ret = am_func(dbp, ret_dbp)) != 0)
74 err: if (ret_dbp != NULL)
75 FREE(ret_dbp, sizeof(*ret_dbp));
78 __db_mutex_unlock((db_mutex_t *)dbp->mutex, -1)) != 0 && ret == 0)
85 * Return a DB handle to the pool for later use.
87 * PUBLIC: int __db_puthandle __P((DB *));
97 if ((ret = __db_mutex_lock((db_mutex_t *)master->mutex, -1,
98 dbp->dbenv == NULL ? NULL : dbp->dbenv->db_yield)) != 0)
101 LIST_INSERT_HEAD(&master->handleq, dbp, links);
103 return (__db_mutex_unlock((db_mutex_t *)master->mutex, -1));
108 * Create a new locker ID and copy the file lock information from
109 * the old DB into the new one.
112 __db_getlockid(dbp, new_dbp)
117 if (F_ISSET(dbp, DB_AM_LOCKING)) {
118 if ((ret = lock_id(dbp->dbenv->lk_info, &new_dbp->locker)) != 0)
120 memcpy(new_dbp->lock.fileid, dbp->lock.fileid, DB_FILE_ID_LEN);
121 new_dbp->lock_dbt.size = sizeof(new_dbp->lock);
122 new_dbp->lock_dbt.data = &new_dbp->lock;