2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 * Locking primitives implementation
64 #include <mach_ldebug.h>
66 #include <kern/locks.h>
67 #include <kern/kalloc.h>
68 #include <kern/misc_protos.h>
69 #include <kern/thread.h>
70 #include <kern/processor.h>
71 #include <kern/cpu_data.h>
72 #include <kern/cpu_number.h>
73 #include <kern/sched_prim.h>
75 #include <kern/debug.h>
78 #include <i386/machine_routines.h> /* machine_timeout_suspended() */
79 #include <machine/machine_cpu.h>
82 #include <sys/kdebug.h>
83 #include <mach/branch_predicates.h>
86 * We need only enough declarations from the BSD-side to be able to
87 * test if our probe is active, and to call __dtrace_probe(). Setting
88 * NEED_DTRACE_DEFS gets a local copy of those definitions pulled in.
91 #define NEED_DTRACE_DEFS
92 #include <../bsd/sys/lockstat.h>
95 #define LCK_RW_LCK_EXCLUSIVE_CODE 0x100
96 #define LCK_RW_LCK_EXCLUSIVE1_CODE 0x101
97 #define LCK_RW_LCK_SHARED_CODE 0x102
98 #define LCK_RW_LCK_SH_TO_EX_CODE 0x103
99 #define LCK_RW_LCK_SH_TO_EX1_CODE 0x104
100 #define LCK_RW_LCK_EX_TO_SH_CODE 0x105
102 #define LCK_RW_LCK_EX_WRITER_SPIN_CODE 0x106
103 #define LCK_RW_LCK_EX_WRITER_WAIT_CODE 0x107
104 #define LCK_RW_LCK_EX_READER_SPIN_CODE 0x108
105 #define LCK_RW_LCK_EX_READER_WAIT_CODE 0x109
106 #define LCK_RW_LCK_SHARED_SPIN_CODE 0x110
107 #define LCK_RW_LCK_SHARED_WAIT_CODE 0x111
108 #define LCK_RW_LCK_SH_TO_EX_SPIN_CODE 0x112
109 #define LCK_RW_LCK_SH_TO_EX_WAIT_CODE 0x113
112 #define ANY_LOCK_DEBUG (USLOCK_DEBUG || LOCK_DEBUG || MUTEX_DEBUG)
114 unsigned int LcksOpts
=0;
120 * Perform simple lock checks.
122 int uslock_check
= 1;
123 int max_lock_loops
= 100000000;
124 decl_simple_lock_data(extern , printf_lock
)
125 decl_simple_lock_data(extern , panic_lock
)
126 #endif /* USLOCK_DEBUG */
128 extern unsigned int not_in_kdp
;
131 * We often want to know the addresses of the callers
132 * of the various lock routines. However, this information
133 * is only used for debugging and statistics.
136 #define INVALID_PC ((void *) VM_MAX_KERNEL_ADDRESS)
137 #define INVALID_THREAD ((void *) VM_MAX_KERNEL_ADDRESS)
139 #define OBTAIN_PC(pc) ((pc) = GET_RETURN_PC())
140 #define DECL_PC(pc) pc_t pc;
141 #else /* ANY_LOCK_DEBUG */
145 * Eliminate lint complaints about unused local pc variables.
147 #define OBTAIN_PC(pc) ++pc
149 #define OBTAIN_PC(pc)
151 #endif /* USLOCK_DEBUG */
155 * Portable lock package implementation of usimple_locks.
159 #define USLDBG(stmt) stmt
160 void usld_lock_init(usimple_lock_t
, unsigned short);
161 void usld_lock_pre(usimple_lock_t
, pc_t
);
162 void usld_lock_post(usimple_lock_t
, pc_t
);
163 void usld_unlock(usimple_lock_t
, pc_t
);
164 void usld_lock_try_pre(usimple_lock_t
, pc_t
);
165 void usld_lock_try_post(usimple_lock_t
, pc_t
);
166 int usld_lock_common_checks(usimple_lock_t
, char *);
167 #else /* USLOCK_DEBUG */
169 #endif /* USLOCK_DEBUG */
172 extern int lck_rw_grab_want(lck_rw_t
*lck
);
173 extern int lck_rw_grab_shared(lck_rw_t
*lck
);
174 extern int lck_rw_held_read_or_upgrade(lck_rw_t
*lck
);
178 * Forward definitions
181 void lck_rw_lock_shared_gen(
184 void lck_rw_lock_exclusive_gen(
187 boolean_t
lck_rw_lock_shared_to_exclusive_success(
190 boolean_t
lck_rw_lock_shared_to_exclusive_failure(
192 int prior_lock_state
);
194 void lck_rw_lock_exclusive_to_shared_gen(
196 int prior_lock_state
);
198 lck_rw_type_t
lck_rw_done_gen(
200 int prior_lock_state
);
202 void lck_rw_clear_promotions_x86(thread_t thread
);
205 * Routine: lck_spin_alloc_init
214 if ((lck
= (lck_spin_t
*)kalloc(sizeof(lck_spin_t
))) != 0)
215 lck_spin_init(lck
, grp
, attr
);
221 * Routine: lck_spin_free
228 lck_spin_destroy(lck
, grp
);
229 kfree(lck
, sizeof(lck_spin_t
));
233 * Routine: lck_spin_init
239 __unused lck_attr_t
*attr
)
241 usimple_lock_init((usimple_lock_t
) lck
, 0);
242 lck_grp_reference(grp
);
243 lck_grp_lckcnt_incr(grp
, LCK_TYPE_SPIN
);
247 * Routine: lck_spin_destroy
254 if (lck
->interlock
== LCK_SPIN_TAG_DESTROYED
)
256 lck
->interlock
= LCK_SPIN_TAG_DESTROYED
;
257 lck_grp_lckcnt_decr(grp
, LCK_TYPE_SPIN
);
258 lck_grp_deallocate(grp
);
263 * Routine: lck_spin_lock
269 usimple_lock((usimple_lock_t
) lck
);
273 * Routine: lck_spin_unlock
279 usimple_unlock((usimple_lock_t
) lck
);
284 * Routine: lck_spin_try_lock
290 return((boolean_t
)usimple_lock_try((usimple_lock_t
) lck
));
294 * Routine: lck_spin_is_acquired
295 * NOT SAFE: To be used only by kernel debugger to avoid deadlock.
296 * Returns: TRUE if lock is acquired.
299 lck_spin_is_acquired(lck_spin_t
*lck
) {
301 panic("panic: spinlock acquired check done outside of kernel debugger");
303 return (lck
->interlock
!= 0)? TRUE
: FALSE
;
307 * Initialize a usimple_lock.
309 * No change in preemption state.
314 __unused
unsigned short tag
)
316 #ifndef MACHINE_SIMPLE_LOCK
317 USLDBG(usld_lock_init(l
, tag
));
318 hw_lock_init(&l
->interlock
);
320 simple_lock_init((simple_lock_t
)l
,tag
);
324 volatile uint32_t spinlock_owner_cpu
= ~0;
325 volatile usimple_lock_t spinlock_timed_out
;
327 uint32_t spinlock_timeout_NMI(uintptr_t thread_addr
) {
331 for (i
= 0; i
< real_ncpus
; i
++) {
332 if ((uintptr_t)cpu_data_ptr
[i
]->cpu_active_thread
== thread_addr
) {
333 spinlock_owner_cpu
= i
;
334 if ((uint32_t) cpu_number() == i
)
336 cpu_datap(i
)->cpu_NMI_acknowledged
= FALSE
;
337 cpu_NMI_interrupt(i
);
338 deadline
= mach_absolute_time() + (LockTimeOut
* 2);
339 while (mach_absolute_time() < deadline
&& cpu_datap(i
)->cpu_NMI_acknowledged
== FALSE
)
345 return spinlock_owner_cpu
;
349 * Acquire a usimple_lock.
351 * Returns with preemption disabled. Note
352 * that the hw_lock routines are responsible for
353 * maintaining preemption state.
359 #ifndef MACHINE_SIMPLE_LOCK
363 USLDBG(usld_lock_pre(l
, pc
));
365 if(__improbable(hw_lock_to(&l
->interlock
, LockTimeOutTSC
) == 0)) {
366 boolean_t uslock_acquired
= FALSE
;
367 while (machine_timeout_suspended()) {
369 if ((uslock_acquired
= hw_lock_to(&l
->interlock
, LockTimeOutTSC
)))
373 if (uslock_acquired
== FALSE
) {
375 uintptr_t lowner
= (uintptr_t)l
->interlock
.lock_data
;
376 spinlock_timed_out
= l
;
377 lock_cpu
= spinlock_timeout_NMI(lowner
);
378 panic("Spinlock acquisition timed out: lock=%p, lock owner thread=0x%lx, current_thread: %p, lock owner active on CPU 0x%x, current owner: 0x%lx", l
, lowner
, current_thread(), lock_cpu
, (uintptr_t)l
->interlock
.lock_data
);
381 USLDBG(usld_lock_post(l
, pc
));
383 simple_lock((simple_lock_t
)l
);
389 * Release a usimple_lock.
391 * Returns with preemption enabled. Note
392 * that the hw_lock routines are responsible for
393 * maintaining preemption state.
399 #ifndef MACHINE_SIMPLE_LOCK
403 USLDBG(usld_unlock(l
, pc
));
404 hw_lock_unlock(&l
->interlock
);
406 simple_unlock_rwmb((simple_lock_t
)l
);
412 * Conditionally acquire a usimple_lock.
414 * On success, returns with preemption disabled.
415 * On failure, returns with preemption in the same state
416 * as when first invoked. Note that the hw_lock routines
417 * are responsible for maintaining preemption state.
419 * XXX No stats are gathered on a miss; I preserved this
420 * behavior from the original assembly-language code, but
421 * doesn't it make sense to log misses? XXX
427 #ifndef MACHINE_SIMPLE_LOCK
428 unsigned int success
;
432 USLDBG(usld_lock_try_pre(l
, pc
));
433 if ((success
= hw_lock_try(&l
->interlock
))) {
434 USLDBG(usld_lock_try_post(l
, pc
));
438 return(simple_lock_try((simple_lock_t
)l
));
444 * States of a usimple_lock. The default when initializing
445 * a usimple_lock is setting it up for debug checking.
447 #define USLOCK_CHECKED 0x0001 /* lock is being checked */
448 #define USLOCK_TAKEN 0x0002 /* lock has been taken */
449 #define USLOCK_INIT 0xBAA0 /* lock has been initialized */
450 #define USLOCK_INITIALIZED (USLOCK_INIT|USLOCK_CHECKED)
451 #define USLOCK_CHECKING(l) (uslock_check && \
452 ((l)->debug.state & USLOCK_CHECKED))
455 * Trace activities of a particularly interesting lock.
457 void usl_trace(usimple_lock_t
, int, pc_t
, const char *);
461 * Initialize the debugging information contained
467 __unused
unsigned short tag
)
469 if (l
== USIMPLE_LOCK_NULL
)
470 panic("lock initialization: null lock pointer");
471 l
->lock_type
= USLOCK_TAG
;
472 l
->debug
.state
= uslock_check
? USLOCK_INITIALIZED
: 0;
473 l
->debug
.lock_cpu
= l
->debug
.unlock_cpu
= 0;
474 l
->debug
.lock_pc
= l
->debug
.unlock_pc
= INVALID_PC
;
475 l
->debug
.lock_thread
= l
->debug
.unlock_thread
= INVALID_THREAD
;
476 l
->debug
.duration
[0] = l
->debug
.duration
[1] = 0;
477 l
->debug
.unlock_cpu
= l
->debug
.unlock_cpu
= 0;
478 l
->debug
.unlock_pc
= l
->debug
.unlock_pc
= INVALID_PC
;
479 l
->debug
.unlock_thread
= l
->debug
.unlock_thread
= INVALID_THREAD
;
484 * These checks apply to all usimple_locks, not just
485 * those with USLOCK_CHECKED turned on.
488 usld_lock_common_checks(
492 if (l
== USIMPLE_LOCK_NULL
)
493 panic("%s: null lock pointer", caller
);
494 if (l
->lock_type
!= USLOCK_TAG
)
495 panic("%s: %p is not a usimple lock, 0x%x", caller
, l
, l
->lock_type
);
496 if (!(l
->debug
.state
& USLOCK_INIT
))
497 panic("%s: %p is not an initialized lock, 0x%x", caller
, l
, l
->debug
.state
);
498 return USLOCK_CHECKING(l
);
503 * Debug checks on a usimple_lock just before attempting
512 char caller
[] = "usimple_lock";
515 if (!usld_lock_common_checks(l
, caller
))
519 * Note that we have a weird case where we are getting a lock when we are]
520 * in the process of putting the system to sleep. We are running with no
521 * current threads, therefore we can't tell if we are trying to retake a lock
522 * we have or someone on the other processor has it. Therefore we just
523 * ignore this test if the locking thread is 0.
526 if ((l
->debug
.state
& USLOCK_TAKEN
) && l
->debug
.lock_thread
&&
527 l
->debug
.lock_thread
== (void *) current_thread()) {
528 printf("%s: lock %p already locked (at %p) by",
529 caller
, l
, l
->debug
.lock_pc
);
530 printf(" current thread %p (new attempt at pc %p)\n",
531 l
->debug
.lock_thread
, pc
);
534 mp_disable_preemption();
535 usl_trace(l
, cpu_number(), pc
, caller
);
536 mp_enable_preemption();
541 * Debug checks on a usimple_lock just after acquiring it.
543 * Pre-emption has been disabled at this point,
544 * so we are safe in using cpu_number.
552 char caller
[] = "successful usimple_lock";
555 if (!usld_lock_common_checks(l
, caller
))
558 if (!((l
->debug
.state
& ~USLOCK_TAKEN
) == USLOCK_INITIALIZED
))
559 panic("%s: lock %p became uninitialized",
561 if ((l
->debug
.state
& USLOCK_TAKEN
))
562 panic("%s: lock 0x%p became TAKEN by someone else",
565 mycpu
= cpu_number();
566 l
->debug
.lock_thread
= (void *)current_thread();
567 l
->debug
.state
|= USLOCK_TAKEN
;
568 l
->debug
.lock_pc
= pc
;
569 l
->debug
.lock_cpu
= mycpu
;
571 usl_trace(l
, mycpu
, pc
, caller
);
576 * Debug checks on a usimple_lock just before
577 * releasing it. Note that the caller has not
578 * yet released the hardware lock.
580 * Preemption is still disabled, so there's
581 * no problem using cpu_number.
589 char caller
[] = "usimple_unlock";
592 if (!usld_lock_common_checks(l
, caller
))
595 mycpu
= cpu_number();
597 if (!(l
->debug
.state
& USLOCK_TAKEN
))
598 panic("%s: lock 0x%p hasn't been taken",
600 if (l
->debug
.lock_thread
!= (void *) current_thread())
601 panic("%s: unlocking lock 0x%p, owned by thread %p",
602 caller
, l
, l
->debug
.lock_thread
);
603 if (l
->debug
.lock_cpu
!= mycpu
) {
604 printf("%s: unlocking lock 0x%p on cpu 0x%x",
606 printf(" (acquired on cpu 0x%x)\n", l
->debug
.lock_cpu
);
609 usl_trace(l
, mycpu
, pc
, caller
);
611 l
->debug
.unlock_thread
= l
->debug
.lock_thread
;
612 l
->debug
.lock_thread
= INVALID_PC
;
613 l
->debug
.state
&= ~USLOCK_TAKEN
;
614 l
->debug
.unlock_pc
= pc
;
615 l
->debug
.unlock_cpu
= mycpu
;
620 * Debug checks on a usimple_lock just before
621 * attempting to acquire it.
623 * Preemption isn't guaranteed to be disabled.
630 char caller
[] = "usimple_lock_try";
632 if (!usld_lock_common_checks(l
, caller
))
634 mp_disable_preemption();
635 usl_trace(l
, cpu_number(), pc
, caller
);
636 mp_enable_preemption();
641 * Debug checks on a usimple_lock just after
642 * successfully attempting to acquire it.
644 * Preemption has been disabled by the
645 * lock acquisition attempt, so it's safe
654 char caller
[] = "successful usimple_lock_try";
656 if (!usld_lock_common_checks(l
, caller
))
659 if (!((l
->debug
.state
& ~USLOCK_TAKEN
) == USLOCK_INITIALIZED
))
660 panic("%s: lock 0x%p became uninitialized",
662 if ((l
->debug
.state
& USLOCK_TAKEN
))
663 panic("%s: lock 0x%p became TAKEN by someone else",
666 mycpu
= cpu_number();
667 l
->debug
.lock_thread
= (void *) current_thread();
668 l
->debug
.state
|= USLOCK_TAKEN
;
669 l
->debug
.lock_pc
= pc
;
670 l
->debug
.lock_cpu
= mycpu
;
672 usl_trace(l
, mycpu
, pc
, caller
);
677 * For very special cases, set traced_lock to point to a
678 * specific lock of interest. The result is a series of
679 * XPRs showing lock operations on that lock. The lock_seq
680 * value is used to show the order of those operations.
682 usimple_lock_t traced_lock
;
683 unsigned int lock_seq
;
690 const char * op_name
)
692 if (traced_lock
== l
) {
694 "seq %d, cpu %d, %s @ %x\n",
695 (uintptr_t) lock_seq
, (uintptr_t) mycpu
,
696 (uintptr_t) op_name
, (uintptr_t) pc
, 0);
702 #endif /* USLOCK_DEBUG */
705 * Routine: lck_rw_alloc_init
713 if ((lck
= (lck_rw_t
*)kalloc(sizeof(lck_rw_t
))) != 0) {
714 bzero(lck
, sizeof(lck_rw_t
));
715 lck_rw_init(lck
, grp
, attr
);
722 * Routine: lck_rw_free
728 lck_rw_destroy(lck
, grp
);
729 kfree(lck
, sizeof(lck_rw_t
));
733 * Routine: lck_rw_init
741 lck_attr_t
*lck_attr
= (attr
!= LCK_ATTR_NULL
) ?
742 attr
: &LockDefaultLckAttr
;
744 hw_lock_byte_init(&lck
->lck_rw_interlock
);
745 lck
->lck_rw_want_write
= FALSE
;
746 lck
->lck_rw_want_upgrade
= FALSE
;
747 lck
->lck_rw_shared_count
= 0;
748 lck
->lck_rw_can_sleep
= TRUE
;
749 lck
->lck_r_waiting
= lck
->lck_w_waiting
= 0;
751 lck
->lck_rw_priv_excl
= ((lck_attr
->lck_attr_val
&
752 LCK_ATTR_RW_SHARED_PRIORITY
) == 0);
754 lck_grp_reference(grp
);
755 lck_grp_lckcnt_incr(grp
, LCK_TYPE_RW
);
759 * Routine: lck_rw_destroy
766 if (lck
->lck_rw_tag
== LCK_RW_TAG_DESTROYED
)
769 lck_rw_assert(lck
, LCK_RW_ASSERT_NOTHELD
);
771 lck
->lck_rw_tag
= LCK_RW_TAG_DESTROYED
;
772 lck_grp_lckcnt_decr(grp
, LCK_TYPE_RW
);
773 lck_grp_deallocate(grp
);
778 * Sleep locks. These use the same data structure and algorithm
779 * as the spin locks, but the process sleeps while it is waiting
780 * for the lock. These work on uniprocessor systems.
783 #define DECREMENTER_TIMEOUT 1000000
785 #define RW_LOCK_READER_EVENT(x) \
786 ((event_t) (((unsigned char*) (x)) + (offsetof(lck_rw_t, lck_rw_tag))))
788 #define RW_LOCK_WRITER_EVENT(x) \
789 ((event_t) (((unsigned char*) (x)) + (offsetof(lck_rw_t, lck_rw_pad8))))
792 * We disable interrupts while holding the RW interlock to prevent an
793 * interrupt from exacerbating hold time.
794 * Hence, local helper functions lck_interlock_lock()/lck_interlock_unlock().
797 lck_interlock_lock(lck_rw_t
*lck
)
801 istate
= ml_set_interrupts_enabled(FALSE
);
802 hw_lock_byte_lock(&lck
->lck_rw_interlock
);
808 lck_interlock_unlock(lck_rw_t
*lck
, boolean_t istate
)
810 hw_lock_byte_unlock(&lck
->lck_rw_interlock
);
811 ml_set_interrupts_enabled(istate
);
815 * This inline is used when busy-waiting for an rw lock.
816 * If interrupts were disabled when the lock primitive was called,
817 * we poll the IPI handler for pending tlb flushes.
818 * XXX This is a hack to avoid deadlocking on the pmap_system_lock.
821 lck_rw_lock_pause(boolean_t interrupts_enabled
)
823 if (!interrupts_enabled
)
824 handle_pending_TLB_flushes();
830 * compute the deadline to spin against when
831 * waiting for a change of state on a lck_rw_t
833 static inline uint64_t
834 lck_rw_deadline_for_spin(lck_rw_t
*lck
)
836 if (lck
->lck_rw_can_sleep
) {
837 if (lck
->lck_r_waiting
|| lck
->lck_w_waiting
|| lck
->lck_rw_shared_count
> machine_info
.max_cpus
) {
839 * there are already threads waiting on this lock... this
840 * implies that they have spun beyond their deadlines waiting for
841 * the desired state to show up so we will not bother spinning at this time...
843 * the current number of threads sharing this lock exceeds our capacity to run them
844 * concurrently and since all states we're going to spin for require the rw_shared_count
845 * to be at 0, we'll not bother spinning since the latency for this to happen is
848 return (mach_absolute_time());
850 return (mach_absolute_time() + MutexSpin
);
852 return (mach_absolute_time() + (100000LL * 1000000000LL));
857 * Routine: lck_rw_lock_exclusive
860 lck_rw_lock_exclusive_gen(
863 uint64_t deadline
= 0;
867 wait_result_t res
= 0;
868 boolean_t istate
= -1;
871 boolean_t dtrace_ls_initialized
= FALSE
;
872 boolean_t dtrace_rwl_excl_spin
, dtrace_rwl_excl_block
, dtrace_ls_enabled
= FALSE
;
873 uint64_t wait_interval
= 0;
874 int readers_at_sleep
= 0;
878 * Try to acquire the lck_rw_want_write bit.
880 while ( !lck_rw_grab_want(lck
)) {
883 if (dtrace_ls_initialized
== FALSE
) {
884 dtrace_ls_initialized
= TRUE
;
885 dtrace_rwl_excl_spin
= (lockstat_probemap
[LS_LCK_RW_LOCK_EXCL_SPIN
] != 0);
886 dtrace_rwl_excl_block
= (lockstat_probemap
[LS_LCK_RW_LOCK_EXCL_BLOCK
] != 0);
887 dtrace_ls_enabled
= dtrace_rwl_excl_spin
|| dtrace_rwl_excl_block
;
888 if (dtrace_ls_enabled
) {
890 * Either sleeping or spinning is happening,
891 * start a timing of our delay interval now.
893 readers_at_sleep
= lck
->lck_rw_shared_count
;
894 wait_interval
= mach_absolute_time();
899 istate
= ml_get_interrupts_enabled();
901 deadline
= lck_rw_deadline_for_spin(lck
);
903 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_WRITER_SPIN_CODE
) | DBG_FUNC_START
, (int)lck
, 0, 0, 0, 0);
905 while (((gotlock
= lck_rw_grab_want(lck
)) == 0) && mach_absolute_time() < deadline
)
906 lck_rw_lock_pause(istate
);
908 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_WRITER_SPIN_CODE
) | DBG_FUNC_END
, (int)lck
, 0, 0, gotlock
, 0);
913 * if we get here, the deadline has expired w/o us
914 * being able to grab the lock exclusively
915 * check to see if we're allowed to do a thread_block
917 if (lck
->lck_rw_can_sleep
) {
919 istate
= lck_interlock_lock(lck
);
921 if (lck
->lck_rw_want_write
) {
923 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_WRITER_WAIT_CODE
) | DBG_FUNC_START
, (int)lck
, 0, 0, 0, 0);
925 lck
->lck_w_waiting
= TRUE
;
927 res
= assert_wait(RW_LOCK_WRITER_EVENT(lck
), THREAD_UNINT
);
928 lck_interlock_unlock(lck
, istate
);
930 if (res
== THREAD_WAITING
) {
931 res
= thread_block(THREAD_CONTINUE_NULL
);
934 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_WRITER_WAIT_CODE
) | DBG_FUNC_END
, (int)lck
, res
, slept
, 0, 0);
936 lck
->lck_rw_want_write
= TRUE
;
937 lck_interlock_unlock(lck
, istate
);
943 * Wait for readers (and upgrades) to finish...
944 * the test for these conditions must be done simultaneously with
945 * a check of the interlock not being held since
946 * the rw_shared_count will drop to 0 first and then want_upgrade
947 * will be set to 1 in the shared_to_exclusive scenario... those
948 * adjustments are done behind the interlock and represent an
949 * atomic change in state and must be considered as such
950 * however, once we see the read count at 0, the want_upgrade not set
951 * and the interlock not held, we are safe to proceed
953 while (lck_rw_held_read_or_upgrade(lck
)) {
957 * Either sleeping or spinning is happening, start
958 * a timing of our delay interval now. If we set it
959 * to -1 we don't have accurate data so we cannot later
960 * decide to record a dtrace spin or sleep event.
962 if (dtrace_ls_initialized
== FALSE
) {
963 dtrace_ls_initialized
= TRUE
;
964 dtrace_rwl_excl_spin
= (lockstat_probemap
[LS_LCK_RW_LOCK_EXCL_SPIN
] != 0);
965 dtrace_rwl_excl_block
= (lockstat_probemap
[LS_LCK_RW_LOCK_EXCL_BLOCK
] != 0);
966 dtrace_ls_enabled
= dtrace_rwl_excl_spin
|| dtrace_rwl_excl_block
;
967 if (dtrace_ls_enabled
) {
969 * Either sleeping or spinning is happening,
970 * start a timing of our delay interval now.
972 readers_at_sleep
= lck
->lck_rw_shared_count
;
973 wait_interval
= mach_absolute_time();
978 istate
= ml_get_interrupts_enabled();
980 deadline
= lck_rw_deadline_for_spin(lck
);
982 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_READER_SPIN_CODE
) | DBG_FUNC_START
, (int)lck
, 0, 0, 0, 0);
984 while ((lockheld
= lck_rw_held_read_or_upgrade(lck
)) && mach_absolute_time() < deadline
)
985 lck_rw_lock_pause(istate
);
987 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_READER_SPIN_CODE
) | DBG_FUNC_END
, (int)lck
, 0, 0, lockheld
, 0);
992 * if we get here, the deadline has expired w/o us
993 * being able to grab the lock exclusively
994 * check to see if we're allowed to do a thread_block
996 if (lck
->lck_rw_can_sleep
) {
998 istate
= lck_interlock_lock(lck
);
1000 if (lck
->lck_rw_shared_count
!= 0 || lck
->lck_rw_want_upgrade
) {
1001 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_READER_WAIT_CODE
) | DBG_FUNC_START
, (int)lck
, 0, 0, 0, 0);
1003 lck
->lck_w_waiting
= TRUE
;
1005 res
= assert_wait(RW_LOCK_WRITER_EVENT(lck
), THREAD_UNINT
);
1006 lck_interlock_unlock(lck
, istate
);
1008 if (res
== THREAD_WAITING
) {
1009 res
= thread_block(THREAD_CONTINUE_NULL
);
1012 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_READER_WAIT_CODE
) | DBG_FUNC_END
, (int)lck
, res
, slept
, 0, 0);
1014 lck_interlock_unlock(lck
, istate
);
1016 * must own the lock now, since we checked for
1017 * readers or upgrade owner behind the interlock
1018 * no need for a call to 'lck_rw_held_read_or_upgrade'
1027 * Decide what latencies we suffered that are Dtrace events.
1028 * If we have set wait_interval, then we either spun or slept.
1029 * At least we get out from under the interlock before we record
1030 * which is the best we can do here to minimize the impact
1032 * If we have set wait_interval to -1, then dtrace was not enabled when we
1033 * started sleeping/spinning so we don't record this event.
1035 if (dtrace_ls_enabled
== TRUE
) {
1037 LOCKSTAT_RECORD2(LS_LCK_RW_LOCK_EXCL_SPIN
, lck
,
1038 mach_absolute_time() - wait_interval
, 1);
1041 * For the blocking case, we also record if when we blocked
1042 * it was held for read or write, and how many readers.
1043 * Notice that above we recorded this before we dropped
1044 * the interlock so the count is accurate.
1046 LOCKSTAT_RECORD4(LS_LCK_RW_LOCK_EXCL_BLOCK
, lck
,
1047 mach_absolute_time() - wait_interval
, 1,
1048 (readers_at_sleep
== 0 ? 1 : 0), readers_at_sleep
);
1051 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_EXCL_ACQUIRE
, lck
, 1);
1057 * Routine: lck_rw_done_gen
1059 * called from the assembly language wrapper...
1060 * prior_lock_state is the value in the 1st
1061 * word of the lock at the time of a successful
1062 * atomic compare and exchange with the new value...
1063 * it represents the state of the lock before we
1064 * decremented the rw_shared_count or cleared either
1065 * rw_want_upgrade or rw_want_write and
1066 * the lck_x_waiting bits... since the wrapper
1067 * routine has already changed the state atomically,
1068 * we just need to decide if we should
1069 * wake up anyone and what value to return... we do
1070 * this by examining the state of the lock before
1076 int prior_lock_state
)
1079 lck_rw_type_t lock_type
;
1081 uint32_t rwlock_count
;
1084 * prior_lock state is a snapshot of the 1st word of the
1085 * lock in question... we'll fake up a pointer to it
1086 * and carefully not access anything beyond whats defined
1087 * in the first word of a lck_rw_t
1089 fake_lck
= (lck_rw_t
*)&prior_lock_state
;
1091 if (fake_lck
->lck_rw_shared_count
<= 1) {
1092 if (fake_lck
->lck_w_waiting
)
1093 thread_wakeup(RW_LOCK_WRITER_EVENT(lck
));
1095 if (!(fake_lck
->lck_rw_priv_excl
&& fake_lck
->lck_w_waiting
) && fake_lck
->lck_r_waiting
)
1096 thread_wakeup(RW_LOCK_READER_EVENT(lck
));
1098 if (fake_lck
->lck_rw_shared_count
)
1099 lock_type
= LCK_RW_TYPE_SHARED
;
1101 lock_type
= LCK_RW_TYPE_EXCLUSIVE
;
1103 /* Check if dropping the lock means that we need to unpromote */
1104 thread
= current_thread();
1105 rwlock_count
= thread
->rwlock_count
--;
1107 if (rwlock_count
== 0) {
1108 panic("rw lock count underflow for thread %p", thread
);
1111 if ((rwlock_count
== 1 /* field now 0 */) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
1112 /* sched_flags checked without lock, but will be rechecked while clearing */
1113 lck_rw_clear_promotion(thread
);
1117 LOCKSTAT_RECORD(LS_LCK_RW_DONE_RELEASE
, lck
, lock_type
== LCK_RW_TYPE_SHARED
? 0 : 1);
1125 * Routine: lck_rw_unlock
1130 lck_rw_type_t lck_rw_type
)
1132 if (lck_rw_type
== LCK_RW_TYPE_SHARED
)
1133 lck_rw_unlock_shared(lck
);
1134 else if (lck_rw_type
== LCK_RW_TYPE_EXCLUSIVE
)
1135 lck_rw_unlock_exclusive(lck
);
1137 panic("lck_rw_unlock(): Invalid RW lock type: %d\n", lck_rw_type
);
1142 * Routine: lck_rw_unlock_shared
1145 lck_rw_unlock_shared(
1150 ret
= lck_rw_done(lck
);
1152 if (ret
!= LCK_RW_TYPE_SHARED
)
1153 panic("lck_rw_unlock(): lock held in mode: %d\n", ret
);
1158 * Routine: lck_rw_unlock_exclusive
1161 lck_rw_unlock_exclusive(
1166 ret
= lck_rw_done(lck
);
1168 if (ret
!= LCK_RW_TYPE_EXCLUSIVE
)
1169 panic("lck_rw_unlock_exclusive(): lock held in mode: %d\n", ret
);
1174 * Routine: lck_rw_lock
1179 lck_rw_type_t lck_rw_type
)
1181 if (lck_rw_type
== LCK_RW_TYPE_SHARED
)
1182 lck_rw_lock_shared(lck
);
1183 else if (lck_rw_type
== LCK_RW_TYPE_EXCLUSIVE
)
1184 lck_rw_lock_exclusive(lck
);
1186 panic("lck_rw_lock(): Invalid RW lock type: %x\n", lck_rw_type
);
1191 * Routine: lck_rw_lock_shared_gen
1193 * assembly fast path code has determined that this lock
1194 * is held exclusively... this is where we spin/block
1195 * until we can acquire the lock in the shared mode
1198 lck_rw_lock_shared_gen(
1201 uint64_t deadline
= 0;
1204 wait_result_t res
= 0;
1205 boolean_t istate
= -1;
1208 uint64_t wait_interval
= 0;
1209 int readers_at_sleep
= 0;
1210 boolean_t dtrace_ls_initialized
= FALSE
;
1211 boolean_t dtrace_rwl_shared_spin
, dtrace_rwl_shared_block
, dtrace_ls_enabled
= FALSE
;
1214 while ( !lck_rw_grab_shared(lck
)) {
1217 if (dtrace_ls_initialized
== FALSE
) {
1218 dtrace_ls_initialized
= TRUE
;
1219 dtrace_rwl_shared_spin
= (lockstat_probemap
[LS_LCK_RW_LOCK_SHARED_SPIN
] != 0);
1220 dtrace_rwl_shared_block
= (lockstat_probemap
[LS_LCK_RW_LOCK_SHARED_BLOCK
] != 0);
1221 dtrace_ls_enabled
= dtrace_rwl_shared_spin
|| dtrace_rwl_shared_block
;
1222 if (dtrace_ls_enabled
) {
1224 * Either sleeping or spinning is happening,
1225 * start a timing of our delay interval now.
1227 readers_at_sleep
= lck
->lck_rw_shared_count
;
1228 wait_interval
= mach_absolute_time();
1233 istate
= ml_get_interrupts_enabled();
1235 deadline
= lck_rw_deadline_for_spin(lck
);
1237 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_SPIN_CODE
) | DBG_FUNC_START
,
1238 (int)lck
, lck
->lck_rw_want_write
, lck
->lck_rw_want_upgrade
, 0, 0);
1240 while (((gotlock
= lck_rw_grab_shared(lck
)) == 0) && mach_absolute_time() < deadline
)
1241 lck_rw_lock_pause(istate
);
1243 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_SPIN_CODE
) | DBG_FUNC_END
,
1244 (int)lck
, lck
->lck_rw_want_write
, lck
->lck_rw_want_upgrade
, gotlock
, 0);
1249 * if we get here, the deadline has expired w/o us
1250 * being able to grab the lock for read
1251 * check to see if we're allowed to do a thread_block
1253 if (lck
->lck_rw_can_sleep
) {
1255 istate
= lck_interlock_lock(lck
);
1257 if ((lck
->lck_rw_want_write
|| lck
->lck_rw_want_upgrade
) &&
1258 ((lck
->lck_rw_shared_count
== 0) || lck
->lck_rw_priv_excl
)) {
1260 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_WAIT_CODE
) | DBG_FUNC_START
,
1261 (int)lck
, lck
->lck_rw_want_write
, lck
->lck_rw_want_upgrade
, 0, 0);
1263 lck
->lck_r_waiting
= TRUE
;
1265 res
= assert_wait(RW_LOCK_READER_EVENT(lck
), THREAD_UNINT
);
1266 lck_interlock_unlock(lck
, istate
);
1268 if (res
== THREAD_WAITING
) {
1269 res
= thread_block(THREAD_CONTINUE_NULL
);
1272 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SHARED_WAIT_CODE
) | DBG_FUNC_END
,
1273 (int)lck
, res
, slept
, 0, 0);
1275 lck
->lck_rw_shared_count
++;
1276 lck_interlock_unlock(lck
, istate
);
1283 if (dtrace_ls_enabled
== TRUE
) {
1285 LOCKSTAT_RECORD2(LS_LCK_RW_LOCK_SHARED_SPIN
, lck
, mach_absolute_time() - wait_interval
, 0);
1287 LOCKSTAT_RECORD4(LS_LCK_RW_LOCK_SHARED_BLOCK
, lck
,
1288 mach_absolute_time() - wait_interval
, 0,
1289 (readers_at_sleep
== 0 ? 1 : 0), readers_at_sleep
);
1292 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE
, lck
, 0);
1298 * Routine: lck_rw_lock_shared_to_exclusive_failure
1300 * assembly fast path code has already dropped our read
1301 * count and determined that someone else owns 'lck_rw_want_upgrade'
1302 * if 'lck_rw_shared_count' == 0, its also already dropped 'lck_w_waiting'
1303 * all we need to do here is determine if a wakeup is needed
1306 lck_rw_lock_shared_to_exclusive_failure(
1308 int prior_lock_state
)
1311 thread_t thread
= current_thread();
1312 uint32_t rwlock_count
;
1314 /* Check if dropping the lock means that we need to unpromote */
1315 rwlock_count
= thread
->rwlock_count
--;
1317 if (rwlock_count
== 0) {
1318 panic("rw lock count underflow for thread %p", thread
);
1321 if ((rwlock_count
== 1 /* field now 0 */) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
1322 /* sched_flags checked without lock, but will be rechecked while clearing */
1323 lck_rw_clear_promotion(thread
);
1327 * prior_lock state is a snapshot of the 1st word of the
1328 * lock in question... we'll fake up a pointer to it
1329 * and carefully not access anything beyond whats defined
1330 * in the first word of a lck_rw_t
1332 fake_lck
= (lck_rw_t
*)&prior_lock_state
;
1334 if (fake_lck
->lck_w_waiting
&& fake_lck
->lck_rw_shared_count
== 1) {
1336 * Someone else has requested upgrade.
1337 * Since we've released the read lock, wake
1338 * him up if he's blocked waiting
1340 thread_wakeup(RW_LOCK_WRITER_EVENT(lck
));
1342 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_CODE
) | DBG_FUNC_NONE
,
1343 (int)lck
, lck
->lck_rw_shared_count
, lck
->lck_rw_want_upgrade
, 0, 0);
1350 * Routine: lck_rw_lock_shared_to_exclusive_failure
1352 * assembly fast path code has already dropped our read
1353 * count and successfully acquired 'lck_rw_want_upgrade'
1354 * we just need to wait for the rest of the readers to drain
1355 * and then we can return as the exclusive holder of this lock
1358 lck_rw_lock_shared_to_exclusive_success(
1361 uint64_t deadline
= 0;
1363 int still_shared
= 0;
1365 boolean_t istate
= -1;
1368 uint64_t wait_interval
= 0;
1369 int readers_at_sleep
= 0;
1370 boolean_t dtrace_ls_initialized
= FALSE
;
1371 boolean_t dtrace_rwl_shared_to_excl_spin
, dtrace_rwl_shared_to_excl_block
, dtrace_ls_enabled
= FALSE
;
1374 while (lck
->lck_rw_shared_count
!= 0) {
1377 if (dtrace_ls_initialized
== FALSE
) {
1378 dtrace_ls_initialized
= TRUE
;
1379 dtrace_rwl_shared_to_excl_spin
= (lockstat_probemap
[LS_LCK_RW_LOCK_SHARED_TO_EXCL_SPIN
] != 0);
1380 dtrace_rwl_shared_to_excl_block
= (lockstat_probemap
[LS_LCK_RW_LOCK_SHARED_TO_EXCL_BLOCK
] != 0);
1381 dtrace_ls_enabled
= dtrace_rwl_shared_to_excl_spin
|| dtrace_rwl_shared_to_excl_block
;
1382 if (dtrace_ls_enabled
) {
1384 * Either sleeping or spinning is happening,
1385 * start a timing of our delay interval now.
1387 readers_at_sleep
= lck
->lck_rw_shared_count
;
1388 wait_interval
= mach_absolute_time();
1393 istate
= ml_get_interrupts_enabled();
1395 deadline
= lck_rw_deadline_for_spin(lck
);
1397 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_SPIN_CODE
) | DBG_FUNC_START
,
1398 (int)lck
, lck
->lck_rw_shared_count
, 0, 0, 0);
1400 while ((still_shared
= lck
->lck_rw_shared_count
) && mach_absolute_time() < deadline
)
1401 lck_rw_lock_pause(istate
);
1403 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_SPIN_CODE
) | DBG_FUNC_END
,
1404 (int)lck
, lck
->lck_rw_shared_count
, 0, 0, 0);
1409 * if we get here, the deadline has expired w/o
1410 * the rw_shared_count having drained to 0
1411 * check to see if we're allowed to do a thread_block
1413 if (lck
->lck_rw_can_sleep
) {
1415 istate
= lck_interlock_lock(lck
);
1417 if (lck
->lck_rw_shared_count
!= 0) {
1418 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_WAIT_CODE
) | DBG_FUNC_START
,
1419 (int)lck
, lck
->lck_rw_shared_count
, 0, 0, 0);
1421 lck
->lck_w_waiting
= TRUE
;
1423 res
= assert_wait(RW_LOCK_WRITER_EVENT(lck
), THREAD_UNINT
);
1424 lck_interlock_unlock(lck
, istate
);
1426 if (res
== THREAD_WAITING
) {
1427 res
= thread_block(THREAD_CONTINUE_NULL
);
1430 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_SH_TO_EX_WAIT_CODE
) | DBG_FUNC_END
,
1431 (int)lck
, res
, slept
, 0, 0);
1433 lck_interlock_unlock(lck
, istate
);
1440 * We infer whether we took the sleep/spin path above by checking readers_at_sleep.
1442 if (dtrace_ls_enabled
== TRUE
) {
1444 LOCKSTAT_RECORD2(LS_LCK_RW_LOCK_SHARED_TO_EXCL_SPIN
, lck
, mach_absolute_time() - wait_interval
, 0);
1446 LOCKSTAT_RECORD4(LS_LCK_RW_LOCK_SHARED_TO_EXCL_BLOCK
, lck
,
1447 mach_absolute_time() - wait_interval
, 1,
1448 (readers_at_sleep
== 0 ? 1 : 0), readers_at_sleep
);
1451 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
, lck
, 1);
1458 * Routine: lck_rw_lock_exclusive_to_shared
1460 * assembly fast path has already dropped
1461 * our exclusive state and bumped lck_rw_shared_count
1462 * all we need to do here is determine if anyone
1463 * needs to be awakened.
1466 lck_rw_lock_exclusive_to_shared_gen(
1468 int prior_lock_state
)
1473 * prior_lock state is a snapshot of the 1st word of the
1474 * lock in question... we'll fake up a pointer to it
1475 * and carefully not access anything beyond whats defined
1476 * in the first word of a lck_rw_t
1478 fake_lck
= (lck_rw_t
*)&prior_lock_state
;
1480 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_TO_SH_CODE
) | DBG_FUNC_START
,
1481 (int)lck
, fake_lck
->lck_rw_want_write
, fake_lck
->lck_rw_want_upgrade
, 0, 0);
1484 * don't wake up anyone waiting to take the lock exclusively
1485 * since we hold a read count... when the read count drops to 0,
1486 * the writers will be woken.
1488 * wake up any waiting readers if we don't have any writers waiting,
1489 * or the lock is NOT marked as rw_priv_excl (writers have privilege)
1491 if (!(fake_lck
->lck_rw_priv_excl
&& fake_lck
->lck_w_waiting
) && fake_lck
->lck_r_waiting
)
1492 thread_wakeup(RW_LOCK_READER_EVENT(lck
));
1494 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_RW_LCK_EX_TO_SH_CODE
) | DBG_FUNC_END
,
1495 (int)lck
, lck
->lck_rw_want_write
, lck
->lck_rw_want_upgrade
, lck
->lck_rw_shared_count
, 0);
1498 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_EXCL_TO_SHARED_DOWNGRADE
, lck
, 0);
1504 * Routine: lck_rw_try_lock
1509 lck_rw_type_t lck_rw_type
)
1511 if (lck_rw_type
== LCK_RW_TYPE_SHARED
)
1512 return(lck_rw_try_lock_shared(lck
));
1513 else if (lck_rw_type
== LCK_RW_TYPE_EXCLUSIVE
)
1514 return(lck_rw_try_lock_exclusive(lck
));
1516 panic("lck_rw_try_lock(): Invalid rw lock type: %x\n", lck_rw_type
);
1527 case LCK_RW_ASSERT_SHARED
:
1528 if (lck
->lck_rw_shared_count
!= 0) {
1532 case LCK_RW_ASSERT_EXCLUSIVE
:
1533 if ((lck
->lck_rw_want_write
||
1534 lck
->lck_rw_want_upgrade
) &&
1535 lck
->lck_rw_shared_count
== 0) {
1539 case LCK_RW_ASSERT_HELD
:
1540 if (lck
->lck_rw_want_write
||
1541 lck
->lck_rw_want_upgrade
||
1542 lck
->lck_rw_shared_count
!= 0) {
1546 case LCK_RW_ASSERT_NOTHELD
:
1547 if (!(lck
->lck_rw_want_write
||
1548 lck
->lck_rw_want_upgrade
||
1549 lck
->lck_rw_shared_count
!= 0)) {
1557 panic("rw lock (%p)%s held (mode=%u), first word %08x\n", lck
, (type
== LCK_RW_ASSERT_NOTHELD
? "" : " not"), type
, *(uint32_t *)lck
);
1560 /* On return to userspace, this routine is called if the rwlock_count is somehow imbalanced */
1562 lck_rw_clear_promotions_x86(thread_t thread
)
1565 /* It's fatal to leave a RW lock locked and return to userspace */
1566 panic("%u rw lock(s) held on return to userspace for thread %p", thread
->rwlock_count
, thread
);
1568 /* Paper over the issue */
1569 thread
->rwlock_count
= 0;
1570 lck_rw_clear_promotion(thread
);
1576 extern zone_t lck_mtx_zone
;
1579 * Routine: lck_mtx_alloc_init
1588 if ((lck
= (lck_mtx_t
*)zalloc(lck_mtx_zone
)) != 0)
1589 lck_mtx_init(lck
, grp
, attr
);
1591 if ((lck
= (lck_mtx_t
*)kalloc(sizeof(lck_mtx_t
))) != 0)
1592 lck_mtx_init(lck
, grp
, attr
);
1598 * Routine: lck_mtx_free
1605 lck_mtx_destroy(lck
, grp
);
1607 zfree(lck_mtx_zone
, lck
);
1609 kfree(lck
, sizeof(lck_mtx_t
));
1614 * Routine: lck_mtx_ext_init
1622 bzero((void *)lck
, sizeof(lck_mtx_ext_t
));
1624 if ((attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
1625 lck
->lck_mtx_deb
.type
= MUTEX_TAG
;
1626 lck
->lck_mtx_attr
|= LCK_MTX_ATTR_DEBUG
;
1629 lck
->lck_mtx_grp
= grp
;
1631 if (grp
->lck_grp_attr
& LCK_GRP_ATTR_STAT
)
1632 lck
->lck_mtx_attr
|= LCK_MTX_ATTR_STAT
;
1634 lck
->lck_mtx
.lck_mtx_is_ext
= 1;
1635 lck
->lck_mtx
.lck_mtx_sw
.lck_mtxd
.lck_mtxd_pad32
= 0xFFFFFFFF;
1639 * Routine: lck_mtx_init
1647 lck_mtx_ext_t
*lck_ext
;
1648 lck_attr_t
*lck_attr
;
1650 if (attr
!= LCK_ATTR_NULL
)
1653 lck_attr
= &LockDefaultLckAttr
;
1655 if ((lck_attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
1656 if ((lck_ext
= (lck_mtx_ext_t
*)kalloc(sizeof(lck_mtx_ext_t
))) != 0) {
1657 lck_mtx_ext_init(lck_ext
, grp
, lck_attr
);
1658 lck
->lck_mtx_tag
= LCK_MTX_TAG_INDIRECT
;
1659 lck
->lck_mtx_ptr
= lck_ext
;
1662 lck
->lck_mtx_owner
= 0;
1663 lck
->lck_mtx_state
= 0;
1665 lck
->lck_mtx_sw
.lck_mtxd
.lck_mtxd_pad32
= 0xFFFFFFFF;
1666 lck_grp_reference(grp
);
1667 lck_grp_lckcnt_incr(grp
, LCK_TYPE_MTX
);
1671 * Routine: lck_mtx_init_ext
1676 lck_mtx_ext_t
*lck_ext
,
1680 lck_attr_t
*lck_attr
;
1682 if (attr
!= LCK_ATTR_NULL
)
1685 lck_attr
= &LockDefaultLckAttr
;
1687 if ((lck_attr
->lck_attr_val
) & LCK_ATTR_DEBUG
) {
1688 lck_mtx_ext_init(lck_ext
, grp
, lck_attr
);
1689 lck
->lck_mtx_tag
= LCK_MTX_TAG_INDIRECT
;
1690 lck
->lck_mtx_ptr
= lck_ext
;
1692 lck
->lck_mtx_owner
= 0;
1693 lck
->lck_mtx_state
= 0;
1695 lck
->lck_mtx_sw
.lck_mtxd
.lck_mtxd_pad32
= 0xFFFFFFFF;
1697 lck_grp_reference(grp
);
1698 lck_grp_lckcnt_incr(grp
, LCK_TYPE_MTX
);
1702 * Routine: lck_mtx_destroy
1709 boolean_t lck_is_indirect
;
1711 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_DESTROYED
)
1714 lck_mtx_assert(lck
, LCK_MTX_ASSERT_NOTOWNED
);
1716 lck_is_indirect
= (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
);
1718 lck_mtx_lock_mark_destroyed(lck
);
1720 if (lck_is_indirect
)
1721 kfree(lck
->lck_mtx_ptr
, sizeof(lck_mtx_ext_t
));
1722 lck_grp_lckcnt_decr(grp
, LCK_TYPE_MTX
);
1723 lck_grp_deallocate(grp
);
1728 #define LCK_MTX_LCK_WAIT_CODE 0x20
1729 #define LCK_MTX_LCK_WAKEUP_CODE 0x21
1730 #define LCK_MTX_LCK_SPIN_CODE 0x22
1731 #define LCK_MTX_LCK_ACQUIRE_CODE 0x23
1732 #define LCK_MTX_LCK_DEMOTE_CODE 0x24
1736 * Routine: lck_mtx_unlock_wakeup_x86
1738 * Invoked on unlock when there is
1739 * contention (i.e. the assembly routine sees that
1740 * that mutex->lck_mtx_waiters != 0 or
1741 * that mutex->lck_mtx_promoted != 0...
1743 * neither the mutex or interlock is held
1746 lck_mtx_unlock_wakeup_x86 (
1748 int prior_lock_state
)
1753 * prior_lock state is a snapshot of the 2nd word of the
1754 * lock in question... we'll fake up a lock with the bits
1755 * copied into place and carefully not access anything
1756 * beyond whats defined in the second word of a lck_mtx_t
1758 fake_lck
.lck_mtx_state
= prior_lock_state
;
1760 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAKEUP_CODE
) | DBG_FUNC_START
,
1761 mutex
, fake_lck
.lck_mtx_promoted
, fake_lck
.lck_mtx_waiters
, fake_lck
.lck_mtx_pri
, 0);
1763 if (__probable(fake_lck
.lck_mtx_waiters
)) {
1764 if (fake_lck
.lck_mtx_waiters
> 1)
1765 thread_wakeup_one_with_pri((event_t
)(((unsigned int*)mutex
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)), fake_lck
.lck_mtx_pri
);
1767 thread_wakeup_one((event_t
)(((unsigned int*)mutex
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
1770 if (__improbable(fake_lck
.lck_mtx_promoted
)) {
1771 thread_t thread
= current_thread();
1774 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_DEMOTE_CODE
) | DBG_FUNC_NONE
,
1775 thread_tid(thread
), thread
->promotions
, thread
->sched_flags
& TH_SFLAG_PROMOTED
, 0, 0);
1777 if (thread
->promotions
> 0) {
1778 spl_t s
= splsched();
1780 thread_lock(thread
);
1782 if (--thread
->promotions
== 0 && (thread
->sched_flags
& TH_SFLAG_PROMOTED
)) {
1784 thread
->sched_flags
&= ~TH_SFLAG_PROMOTED
;
1786 if (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
) {
1787 /* Thread still has a RW lock promotion */
1788 } else if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
1789 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DEMOTE
) | DBG_FUNC_NONE
,
1790 thread
->sched_pri
, DEPRESSPRI
, 0, mutex
, 0);
1792 set_sched_pri(thread
, DEPRESSPRI
);
1795 if (thread
->priority
< thread
->sched_pri
) {
1796 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DEMOTE
) | DBG_FUNC_NONE
,
1797 thread
->sched_pri
, thread
->priority
, 0, mutex
, 0);
1799 SCHED(compute_priority
)(thread
, FALSE
);
1803 thread_unlock(thread
);
1807 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAKEUP_CODE
) | DBG_FUNC_END
,
1808 mutex
, 0, mutex
->lck_mtx_waiters
, 0, 0);
1813 * Routine: lck_mtx_lock_acquire_x86
1815 * Invoked on acquiring the mutex when there is
1816 * contention (i.e. the assembly routine sees that
1817 * that mutex->lck_mtx_waiters != 0 or
1818 * thread->was_promoted_on_wakeup != 0)...
1820 * mutex is owned... interlock is held... preemption is disabled
1823 lck_mtx_lock_acquire_x86(
1830 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_ACQUIRE_CODE
) | DBG_FUNC_START
,
1831 mutex
, thread
->was_promoted_on_wakeup
, mutex
->lck_mtx_waiters
, mutex
->lck_mtx_pri
, 0);
1833 if (mutex
->lck_mtx_waiters
)
1834 priority
= mutex
->lck_mtx_pri
;
1838 thread
= (thread_t
)mutex
->lck_mtx_owner
; /* faster then current_thread() */
1840 if (thread
->sched_pri
< priority
|| thread
->was_promoted_on_wakeup
) {
1842 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_PROMOTE
) | DBG_FUNC_NONE
,
1843 thread
->sched_pri
, priority
, thread
->was_promoted_on_wakeup
, mutex
, 0);
1846 thread_lock(thread
);
1848 if (thread
->sched_pri
< priority
) {
1849 /* Do not promote past promotion ceiling */
1850 assert(priority
<= MAXPRI_PROMOTE
);
1851 set_sched_pri(thread
, priority
);
1853 if (mutex
->lck_mtx_promoted
== 0) {
1854 mutex
->lck_mtx_promoted
= 1;
1856 thread
->promotions
++;
1857 thread
->sched_flags
|= TH_SFLAG_PROMOTED
;
1859 thread
->was_promoted_on_wakeup
= 0;
1861 thread_unlock(thread
);
1864 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_ACQUIRE_CODE
) | DBG_FUNC_END
,
1865 mutex
, 0, mutex
->lck_mtx_waiters
, 0, 0);
1871 * Routine: lck_mtx_lock_spinwait_x86
1873 * Invoked trying to acquire a mutex when there is contention but
1874 * the holder is running on another processor. We spin for up to a maximum
1875 * time waiting for the lock to be released.
1877 * Called with the interlock unlocked.
1878 * returns 0 if mutex acquired
1879 * returns 1 if we spun
1880 * returns 2 if we didn't spin due to the holder not running
1883 lck_mtx_lock_spinwait_x86(
1892 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_SPIN_CODE
) | DBG_FUNC_START
,
1893 mutex
, mutex
->lck_mtx_owner
, mutex
->lck_mtx_waiters
, 0, 0);
1895 deadline
= mach_absolute_time() + MutexSpin
;
1899 * - mutex is locked, and
1900 * - its locked as a spin lock, and
1901 * - owner is running on another processor, and
1902 * - owner (processor) is not idling, and
1903 * - we haven't spun for long enough.
1906 if (__probable(lck_mtx_lock_grab_mutex(mutex
))) {
1910 if ((holder
= (thread_t
) mutex
->lck_mtx_owner
) != NULL
) {
1912 if ( !(holder
->machine
.specFlags
& OnProc
) ||
1913 (holder
->state
& TH_IDLE
)) {
1923 } while (mach_absolute_time() < deadline
);
1928 * We've already kept a count via deadline of how long we spun.
1929 * If dtrace is active, then we compute backwards to decide how
1932 * Note that we record a different probe id depending on whether
1933 * this is a direct or indirect mutex. This allows us to
1934 * penalize only lock groups that have debug/stats enabled
1935 * with dtrace processing if desired.
1937 if (__probable(mutex
->lck_mtx_is_ext
== 0)) {
1938 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN
, mutex
,
1939 mach_absolute_time() - (deadline
- MutexSpin
));
1941 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_SPIN
, mutex
,
1942 mach_absolute_time() - (deadline
- MutexSpin
));
1944 /* The lockstat acquire event is recorded by the assembly code beneath us. */
1947 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_SPIN_CODE
) | DBG_FUNC_END
,
1948 mutex
, mutex
->lck_mtx_owner
, mutex
->lck_mtx_waiters
, retval
, 0);
1956 * Routine: lck_mtx_lock_wait_x86
1958 * Invoked in order to wait on contention.
1960 * Called with the interlock locked and
1961 * preemption disabled...
1962 * returns it unlocked and with preemption enabled
1965 lck_mtx_lock_wait_x86 (
1968 thread_t self
= current_thread();
1973 uint64_t sleep_start
= 0;
1975 if (lockstat_probemap
[LS_LCK_MTX_LOCK_BLOCK
] || lockstat_probemap
[LS_LCK_MTX_EXT_LOCK_BLOCK
]) {
1976 sleep_start
= mach_absolute_time();
1979 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_START
,
1980 mutex
, mutex
->lck_mtx_owner
, mutex
->lck_mtx_waiters
, mutex
->lck_mtx_pri
, 0);
1982 priority
= self
->sched_pri
;
1984 if (priority
< self
->priority
)
1985 priority
= self
->priority
;
1986 if (priority
< BASEPRI_DEFAULT
)
1987 priority
= BASEPRI_DEFAULT
;
1989 /* Do not promote past promotion ceiling */
1990 priority
= MIN(priority
, MAXPRI_PROMOTE
);
1992 if (mutex
->lck_mtx_waiters
== 0 || priority
> mutex
->lck_mtx_pri
)
1993 mutex
->lck_mtx_pri
= priority
;
1994 mutex
->lck_mtx_waiters
++;
1996 if ( (holder
= (thread_t
)mutex
->lck_mtx_owner
) &&
1997 holder
->sched_pri
< mutex
->lck_mtx_pri
) {
1999 thread_lock(holder
);
2001 /* holder priority may have been bumped by another thread
2002 * before thread_lock was taken
2004 if (holder
->sched_pri
< mutex
->lck_mtx_pri
) {
2005 KERNEL_DEBUG_CONSTANT(
2006 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_PROMOTE
) | DBG_FUNC_NONE
,
2007 holder
->sched_pri
, priority
, thread_tid(holder
), mutex
, 0);
2008 /* Assert that we're not altering the priority of a
2009 * thread above the MAXPRI_PROMOTE band
2011 assert(holder
->sched_pri
< MAXPRI_PROMOTE
);
2012 set_sched_pri(holder
, priority
);
2014 if (mutex
->lck_mtx_promoted
== 0) {
2015 holder
->promotions
++;
2016 holder
->sched_flags
|= TH_SFLAG_PROMOTED
;
2018 mutex
->lck_mtx_promoted
= 1;
2021 thread_unlock(holder
);
2024 assert_wait((event_t
)(((unsigned int*)mutex
)+((sizeof(lck_mtx_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
2026 lck_mtx_ilk_unlock(mutex
);
2028 thread_block(THREAD_CONTINUE_NULL
);
2030 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_END
,
2031 mutex
, mutex
->lck_mtx_owner
, mutex
->lck_mtx_waiters
, mutex
->lck_mtx_pri
, 0);
2035 * Record the Dtrace lockstat probe for blocking, block time
2036 * measured from when we were entered.
2039 if (mutex
->lck_mtx_is_ext
== 0) {
2040 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_BLOCK
, mutex
,
2041 mach_absolute_time() - sleep_start
);
2043 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_BLOCK
, mutex
,
2044 mach_absolute_time() - sleep_start
);