]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_lock.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
25 * The Regents of the University of California. All rights reserved.
27 * This code contains ideas from software contributed to Berkeley by
28 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
29 * System project at Carnegie-Mellon University.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
62 #include <sys/param.h>
63 #include <sys/proc_internal.h>
65 #include <kern/cpu_number.h>
66 #include <kern/thread.h>
68 #include <mach/mach_types.h>
71 * Locking primitives implementation.
72 * Locks provide shared/exclusive sychronization.
76 #define COUNT(p, x) if (p) (p)->p_locks += (x)
84 * For multiprocessor system, try spin lock first.
86 * This should be inline expanded below, but we cannot have #if
87 * inside a multiline define.
89 int lock_wait_time
= 100;
90 #define PAUSE(lkp, wanted) \
91 if (lock_wait_time > 0) { \
94 for (i = lock_wait_time; i > 0; i--) \
101 #else /* NCPUS == 1 */
104 * It is an error to spin on a uniprocessor as nothing will ever cause
105 * the simple lock to clear while we are executing.
107 #define PAUSE(lkp, wanted)
109 #endif /* NCPUS == 1 */
112 * Acquire a resource.
114 #define ACQUIRE(lkp, error, extflags, wanted) \
115 PAUSE(lkp, wanted); \
116 for (error = 0; wanted; ) { \
117 (lkp)->lk_waitcount++; \
118 error = tsleep((void *)lkp, (lkp)->lk_prio, \
119 (lkp)->lk_wmesg, (lkp)->lk_timo); \
120 (lkp)->lk_waitcount--; \
123 if ((extflags) & LK_SLEEPFAIL) { \
130 * Initialize a lock; required before use.
133 lockinit(lkp
, prio
, wmesg
, timo
, flags
)
134 struct lock__bsd__
*lkp
;
141 bzero(lkp
, sizeof(struct lock__bsd__
));
142 lkp
->lk_flags
= flags
& LK_EXTFLG_MASK
;
145 lkp
->lk_wmesg
= wmesg
;
146 lkp
->lk_lockholder
= LK_NOPROC
;
147 lkp
->lk_lockthread
= 0;
151 * Determine the status of a lock.
155 struct lock__bsd__
*lkp
;
159 if (lkp
->lk_exclusivecount
!= 0)
160 lock_type
= LK_EXCLUSIVE
;
161 else if (lkp
->lk_sharecount
!= 0)
162 lock_type
= LK_SHARED
;
167 * Set, change, or release a lock.
169 * Shared requests increment the shared count. Exclusive requests set the
170 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
171 * accepted shared locks and shared-to-exclusive upgrades to go away.
174 lockmgr(lkp
, flags
, interlkp
, p
)
175 struct lock__bsd__
*lkp
;
185 error
= 0; self
= current_thread();
190 extflags
= (flags
| lkp
->lk_flags
) & LK_EXTFLG_MASK
;
193 * Once a lock has drained, the LK_DRAINING flag is set and an
194 * exclusive lock is returned. The only valid operation thereafter
195 * is a single release of that exclusive lock. This final release
196 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
197 * further requests of any sort will result in a panic. The bits
198 * selected for these two flags are chosen so that they will be set
199 * in memory that is freed (freed memory is filled with 0xdeadbeef).
200 * The final release is permitted to give a new lease on life to
201 * the lock by specifying LK_REENABLE.
203 if (lkp
->lk_flags
& (LK_DRAINING
|LK_DRAINED
)) {
204 if (lkp
->lk_flags
& LK_DRAINED
)
205 panic("lockmgr: using decommissioned lock");
206 if ((flags
& LK_TYPE_MASK
) != LK_RELEASE
||
207 (lkp
->lk_lockholder
!= pid
&& lkp
->lk_lockthread
!= self
)
208 panic("lockmgr: non-release on draining lock: %d\n",
209 flags
& LK_TYPE_MASK
);
210 lkp
->lk_flags
&= ~LK_DRAINING
;
211 if ((flags
& LK_REENABLE
) == 0)
212 lkp
->lk_flags
|= LK_DRAINED
;
216 switch (flags
& LK_TYPE_MASK
) {
219 if (lkp
->lk_lockholder
!= pid
|| lkp
->lk_lockthread
!= self
) {
221 * If just polling, check to see if we will block.
223 if ((extflags
& LK_NOWAIT
) && (lkp
->lk_flags
&
224 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
))) {
229 * Wait for exclusive locks and upgrades to clear.
231 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_flags
&
232 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
));
235 lkp
->lk_sharecount
++;
240 * We hold an exclusive lock, so downgrade it to shared.
241 * An alternative would be to fail with EDEADLK.
243 lkp
->lk_sharecount
++;
245 /* fall into downgrade */
248 if (lkp
->lk_lockholder
!= pid
||
249 lkp
->lk_lockthread
!= self
||
250 lkp
->lk_exclusivecount
== 0)
251 panic("lockmgr: not holding exclusive lock");
252 lkp
->lk_sharecount
+= lkp
->lk_exclusivecount
;
253 lkp
->lk_exclusivecount
= 0;
254 lkp
->lk_flags
&= ~LK_HAVE_EXCL
;
255 lkp
->lk_lockholder
= LK_NOPROC
;
256 lkp
->lk_lockthread
= 0;
257 if (lkp
->lk_waitcount
)
263 * If another process is ahead of us to get an upgrade,
264 * then we want to fail rather than have an intervening
267 if (lkp
->lk_flags
& LK_WANT_UPGRADE
) {
268 lkp
->lk_sharecount
--;
273 /* fall into normal upgrade */
277 * Upgrade a shared lock to an exclusive one. If another
278 * shared lock has already requested an upgrade to an
279 * exclusive lock, our shared lock is released and an
280 * exclusive lock is requested (which will be granted
281 * after the upgrade). If we return an error, the file
282 * will always be unlocked.
284 if ((lkp
->lk_lockholder
== pid
&&
285 lkp
->lk_lockthread
== self
) ||
286 lkp
->lk_sharecount
<= 0)
287 panic("lockmgr: upgrade exclusive lock");
288 lkp
->lk_sharecount
--;
291 * If we are just polling, check to see if we will block.
293 if ((extflags
& LK_NOWAIT
) &&
294 ((lkp
->lk_flags
& LK_WANT_UPGRADE
) ||
295 lkp
->lk_sharecount
> 1)) {
299 if ((lkp
->lk_flags
& LK_WANT_UPGRADE
) == 0) {
301 * We are first shared lock to request an upgrade, so
302 * request upgrade and wait for the shared count to
303 * drop to zero, then take exclusive lock.
305 lkp
->lk_flags
|= LK_WANT_UPGRADE
;
306 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_sharecount
);
307 lkp
->lk_flags
&= ~LK_WANT_UPGRADE
;
310 lkp
->lk_flags
|= LK_HAVE_EXCL
;
311 lkp
->lk_lockholder
= pid
;
312 lkp
->lk_lockthread
= self
;
313 if (lkp
->lk_exclusivecount
!= 0)
314 panic("lockmgr: non-zero exclusive count");
315 lkp
->lk_exclusivecount
= 1;
320 * Someone else has requested upgrade. Release our shared
321 * lock, awaken upgrade requestor if we are the last shared
322 * lock, then request an exclusive lock.
324 if (lkp
->lk_sharecount
== 0 && lkp
->lk_waitcount
)
326 /* fall into exclusive request */
329 if (lkp
->lk_lockholder
== pid
&& lkp
->lk_lockthread
== self
) {
333 if ((extflags
& LK_CANRECURSE
) == 0)
334 panic("lockmgr: locking against myself");
335 lkp
->lk_exclusivecount
++;
340 * If we are just polling, check to see if we will sleep.
342 if ((extflags
& LK_NOWAIT
) && ((lkp
->lk_flags
&
343 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
344 lkp
->lk_sharecount
!= 0)) {
349 * Try to acquire the want_exclusive flag.
351 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_flags
&
352 (LK_HAVE_EXCL
| LK_WANT_EXCL
));
355 lkp
->lk_flags
|= LK_WANT_EXCL
;
357 * Wait for shared locks and upgrades to finish.
359 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_sharecount
!= 0 ||
360 (lkp
->lk_flags
& LK_WANT_UPGRADE
));
361 lkp
->lk_flags
&= ~LK_WANT_EXCL
;
364 lkp
->lk_flags
|= LK_HAVE_EXCL
;
365 lkp
->lk_lockholder
= pid
;
366 lkp
->lk_lockthread
= self
;
367 if (lkp
->lk_exclusivecount
!= 0)
368 panic("lockmgr: non-zero exclusive count");
369 lkp
->lk_exclusivecount
= 1;
374 if (lkp
->lk_exclusivecount
!= 0) {
375 if (pid
!= lkp
->lk_lockholder
||
376 lkp
->lk_lockthread
!= self
)
377 panic("lockmgr: pid %d, thread 0x%8x,"
378 " not exclusive lock holder pid %d"
379 " thread 0x%8x unlocking, exclusive count %d",
380 pid
, self
, lkp
->lk_lockholder
,
381 lkp
->lk_lockthread
, lkp
->lk_exclusivecount
);
382 lkp
->lk_exclusivecount
--;
384 if (lkp
->lk_exclusivecount
== 0) {
385 lkp
->lk_flags
&= ~LK_HAVE_EXCL
;
386 lkp
->lk_lockholder
= LK_NOPROC
;
387 lkp
->lk_lockthread
= 0;
389 } else if (lkp
->lk_sharecount
!= 0) {
390 lkp
->lk_sharecount
--;
393 if (lkp
->lk_waitcount
)
399 * Check that we do not already hold the lock, as it can
400 * never drain if we do. Unfortunately, we have no way to
401 * check for holding a shared lock, but at least we can
402 * check for an exclusive one.
404 if (lkp
->lk_lockholder
== pid
&& lkp
->lk_lockthread
== self
)
405 panic("lockmgr: draining against myself");
407 * If we are just polling, check to see if we will sleep.
409 if ((extflags
& LK_NOWAIT
) && ((lkp
->lk_flags
&
410 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
411 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0)) {
415 PAUSE(lkp
, ((lkp
->lk_flags
&
416 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
417 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0));
418 for (error
= 0; ((lkp
->lk_flags
&
419 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
420 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0); ) {
421 lkp
->lk_flags
|= LK_WAITDRAIN
;
422 if (error
= tsleep((void *)&lkp
->lk_flags
, lkp
->lk_prio
,
423 lkp
->lk_wmesg
, lkp
->lk_timo
))
425 if ((extflags
) & LK_SLEEPFAIL
)
428 lkp
->lk_flags
|= LK_DRAINING
| LK_HAVE_EXCL
;
429 lkp
->lk_lockholder
= pid
;
430 lkp
->lk_lockthread
= self
;
431 lkp
->lk_exclusivecount
= 1;
436 panic("lockmgr: unknown locktype request %d",
437 flags
& LK_TYPE_MASK
);
440 if ((lkp
->lk_flags
& LK_WAITDRAIN
) && ((lkp
->lk_flags
&
441 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) == 0 &&
442 lkp
->lk_sharecount
== 0 && lkp
->lk_waitcount
== 0)) {
443 lkp
->lk_flags
&= ~LK_WAITDRAIN
;
444 wakeup((void *)&lkp
->lk_flags
);
450 * Print out information about state of a lock. Used by VOP_PRINT
451 * routines to display ststus about contained locks.
454 lockmgr_printinfo(lkp
)
455 struct lock__bsd__
*lkp
;
458 if (lkp
->lk_sharecount
)
459 printf(" lock type %s: SHARED (count %d)", lkp
->lk_wmesg
,
461 else if (lkp
->lk_flags
& LK_HAVE_EXCL
)
462 printf(" lock type %s: EXCL (count %d) by pid %d",
463 lkp
->lk_wmesg
, lkp
->lk_exclusivecount
, lkp
->lk_lockholder
);
464 if (lkp
->lk_waitcount
> 0)
465 printf(" with %d pending", lkp
->lk_waitcount
);