Update.
[glibc.git] / db2 / db / db_thread.c
blob73e2a512869812159d89a0d870305757ac652ed0
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)db_thread.c 8.15 (Sleepycat) 4/26/98";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
17 #include <errno.h>
18 #include <string.h>
19 #endif
21 #include "db_int.h"
22 #include "db_page.h"
23 #include "db_am.h"
25 static int __db_getlockid __P((DB *, DB *));
28 * __db_gethandle --
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 **));
35 int
36 __db_gethandle(dbp, am_func, dbpp)
37 DB *dbp, **dbpp;
38 int (*am_func) __P((DB *, DB *));
40 DB *ret_dbp;
41 int ret, t_ret;
43 if ((ret = __db_mutex_lock((db_mutex_t *)dbp->mutexp, -1)) != 0)
44 return (ret);
46 if ((ret_dbp = LIST_FIRST(&dbp->handleq)) != NULL)
47 /* Simply take one off the list. */
48 LIST_REMOVE(ret_dbp, links);
49 else {
50 /* Allocate a new handle. */
51 if ((ret_dbp = (DB *)__db_malloc(sizeof(*dbp))) == NULL) {
52 ret = ENOMEM;
53 goto err;
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)
61 goto err;
63 /* Finally, call the access method specific dup function. */
64 if ((ret = am_func(dbp, ret_dbp)) != 0)
65 goto err;
68 *dbpp = ret_dbp;
70 if (0) {
71 err: if (ret_dbp != NULL)
72 FREE(ret_dbp, sizeof(*ret_dbp));
74 if ((t_ret =
75 __db_mutex_unlock((db_mutex_t *)dbp->mutexp, -1)) != 0 && ret == 0)
76 ret = t_ret;
77 return (ret);
81 * __db_puthandle --
82 * Return a DB handle to the pool for later use.
84 * PUBLIC: int __db_puthandle __P((DB *));
86 int
87 __db_puthandle(dbp)
88 DB *dbp;
90 DB *master;
91 int ret;
93 master = dbp->master;
94 if ((ret = __db_mutex_lock((db_mutex_t *)master->mutexp, -1)) != 0)
95 return (ret);
97 LIST_INSERT_HEAD(&master->handleq, dbp, links);
99 return (__db_mutex_unlock((db_mutex_t *)master->mutexp, -1));
103 * __db_getlockid --
104 * Create a new locker ID and copy the file lock information from
105 * the old DB into the new one.
107 static int
108 __db_getlockid(dbp, new_dbp)
109 DB *dbp, *new_dbp;
111 int ret;
113 if (F_ISSET(dbp, DB_AM_LOCKING)) {
114 if ((ret = lock_id(dbp->dbenv->lk_info, &new_dbp->locker)) != 0)
115 return (ret);
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;
120 return (0);