]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_lock.c
0ca1530b04dd24becb8270834dc7ffa0e414ede7
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
26 * The Regents of the University of California. All rights reserved.
28 * This code contains ideas from software contributed to Berkeley by
29 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
30 * System project at Carnegie-Mellon University.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
63 #include <sys/param.h>
64 #include <sys/proc_internal.h>
66 #include <kern/cpu_number.h>
67 #include <kern/thread.h>
69 #include <mach/mach_types.h>
72 * Locking primitives implementation.
73 * Locks provide shared/exclusive sychronization.
77 #define COUNT(p, x) if (p) (p)->p_locks += (x)
85 * For multiprocessor system, try spin lock first.
87 * This should be inline expanded below, but we cannot have #if
88 * inside a multiline define.
90 int lock_wait_time
= 100;
91 #define PAUSE(lkp, wanted) \
92 if (lock_wait_time > 0) { \
95 for (i = lock_wait_time; i > 0; i--) \
102 #else /* NCPUS == 1 */
105 * It is an error to spin on a uniprocessor as nothing will ever cause
106 * the simple lock to clear while we are executing.
108 #define PAUSE(lkp, wanted)
110 #endif /* NCPUS == 1 */
113 * Acquire a resource.
115 #define ACQUIRE(lkp, error, extflags, wanted) \
116 PAUSE(lkp, wanted); \
117 for (error = 0; wanted; ) { \
118 (lkp)->lk_waitcount++; \
119 error = tsleep((void *)lkp, (lkp)->lk_prio, \
120 (lkp)->lk_wmesg, (lkp)->lk_timo); \
121 (lkp)->lk_waitcount--; \
124 if ((extflags) & LK_SLEEPFAIL) { \
131 * Initialize a lock; required before use.
134 lockinit(lkp
, prio
, wmesg
, timo
, flags
)
135 struct lock__bsd__
*lkp
;
142 bzero(lkp
, sizeof(struct lock__bsd__
));
143 lkp
->lk_flags
= flags
& LK_EXTFLG_MASK
;
146 lkp
->lk_wmesg
= wmesg
;
147 lkp
->lk_lockholder
= LK_NOPROC
;
148 lkp
->lk_lockthread
= 0;
152 * Determine the status of a lock.
156 struct lock__bsd__
*lkp
;
160 if (lkp
->lk_exclusivecount
!= 0)
161 lock_type
= LK_EXCLUSIVE
;
162 else if (lkp
->lk_sharecount
!= 0)
163 lock_type
= LK_SHARED
;
168 * Set, change, or release a lock.
170 * Shared requests increment the shared count. Exclusive requests set the
171 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
172 * accepted shared locks and shared-to-exclusive upgrades to go away.
175 lockmgr(lkp
, flags
, interlkp
, p
)
176 struct lock__bsd__
*lkp
;
186 error
= 0; self
= current_thread();
191 extflags
= (flags
| lkp
->lk_flags
) & LK_EXTFLG_MASK
;
194 * Once a lock has drained, the LK_DRAINING flag is set and an
195 * exclusive lock is returned. The only valid operation thereafter
196 * is a single release of that exclusive lock. This final release
197 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
198 * further requests of any sort will result in a panic. The bits
199 * selected for these two flags are chosen so that they will be set
200 * in memory that is freed (freed memory is filled with 0xdeadbeef).
201 * The final release is permitted to give a new lease on life to
202 * the lock by specifying LK_REENABLE.
204 if (lkp
->lk_flags
& (LK_DRAINING
|LK_DRAINED
)) {
205 if (lkp
->lk_flags
& LK_DRAINED
)
206 panic("lockmgr: using decommissioned lock");
207 if ((flags
& LK_TYPE_MASK
) != LK_RELEASE
||
208 (lkp
->lk_lockholder
!= pid
&& lkp
->lk_lockthread
!= self
)
209 panic("lockmgr: non-release on draining lock: %d\n",
210 flags
& LK_TYPE_MASK
);
211 lkp
->lk_flags
&= ~LK_DRAINING
;
212 if ((flags
& LK_REENABLE
) == 0)
213 lkp
->lk_flags
|= LK_DRAINED
;
217 switch (flags
& LK_TYPE_MASK
) {
220 if (lkp
->lk_lockholder
!= pid
|| lkp
->lk_lockthread
!= self
) {
222 * If just polling, check to see if we will block.
224 if ((extflags
& LK_NOWAIT
) && (lkp
->lk_flags
&
225 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
))) {
230 * Wait for exclusive locks and upgrades to clear.
232 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_flags
&
233 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
));
236 lkp
->lk_sharecount
++;
241 * We hold an exclusive lock, so downgrade it to shared.
242 * An alternative would be to fail with EDEADLK.
244 lkp
->lk_sharecount
++;
246 /* fall into downgrade */
249 if (lkp
->lk_lockholder
!= pid
||
250 lkp
->lk_lockthread
!= self
||
251 lkp
->lk_exclusivecount
== 0)
252 panic("lockmgr: not holding exclusive lock");
253 lkp
->lk_sharecount
+= lkp
->lk_exclusivecount
;
254 lkp
->lk_exclusivecount
= 0;
255 lkp
->lk_flags
&= ~LK_HAVE_EXCL
;
256 lkp
->lk_lockholder
= LK_NOPROC
;
257 lkp
->lk_lockthread
= 0;
258 if (lkp
->lk_waitcount
)
264 * If another process is ahead of us to get an upgrade,
265 * then we want to fail rather than have an intervening
268 if (lkp
->lk_flags
& LK_WANT_UPGRADE
) {
269 lkp
->lk_sharecount
--;
274 /* fall into normal upgrade */
278 * Upgrade a shared lock to an exclusive one. If another
279 * shared lock has already requested an upgrade to an
280 * exclusive lock, our shared lock is released and an
281 * exclusive lock is requested (which will be granted
282 * after the upgrade). If we return an error, the file
283 * will always be unlocked.
285 if ((lkp
->lk_lockholder
== pid
&&
286 lkp
->lk_lockthread
== self
) ||
287 lkp
->lk_sharecount
<= 0)
288 panic("lockmgr: upgrade exclusive lock");
289 lkp
->lk_sharecount
--;
292 * If we are just polling, check to see if we will block.
294 if ((extflags
& LK_NOWAIT
) &&
295 ((lkp
->lk_flags
& LK_WANT_UPGRADE
) ||
296 lkp
->lk_sharecount
> 1)) {
300 if ((lkp
->lk_flags
& LK_WANT_UPGRADE
) == 0) {
302 * We are first shared lock to request an upgrade, so
303 * request upgrade and wait for the shared count to
304 * drop to zero, then take exclusive lock.
306 lkp
->lk_flags
|= LK_WANT_UPGRADE
;
307 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_sharecount
);
308 lkp
->lk_flags
&= ~LK_WANT_UPGRADE
;
311 lkp
->lk_flags
|= LK_HAVE_EXCL
;
312 lkp
->lk_lockholder
= pid
;
313 lkp
->lk_lockthread
= self
;
314 if (lkp
->lk_exclusivecount
!= 0)
315 panic("lockmgr: non-zero exclusive count");
316 lkp
->lk_exclusivecount
= 1;
321 * Someone else has requested upgrade. Release our shared
322 * lock, awaken upgrade requestor if we are the last shared
323 * lock, then request an exclusive lock.
325 if (lkp
->lk_sharecount
== 0 && lkp
->lk_waitcount
)
327 /* fall into exclusive request */
330 if (lkp
->lk_lockholder
== pid
&& lkp
->lk_lockthread
== self
) {
334 if ((extflags
& LK_CANRECURSE
) == 0)
335 panic("lockmgr: locking against myself");
336 lkp
->lk_exclusivecount
++;
341 * If we are just polling, check to see if we will sleep.
343 if ((extflags
& LK_NOWAIT
) && ((lkp
->lk_flags
&
344 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
345 lkp
->lk_sharecount
!= 0)) {
350 * Try to acquire the want_exclusive flag.
352 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_flags
&
353 (LK_HAVE_EXCL
| LK_WANT_EXCL
));
356 lkp
->lk_flags
|= LK_WANT_EXCL
;
358 * Wait for shared locks and upgrades to finish.
360 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_sharecount
!= 0 ||
361 (lkp
->lk_flags
& LK_WANT_UPGRADE
));
362 lkp
->lk_flags
&= ~LK_WANT_EXCL
;
365 lkp
->lk_flags
|= LK_HAVE_EXCL
;
366 lkp
->lk_lockholder
= pid
;
367 lkp
->lk_lockthread
= self
;
368 if (lkp
->lk_exclusivecount
!= 0)
369 panic("lockmgr: non-zero exclusive count");
370 lkp
->lk_exclusivecount
= 1;
375 if (lkp
->lk_exclusivecount
!= 0) {
376 if (pid
!= lkp
->lk_lockholder
||
377 lkp
->lk_lockthread
!= self
)
378 panic("lockmgr: pid %d, thread 0x%8x,"
379 " not exclusive lock holder pid %d"
380 " thread 0x%8x unlocking, exclusive count %d",
381 pid
, self
, lkp
->lk_lockholder
,
382 lkp
->lk_lockthread
, lkp
->lk_exclusivecount
);
383 lkp
->lk_exclusivecount
--;
385 if (lkp
->lk_exclusivecount
== 0) {
386 lkp
->lk_flags
&= ~LK_HAVE_EXCL
;
387 lkp
->lk_lockholder
= LK_NOPROC
;
388 lkp
->lk_lockthread
= 0;
390 } else if (lkp
->lk_sharecount
!= 0) {
391 lkp
->lk_sharecount
--;
394 if (lkp
->lk_waitcount
)
400 * Check that we do not already hold the lock, as it can
401 * never drain if we do. Unfortunately, we have no way to
402 * check for holding a shared lock, but at least we can
403 * check for an exclusive one.
405 if (lkp
->lk_lockholder
== pid
&& lkp
->lk_lockthread
== self
)
406 panic("lockmgr: draining against myself");
408 * If we are just polling, check to see if we will sleep.
410 if ((extflags
& LK_NOWAIT
) && ((lkp
->lk_flags
&
411 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
412 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0)) {
416 PAUSE(lkp
, ((lkp
->lk_flags
&
417 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
418 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0));
419 for (error
= 0; ((lkp
->lk_flags
&
420 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
421 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0); ) {
422 lkp
->lk_flags
|= LK_WAITDRAIN
;
423 if (error
= tsleep((void *)&lkp
->lk_flags
, lkp
->lk_prio
,
424 lkp
->lk_wmesg
, lkp
->lk_timo
))
426 if ((extflags
) & LK_SLEEPFAIL
)
429 lkp
->lk_flags
|= LK_DRAINING
| LK_HAVE_EXCL
;
430 lkp
->lk_lockholder
= pid
;
431 lkp
->lk_lockthread
= self
;
432 lkp
->lk_exclusivecount
= 1;
437 panic("lockmgr: unknown locktype request %d",
438 flags
& LK_TYPE_MASK
);
441 if ((lkp
->lk_flags
& LK_WAITDRAIN
) && ((lkp
->lk_flags
&
442 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) == 0 &&
443 lkp
->lk_sharecount
== 0 && lkp
->lk_waitcount
== 0)) {
444 lkp
->lk_flags
&= ~LK_WAITDRAIN
;
445 wakeup((void *)&lkp
->lk_flags
);
451 * Print out information about state of a lock. Used by VOP_PRINT
452 * routines to display ststus about contained locks.
455 lockmgr_printinfo(lkp
)
456 struct lock__bsd__
*lkp
;
459 if (lkp
->lk_sharecount
)
460 printf(" lock type %s: SHARED (count %d)", lkp
->lk_wmesg
,
462 else if (lkp
->lk_flags
& LK_HAVE_EXCL
)
463 printf(" lock type %s: EXCL (count %d) by pid %d",
464 lkp
->lk_wmesg
, lkp
->lk_exclusivecount
, lkp
->lk_lockholder
);
465 if (lkp
->lk_waitcount
> 0)
466 printf(" with %d pending", lkp
->lk_waitcount
);