5 ****************************************************************************
6 * Copyright IBM Corporation 1988, 1989 - All Rights Reserved *
8 * Permission to use, copy, modify, and distribute this software and its *
9 * documentation for any purpose and without fee is hereby granted, *
10 * provided that the above copyright notice appear in all copies and *
11 * that both that copyright notice and this permission notice appear in *
12 * supporting documentation, and that the name of IBM not be used in *
13 * advertising or publicity pertaining to distribution of the software *
14 * without specific, written prior permission. *
16 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY *
19 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER *
20 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *
21 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
22 ****************************************************************************
27 /*******************************************************************\
29 * Information Technology Center *
30 * Carnegie-Mellon University *
34 \*******************************************************************/
37 Include file for using Vice locking routines.
40 /* The following macros allow multi statement macros to be defined safely, i.e.
41 - the multi statement macro can be the object of an if statement;
42 - the call to the multi statement macro may be legally followed by a semi-colon.
43 BEGINMAC and ENDMAC have been tested with both the portable C compiler and
44 Hi-C. Both compilers were from the Palo Alto 4.2BSD software releases, and
45 both optimized out the constant loop code. For an example of the use
46 of BEGINMAC and ENDMAC, see the definition for ReleaseWriteLock, below.
47 An alternative to this, using "if(1)" for BEGINMAC is not used because it
48 may generate worse code with pcc, and may generate warning messages with hi-C.
52 #define ENDMAC } while (0)
55 * all locks wait on excl_locked except for READ_LOCK, which waits on
60 unsigned char wait_states
; /* type of lockers waiting */
61 unsigned char excl_locked
; /* anyone have boosted, shared or write lock? */
62 unsigned char readers_reading
;/* # readers actually with read locks */
63 unsigned char num_waiting
; /* probably need this soon */
64 int thread_index
; /* thread that have a exclusive lock */
68 #endif /* LOCK_TRACE */
73 void Lock_ReleaseR(struct Lock
*);
74 void Lock_ReleaseW(struct Lock
*);
75 void Lock_Obtain(struct Lock
*, int);
76 void Lock_Init(struct Lock
*);
81 /* this next is not a flag, but rather a parameter to Lock_Obtain */
82 #define BOOSTED_LOCK 6
84 /* next defines wait_states for which we wait on excl_locked */
85 #define EXCL_LOCKS (WRITE_LOCK|SHARED_LOCK)
88 #define DEBUGWRITE(message,lock) do { \
89 fprintf(stderr,"th#%d ", LWP_Index()); \
90 fprintf(stderr,message,lock,__FILE__,__LINE__); } while (0)
91 #define DEBUGWRITE_4(message,lock1,how1,lock2,how2) do { \
92 fprintf(stderr,"th#%d ", LWP_Index()); \
93 fprintf(stderr,message,lock1,how1,lock2,how2,__FILE__,__LINE__); } while (0)
95 #define DEBUGWRITE(message,lock) do { ; } while (0)
96 #define DEBUGWRITE_4(message,lock1,how1,lock2,how2) do { ; } while (0)
100 #define StoreFileLine(lock, f, l) \
103 #define GetTraceFile(lock) (lock)->file
104 #define GetTraceLine(lock) (lock)->line
106 #define StoreFileLine(lock, f, l)
107 #define GetTraceFile(lock) "no_lock_trace"
108 #define GetTraceLine(lock) 0
111 #define LOCK_CHECK_OWNER 1
113 #ifdef LOCK_CHECK_OWNER
114 #define AssertNotLockOwner(lock,owner) do { \
115 if ((lock)->thread_index == (owner)) { \
117 "AssertNotLockOwner failed on %s:%d with lock %p, " \
118 "owned by %d, locked at (%s:%d)\n", \
119 __FILE__, __LINE__, \
121 GetTraceFile(lock), GetTraceLine(lock)); \
126 #define AssertLockOwner(lock,owner) do { \
127 if ((lock)->thread_index != (owner)) { \
129 "AssertLockOwner failed on %s:%d with lock %p, " \
130 "should be owned by %d, " \
131 "was owned by %d, locked at (%s:%d)\n", \
132 __FILE__, __LINE__, \
133 (lock), (owner), (lock)->thread_index, \
134 GetTraceFile(lock), GetTraceLine(lock)); \
139 #else /* !LOCK_CHECK_OWNER */
141 #define AssertNotLockOwner(lock,owner) do { ; } while (0)
142 #define AssertLockOwner(lock,owner) do { ; } while (0)
144 #endif /* LOCK_CHECK_OWNER */
146 #define AssertExclLocked(lock) \
148 AssertLockOwner(lock,LWP_Index()); \
151 #define AssertNotExclLocked(lock) \
153 AssertLockOwner(lock,LWP_INVALIDTHREADID); \
156 #define ObtainReadLock(lock) \
158 DEBUGWRITE("ObtainReadLock: %p at %s:%d starting\n",lock); \
159 AssertNotLockOwner(lock,LWP_Index()); \
160 RealObtainReadLock(lock) \
161 AssertLockOwner(lock,LWP_INVALIDTHREADID); \
162 StoreFileLine(lock, __FILE__, __LINE__) \
163 DEBUGWRITE("ObtainReadLock: %p at %s:%d got it\n",lock);\
166 #define ObtainWriteLock(lock) \
168 DEBUGWRITE("ObtainWriteLock: %p at %s:%d starting\n",lock); \
169 AssertNotLockOwner(lock,LWP_Index()); \
170 RealObtainWriteLock(lock) \
171 AssertLockOwner(lock,LWP_Index()); \
172 StoreFileLine(lock, __FILE__, __LINE__) \
173 DEBUGWRITE("ObtainWriteLock: %p at %s:%d got it\n",lock);\
176 #define ObtainSharedLock(lock) \
178 DEBUGWRITE("ObtainSharedLock: %p at %s:%d starting\n",lock); \
179 AssertNotLockOwner(lock,LWP_Index()); \
180 RealObtainSharedLock(lock) \
181 StoreFileLine(lock, __FILE__, __LINE__) \
182 DEBUGWRITE("ObtainSharedLock: %p at %s:%d got it\n",lock);\
185 #define BoostSharedLock(lock) \
187 DEBUGWRITE("BoostSharedLock: %p at %s:%d starting\n",lock); \
188 AssertNotLockOwner(lock,LWP_Index()); \
189 RealBoostSharedLock(lock) \
190 AssertLockOwner(lock,LWP_Index()); \
191 StoreFileLine(lock, __FILE__, __LINE__) \
192 DEBUGWRITE("BoostSharedLock: %p at %s:%d got it\n",lock);\
195 #define UnBoostSharedLock(lock) \
197 DEBUGWRITE("UnBoostSharedLock: %p at %s:%d starting\n",lock); \
198 AssertNotLockOwner(lock,LWP_Index()); \
199 RealUnboostSharedLock(lock) \
200 AssertLockOwner(lock,LWP_INVALIDTHREADID); \
201 StoreFileLine(lock, __FILE__, __LINE__) \
202 DEBUGWRITE("UnBoostSharedLock: %p at %s:%d got it\n",lock);\
205 #define ReleaseReadLock(lock) \
207 DEBUGWRITE("ReleaseReadLock: %p at %s:%d\n",lock);\
208 AssertNotLockOwner(lock,LWP_Index()); \
209 RealReleaseReadLock(lock) \
210 AssertLockOwner(lock,LWP_INVALIDTHREADID); \
213 #define ReleaseWriteLock(lock) \
215 DEBUGWRITE("ReleaseWriteLock: %p at %s:%d\n",lock);\
216 AssertLockOwner(lock,LWP_Index()); \
217 RealReleaseWriteLock(lock) \
218 AssertLockOwner(lock,LWP_INVALIDTHREADID); \
221 #define ReleaseSharedLock(lock) \
223 DEBUGWRITE("ReleaseSharedLock: %p at %s:%d\n",lock);\
224 RealReleaseSharedLock(lock) \
225 AssertLockOwner(lock,LWP_INVALIDTHREADID); \
228 #define RealObtainReadLock(lock) \
229 if (!((lock)->excl_locked & WRITE_LOCK) && !(lock)->wait_states)\
230 (lock) -> readers_reading++;\
232 Lock_Obtain(lock, READ_LOCK);
234 #define RealObtainWriteLock(lock)\
235 if (!(lock)->excl_locked && !(lock)->readers_reading) \
236 (lock) -> excl_locked = WRITE_LOCK;\
238 Lock_Obtain(lock, WRITE_LOCK); \
239 (lock) -> thread_index = LWP_Index();
242 #define RealObtainSharedLock(lock)\
243 if (!(lock)->excl_locked && !(lock)->wait_states)\
244 (lock) -> excl_locked = SHARED_LOCK;\
246 Lock_Obtain(lock, SHARED_LOCK);
248 #define RealBoostSharedLock(lock)\
249 if (!(lock)->readers_reading)\
250 (lock)->excl_locked = WRITE_LOCK;\
252 Lock_Obtain(lock, BOOSTED_LOCK); \
253 (lock) -> thread_index = LWP_Index();
255 /* this must only be called with a WRITE or boosted SHARED lock! */
256 #define RealUnboostSharedLock(lock)\
257 (lock) -> excl_locked = SHARED_LOCK; \
258 (lock) -> thread_index = LWP_INVALIDTHREADID; \
259 if((lock)->wait_states) \
262 #define RealReleaseReadLock(lock)\
263 if (!--(lock)->readers_reading && (lock)->wait_states)\
264 Lock_ReleaseW(lock) ;
267 #define RealReleaseWriteLock(lock)\
268 (lock) -> thread_index = LWP_INVALIDTHREADID; \
269 (lock)->excl_locked &= ~WRITE_LOCK;\
270 if ((lock)->wait_states) Lock_ReleaseR(lock);
272 /* can be used on shared or boosted (write) locks */
273 #define RealReleaseSharedLock(lock)\
274 (lock) -> thread_index = LWP_INVALIDTHREADID; \
275 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
276 if ((lock)->wait_states) Lock_ReleaseR(lock);
278 /* I added this next macro to make sure it is safe to nuke a lock -- Mike K. */
279 #define LockWaiters(lock)\
280 ((int) ((lock)->num_waiting))
282 #define CheckLock(lock)\
283 ((lock)->excl_locked? (int) -1 : (int) (lock)->readers_reading)
285 #define WriteLocked(lock)\
286 ((lock)->excl_locked & WRITE_LOCK)
288 void LWP_WaitProcessR(char *addr
, struct Lock
*alock
);
289 void LWP_WaitProcessW(char *addr
, struct Lock
*alock
);
290 void LWP_WaitProcessS(char *addr
, struct Lock
*alock
);
292 /* This attempts to obtain two locks in a secure fashion */
294 #define ObtainTwoLocks(lock1, how1, lock2, how2) \
296 DEBUGWRITE_4("ObtainTwoLocks: %p(%d) %p(%d) at %s:%d\n",lock1,how1,lock2,how2);\
297 _ObtainTwoLocks(lock1, how1, lock2, how2);\
301 _ObtainTwoLocks(struct Lock
*lock1
, int how1
,
302 struct Lock
*lock2
, int how2
);