]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_lock.c
2 * Copyright (c) 2000 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>
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 simple_unlock(&lkp->lk_interlock); \
95 for (i = lock_wait_time; i > 0; i--) \
98 simple_lock(&lkp->lk_interlock); \
103 #else /* NCPUS == 1 */
106 * It is an error to spin on a uniprocessor as nothing will ever cause
107 * the simple lock to clear while we are executing.
109 #define PAUSE(lkp, wanted)
111 #endif /* NCPUS == 1 */
114 * Acquire a resource.
116 #define ACQUIRE(lkp, error, extflags, wanted) \
117 PAUSE(lkp, wanted); \
118 for (error = 0; wanted; ) { \
119 (lkp)->lk_waitcount++; \
120 simple_unlock(&(lkp)->lk_interlock); \
121 error = tsleep((void *)lkp, (lkp)->lk_prio, \
122 (lkp)->lk_wmesg, (lkp)->lk_timo); \
123 simple_lock(&(lkp)->lk_interlock); \
124 (lkp)->lk_waitcount--; \
127 if ((extflags) & LK_SLEEPFAIL) { \
134 * Initialize a lock; required before use.
137 lockinit(lkp
, prio
, wmesg
, timo
, flags
)
138 struct lock__bsd__
*lkp
;
145 bzero(lkp
, sizeof(struct lock__bsd__
));
146 simple_lock_init(&lkp
->lk_interlock
);
147 lkp
->lk_flags
= flags
& LK_EXTFLG_MASK
;
150 lkp
->lk_wmesg
= wmesg
;
151 lkp
->lk_lockholder
= LK_NOPROC
;
152 lkp
->lk_lockthread
= 0;
156 * Determine the status of a lock.
160 struct lock__bsd__
*lkp
;
164 simple_lock(&lkp
->lk_interlock
);
165 if (lkp
->lk_exclusivecount
!= 0)
166 lock_type
= LK_EXCLUSIVE
;
167 else if (lkp
->lk_sharecount
!= 0)
168 lock_type
= LK_SHARED
;
169 simple_unlock(&lkp
->lk_interlock
);
174 * Set, change, or release a lock.
176 * Shared requests increment the shared count. Exclusive requests set the
177 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
178 * accepted shared locks and shared-to-exclusive upgrades to go away.
181 lockmgr(lkp
, flags
, interlkp
, p
)
182 struct lock__bsd__
*lkp
;
184 simple_lock_t interlkp
;
192 error
= 0; self
= current_thread();
197 simple_lock(&lkp
->lk_interlock
);
198 if (flags
& LK_INTERLOCK
)
199 simple_unlock(interlkp
);
200 extflags
= (flags
| lkp
->lk_flags
) & LK_EXTFLG_MASK
;
203 * Once a lock has drained, the LK_DRAINING flag is set and an
204 * exclusive lock is returned. The only valid operation thereafter
205 * is a single release of that exclusive lock. This final release
206 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
207 * further requests of any sort will result in a panic. The bits
208 * selected for these two flags are chosen so that they will be set
209 * in memory that is freed (freed memory is filled with 0xdeadbeef).
210 * The final release is permitted to give a new lease on life to
211 * the lock by specifying LK_REENABLE.
213 if (lkp
->lk_flags
& (LK_DRAINING
|LK_DRAINED
)) {
214 if (lkp
->lk_flags
& LK_DRAINED
)
215 panic("lockmgr: using decommissioned lock");
216 if ((flags
& LK_TYPE_MASK
) != LK_RELEASE
||
217 (lkp
->lk_lockholder
!= pid
&& lkp
->lk_lockthread
!= self
)
218 panic("lockmgr: non-release on draining lock: %d\n",
219 flags
& LK_TYPE_MASK
);
220 lkp
->lk_flags
&= ~LK_DRAINING
;
221 if ((flags
& LK_REENABLE
) == 0)
222 lkp
->lk_flags
|= LK_DRAINED
;
226 switch (flags
& LK_TYPE_MASK
) {
229 if (lkp
->lk_lockholder
!= pid
|| lkp
->lk_lockthread
!= self
) {
231 * If just polling, check to see if we will block.
233 if ((extflags
& LK_NOWAIT
) && (lkp
->lk_flags
&
234 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
))) {
239 * Wait for exclusive locks and upgrades to clear.
241 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_flags
&
242 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
));
245 lkp
->lk_sharecount
++;
250 * We hold an exclusive lock, so downgrade it to shared.
251 * An alternative would be to fail with EDEADLK.
253 lkp
->lk_sharecount
++;
255 /* fall into downgrade */
258 if (lkp
->lk_lockholder
!= pid
||
259 lkp
->lk_lockthread
!= self
||
260 lkp
->lk_exclusivecount
== 0)
261 panic("lockmgr: not holding exclusive lock");
262 lkp
->lk_sharecount
+= lkp
->lk_exclusivecount
;
263 lkp
->lk_exclusivecount
= 0;
264 lkp
->lk_flags
&= ~LK_HAVE_EXCL
;
265 lkp
->lk_lockholder
= LK_NOPROC
;
266 lkp
->lk_lockthread
= 0;
267 if (lkp
->lk_waitcount
)
273 * If another process is ahead of us to get an upgrade,
274 * then we want to fail rather than have an intervening
277 if (lkp
->lk_flags
& LK_WANT_UPGRADE
) {
278 lkp
->lk_sharecount
--;
283 /* fall into normal upgrade */
287 * Upgrade a shared lock to an exclusive one. If another
288 * shared lock has already requested an upgrade to an
289 * exclusive lock, our shared lock is released and an
290 * exclusive lock is requested (which will be granted
291 * after the upgrade). If we return an error, the file
292 * will always be unlocked.
294 if ((lkp
->lk_lockholder
== pid
&&
295 lkp
->lk_lockthread
== self
) ||
296 lkp
->lk_sharecount
<= 0)
297 panic("lockmgr: upgrade exclusive lock");
298 lkp
->lk_sharecount
--;
301 * If we are just polling, check to see if we will block.
303 if ((extflags
& LK_NOWAIT
) &&
304 ((lkp
->lk_flags
& LK_WANT_UPGRADE
) ||
305 lkp
->lk_sharecount
> 1)) {
309 if ((lkp
->lk_flags
& LK_WANT_UPGRADE
) == 0) {
311 * We are first shared lock to request an upgrade, so
312 * request upgrade and wait for the shared count to
313 * drop to zero, then take exclusive lock.
315 lkp
->lk_flags
|= LK_WANT_UPGRADE
;
316 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_sharecount
);
317 lkp
->lk_flags
&= ~LK_WANT_UPGRADE
;
320 lkp
->lk_flags
|= LK_HAVE_EXCL
;
321 lkp
->lk_lockholder
= pid
;
322 lkp
->lk_lockthread
= self
;
323 if (lkp
->lk_exclusivecount
!= 0)
324 panic("lockmgr: non-zero exclusive count");
325 lkp
->lk_exclusivecount
= 1;
330 * Someone else has requested upgrade. Release our shared
331 * lock, awaken upgrade requestor if we are the last shared
332 * lock, then request an exclusive lock.
334 if (lkp
->lk_sharecount
== 0 && lkp
->lk_waitcount
)
336 /* fall into exclusive request */
339 if (lkp
->lk_lockholder
== pid
&& lkp
->lk_lockthread
== self
) {
343 if ((extflags
& LK_CANRECURSE
) == 0)
344 panic("lockmgr: locking against myself");
345 lkp
->lk_exclusivecount
++;
350 * If we are just polling, check to see if we will sleep.
352 if ((extflags
& LK_NOWAIT
) && ((lkp
->lk_flags
&
353 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
354 lkp
->lk_sharecount
!= 0)) {
359 * Try to acquire the want_exclusive flag.
361 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_flags
&
362 (LK_HAVE_EXCL
| LK_WANT_EXCL
));
365 lkp
->lk_flags
|= LK_WANT_EXCL
;
367 * Wait for shared locks and upgrades to finish.
369 ACQUIRE(lkp
, error
, extflags
, lkp
->lk_sharecount
!= 0 ||
370 (lkp
->lk_flags
& LK_WANT_UPGRADE
));
371 lkp
->lk_flags
&= ~LK_WANT_EXCL
;
374 lkp
->lk_flags
|= LK_HAVE_EXCL
;
375 lkp
->lk_lockholder
= pid
;
376 lkp
->lk_lockthread
= self
;
377 if (lkp
->lk_exclusivecount
!= 0)
378 panic("lockmgr: non-zero exclusive count");
379 lkp
->lk_exclusivecount
= 1;
384 if (lkp
->lk_exclusivecount
!= 0) {
385 if (pid
!= lkp
->lk_lockholder
||
386 lkp
->lk_lockthread
!= self
)
387 panic("lockmgr: pid %d, not %s %d unlocking",
388 pid
, "exclusive lock holder",
390 lkp
->lk_exclusivecount
--;
392 if (lkp
->lk_exclusivecount
== 0) {
393 lkp
->lk_flags
&= ~LK_HAVE_EXCL
;
394 lkp
->lk_lockholder
= LK_NOPROC
;
395 lkp
->lk_lockthread
= 0;
397 } else if (lkp
->lk_sharecount
!= 0) {
398 lkp
->lk_sharecount
--;
401 if (lkp
->lk_waitcount
)
407 * Check that we do not already hold the lock, as it can
408 * never drain if we do. Unfortunately, we have no way to
409 * check for holding a shared lock, but at least we can
410 * check for an exclusive one.
412 if (lkp
->lk_lockholder
== pid
&& lkp
->lk_lockthread
== self
)
413 panic("lockmgr: draining against myself");
415 * If we are just polling, check to see if we will sleep.
417 if ((extflags
& LK_NOWAIT
) && ((lkp
->lk_flags
&
418 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
419 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0)) {
423 PAUSE(lkp
, ((lkp
->lk_flags
&
424 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
425 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0));
426 for (error
= 0; ((lkp
->lk_flags
&
427 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) ||
428 lkp
->lk_sharecount
!= 0 || lkp
->lk_waitcount
!= 0); ) {
429 lkp
->lk_flags
|= LK_WAITDRAIN
;
430 simple_unlock(&lkp
->lk_interlock
);
431 if (error
= tsleep((void *)&lkp
->lk_flags
, lkp
->lk_prio
,
432 lkp
->lk_wmesg
, lkp
->lk_timo
))
434 if ((extflags
) & LK_SLEEPFAIL
)
436 simple_lock(&lkp
->lk_interlock
);
438 lkp
->lk_flags
|= LK_DRAINING
| LK_HAVE_EXCL
;
439 lkp
->lk_lockholder
= pid
;
440 lkp
->lk_lockthread
= self
;
441 lkp
->lk_exclusivecount
= 1;
446 simple_unlock(&lkp
->lk_interlock
);
447 panic("lockmgr: unknown locktype request %d",
448 flags
& LK_TYPE_MASK
);
451 if ((lkp
->lk_flags
& LK_WAITDRAIN
) && ((lkp
->lk_flags
&
452 (LK_HAVE_EXCL
| LK_WANT_EXCL
| LK_WANT_UPGRADE
)) == 0 &&
453 lkp
->lk_sharecount
== 0 && lkp
->lk_waitcount
== 0)) {
454 lkp
->lk_flags
&= ~LK_WAITDRAIN
;
455 wakeup((void *)&lkp
->lk_flags
);
457 simple_unlock(&lkp
->lk_interlock
);
462 * Print out information about state of a lock. Used by VOP_PRINT
463 * routines to display ststus about contained locks.
465 lockmgr_printinfo(lkp
)
466 struct lock__bsd__
*lkp
;
469 if (lkp
->lk_sharecount
)
470 printf(" lock type %s: SHARED (count %d)", lkp
->lk_wmesg
,
472 else if (lkp
->lk_flags
& LK_HAVE_EXCL
)
473 printf(" lock type %s: EXCL (count %d) by pid %d",
474 lkp
->lk_wmesg
, lkp
->lk_exclusivecount
, lkp
->lk_lockholder
);
475 if (lkp
->lk_waitcount
> 0)
476 printf(" with %d pending", lkp
->lk_waitcount
);