Remove the RCSID macro
[openafs.git] / src / ubik / lock.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <sys/types.h>
15 #include <stdarg.h>
16 #include <errno.h>
17
18 #ifndef AFS_NT40_ENV
19 #include <sys/file.h>
20 #endif
21
22 #include <lock.h>
23 #include <rx/xdr.h>
24
25 #define UBIK_INTERNALS 1
26 #include "ubik.h"
27 #include "ubik_int.h"
28
29 /*! \file
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
33  * transaction.
34  *
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
37  * one lock, however.
38  *
39  * It is the responsibility of the user to avoid deadlock by setting locks in a partial order.
40  *
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.
47  * 
48  */
49
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)
54
55 struct Lock rwlock;
56 int rwlockinit = 1;
57
58 /*!
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
62  * #EWOULDBLOCK.
63  *
64  * \note The #DBHOLD lock must be held.
65  */
66 extern int
67 ulock_getLock(struct ubik_trans *atrans, int atype, int await)
68 {
69     struct ubik_dbase *dbase = atrans->dbase;
70
71     /* On first pass, initialize the lock */
72     if (rwlockinit) {
73         Lock_Init(&rwlock);
74         rwlockinit = 0;
75     }
76
77     if ((atype != LOCKREAD) && (atype != LOCKWRITE))
78         return EINVAL;
79
80     if (atrans->flags & TRDONE)
81         return UDONE;
82
83     if (atrans->locktype != 0) {
84         ubik_print("Ubik: Internal Error: attempted to take lock twice\n");
85         abort();
86     }
87
88 /*
89  *ubik_print("Ubik: DEBUG: Thread 0x%x request %s lock\n", lwp_cpptr,
90  *           ((atype == LOCKREAD) ? "READ" : "WRITE"));
91  */
92
93     /* Check if the lock would would block */
94     if (!await) {
95         if (atype == LOCKREAD) {
96             if (WouldReadBlock(&rwlock))
97                 return EAGAIN;
98         } else {
99             if (WouldWriteBlock(&rwlock))
100                 return EAGAIN;
101         }
102     }
103
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.
108      * #else
109      * * locktype. This field also tells us if the thread is 
110      * * waiting for a lock: It will be equal to LOCKWAIT.
111      * #endif 
112      */
113 #if defined(UBIK_PAUSE)
114     if (atrans->flags & TRSETLOCK) {
115         printf("Ubik: Internal Error: TRSETLOCK already set?\n");
116         return EBUSY;
117     }
118     atrans->flags |= TRSETLOCK;
119 #else
120     atrans->locktype = LOCKWAIT;
121 #endif /* UBIK_PAUSE */
122     DBRELE(dbase);
123     if (atype == LOCKREAD) {
124         ObtainReadLock(&rwlock);
125     } else {
126         ObtainWriteLock(&rwlock);
127     }
128     DBHOLD(dbase);
129     atrans->locktype = atype;
130 #if defined(UBIK_PAUSE)
131     atrans->flags &= ~TRSETLOCK;
132 #if 0
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.
135      */
136     if (atrans->flags & TRSTALE) {
137         udisk_end(atrans);
138         return UINTERNAL;
139     }
140 #endif
141 #endif /* UBIK_PAUSE */
142
143 /*
144  *ubik_print("Ubik: DEBUG: Thread 0x%x took %s lock\n", lwp_cpptr,
145  *           ((atype == LOCKREAD) ? "READ" : "WRITE"));
146  */
147     return 0;
148 }
149
150 /*!
151  * \brief Release the transaction lock.
152  */
153 void
154 ulock_relLock(struct ubik_trans *atrans)
155 {
156     if (rwlockinit)
157         return;
158
159     if (atrans->locktype == LOCKREAD) {
160         ReleaseReadLock(&rwlock);
161     } else if (atrans->locktype == LOCKWRITE) {
162         ReleaseWriteLock(&rwlock);
163     }
164
165 /*
166  *ubik_print("Ubik: DEBUG: Thread 0x%x %s unlock\n", lwp_cpptr,
167  *           ((atrans->locktype == LOCKREAD) ? "READ" : "WRITE"));
168  */
169
170     atrans->locktype = 0;
171     return;
172 }
173
174 /*!
175  * \brief debugging hooks
176  */
177 void
178 ulock_Debug(struct ubik_debug *aparm)
179 {
180     if (rwlockinit) {
181         aparm->anyReadLocks = 0;
182         aparm->anyWriteLocks = 0;
183     } else {
184         aparm->anyReadLocks = rwlock.readers_reading;
185         aparm->anyWriteLocks = ((rwlock.excl_locked == WRITE_LOCK) ? 1 : 0);
186     }
187 }