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>
14 #include <sys/types.h>
25 #define UBIK_INTERNALS 1
30 * Locks hang off of each transaction, with all the transaction hanging off of
31 * the appropriate dbase. This package expects to be used in a two-phase locking
32 * protocol, so it doesn't provide a way to release anything but all of the locks in the
35 * At present, it doesn't support the setting of more than one byte-position lock at a time, that is
36 * the length field must be 1. This doesn't mean that a single transaction can't set more than
39 * It is the responsibility of the user to avoid deadlock by setting locks in a partial order.
41 * #EWOULDBLOCK has been replaced in this file by #EAGAIN. Many Unix's but not
42 * all (eg. HP) do not replace #EWOULDBLOCK with #EAGAIN. The bad news is this
43 * goes over the wire. The good news is that the code path is never triggered
44 * as it requires ulock_getLock to be called with await = 0. And ulock_SetLock
45 * isn't even used in this code base. Since NT doesn't have a native
46 * #EAGAIN, we are replacing all instances of #EWOULDBLOCK with #EAGAIN.
50 #define WouldReadBlock(lock)\
51 ((((lock)->excl_locked & WRITE_LOCK) || (lock)->wait_states) ? 0 : 1)
52 #define WouldWriteBlock(lock)\
53 ((((lock)->excl_locked & WRITE_LOCK) || (lock)->readers_reading) ? 0 : 1)
59 * \brief Set a transaction lock.
60 * \param atype is #LOCKREAD or #LOCKWRITE.
61 * \param await is TRUE if you want to wait for the lock instead of returning
64 * \note The #DBHOLD lock must be held.
67 ulock_getLock(struct ubik_trans *atrans, int atype, int await)
69 struct ubik_dbase *dbase = atrans->dbase;
71 /* On first pass, initialize the lock */
77 if ((atype != LOCKREAD) && (atype != LOCKWRITE))
80 if (atrans->flags & TRDONE)
83 if (atrans->locktype != 0) {
84 ubik_print("Ubik: Internal Error: attempted to take lock twice\n");
89 *ubik_print("Ubik: DEBUG: Thread 0x%x request %s lock\n", lwp_cpptr,
90 * ((atype == LOCKREAD) ? "READ" : "WRITE"));
93 /* Check if the lock would would block */
95 if (atype == LOCKREAD) {
96 if (WouldReadBlock(&rwlock))
99 if (WouldWriteBlock(&rwlock))
104 /* Create new lock record and add to spec'd transaction:
105 * #if defined(UBIK_PAUSE)
106 * * locktype. Before doing that, set TRSETLOCK,
107 * * to tell udisk_end that another thread (us) is waiting.
109 * * locktype. This field also tells us if the thread is
110 * * waiting for a lock: It will be equal to LOCKWAIT.
113 #if defined(UBIK_PAUSE)
114 if (atrans->flags & TRSETLOCK) {
115 printf("Ubik: Internal Error: TRSETLOCK already set?\n");
118 atrans->flags |= TRSETLOCK;
120 atrans->locktype = LOCKWAIT;
121 #endif /* UBIK_PAUSE */
123 if (atype == LOCKREAD) {
124 ObtainReadLock(&rwlock);
126 ObtainWriteLock(&rwlock);
129 atrans->locktype = atype;
130 #if defined(UBIK_PAUSE)
131 atrans->flags &= ~TRSETLOCK;
133 /* We don't do this here, because this can only happen in SDISK_Lock,
134 * and there's already code there to catch this condition.
136 if (atrans->flags & TRSTALE) {
141 #endif /* UBIK_PAUSE */
144 *ubik_print("Ubik: DEBUG: Thread 0x%x took %s lock\n", lwp_cpptr,
145 * ((atype == LOCKREAD) ? "READ" : "WRITE"));
151 * \brief Release the transaction lock.
154 ulock_relLock(struct ubik_trans *atrans)
159 if (atrans->locktype == LOCKREAD) {
160 ReleaseReadLock(&rwlock);
161 } else if (atrans->locktype == LOCKWRITE) {
162 ReleaseWriteLock(&rwlock);
166 *ubik_print("Ubik: DEBUG: Thread 0x%x %s unlock\n", lwp_cpptr,
167 * ((atrans->locktype == LOCKREAD) ? "READ" : "WRITE"));
170 atrans->locktype = 0;
175 * \brief debugging hooks
178 ulock_Debug(struct ubik_debug *aparm)
181 aparm->anyReadLocks = 0;
182 aparm->anyWriteLocks = 0;
184 aparm->anyReadLocks = rwlock.readers_reading;
185 aparm->anyWriteLocks = ((rwlock.excl_locked == WRITE_LOCK) ? 1 : 0);