2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
15 #include <sys/types.h>
23 #define UBIK_INTERNALS 1
27 /* Locks hang off of each transaction, with all the transaction hanging off of
28 * the appropriate dbase. This package expects to be used in a two-phase locking
29 * protocol, so it doesn't provide a way to release anything but all of the locks in the
32 * At present, it doesn't support the setting of more than one byte-position lock at a time, that is
33 * the length field must be 1. This doesn't mean that a single transaction can't set more than
36 * It is the responsibility of the user to avoid deadlock by setting locks in a partial order.
38 * EWOULDBLOCK has been replaced in this file by EAGAIN. Many Unix's but not
39 * all (eg. HP) do not replace EWOULDBLOCK with EAGAIN. The bad news is this
40 * goes over the wire. The good news is that the code path is never triggered
41 * as it requires ulock_getLock to be called with await = 0. And ulock_SetLock
42 * isn't even used in this code base. Since NT doesn't have a native
43 * EAGAIN, we are replacing all instances of EWOULDBLOCK with EAGAIN.
47 #define WouldReadBlock(lock)\
48 ((((lock)->excl_locked & WRITE_LOCK) || (lock)->wait_states) ? 0 : 1)
49 #define WouldWriteBlock(lock)\
50 ((((lock)->excl_locked & WRITE_LOCK) || (lock)->readers_reading) ? 0 : 1)
55 /* Set a transaction lock. Atype is LOCKREAD or LOCKWRITE, await is
56 * true if you want to wait for the lock instead of returning
59 * The DBHOLD lock must be held.
61 ulock_getLock(atrans, atype, await)
62 struct ubik_trans *atrans;
65 struct ubik_trans *tt;
66 struct ubik_dbase *dbase=atrans->dbase;
68 /* On first pass, initialize the lock */
74 if ((atype != LOCKREAD) && (atype != LOCKWRITE))
77 if (atrans->flags & TRDONE)
80 if (atrans->locktype != 0) {
81 ubik_print("Ubik: Internal Error: attempted to take lock twice\n");
86 *ubik_print("Ubik: DEBUG: Thread 0x%x request %s lock\n", lwp_cpptr,
87 * ((atype == LOCKREAD) ? "READ" : "WRITE"));
90 /* Check if the lock would would block */
92 if (atype == LOCKREAD) {
93 if (WouldReadBlock(&rwlock)) return EAGAIN;
95 if (WouldWriteBlock(&rwlock)) return EAGAIN;
99 /* Create new lock record and add to spec'd transaction:
100 * locktype. This field also tells us if the thread is
101 * waiting for a lock: It will be equal to LOCKWAIT.
103 atrans->locktype = LOCKWAIT;
105 if (atype == LOCKREAD) {
106 ObtainReadLock(&rwlock);
108 ObtainWriteLock(&rwlock);
111 atrans->locktype = atype;
114 *ubik_print("Ubik: DEBUG: Thread 0x%x took %s lock\n", lwp_cpptr,
115 * ((atype == LOCKREAD) ? "READ" : "WRITE"));
120 /* Release the transaction lock */
121 ulock_relLock(atrans)
122 struct ubik_trans *atrans;
124 if (rwlockinit) return EINVAL;
126 if (atrans->locktype == LOCKREAD) {
127 ReleaseReadLock(&rwlock);
128 } else if (atrans->locktype == LOCKWRITE) {
129 ReleaseWriteLock(&rwlock);
133 *ubik_print("Ubik: DEBUG: Thread 0x%x %s unlock\n", lwp_cpptr,
134 * ((atrans->locktype == LOCKREAD) ? "READ" : "WRITE"));
137 atrans->locktype = 0;
140 /* debugging hooks */
142 struct ubik_debug *aparm;
145 aparm->anyReadLocks = 0;
146 aparm->anyWriteLocks = 0;
148 aparm->anyReadLocks = rwlock.readers_reading;
149 aparm->anyWriteLocks = ((rwlock.excl_locked == WRITE_LOCK) ? 1 : 0);