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@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
52 * Author: Avadis Tevanian, Jr., Michael Wayne Young
55 * Locking primitives implementation
59 #include <mach_ldebug.h>
61 #include <kern/kalloc.h>
62 #include <kern/lock.h>
63 #include <kern/locks.h>
64 #include <kern/misc_protos.h>
65 #include <kern/thread.h>
66 #include <kern/processor.h>
67 #include <kern/sched_prim.h>
69 #include <kern/debug.h>
73 #include <ddb/db_command.h>
74 #include <ddb/db_output.h>
75 #include <ddb/db_sym.h>
76 #include <ddb/db_print.h>
80 #include <ppc/Firmware.h>
83 #include <sys/kdebug.h>
85 #define LCK_RW_LCK_EXCLUSIVE_CODE 0x100
86 #define LCK_RW_LCK_EXCLUSIVE1_CODE 0x101
87 #define LCK_RW_LCK_SHARED_CODE 0x102
88 #define LCK_RW_LCK_SH_TO_EX_CODE 0x103
89 #define LCK_RW_LCK_SH_TO_EX1_CODE 0x104
90 #define LCK_RW_LCK_EX_TO_SH_CODE 0x105
93 #define ANY_LOCK_DEBUG (USLOCK_DEBUG || LOCK_DEBUG || MUTEX_DEBUG)
95 unsigned int lock_wait_time
[2] = { (unsigned int)-1, 0 } ;
102 * Perform simple lock checks.
104 int uslock_check
= 1;
105 int max_lock_loops
= 100000000;
106 decl_simple_lock_data(extern , printf_lock
)
107 decl_simple_lock_data(extern , panic_lock
)
109 decl_simple_lock_data(extern , kdb_lock
)
110 #endif /* MACH_KDB */
111 #endif /* USLOCK_DEBUG */
115 * We often want to know the addresses of the callers
116 * of the various lock routines. However, this information
117 * is only used for debugging and statistics.
120 #define INVALID_PC ((void *) VM_MAX_KERNEL_ADDRESS)
121 #define INVALID_THREAD ((void *) VM_MAX_KERNEL_ADDRESS)
123 #define OBTAIN_PC(pc,l) ((pc) = (void *) GET_RETURN_PC(&(l)))
124 #else /* ANY_LOCK_DEBUG */
127 * Eliminate lint complaints about unused local pc variables.
129 #define OBTAIN_PC(pc,l) ++pc
131 #define OBTAIN_PC(pc,l)
133 #endif /* USLOCK_DEBUG */
137 * Portable lock package implementation of usimple_locks.
141 #define USLDBG(stmt) stmt
142 void usld_lock_init(usimple_lock_t
, unsigned short);
143 void usld_lock_pre(usimple_lock_t
, pc_t
);
144 void usld_lock_post(usimple_lock_t
, pc_t
);
145 void usld_unlock(usimple_lock_t
, pc_t
);
146 void usld_lock_try_pre(usimple_lock_t
, pc_t
);
147 void usld_lock_try_post(usimple_lock_t
, pc_t
);
148 int usld_lock_common_checks(usimple_lock_t
, char *);
149 #else /* USLOCK_DEBUG */
151 #endif /* USLOCK_DEBUG */
154 * Routine: lck_spin_alloc_init
162 if ((lck
= (lck_spin_t
*)kalloc(sizeof(lck_spin_t
))) != 0)
163 lck_spin_init(lck
, grp
, attr
);
169 * Routine: lck_spin_free
175 lck_spin_destroy(lck
, grp
);
176 kfree((void *)lck
, sizeof(lck_spin_t
));
180 * Routine: lck_spin_init
186 __unused lck_attr_t
*attr
) {
189 lck_grp_reference(grp
);
190 lck_grp_lckcnt_incr(grp
, LCK_TYPE_SPIN
);
194 * Routine: lck_spin_destroy
200 if (lck
->interlock
== LCK_SPIN_TAG_DESTROYED
)
202 lck
->interlock
= LCK_SPIN_TAG_DESTROYED
;
203 lck_grp_lckcnt_decr(grp
, LCK_TYPE_SPIN
);
204 lck_grp_deallocate(grp
);
208 * Initialize a usimple_lock.
210 * No change in preemption state.
217 #ifndef MACHINE_SIMPLE_LOCK
218 USLDBG(usld_lock_init(l
, tag
));
219 hw_lock_init(&l
->interlock
);
221 simple_lock_init((simple_lock_t
)l
,tag
);
227 * Acquire a usimple_lock.
229 * Returns with preemption disabled. Note
230 * that the hw_lock routines are responsible for
231 * maintaining preemption state.
237 #ifndef MACHINE_SIMPLE_LOCK
242 #endif /* USLOCK_DEBUG */
245 USLDBG(usld_lock_pre(l
, pc
));
247 if(!hw_lock_to(&l
->interlock
, LockTimeOut
)) /* Try to get the lock with a timeout */
248 panic("simple lock deadlock detection - l=0x%08X, cpu=%d, ret=0x%08X", l
, cpu_number(), pc
);
250 USLDBG(usld_lock_post(l
, pc
));
252 simple_lock((simple_lock_t
)l
);
258 * Release a usimple_lock.
260 * Returns with preemption enabled. Note
261 * that the hw_lock routines are responsible for
262 * maintaining preemption state.
268 #ifndef MACHINE_SIMPLE_LOCK
272 USLDBG(usld_unlock(l
, pc
));
274 hw_lock_unlock(&l
->interlock
);
276 simple_unlock_rwmb((simple_lock_t
)l
);
282 * Conditionally acquire a usimple_lock.
284 * On success, returns with preemption disabled.
285 * On failure, returns with preemption in the same state
286 * as when first invoked. Note that the hw_lock routines
287 * are responsible for maintaining preemption state.
289 * XXX No stats are gathered on a miss; I preserved this
290 * behavior from the original assembly-language code, but
291 * doesn't it make sense to log misses? XXX
297 #ifndef MACHINE_SIMPLE_LOCK
299 unsigned int success
;
302 USLDBG(usld_lock_try_pre(l
, pc
));
303 if (success
= hw_lock_try(&l
->interlock
)) {
304 USLDBG(usld_lock_try_post(l
, pc
));
308 return(simple_lock_try((simple_lock_t
)l
));
314 * States of a usimple_lock. The default when initializing
315 * a usimple_lock is setting it up for debug checking.
317 #define USLOCK_CHECKED 0x0001 /* lock is being checked */
318 #define USLOCK_TAKEN 0x0002 /* lock has been taken */
319 #define USLOCK_INIT 0xBAA0 /* lock has been initialized */
320 #define USLOCK_INITIALIZED (USLOCK_INIT|USLOCK_CHECKED)
321 #define USLOCK_CHECKING(l) (uslock_check && \
322 ((l)->debug.state & USLOCK_CHECKED))
325 * Trace activities of a particularly interesting lock.
327 void usl_trace(usimple_lock_t
, int, pc_t
, const char *);
331 * Initialize the debugging information contained
339 if (l
== USIMPLE_LOCK_NULL
)
340 panic("lock initialization: null lock pointer");
341 l
->lock_type
= USLOCK_TAG
;
342 l
->debug
.state
= uslock_check
? USLOCK_INITIALIZED
: 0;
343 l
->debug
.lock_cpu
= l
->debug
.unlock_cpu
= 0;
344 l
->debug
.lock_pc
= l
->debug
.unlock_pc
= INVALID_PC
;
345 l
->debug
.lock_thread
= l
->debug
.unlock_thread
= INVALID_THREAD
;
346 l
->debug
.duration
[0] = l
->debug
.duration
[1] = 0;
347 l
->debug
.unlock_cpu
= l
->debug
.unlock_cpu
= 0;
348 l
->debug
.unlock_pc
= l
->debug
.unlock_pc
= INVALID_PC
;
349 l
->debug
.unlock_thread
= l
->debug
.unlock_thread
= INVALID_THREAD
;
354 * These checks apply to all usimple_locks, not just
355 * those with USLOCK_CHECKED turned on.
358 usld_lock_common_checks(
362 if (l
== USIMPLE_LOCK_NULL
)
363 panic("%s: null lock pointer", caller
);
364 if (l
->lock_type
!= USLOCK_TAG
)
365 panic("%s: 0x%x is not a usimple lock", caller
, (integer_t
) l
);
366 if (!(l
->debug
.state
& USLOCK_INIT
))
367 panic("%s: 0x%x is not an initialized lock",
368 caller
, (integer_t
) l
);
369 return USLOCK_CHECKING(l
);
374 * Debug checks on a usimple_lock just before attempting
383 char *caller
= "usimple_lock";
386 if (!usld_lock_common_checks(l
, caller
))
390 * Note that we have a weird case where we are getting a lock when we are]
391 * in the process of putting the system to sleep. We are running with no
392 * current threads, therefore we can't tell if we are trying to retake a lock
393 * we have or someone on the other processor has it. Therefore we just
394 * ignore this test if the locking thread is 0.
397 if ((l
->debug
.state
& USLOCK_TAKEN
) && l
->debug
.lock_thread
&&
398 l
->debug
.lock_thread
== (void *) current_thread()) {
399 printf("%s: lock 0x%x already locked (at 0x%x) by",
400 caller
, (integer_t
) l
, l
->debug
.lock_pc
);
401 printf(" current thread 0x%x (new attempt at pc 0x%x)\n",
402 l
->debug
.lock_thread
, pc
);
405 mp_disable_preemption();
406 usl_trace(l
, cpu_number(), pc
, caller
);
407 mp_enable_preemption();
412 * Debug checks on a usimple_lock just after acquiring it.
414 * Pre-emption has been disabled at this point,
415 * so we are safe in using cpu_number.
423 char *caller
= "successful usimple_lock";
426 if (!usld_lock_common_checks(l
, caller
))
429 if (!((l
->debug
.state
& ~USLOCK_TAKEN
) == USLOCK_INITIALIZED
))
430 panic("%s: lock 0x%x became uninitialized",
431 caller
, (integer_t
) l
);
432 if ((l
->debug
.state
& USLOCK_TAKEN
))
433 panic("%s: lock 0x%x became TAKEN by someone else",
434 caller
, (integer_t
) l
);
436 mycpu
= cpu_number();
437 l
->debug
.lock_thread
= (void *)current_thread();
438 l
->debug
.state
|= USLOCK_TAKEN
;
439 l
->debug
.lock_pc
= pc
;
440 l
->debug
.lock_cpu
= mycpu
;
442 usl_trace(l
, mycpu
, pc
, caller
);
447 * Debug checks on a usimple_lock just before
448 * releasing it. Note that the caller has not
449 * yet released the hardware lock.
451 * Preemption is still disabled, so there's
452 * no problem using cpu_number.
460 char *caller
= "usimple_unlock";
463 if (!usld_lock_common_checks(l
, caller
))
466 mycpu
= cpu_number();
468 if (!(l
->debug
.state
& USLOCK_TAKEN
))
469 panic("%s: lock 0x%x hasn't been taken",
470 caller
, (integer_t
) l
);
471 if (l
->debug
.lock_thread
!= (void *) current_thread())
472 panic("%s: unlocking lock 0x%x, owned by thread 0x%x",
473 caller
, (integer_t
) l
, l
->debug
.lock_thread
);
474 if (l
->debug
.lock_cpu
!= mycpu
) {
475 printf("%s: unlocking lock 0x%x on cpu 0x%x",
476 caller
, (integer_t
) l
, mycpu
);
477 printf(" (acquired on cpu 0x%x)\n", l
->debug
.lock_cpu
);
480 usl_trace(l
, mycpu
, pc
, caller
);
482 l
->debug
.unlock_thread
= l
->debug
.lock_thread
;
483 l
->debug
.lock_thread
= INVALID_PC
;
484 l
->debug
.state
&= ~USLOCK_TAKEN
;
485 l
->debug
.unlock_pc
= pc
;
486 l
->debug
.unlock_cpu
= mycpu
;
491 * Debug checks on a usimple_lock just before
492 * attempting to acquire it.
494 * Preemption isn't guaranteed to be disabled.
501 char *caller
= "usimple_lock_try";
503 if (!usld_lock_common_checks(l
, caller
))
505 mp_disable_preemption();
506 usl_trace(l
, cpu_number(), pc
, caller
);
507 mp_enable_preemption();
512 * Debug checks on a usimple_lock just after
513 * successfully attempting to acquire it.
515 * Preemption has been disabled by the
516 * lock acquisition attempt, so it's safe
525 char *caller
= "successful usimple_lock_try";
527 if (!usld_lock_common_checks(l
, caller
))
530 if (!((l
->debug
.state
& ~USLOCK_TAKEN
) == USLOCK_INITIALIZED
))
531 panic("%s: lock 0x%x became uninitialized",
532 caller
, (integer_t
) l
);
533 if ((l
->debug
.state
& USLOCK_TAKEN
))
534 panic("%s: lock 0x%x became TAKEN by someone else",
535 caller
, (integer_t
) l
);
537 mycpu
= cpu_number();
538 l
->debug
.lock_thread
= (void *) current_thread();
539 l
->debug
.state
|= USLOCK_TAKEN
;
540 l
->debug
.lock_pc
= pc
;
541 l
->debug
.lock_cpu
= mycpu
;
543 usl_trace(l
, mycpu
, pc
, caller
);
548 * For very special cases, set traced_lock to point to a
549 * specific lock of interest. The result is a series of
550 * XPRs showing lock operations on that lock. The lock_seq
551 * value is used to show the order of those operations.
553 usimple_lock_t traced_lock
;
554 unsigned int lock_seq
;
561 const char * op_name
)
563 if (traced_lock
== l
) {
565 "seq %d, cpu %d, %s @ %x\n",
566 (integer_t
) lock_seq
, (integer_t
) mycpu
,
567 (integer_t
) op_name
, (integer_t
) pc
, 0);
573 #endif /* USLOCK_DEBUG */
576 * The C portion of the shared/exclusive locks package.
583 void lck_rw_lock_exclusive_gen(
586 lck_rw_type_t
lck_rw_done_gen(
590 lck_rw_lock_shared_gen(
594 lck_rw_lock_shared_to_exclusive_gen(
598 lck_rw_lock_exclusive_to_shared_gen(
602 lck_rw_try_lock_exclusive_gen(
606 lck_rw_try_lock_shared_gen(
609 void lck_rw_ext_init(
614 void lck_rw_ext_backtrace(
617 void lck_rw_lock_exclusive_ext(
621 lck_rw_type_t
lck_rw_done_ext(
626 lck_rw_lock_shared_ext(
631 lck_rw_lock_shared_to_exclusive_ext(
636 lck_rw_lock_exclusive_to_shared_ext(
641 lck_rw_try_lock_exclusive_ext(
646 lck_rw_try_lock_shared_ext(
664 * Routine: lock_alloc
666 * Allocate a lock for external users who cannot
667 * hard-code the structure definition into their
669 * For now just use kalloc, but a zone is probably
675 __unused
unsigned short tag
,
676 __unused
unsigned short tag1
)
680 if ((lck
= (lock_t
*)kalloc(sizeof(lock_t
))) != 0)
681 lock_init(lck
, can_sleep
, tag
, tag1
);
688 * Initialize a lock; required before use.
689 * Note that clients declare the "struct lock"
690 * variables and then initialize them, rather
691 * than getting a new one from this module.
697 __unused
unsigned short tag
,
698 __unused
unsigned short tag1
)
701 panic("lock_init: sleep mode must be set to TRUE\n");
703 (void) memset((void *) lck
, 0, sizeof(lock_t
));
705 lck
->lck_rw_deb
.type
= RW_TAG
;
706 lck
->lck_rw_attr
|= (LCK_RW_ATTR_DEBUG
|LCK_RW_ATTR_DIS_THREAD
|LCK_RW_ATTR_DIS_MYLOCK
);
715 * Free a lock allocated for external users.
716 * For now just use kfree, but a zone is probably
723 kfree((void *)lck
, sizeof(lock_t
));
731 lck_rw_lock_exclusive_ext((lck_rw_ext_t
*)lck
, (lck_rw_t
*)lck
);
738 (void)lck_rw_done_ext((lck_rw_ext_t
*)lck
, (lck_rw_t
*)lck
);
745 lck_rw_lock_shared_ext((lck_rw_ext_t
*)lck
, (lck_rw_t
*)lck
);
752 return(lck_rw_lock_shared_to_exclusive_ext((lck_rw_ext_t
*)lck
, (lck_rw_t
*)lck
));
757 register lock_t
*lck
)
759 lck_rw_lock_exclusive_to_shared_ext((lck_rw_ext_t
*)lck
, (lck_rw_t
*)lck
);
764 * Routine: lck_rw_alloc_init
772 if ((lck
= (lck_rw_t
*)kalloc(sizeof(lck_rw_t
))) != 0)
773 lck_rw_init(lck
, grp
, attr
);
779 * Routine: lck_rw_free
785 lck_rw_destroy(lck
, grp
);
786 kfree((void *)lck
, sizeof(lck_rw_t
));
790 * Routine: lck_rw_init
797 lck_rw_ext_t
*lck_ext
;
798 lck_attr_t
*lck_attr
;
800 if (attr
!= LCK_ATTR_NULL
)
803 lck_attr
= &LockDefaultLckAttr
;
805 if ((lck_attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
806 if ((lck_ext
= (lck_rw_ext_t
*)kalloc(sizeof(lck_rw_ext_t
))) != 0) {
807 lck_rw_ext_init(lck_ext
, grp
, lck_attr
);
808 lck
->lck_rw_tag
= LCK_RW_TAG_INDIRECT
;
809 lck
->lck_rw_ptr
= lck_ext
;
812 (void) memset((void *) lck
, 0, sizeof(lck_rw_t
));
815 lck_grp_reference(grp
);
816 lck_grp_lckcnt_incr(grp
, LCK_TYPE_RW
);
820 * Routine: lck_rw_ext_init
828 bzero((void *)lck
, sizeof(lck_rw_ext_t
));
830 if ((attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
831 lck
->lck_rw_deb
.type
= RW_TAG
;
832 lck
->lck_rw_attr
|= LCK_RW_ATTR_DEBUG
;
835 lck
->lck_rw_grp
= grp
;
837 if (grp
->lck_grp_attr
& LCK_GRP_ATTR_STAT
)
838 lck
->lck_rw_attr
|= LCK_RW_ATTR_STAT
;
842 * Routine: lck_rw_destroy
848 boolean_t lck_is_indirect
;
850 if (lck
->lck_rw_tag
== LCK_RW_TAG_DESTROYED
)
852 lck_is_indirect
= (lck
->lck_rw_tag
== LCK_RW_TAG_INDIRECT
);
853 lck
->lck_rw_tag
= LCK_RW_TAG_DESTROYED
;
855 kfree((void *)lck
->lck_rw_ptr
, sizeof(lck_rw_ext_t
));
857 lck_grp_lckcnt_decr(grp
, LCK_TYPE_RW
);
858 lck_grp_deallocate(grp
);
863 * Routine: lck_rw_lock
868 lck_rw_type_t lck_rw_type
)
870 if (lck_rw_type
== LCK_RW_TYPE_SHARED
)
871 lck_rw_lock_shared(lck
);
872 else if (lck_rw_type
== LCK_RW_TYPE_EXCLUSIVE
)
873 lck_rw_lock_exclusive(lck
);
875 panic("lck_rw_lock(): Invalid RW lock type: %d\n", lck_rw_type
);
880 * Routine: lck_rw_unlock
885 lck_rw_type_t lck_rw_type
)
887 if (lck_rw_type
== LCK_RW_TYPE_SHARED
)
888 lck_rw_unlock_shared(lck
);
889 else if (lck_rw_type
== LCK_RW_TYPE_EXCLUSIVE
)
890 lck_rw_unlock_exclusive(lck
);
892 panic("lck_rw_unlock(): Invalid RW lock type: %d\n", lck_rw_type
);
897 * Routine: lck_rw_unlock_shared
900 lck_rw_unlock_shared(
905 ret
= lck_rw_done(lck
);
907 if (ret
!= LCK_RW_TYPE_SHARED
)
908 panic("lck_rw_unlock(): lock held in mode: %d\n", ret
);
913 * Routine: lck_rw_unlock_exclusive
916 lck_rw_unlock_exclusive(
921 ret
= lck_rw_done(lck
);
923 if (ret
!= LCK_RW_TYPE_EXCLUSIVE
)
924 panic("lck_rw_unlock_exclusive(): lock held in mode: %d\n", ret
);
929 * Routine: lck_rw_try_lock
934 lck_rw_type_t lck_rw_type
)
936 if (lck_rw_type
== LCK_RW_TYPE_SHARED
)
937 return(lck_rw_try_lock_shared(lck
));
938 else if (lck_rw_type
== LCK_RW_TYPE_EXCLUSIVE
)
939 return(lck_rw_try_lock_exclusive(lck
));
941 panic("lck_rw_try_lock(): Invalid rw lock type: %x\n", lck_rw_type
);
948 * Routine: lck_rw_lock_exclusive_gen
951 lck_rw_lock_exclusive_gen(
955 boolean_t lock_miss
= FALSE
;
958 lck_rw_ilk_lock(lck
);
961 * Try to acquire the lck_rw_want_excl bit.
963 while (lck
->lck_rw_want_excl
) {
964 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE_CODE
) | DBG_FUNC_START
, (int)lck
, 0, 0, 0, 0);
970 i
= lock_wait_time
[1];
972 lck_rw_ilk_unlock(lck
);
973 while (--i
!= 0 && lck
->lck_rw_want_excl
)
975 lck_rw_ilk_lock(lck
);
978 if (lck
->lck_rw_want_excl
) {
979 lck
->lck_rw_waiting
= TRUE
;
980 res
= assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
981 if (res
== THREAD_WAITING
) {
982 lck_rw_ilk_unlock(lck
);
983 res
= thread_block(THREAD_CONTINUE_NULL
);
984 lck_rw_ilk_lock(lck
);
987 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE_CODE
) | DBG_FUNC_END
, (int)lck
, res
, 0, 0, 0);
989 lck
->lck_rw_want_excl
= TRUE
;
991 /* Wait for readers (and upgrades) to finish */
993 while ((lck
->lck_rw_shared_cnt
!= 0) || lck
->lck_rw_want_upgrade
) {
998 i
= lock_wait_time
[1];
1000 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE1_CODE
) | DBG_FUNC_START
,
1001 (int)lck
, lck
->lck_rw_shared_cnt
, lck
->lck_rw_want_upgrade
, i
, 0);
1004 lck_rw_ilk_unlock(lck
);
1005 while (--i
!= 0 && (lck
->lck_rw_shared_cnt
!= 0 ||
1006 lck
->lck_rw_want_upgrade
))
1008 lck_rw_ilk_lock(lck
);
1011 if (lck
->lck_rw_shared_cnt
!= 0 || lck
->lck_rw_want_upgrade
) {
1012 lck
->lck_rw_waiting
= TRUE
;
1013 res
= assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1014 if (res
== THREAD_WAITING
) {
1015 lck_rw_ilk_unlock(lck
);
1016 res
= thread_block(THREAD_CONTINUE_NULL
);
1017 lck_rw_ilk_lock(lck
);
1020 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE1_CODE
) | DBG_FUNC_END
,
1021 (int)lck
, lck
->lck_rw_shared_cnt
, lck
->lck_rw_want_upgrade
, res
, 0);
1024 lck_rw_ilk_unlock(lck
);
1029 * Routine: lck_rw_done_gen
1035 boolean_t do_wakeup
= FALSE
;
1036 lck_rw_type_t lck_rw_type
;
1039 lck_rw_ilk_lock(lck
);
1041 if (lck
->lck_rw_shared_cnt
!= 0) {
1042 lck_rw_type
= LCK_RW_TYPE_SHARED
;
1043 lck
->lck_rw_shared_cnt
--;
1046 lck_rw_type
= LCK_RW_TYPE_EXCLUSIVE
;
1047 if (lck
->lck_rw_want_upgrade
)
1048 lck
->lck_rw_want_upgrade
= FALSE
;
1050 lck
->lck_rw_want_excl
= FALSE
;
1054 * There is no reason to wakeup a lck_rw_waiting thread
1055 * if the read-count is non-zero. Consider:
1056 * we must be dropping a read lock
1057 * threads are waiting only if one wants a write lock
1058 * if there are still readers, they can't proceed
1061 if (lck
->lck_rw_waiting
&& (lck
->lck_rw_shared_cnt
== 0)) {
1062 lck
->lck_rw_waiting
= FALSE
;
1066 lck_rw_ilk_unlock(lck
);
1069 thread_wakeup((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))));
1070 return(lck_rw_type
);
1075 * Routine: lck_rw_lock_shared_gen
1078 lck_rw_lock_shared_gen(
1084 lck_rw_ilk_lock(lck
);
1086 while (lck
->lck_rw_want_excl
|| lck
->lck_rw_want_upgrade
) {
1087 i
= lock_wait_time
[1];
1089 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_CODE
) | DBG_FUNC_START
,
1090 (int)lck
, lck
->lck_rw_want_excl
, lck
->lck_rw_want_upgrade
, i
, 0);
1093 lck_rw_ilk_unlock(lck
);
1094 while (--i
!= 0 && (lck
->lck_rw_want_excl
|| lck
->lck_rw_want_upgrade
))
1096 lck_rw_ilk_lock(lck
);
1099 if (lck
->lck_rw_want_excl
|| lck
->lck_rw_want_upgrade
) {
1100 lck
->lck_rw_waiting
= TRUE
;
1101 res
= assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1102 if (res
== THREAD_WAITING
) {
1103 lck_rw_ilk_unlock(lck
);
1104 res
= thread_block(THREAD_CONTINUE_NULL
);
1105 lck_rw_ilk_lock(lck
);
1108 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_CODE
) | DBG_FUNC_END
,
1109 (int)lck
, lck
->lck_rw_want_excl
, lck
->lck_rw_want_upgrade
, res
, 0);
1112 lck
->lck_rw_shared_cnt
++;
1114 lck_rw_ilk_unlock(lck
);
1119 * Routine: lck_rw_lock_shared_to_exclusive_gen
1121 * Improves a read-only lock to one with
1122 * write permission. If another reader has
1123 * already requested an upgrade to a write lock,
1124 * no lock is held upon return.
1126 * Returns TRUE if the upgrade *failed*.
1130 lck_rw_lock_shared_to_exclusive_gen(
1134 boolean_t do_wakeup
= FALSE
;
1137 lck_rw_ilk_lock(lck
);
1139 lck
->lck_rw_shared_cnt
--;
1141 if (lck
->lck_rw_want_upgrade
) {
1142 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_CODE
) | DBG_FUNC_START
,
1143 (int)lck
, lck
->lck_rw_shared_cnt
, lck
->lck_rw_want_upgrade
, 0, 0);
1146 * Someone else has requested upgrade.
1147 * Since we've released a read lock, wake
1150 if (lck
->lck_rw_waiting
&& (lck
->lck_rw_shared_cnt
== 0)) {
1151 lck
->lck_rw_waiting
= FALSE
;
1155 lck_rw_ilk_unlock(lck
);
1158 thread_wakeup((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))));
1160 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_CODE
) | DBG_FUNC_END
,
1161 (int)lck
, lck
->lck_rw_shared_cnt
, lck
->lck_rw_want_upgrade
, 0, 0);
1166 lck
->lck_rw_want_upgrade
= TRUE
;
1168 while (lck
->lck_rw_shared_cnt
!= 0) {
1169 i
= lock_wait_time
[1];
1171 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX1_CODE
) | DBG_FUNC_START
,
1172 (int)lck
, lck
->lck_rw_shared_cnt
, i
, 0, 0);
1175 lck_rw_ilk_unlock(lck
);
1176 while (--i
!= 0 && lck
->lck_rw_shared_cnt
!= 0)
1178 lck_rw_ilk_lock(lck
);
1181 if (lck
->lck_rw_shared_cnt
!= 0) {
1182 lck
->lck_rw_waiting
= TRUE
;
1183 res
= assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1184 if (res
== THREAD_WAITING
) {
1185 lck_rw_ilk_unlock(lck
);
1186 res
= thread_block(THREAD_CONTINUE_NULL
);
1187 lck_rw_ilk_lock(lck
);
1190 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX1_CODE
) | DBG_FUNC_END
,
1191 (int)lck
, lck
->lck_rw_shared_cnt
, 0, 0, 0);
1194 lck_rw_ilk_unlock(lck
);
1200 * Routine: lck_rw_lock_exclusive_to_shared_gen
1203 lck_rw_lock_exclusive_to_shared_gen(
1206 boolean_t do_wakeup
= FALSE
;
1208 lck_rw_ilk_lock(lck
);
1210 lck
->lck_rw_shared_cnt
++;
1211 if (lck
->lck_rw_want_upgrade
)
1212 lck
->lck_rw_want_upgrade
= FALSE
;
1214 lck
->lck_rw_want_excl
= FALSE
;
1216 if (lck
->lck_rw_waiting
) {
1217 lck
->lck_rw_waiting
= FALSE
;
1221 lck_rw_ilk_unlock(lck
);
1224 thread_wakeup((event_t
)(((unsigned int*)lck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))));
1230 * Routine: lck_rw_try_lock_exclusive_gen
1232 * Tries to get a write lock.
1234 * Returns FALSE if the lock is not held on return.
1238 lck_rw_try_lock_exclusive_gen(
1241 lck_rw_ilk_lock(lck
);
1243 if (lck
->lck_rw_want_excl
|| lck
->lck_rw_want_upgrade
|| lck
->lck_rw_shared_cnt
) {
1247 lck_rw_ilk_unlock(lck
);
1255 lck
->lck_rw_want_excl
= TRUE
;
1257 lck_rw_ilk_unlock(lck
);
1263 * Routine: lck_rw_try_lock_shared_gen
1265 * Tries to get a read lock.
1267 * Returns FALSE if the lock is not held on return.
1271 lck_rw_try_lock_shared_gen(
1274 lck_rw_ilk_lock(lck
);
1276 if (lck
->lck_rw_want_excl
|| lck
->lck_rw_want_upgrade
) {
1277 lck_rw_ilk_unlock(lck
);
1281 lck
->lck_rw_shared_cnt
++;
1283 lck_rw_ilk_unlock(lck
);
1290 * Routine: lck_rw_ext_backtrace
1293 lck_rw_ext_backtrace(
1296 unsigned int *stackptr
, *stackptr_prev
;
1299 __asm__
volatile("mr %0,r1" : "=r" (stackptr
));
1301 while (frame
< LCK_FRAMES_MAX
) {
1302 stackptr_prev
= stackptr
;
1303 stackptr
= ( unsigned int *)*stackptr
;
1304 if ( (((unsigned int)stackptr_prev
) ^ ((unsigned int)stackptr
)) > 8192)
1306 lck
->lck_rw_deb
.stack
[frame
] = *(stackptr
+2);
1309 while (frame
< LCK_FRAMES_MAX
) {
1310 lck
->lck_rw_deb
.stack
[frame
] = 0;
1317 * Routine: lck_rw_lock_exclusive_ext
1320 lck_rw_lock_exclusive_ext(
1326 boolean_t lock_miss
= FALSE
;
1327 boolean_t lock_wait
= FALSE
;
1328 boolean_t lock_stat
;
1330 lck_rw_check_type(lck
, rlck
);
1332 if ( ((lck
->lck_rw_attr
& (LCK_RW_ATTR_DEBUG
|LCK_RW_ATTR_DIS_MYLOCK
)) == LCK_RW_ATTR_DEBUG
)
1333 && (lck
->lck_rw_deb
.thread
== current_thread()))
1334 panic("rw lock (0x%08X) recursive lock attempt\n", rlck
);
1336 lck_rw_ilk_lock(&lck
->lck_rw
);
1338 lock_stat
= (lck
->lck_rw_attr
& LCK_RW_ATTR_STAT
) ? TRUE
: FALSE
;
1341 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
++;
1344 * Try to acquire the lck_rw.lck_rw_want_excl bit.
1346 while (lck
->lck_rw
.lck_rw_want_excl
) {
1347 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE_CODE
) | DBG_FUNC_START
, (int)rlck
, 0, 0, 0, 0);
1349 if (lock_stat
&& !lock_miss
) {
1351 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
++;
1354 i
= lock_wait_time
[1];
1356 lck_rw_ilk_unlock(&lck
->lck_rw
);
1357 while (--i
!= 0 && lck
->lck_rw
.lck_rw_want_excl
)
1359 lck_rw_ilk_lock(&lck
->lck_rw
);
1362 if (lck
->lck_rw
.lck_rw_want_excl
) {
1363 lck
->lck_rw
.lck_rw_waiting
= TRUE
;
1364 res
= assert_wait((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1365 if (res
== THREAD_WAITING
) {
1366 if (lock_stat
&& !lock_wait
) {
1368 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
++;
1370 lck_rw_ilk_unlock(&lck
->lck_rw
);
1371 res
= thread_block(THREAD_CONTINUE_NULL
);
1372 lck_rw_ilk_lock(&lck
->lck_rw
);
1375 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE_CODE
) | DBG_FUNC_END
, (int)rlck
, res
, 0, 0, 0);
1377 lck
->lck_rw
.lck_rw_want_excl
= TRUE
;
1379 /* Wait for readers (and upgrades) to finish */
1381 while ((lck
->lck_rw
.lck_rw_shared_cnt
!= 0) || lck
->lck_rw
.lck_rw_want_upgrade
) {
1382 i
= lock_wait_time
[1];
1384 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE1_CODE
) | DBG_FUNC_START
,
1385 (int)rlck
, lck
->lck_rw
.lck_rw_shared_cnt
, lck
->lck_rw
.lck_rw_want_upgrade
, i
, 0);
1387 if (lock_stat
&& !lock_miss
) {
1389 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
++;
1393 lck_rw_ilk_unlock(&lck
->lck_rw
);
1394 while (--i
!= 0 && (lck
->lck_rw
.lck_rw_shared_cnt
!= 0 ||
1395 lck
->lck_rw
.lck_rw_want_upgrade
))
1397 lck_rw_ilk_lock(&lck
->lck_rw
);
1400 if (lck
->lck_rw
.lck_rw_shared_cnt
!= 0 || lck
->lck_rw
.lck_rw_want_upgrade
) {
1401 lck
->lck_rw
.lck_rw_waiting
= TRUE
;
1402 res
= assert_wait((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1403 if (res
== THREAD_WAITING
) {
1404 if (lock_stat
&& !lock_wait
) {
1406 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
++;
1408 lck_rw_ilk_unlock(&lck
->lck_rw
);
1409 res
= thread_block(THREAD_CONTINUE_NULL
);
1410 lck_rw_ilk_lock(&lck
->lck_rw
);
1413 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EXCLUSIVE1_CODE
) | DBG_FUNC_END
,
1414 (int)rlck
, lck
->lck_rw
.lck_rw_shared_cnt
, lck
->lck_rw
.lck_rw_want_upgrade
, res
, 0);
1417 lck
->lck_rw_deb
.pc_excl
= __builtin_return_address(0);
1418 if (LcksOpts
& enaLkExtStck
)
1419 lck_rw_ext_backtrace(lck
);
1420 lck
->lck_rw_deb
.thread
= current_thread();
1422 lck_rw_ilk_unlock(&lck
->lck_rw
);
1427 * Routine: lck_rw_done_ext
1434 boolean_t do_wakeup
= FALSE
;
1435 lck_rw_type_t lck_rw_type
;
1438 lck_rw_check_type(lck
, rlck
);
1440 lck_rw_ilk_lock(&lck
->lck_rw
);
1442 if (lck
->lck_rw
.lck_rw_shared_cnt
!= 0) {
1443 lck_rw_type
= LCK_RW_TYPE_SHARED
;
1444 lck
->lck_rw
.lck_rw_shared_cnt
--;
1447 lck_rw_type
= LCK_RW_TYPE_EXCLUSIVE
;
1448 if (lck
->lck_rw
.lck_rw_want_upgrade
)
1449 lck
->lck_rw
.lck_rw_want_upgrade
= FALSE
;
1450 else if (lck
->lck_rw
.lck_rw_want_excl
)
1451 lck
->lck_rw
.lck_rw_want_excl
= FALSE
;
1453 panic("rw lock (0x%08X) bad state (0x%08X) on attempt to release a shared or exlusive right\n",
1455 if (lck
->lck_rw_deb
.thread
== THREAD_NULL
)
1456 panic("rw lock (0x%08X) not held\n",
1458 else if ( ((lck
->lck_rw_attr
& (LCK_RW_ATTR_DEBUG
|LCK_RW_ATTR_DIS_THREAD
)) == LCK_RW_ATTR_DEBUG
)
1459 && (lck
->lck_rw_deb
.thread
!= current_thread()))
1460 panic("rw lock (0x%08X) unlocked by non-owner(0x%08X), current owner(0x%08X)\n",
1461 rlck
, current_thread(), lck
->lck_rw_deb
.thread
);
1462 lck
->lck_rw_deb
.thread
= THREAD_NULL
;
1465 if (lck
->lck_rw_attr
& LCK_RW_ATTR_DEBUG
)
1466 lck
->lck_rw_deb
.pc_done
= __builtin_return_address(0);
1469 * There is no reason to wakeup a waiting thread
1470 * if the read-count is non-zero. Consider:
1471 * we must be dropping a read lock
1472 * threads are waiting only if one wants a write lock
1473 * if there are still readers, they can't proceed
1476 if (lck
->lck_rw
.lck_rw_waiting
&& (lck
->lck_rw
.lck_rw_shared_cnt
== 0)) {
1477 lck
->lck_rw
.lck_rw_waiting
= FALSE
;
1481 lck_rw_ilk_unlock(&lck
->lck_rw
);
1484 thread_wakeup((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))));
1485 return(lck_rw_type
);
1490 * Routine: lck_rw_lock_shared_ext
1493 lck_rw_lock_shared_ext(
1499 boolean_t lock_miss
= FALSE
;
1500 boolean_t lock_wait
= FALSE
;
1501 boolean_t lock_stat
;
1503 lck_rw_check_type(lck
, rlck
);
1505 lck_rw_ilk_lock(&lck
->lck_rw
);
1507 lock_stat
= (lck
->lck_rw_attr
& LCK_RW_ATTR_STAT
) ? TRUE
: FALSE
;
1510 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
++;
1512 while (lck
->lck_rw
.lck_rw_want_excl
|| lck
->lck_rw
.lck_rw_want_upgrade
) {
1513 i
= lock_wait_time
[1];
1515 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_CODE
) | DBG_FUNC_START
,
1516 (int)rlck
, lck
->lck_rw
.lck_rw_want_excl
, lck
->lck_rw
.lck_rw_want_upgrade
, i
, 0);
1518 if (lock_stat
&& !lock_miss
) {
1520 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
++;
1524 lck_rw_ilk_unlock(&lck
->lck_rw
);
1525 while (--i
!= 0 && (lck
->lck_rw
.lck_rw_want_excl
|| lck
->lck_rw
.lck_rw_want_upgrade
))
1527 lck_rw_ilk_lock(&lck
->lck_rw
);
1530 if (lck
->lck_rw
.lck_rw_want_excl
|| lck
->lck_rw
.lck_rw_want_upgrade
) {
1531 lck
->lck_rw
.lck_rw_waiting
= TRUE
;
1532 res
= assert_wait((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1533 if (res
== THREAD_WAITING
) {
1534 if (lock_stat
&& !lock_wait
) {
1536 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
++;
1538 lck_rw_ilk_unlock(&lck
->lck_rw
);
1539 res
= thread_block(THREAD_CONTINUE_NULL
);
1540 lck_rw_ilk_lock(&lck
->lck_rw
);
1543 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_CODE
) | DBG_FUNC_END
,
1544 (int)rlck
, lck
->lck_rw
.lck_rw_want_excl
, lck
->lck_rw
.lck_rw_want_upgrade
, res
, 0);
1547 lck
->lck_rw
.lck_rw_shared_cnt
++;
1549 lck_rw_ilk_unlock(&lck
->lck_rw
);
1554 * Routine: lck_rw_lock_shared_to_exclusive_ext
1556 * Improves a read-only lock to one with
1557 * write permission. If another reader has
1558 * already requested an upgrade to a write lock,
1559 * no lock is held upon return.
1561 * Returns TRUE if the upgrade *failed*.
1565 lck_rw_lock_shared_to_exclusive_ext(
1570 boolean_t do_wakeup
= FALSE
;
1572 boolean_t lock_miss
= FALSE
;
1573 boolean_t lock_wait
= FALSE
;
1574 boolean_t lock_stat
;
1576 lck_rw_check_type(lck
, rlck
);
1578 if (lck
->lck_rw_deb
.thread
== current_thread())
1579 panic("rw lock (0x%08X) recursive lock attempt\n", rlck
);
1581 lck_rw_ilk_lock(&lck
->lck_rw
);
1583 lock_stat
= (lck
->lck_rw_attr
& LCK_RW_ATTR_STAT
) ? TRUE
: FALSE
;
1586 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
++;
1588 lck
->lck_rw
.lck_rw_shared_cnt
--;
1590 if (lck
->lck_rw
.lck_rw_want_upgrade
) {
1591 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_CODE
) | DBG_FUNC_START
,
1592 (int)rlck
, lck
->lck_rw
.lck_rw_shared_cnt
, lck
->lck_rw
.lck_rw_want_upgrade
, 0, 0);
1595 * Someone else has requested upgrade.
1596 * Since we've released a read lock, wake
1599 if (lck
->lck_rw
.lck_rw_waiting
&& (lck
->lck_rw
.lck_rw_shared_cnt
== 0)) {
1600 lck
->lck_rw
.lck_rw_waiting
= FALSE
;
1604 lck_rw_ilk_unlock(&lck
->lck_rw
);
1607 thread_wakeup((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))));
1609 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_CODE
) | DBG_FUNC_END
,
1610 (int)rlck
, lck
->lck_rw
.lck_rw_shared_cnt
, lck
->lck_rw
.lck_rw_want_upgrade
, 0, 0);
1615 lck
->lck_rw
.lck_rw_want_upgrade
= TRUE
;
1617 while (lck
->lck_rw
.lck_rw_shared_cnt
!= 0) {
1618 i
= lock_wait_time
[1];
1620 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX1_CODE
) | DBG_FUNC_START
,
1621 (int)rlck
, lck
->lck_rw
.lck_rw_shared_cnt
, i
, 0, 0);
1623 if (lock_stat
&& !lock_miss
) {
1625 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
++;
1629 lck_rw_ilk_unlock(&lck
->lck_rw
);
1630 while (--i
!= 0 && lck
->lck_rw
.lck_rw_shared_cnt
!= 0)
1632 lck_rw_ilk_lock(&lck
->lck_rw
);
1635 if (lck
->lck_rw
.lck_rw_shared_cnt
!= 0) {
1636 lck
->lck_rw
.lck_rw_waiting
= TRUE
;
1637 res
= assert_wait((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
1638 if (res
== THREAD_WAITING
) {
1639 if (lock_stat
&& !lock_wait
) {
1641 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
++;
1643 lck_rw_ilk_unlock(&lck
->lck_rw
);
1644 res
= thread_block(THREAD_CONTINUE_NULL
);
1645 lck_rw_ilk_lock(&lck
->lck_rw
);
1648 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX1_CODE
) | DBG_FUNC_END
,
1649 (int)rlck
, lck
->lck_rw
.lck_rw_shared_cnt
, 0, 0, 0);
1652 lck
->lck_rw_deb
.pc_excl
= __builtin_return_address(0);
1653 if (LcksOpts
& enaLkExtStck
)
1654 lck_rw_ext_backtrace(lck
);
1655 lck
->lck_rw_deb
.thread
= current_thread();
1657 lck_rw_ilk_unlock(&lck
->lck_rw
);
1663 * Routine: lck_rw_lock_exclusive_to_shared_ext
1666 lck_rw_lock_exclusive_to_shared_ext(
1670 boolean_t do_wakeup
= FALSE
;
1672 lck_rw_check_type(lck
, rlck
);
1674 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_TO_SH_CODE
) | DBG_FUNC_START
,
1675 (int)rlck
, lck
->lck_rw
.lck_rw_want_excl
, lck
->lck_rw
.lck_rw_want_upgrade
, 0, 0);
1677 lck_rw_ilk_lock(&lck
->lck_rw
);
1679 lck
->lck_rw
.lck_rw_shared_cnt
++;
1680 if (lck
->lck_rw
.lck_rw_want_upgrade
)
1681 lck
->lck_rw
.lck_rw_want_upgrade
= FALSE
;
1682 else if (lck
->lck_rw
.lck_rw_want_excl
)
1683 lck
->lck_rw
.lck_rw_want_excl
= FALSE
;
1685 panic("rw lock (0x%08X) bad state (0x%08X) on attempt to release a shared or exlusive right\n",
1687 if (lck
->lck_rw_deb
.thread
== THREAD_NULL
)
1688 panic("rw lock (0x%08X) not held\n",
1690 else if ( ((lck
->lck_rw_attr
& (LCK_RW_ATTR_DEBUG
|LCK_RW_ATTR_DIS_THREAD
)) == LCK_RW_ATTR_DEBUG
)
1691 && (lck
->lck_rw_deb
.thread
!= current_thread()))
1692 panic("rw lock (0x%08X) unlocked by non-owner(0x%08X), current owner(0x%08X)\n",
1693 rlck
, current_thread(), lck
->lck_rw_deb
.thread
);
1695 lck
->lck_rw_deb
.thread
= THREAD_NULL
;
1697 if (lck
->lck_rw
.lck_rw_waiting
) {
1698 lck
->lck_rw
.lck_rw_waiting
= FALSE
;
1702 lck_rw_ilk_unlock(&lck
->lck_rw
);
1705 thread_wakeup((event_t
)(((unsigned int*)rlck
)+((sizeof(lck_rw_t
)-1)/sizeof(unsigned int))));
1707 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_TO_SH_CODE
) | DBG_FUNC_END
,
1708 (int)rlck
, lck
->lck_rw
.lck_rw_want_excl
, lck
->lck_rw
.lck_rw_want_upgrade
, lck
->lck_rw
.lck_rw_shared_cnt
, 0);
1714 * Routine: lck_rw_try_lock_exclusive_ext
1716 * Tries to get a write lock.
1718 * Returns FALSE if the lock is not held on return.
1722 lck_rw_try_lock_exclusive_ext(
1726 boolean_t lock_stat
;
1728 lck_rw_check_type(lck
, rlck
);
1730 lck_rw_ilk_lock(&lck
->lck_rw
);
1732 lock_stat
= (lck
->lck_rw_attr
& LCK_RW_ATTR_STAT
) ? TRUE
: FALSE
;
1735 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
++;
1737 if (lck
->lck_rw
.lck_rw_want_excl
|| lck
->lck_rw
.lck_rw_want_upgrade
|| lck
->lck_rw
.lck_rw_shared_cnt
) {
1742 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
++;
1744 lck_rw_ilk_unlock(&lck
->lck_rw
);
1752 lck
->lck_rw
.lck_rw_want_excl
= TRUE
;
1753 lck
->lck_rw_deb
.pc_excl
= __builtin_return_address(0);
1754 if (LcksOpts
& enaLkExtStck
)
1755 lck_rw_ext_backtrace(lck
);
1756 lck
->lck_rw_deb
.thread
= current_thread();
1758 lck_rw_ilk_unlock(&lck
->lck_rw
);
1764 * Routine: lck_rw_try_lock_shared_ext
1766 * Tries to get a read lock.
1768 * Returns FALSE if the lock is not held on return.
1772 lck_rw_try_lock_shared_ext(
1776 boolean_t lock_stat
;
1778 lck_rw_check_type(lck
, rlck
);
1780 lck_rw_ilk_lock(&lck
->lck_rw
);
1782 lock_stat
= (lck
->lck_rw_attr
& LCK_RW_ATTR_STAT
) ? TRUE
: FALSE
;
1785 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
++;
1787 if (lck
->lck_rw
.lck_rw_want_excl
|| lck
->lck_rw
.lck_rw_want_upgrade
) {
1789 lck
->lck_rw_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
++;
1791 lck_rw_ilk_unlock(&lck
->lck_rw
);
1795 lck
->lck_rw
.lck_rw_shared_cnt
++;
1797 lck_rw_ilk_unlock(&lck
->lck_rw
);
1807 if (lck
->lck_rw_deb
.type
!= RW_TAG
)
1808 panic("rw lock (0x%08X) not a rw lock type (0x%08X)\n",rlck
, lck
->lck_rw_deb
.type
);
1812 * The C portion of the mutex package. These routines are only invoked
1813 * if the optimized assembler routines can't do the work.
1817 * Forward definition
1820 void lck_mtx_ext_init(
1826 * Routine: mutex_alloc
1828 * Allocate a mutex for external users who cannot
1829 * hard-code the structure definition into their
1831 * For now just use kalloc, but a zone is probably
1840 if ((m
= (mutex_t
*)kalloc(sizeof(mutex_t
))) != 0)
1846 * Routine: mutex_free
1852 kfree((void *)m
, sizeof(mutex_t
));
1856 * Routine: lck_mtx_alloc_init
1864 if ((lck
= (lck_mtx_t
*)kalloc(sizeof(lck_mtx_t
))) != 0)
1865 lck_mtx_init(lck
, grp
, attr
);
1871 * Routine: lck_mtx_free
1877 lck_mtx_destroy(lck
, grp
);
1878 kfree((void *)lck
, sizeof(lck_mtx_t
));
1882 * Routine: lck_mtx_init
1889 lck_mtx_ext_t
*lck_ext
;
1890 lck_attr_t
*lck_attr
;
1892 if (attr
!= LCK_ATTR_NULL
)
1895 lck_attr
= &LockDefaultLckAttr
;
1897 if ((lck_attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
1898 if ((lck_ext
= (lck_mtx_ext_t
*)kalloc(sizeof(lck_mtx_ext_t
))) != 0) {
1899 lck_mtx_ext_init(lck_ext
, grp
, lck_attr
);
1900 lck
->lck_mtx_tag
= LCK_MTX_TAG_INDIRECT
;
1901 lck
->lck_mtx_ptr
= lck_ext
;
1904 lck
->lck_mtx_data
= 0;
1905 lck
->lck_mtx_waiters
= 0;
1906 lck
->lck_mtx_pri
= 0;
1908 lck_grp_reference(grp
);
1909 lck_grp_lckcnt_incr(grp
, LCK_TYPE_MTX
);
1913 * Routine: lck_mtx_ext_init
1921 bzero((void *)lck
, sizeof(lck_mtx_ext_t
));
1923 if ((attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
1924 lck
->lck_mtx_deb
.type
= MUTEX_TAG
;
1925 lck
->lck_mtx_attr
|= LCK_MTX_ATTR_DEBUG
;
1928 lck
->lck_mtx_grp
= grp
;
1930 if (grp
->lck_grp_attr
& LCK_GRP_ATTR_STAT
)
1931 lck
->lck_mtx_attr
|= LCK_MTX_ATTR_STAT
;
1935 * Routine: lck_mtx_destroy
1941 boolean_t lck_is_indirect
;
1943 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_DESTROYED
)
1945 lck_is_indirect
= (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
);
1946 lck
->lck_mtx_tag
= LCK_MTX_TAG_DESTROYED
;
1947 if (lck_is_indirect
)
1948 kfree((void *)lck
->lck_mtx_ptr
, sizeof(lck_mtx_ext_t
));
1950 lck_grp_lckcnt_decr(grp
, LCK_TYPE_MTX
);
1951 lck_grp_deallocate(grp
);
1958 * Routines to print out simple_locks and mutexes in a nicely-formatted
1962 char *simple_lock_labels
= "ENTRY ILK THREAD DURATION CALLER";
1963 char *mutex_labels
= "ENTRY LOCKED WAITERS THREAD CALLER";
1965 void db_print_simple_lock(
1966 simple_lock_t addr
);
1968 void db_print_mutex(
1972 db_show_one_simple_lock (
1974 boolean_t have_addr
,
1978 simple_lock_t saddr
= (simple_lock_t
)addr
;
1980 if (saddr
== (simple_lock_t
)0 || !have_addr
) {
1981 db_error ("No simple_lock\n");
1984 else if (saddr
->lock_type
!= USLOCK_TAG
)
1985 db_error ("Not a simple_lock\n");
1986 #endif /* USLOCK_DEBUG */
1988 db_printf ("%s\n", simple_lock_labels
);
1989 db_print_simple_lock (saddr
);
1993 db_print_simple_lock (
1997 db_printf ("%08x %3d", addr
, *hw_lock_addr(addr
->interlock
));
1999 db_printf (" %08x", addr
->debug
.lock_thread
);
2000 db_printf (" %08x ", addr
->debug
.duration
[1]);
2001 db_printsym ((int)addr
->debug
.lock_pc
, DB_STGY_ANY
);
2002 #endif /* USLOCK_DEBUG */
2009 boolean_t have_addr
,
2013 mutex_t
* maddr
= (mutex_t
*)addr
;
2015 if (maddr
== (mutex_t
*)0 || !have_addr
)
2016 db_error ("No mutex\n");
2018 else if (maddr
->lck_mtx_deb
.type
!= MUTEX_TAG
)
2019 db_error ("Not a mutex\n");
2020 #endif /* MACH_LDEBUG */
2022 db_printf ("%s\n", mutex_labels
);
2023 db_print_mutex (maddr
);
2030 db_printf ("%08x %6d %7d",
2031 addr
, *addr
, addr
->lck_mtx
.lck_mtx_waiters
);
2033 db_printf (" %08x ", addr
->lck_mtx_deb
.thread
);
2034 db_printsym (addr
->lck_mtx_deb
.stack
[0], DB_STGY_ANY
);
2035 #endif /* MACH_LDEBUG */
2043 db_printf("shared_count = 0x%x, %swant_upgrade, %swant_exclusive, ",
2044 lock
->lck_rw
.lck_rw_shared_cnt
,
2045 lock
->lck_rw
.lck_rw_want_upgrade
? "" : "!",
2046 lock
->lck_rw
.lck_rw_want_excl
? "" : "!");
2047 db_printf("%swaiting\n",
2048 lock
->lck_rw
.lck_rw_waiting
? "" : "!");
2049 db_printf("%sInterlock\n",
2050 lock
->lck_rw
.lck_rw_interlock
? "" : "!");
2053 #endif /* MACH_KDB */