2 * Copyright (c) 2000-2007 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.
57 #include <mach_ldebug.h>
60 #include <mach/kern_return.h>
61 #include <mach/mach_host_server.h>
62 #include <mach_debug/lockgroup_info.h>
64 #include <kern/locks.h>
65 #include <kern/misc_protos.h>
66 #include <kern/kalloc.h>
67 #include <kern/thread.h>
68 #include <kern/processor.h>
69 #include <kern/sched_prim.h>
70 #include <kern/debug.h>
74 #include <sys/kdebug.h>
78 * We need only enough declarations from the BSD-side to be able to
79 * test if our probe is active, and to call __dtrace_probe(). Setting
80 * NEED_DTRACE_DEFS gets a local copy of those definitions pulled in.
82 #define NEED_DTRACE_DEFS
83 #include <../bsd/sys/lockstat.h>
86 #define LCK_MTX_SLEEP_CODE 0
87 #define LCK_MTX_SLEEP_DEADLINE_CODE 1
88 #define LCK_MTX_LCK_WAIT_CODE 2
89 #define LCK_MTX_UNLCK_WAKEUP_CODE 3
92 static queue_head_t lck_grp_queue
;
93 static unsigned int lck_grp_cnt
;
95 decl_lck_mtx_data(static,lck_grp_lock
)
96 static lck_mtx_ext_t lck_grp_lock_ext
;
98 lck_grp_attr_t LockDefaultGroupAttr
;
99 lck_grp_t LockCompatGroup
;
100 lck_attr_t LockDefaultLckAttr
;
103 * Routine: lck_mod_init
111 * Obtain "lcks" options:this currently controls lock statistics
113 if (!PE_parse_boot_argn("lcks", &LcksOpts
, sizeof (LcksOpts
)))
116 queue_init(&lck_grp_queue
);
119 * Need to bootstrap the LockCompatGroup instead of calling lck_grp_init() here. This avoids
120 * grabbing the lck_grp_lock before it is initialized.
123 bzero(&LockCompatGroup
, sizeof(lck_grp_t
));
124 (void) strncpy(LockCompatGroup
.lck_grp_name
, "Compatibility APIs", LCK_GRP_MAX_NAME
);
126 if (LcksOpts
& enaLkStat
)
127 LockCompatGroup
.lck_grp_attr
= LCK_GRP_ATTR_STAT
;
129 LockCompatGroup
.lck_grp_attr
= LCK_ATTR_NONE
;
131 LockCompatGroup
.lck_grp_refcnt
= 1;
133 enqueue_tail(&lck_grp_queue
, (queue_entry_t
)&LockCompatGroup
);
136 lck_grp_attr_setdefault(&LockDefaultGroupAttr
);
137 lck_attr_setdefault(&LockDefaultLckAttr
);
139 lck_mtx_init_ext(&lck_grp_lock
, &lck_grp_lock_ext
, &LockCompatGroup
, &LockDefaultLckAttr
);
144 * Routine: lck_grp_attr_alloc_init
148 lck_grp_attr_alloc_init(
151 lck_grp_attr_t
*attr
;
153 if ((attr
= (lck_grp_attr_t
*)kalloc(sizeof(lck_grp_attr_t
))) != 0)
154 lck_grp_attr_setdefault(attr
);
161 * Routine: lck_grp_attr_setdefault
165 lck_grp_attr_setdefault(
166 lck_grp_attr_t
*attr
)
168 if (LcksOpts
& enaLkStat
)
169 attr
->grp_attr_val
= LCK_GRP_ATTR_STAT
;
171 attr
->grp_attr_val
= 0;
176 * Routine: lck_grp_attr_setstat
180 lck_grp_attr_setstat(
181 lck_grp_attr_t
*attr
)
183 (void)hw_atomic_or(&attr
->grp_attr_val
, LCK_GRP_ATTR_STAT
);
188 * Routine: lck_grp_attr_free
193 lck_grp_attr_t
*attr
)
195 kfree(attr
, sizeof(lck_grp_attr_t
));
200 * Routine: lck_grp_alloc_init
205 const char* grp_name
,
206 lck_grp_attr_t
*attr
)
210 if ((grp
= (lck_grp_t
*)kalloc(sizeof(lck_grp_t
))) != 0)
211 lck_grp_init(grp
, grp_name
, attr
);
218 * Routine: lck_grp_init
224 const char* grp_name
,
225 lck_grp_attr_t
*attr
)
227 bzero((void *)grp
, sizeof(lck_grp_t
));
229 (void) strncpy(grp
->lck_grp_name
, grp_name
, LCK_GRP_MAX_NAME
);
231 if (attr
!= LCK_GRP_ATTR_NULL
)
232 grp
->lck_grp_attr
= attr
->grp_attr_val
;
233 else if (LcksOpts
& enaLkStat
)
234 grp
->lck_grp_attr
= LCK_GRP_ATTR_STAT
;
236 grp
->lck_grp_attr
= LCK_ATTR_NONE
;
238 grp
->lck_grp_refcnt
= 1;
240 lck_mtx_lock(&lck_grp_lock
);
241 enqueue_tail(&lck_grp_queue
, (queue_entry_t
)grp
);
243 lck_mtx_unlock(&lck_grp_lock
);
249 * Routine: lck_grp_free
256 lck_mtx_lock(&lck_grp_lock
);
258 (void)remque((queue_entry_t
)grp
);
259 lck_mtx_unlock(&lck_grp_lock
);
260 lck_grp_deallocate(grp
);
265 * Routine: lck_grp_reference
272 (void)hw_atomic_add(&grp
->lck_grp_refcnt
, 1);
277 * Routine: lck_grp_deallocate
284 if (hw_atomic_sub(&grp
->lck_grp_refcnt
, 1) == 0)
285 kfree(grp
, sizeof(lck_grp_t
));
289 * Routine: lck_grp_lckcnt_incr
297 unsigned int *lckcnt
;
301 lckcnt
= &grp
->lck_grp_spincnt
;
304 lckcnt
= &grp
->lck_grp_mtxcnt
;
307 lckcnt
= &grp
->lck_grp_rwcnt
;
310 return panic("lck_grp_lckcnt_incr(): invalid lock type: %d\n", lck_type
);
313 (void)hw_atomic_add(lckcnt
, 1);
317 * Routine: lck_grp_lckcnt_decr
325 unsigned int *lckcnt
;
329 lckcnt
= &grp
->lck_grp_spincnt
;
332 lckcnt
= &grp
->lck_grp_mtxcnt
;
335 lckcnt
= &grp
->lck_grp_rwcnt
;
338 return panic("lck_grp_lckcnt_decr(): invalid lock type: %d\n", lck_type
);
341 (void)hw_atomic_sub(lckcnt
, 1);
345 * Routine: lck_attr_alloc_init
354 if ((attr
= (lck_attr_t
*)kalloc(sizeof(lck_attr_t
))) != 0)
355 lck_attr_setdefault(attr
);
362 * Routine: lck_attr_setdefault
370 if (LcksOpts
& enaLkDeb
)
371 attr
->lck_attr_val
= LCK_ATTR_DEBUG
;
373 attr
->lck_attr_val
= LCK_ATTR_NONE
;
375 attr
->lck_attr_val
= LCK_ATTR_DEBUG
;
381 * Routine: lck_attr_setdebug
387 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_DEBUG
);
391 * Routine: lck_attr_setdebug
397 (void)hw_atomic_and(&attr
->lck_attr_val
, ~LCK_ATTR_DEBUG
);
402 * Routine: lck_attr_rw_shared_priority
405 lck_attr_rw_shared_priority(
408 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_RW_SHARED_PRIORITY
);
413 * Routine: lck_attr_free
419 kfree(attr
, sizeof(lck_attr_t
));
424 * Routine: lck_spin_sleep
429 lck_sleep_action_t lck_sleep_action
,
431 wait_interrupt_t interruptible
)
435 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
436 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
438 res
= assert_wait(event
, interruptible
);
439 if (res
== THREAD_WAITING
) {
440 lck_spin_unlock(lck
);
441 res
= thread_block(THREAD_CONTINUE_NULL
);
442 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
446 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
447 lck_spin_unlock(lck
);
454 * Routine: lck_spin_sleep_deadline
457 lck_spin_sleep_deadline(
459 lck_sleep_action_t lck_sleep_action
,
461 wait_interrupt_t interruptible
,
466 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
467 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
469 res
= assert_wait_deadline(event
, interruptible
, deadline
);
470 if (res
== THREAD_WAITING
) {
471 lck_spin_unlock(lck
);
472 res
= thread_block(THREAD_CONTINUE_NULL
);
473 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
477 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
478 lck_spin_unlock(lck
);
485 * Routine: lck_mtx_sleep
490 lck_sleep_action_t lck_sleep_action
,
492 wait_interrupt_t interruptible
)
496 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_START
,
497 (int)lck
, (int)lck_sleep_action
, (int)event
, (int)interruptible
, 0);
499 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
500 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
502 res
= assert_wait(event
, interruptible
);
503 if (res
== THREAD_WAITING
) {
505 res
= thread_block(THREAD_CONTINUE_NULL
);
506 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
507 if ((lck_sleep_action
& LCK_SLEEP_SPIN
))
508 lck_mtx_lock_spin(lck
);
514 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
517 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
524 * Routine: lck_mtx_sleep_deadline
527 lck_mtx_sleep_deadline(
529 lck_sleep_action_t lck_sleep_action
,
531 wait_interrupt_t interruptible
,
536 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_START
,
537 (int)lck
, (int)lck_sleep_action
, (int)event
, (int)interruptible
, 0);
539 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
540 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
542 res
= assert_wait_deadline(event
, interruptible
, deadline
);
543 if (res
== THREAD_WAITING
) {
545 res
= thread_block(THREAD_CONTINUE_NULL
);
546 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
547 if ((lck_sleep_action
& LCK_SLEEP_SPIN
))
548 lck_mtx_lock_spin(lck
);
554 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
557 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
563 * Routine: lck_mtx_lock_wait
565 * Invoked in order to wait on contention.
567 * Called with the interlock locked and
568 * returns it unlocked.
575 thread_t self
= current_thread();
578 spl_t s
= splsched();
580 uint64_t sleep_start
= 0;
582 if (lockstat_probemap
[LS_LCK_MTX_LOCK_BLOCK
] || lockstat_probemap
[LS_LCK_MTX_EXT_LOCK_BLOCK
]) {
583 sleep_start
= mach_absolute_time();
587 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
590 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
592 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
594 priority
= self
->sched_pri
;
595 if (priority
< self
->priority
)
596 priority
= self
->priority
;
597 if (priority
< BASEPRI_DEFAULT
)
598 priority
= BASEPRI_DEFAULT
;
601 if (mutex
->lck_mtx_pri
== 0)
602 holder
->promotions
++;
603 holder
->sched_flags
|= TH_SFLAG_PROMOTED
;
604 if ( mutex
->lck_mtx_pri
< priority
&&
605 holder
->sched_pri
< priority
) {
606 KERNEL_DEBUG_CONSTANT(
607 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
608 holder
->sched_pri
, priority
, holder
, lck
, 0);
610 set_sched_pri(holder
, priority
);
612 thread_unlock(holder
);
615 if (mutex
->lck_mtx_pri
< priority
)
616 mutex
->lck_mtx_pri
= priority
;
617 if (self
->pending_promoter
[self
->pending_promoter_index
] == NULL
) {
618 self
->pending_promoter
[self
->pending_promoter_index
] = mutex
;
619 mutex
->lck_mtx_waiters
++;
622 if (self
->pending_promoter
[self
->pending_promoter_index
] != mutex
) {
623 self
->pending_promoter
[++self
->pending_promoter_index
] = mutex
;
624 mutex
->lck_mtx_waiters
++;
627 assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_mtx_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
628 lck_mtx_ilk_unlock(mutex
);
630 thread_block(THREAD_CONTINUE_NULL
);
632 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
635 * Record the Dtrace lockstat probe for blocking, block time
636 * measured from when we were entered.
639 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
) {
640 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_BLOCK
, lck
,
641 mach_absolute_time() - sleep_start
);
643 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_BLOCK
, lck
,
644 mach_absolute_time() - sleep_start
);
651 * Routine: lck_mtx_lock_acquire
653 * Invoked on acquiring the mutex when there is
656 * Returns the current number of waiters.
658 * Called with the interlock locked.
661 lck_mtx_lock_acquire(
664 thread_t thread
= current_thread();
667 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
670 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
672 if (thread
->pending_promoter
[thread
->pending_promoter_index
] == mutex
) {
673 thread
->pending_promoter
[thread
->pending_promoter_index
] = NULL
;
674 if (thread
->pending_promoter_index
> 0)
675 thread
->pending_promoter_index
--;
676 mutex
->lck_mtx_waiters
--;
679 if (mutex
->lck_mtx_waiters
> 0) {
680 integer_t priority
= mutex
->lck_mtx_pri
;
681 spl_t s
= splsched();
684 thread
->promotions
++;
685 thread
->sched_flags
|= TH_SFLAG_PROMOTED
;
686 if (thread
->sched_pri
< priority
) {
687 KERNEL_DEBUG_CONSTANT(
688 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
689 thread
->sched_pri
, priority
, 0, lck
, 0);
691 set_sched_pri(thread
, priority
);
693 thread_unlock(thread
);
697 mutex
->lck_mtx_pri
= 0;
699 return (mutex
->lck_mtx_waiters
);
703 * Routine: lck_mtx_unlock_wakeup
705 * Invoked on unlock when there is contention.
707 * Called with the interlock locked.
710 lck_mtx_unlock_wakeup (
714 thread_t thread
= current_thread();
717 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
720 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
722 if (thread
!= holder
)
723 panic("lck_mtx_unlock_wakeup: mutex %p holder %p\n", mutex
, holder
);
725 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
727 assert(mutex
->lck_mtx_waiters
> 0);
728 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
730 if (thread
->promotions
> 0) {
731 spl_t s
= splsched();
734 if ( --thread
->promotions
== 0 &&
735 (thread
->sched_flags
& TH_SFLAG_PROMOTED
) ) {
736 thread
->sched_flags
&= ~TH_SFLAG_PROMOTED
;
737 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
738 KERNEL_DEBUG_CONSTANT(
739 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) | DBG_FUNC_NONE
,
740 thread
->sched_pri
, DEPRESSPRI
, 0, lck
, 0);
742 set_sched_pri(thread
, DEPRESSPRI
);
745 if (thread
->priority
< thread
->sched_pri
) {
746 KERNEL_DEBUG_CONSTANT(
747 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) |
749 thread
->sched_pri
, thread
->priority
,
753 SCHED(compute_priority
)(thread
, FALSE
);
756 thread_unlock(thread
);
760 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
764 lck_mtx_unlockspin_wakeup (
767 assert(lck
->lck_mtx_waiters
> 0);
768 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
770 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_NONE
, (int)lck
, 0, 0, 1, 0);
773 * When there are waiters, we skip the hot-patch spot in the
774 * fastpath, so we record it here.
776 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE
, lck
, 0);
782 * Routine: mutex_pause
784 * Called by former callers of simple_lock_pause().
786 #define MAX_COLLISION_COUNTS 32
787 #define MAX_COLLISION 8
789 unsigned int max_collision_count
[MAX_COLLISION_COUNTS
];
791 uint32_t collision_backoffs
[MAX_COLLISION
] = {
792 10, 50, 100, 200, 400, 600, 800, 1000
797 mutex_pause(uint32_t collisions
)
799 wait_result_t wait_result
;
802 if (collisions
>= MAX_COLLISION_COUNTS
)
803 collisions
= MAX_COLLISION_COUNTS
- 1;
804 max_collision_count
[collisions
]++;
806 if (collisions
>= MAX_COLLISION
)
807 collisions
= MAX_COLLISION
- 1;
808 back_off
= collision_backoffs
[collisions
];
810 wait_result
= assert_wait_timeout((event_t
)mutex_pause
, THREAD_UNINT
, back_off
, NSEC_PER_USEC
);
811 assert(wait_result
== THREAD_WAITING
);
813 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
814 assert(wait_result
== THREAD_TIMED_OUT
);
818 unsigned int mutex_yield_wait
= 0;
819 unsigned int mutex_yield_no_wait
= 0;
828 lck_mtx_assert(lck
, LCK_MTX_ASSERT_OWNED
);
831 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
)
832 waiters
= lck
->lck_mtx_ptr
->lck_mtx
.lck_mtx_waiters
;
834 waiters
= lck
->lck_mtx_waiters
;
837 mutex_yield_no_wait
++;
848 * Routine: lck_rw_sleep
853 lck_sleep_action_t lck_sleep_action
,
855 wait_interrupt_t interruptible
)
858 lck_rw_type_t lck_rw_type
;
860 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
861 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
863 res
= assert_wait(event
, interruptible
);
864 if (res
== THREAD_WAITING
) {
865 lck_rw_type
= lck_rw_done(lck
);
866 res
= thread_block(THREAD_CONTINUE_NULL
);
867 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
868 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
869 lck_rw_lock(lck
, lck_rw_type
);
870 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
871 lck_rw_lock_exclusive(lck
);
873 lck_rw_lock_shared(lck
);
877 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
878 (void)lck_rw_done(lck
);
885 * Routine: lck_rw_sleep_deadline
888 lck_rw_sleep_deadline(
890 lck_sleep_action_t lck_sleep_action
,
892 wait_interrupt_t interruptible
,
896 lck_rw_type_t lck_rw_type
;
898 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
899 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
901 res
= assert_wait_deadline(event
, interruptible
, deadline
);
902 if (res
== THREAD_WAITING
) {
903 lck_rw_type
= lck_rw_done(lck
);
904 res
= thread_block(THREAD_CONTINUE_NULL
);
905 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
906 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
907 lck_rw_lock(lck
, lck_rw_type
);
908 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
909 lck_rw_lock_exclusive(lck
);
911 lck_rw_lock_shared(lck
);
915 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
916 (void)lck_rw_done(lck
);
924 lockgroup_info_array_t
*lockgroup_infop
,
925 mach_msg_type_number_t
*lockgroup_infoCntp
)
927 lockgroup_info_t
*lockgroup_info_base
;
928 lockgroup_info_t
*lockgroup_info
;
929 vm_offset_t lockgroup_info_addr
;
930 vm_size_t lockgroup_info_size
;
937 if (host
== HOST_NULL
)
938 return KERN_INVALID_HOST
;
940 lck_mtx_lock(&lck_grp_lock
);
942 lockgroup_info_size
= round_page(lck_grp_cnt
* sizeof *lockgroup_info
);
943 kr
= kmem_alloc_pageable(ipc_kernel_map
,
944 &lockgroup_info_addr
, lockgroup_info_size
);
945 if (kr
!= KERN_SUCCESS
) {
946 lck_mtx_unlock(&lck_grp_lock
);
950 lockgroup_info_base
= (lockgroup_info_t
*) lockgroup_info_addr
;
951 lck_grp
= (lck_grp_t
*)queue_first(&lck_grp_queue
);
952 lockgroup_info
= lockgroup_info_base
;
954 for (i
= 0; i
< lck_grp_cnt
; i
++) {
956 lockgroup_info
->lock_spin_cnt
= lck_grp
->lck_grp_spincnt
;
957 lockgroup_info
->lock_spin_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_util_cnt
;
958 lockgroup_info
->lock_spin_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cnt
;
959 lockgroup_info
->lock_spin_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_miss_cnt
;
960 lockgroup_info
->lock_spin_held_max
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_max
;
961 lockgroup_info
->lock_spin_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cum
;
963 lockgroup_info
->lock_mtx_cnt
= lck_grp
->lck_grp_mtxcnt
;
964 lockgroup_info
->lock_mtx_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_util_cnt
;
965 lockgroup_info
->lock_mtx_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cnt
;
966 lockgroup_info
->lock_mtx_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_miss_cnt
;
967 lockgroup_info
->lock_mtx_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cnt
;
968 lockgroup_info
->lock_mtx_held_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_max
;
969 lockgroup_info
->lock_mtx_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cum
;
970 lockgroup_info
->lock_mtx_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_max
;
971 lockgroup_info
->lock_mtx_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cum
;
973 lockgroup_info
->lock_rw_cnt
= lck_grp
->lck_grp_rwcnt
;
974 lockgroup_info
->lock_rw_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
;
975 lockgroup_info
->lock_rw_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cnt
;
976 lockgroup_info
->lock_rw_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
;
977 lockgroup_info
->lock_rw_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
;
978 lockgroup_info
->lock_rw_held_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_max
;
979 lockgroup_info
->lock_rw_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cum
;
980 lockgroup_info
->lock_rw_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_max
;
981 lockgroup_info
->lock_rw_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cum
;
983 (void) strncpy(lockgroup_info
->lockgroup_name
,lck_grp
->lck_grp_name
, LOCKGROUP_MAX_NAME
);
985 lck_grp
= (lck_grp_t
*)(queue_next((queue_entry_t
)(lck_grp
)));
989 *lockgroup_infoCntp
= lck_grp_cnt
;
990 lck_mtx_unlock(&lck_grp_lock
);
992 used
= (*lockgroup_infoCntp
) * sizeof *lockgroup_info
;
994 if (used
!= lockgroup_info_size
)
995 bzero((char *) lockgroup_info
, lockgroup_info_size
- used
);
997 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)lockgroup_info_addr
,
998 (vm_map_size_t
)lockgroup_info_size
, TRUE
, ©
);
999 assert(kr
== KERN_SUCCESS
);
1001 *lockgroup_infop
= (lockgroup_info_t
*) copy
;
1003 return(KERN_SUCCESS
);
1007 * Compatibility module
1010 extern lck_rw_t
*lock_alloc_EXT( boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
1011 extern void lock_done_EXT(lck_rw_t
*lock
);
1012 extern void lock_free_EXT(lck_rw_t
*lock
);
1013 extern void lock_init_EXT(lck_rw_t
*lock
, boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
1014 extern void lock_read_EXT(lck_rw_t
*lock
);
1015 extern boolean_t
lock_read_to_write_EXT(lck_rw_t
*lock
);
1016 extern void lock_write_EXT(lck_rw_t
*lock
);
1017 extern void lock_write_to_read_EXT(lck_rw_t
*lock
);
1018 extern wait_result_t
thread_sleep_lock_write_EXT(
1019 event_t event
, lck_rw_t
*lock
, wait_interrupt_t interruptible
);
1021 extern void usimple_lock_EXT(lck_spin_t
*lock
);
1022 extern void usimple_lock_init_EXT(lck_spin_t
*lock
, unsigned short tag
);
1023 extern unsigned int usimple_lock_try_EXT(lck_spin_t
*lock
);
1024 extern void usimple_unlock_EXT(lck_spin_t
*lock
);
1025 extern wait_result_t
thread_sleep_usimple_lock_EXT(event_t event
, lck_spin_t
*lock
, wait_interrupt_t interruptible
);
1028 lck_mtx_t
* mutex_alloc_EXT(__unused
unsigned short tag
);
1029 void mutex_free_EXT(lck_mtx_t
*mutex
);
1030 void mutex_init_EXT(lck_mtx_t
*mutex
, __unused
unsigned short tag
);
1031 wait_result_t
thread_sleep_mutex_EXT(event_t event
, lck_mtx_t
*mutex
, wait_interrupt_t interruptible
);
1032 wait_result_t
thread_sleep_mutex_deadline_EXT(event_t event
, lck_mtx_t
*mutex
, uint64_t deadline
, wait_interrupt_t interruptible
);
1036 __unused boolean_t can_sleep
,
1037 __unused
unsigned short tag0
,
1038 __unused
unsigned short tag1
)
1040 return( lck_rw_alloc_init( &LockCompatGroup
, LCK_ATTR_NULL
));
1047 (void) lck_rw_done(lock
);
1054 lck_rw_free(lock
, &LockCompatGroup
);
1060 __unused boolean_t can_sleep
,
1061 __unused
unsigned short tag0
,
1062 __unused
unsigned short tag1
)
1064 lck_rw_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1071 lck_rw_lock_shared( lock
);
1075 lock_read_to_write_EXT(
1078 return( lck_rw_lock_shared_to_exclusive(lock
));
1085 lck_rw_lock_exclusive(lock
);
1089 lock_write_to_read_EXT(
1092 lck_rw_lock_exclusive_to_shared(lock
);
1096 thread_sleep_lock_write_EXT(
1099 wait_interrupt_t interruptible
)
1101 return( lck_rw_sleep(lock
, LCK_SLEEP_EXCLUSIVE
, event
, interruptible
));
1108 lck_spin_lock(lock
);
1112 usimple_lock_init_EXT(
1114 __unused
unsigned short tag
)
1116 lck_spin_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1120 usimple_lock_try_EXT(
1123 return(lck_spin_try_lock(lock
));
1130 lck_spin_unlock(lock
);
1134 thread_sleep_usimple_lock_EXT(
1137 wait_interrupt_t interruptible
)
1139 return( lck_spin_sleep(lock
, LCK_SLEEP_DEFAULT
, event
, interruptible
));
1143 __unused
unsigned short tag
)
1145 return(lck_mtx_alloc_init(&LockCompatGroup
, LCK_ATTR_NULL
));
1152 lck_mtx_free(mutex
, &LockCompatGroup
);
1158 __unused
unsigned short tag
)
1160 lck_mtx_init(mutex
, &LockCompatGroup
, LCK_ATTR_NULL
);
1164 thread_sleep_mutex_EXT(
1167 wait_interrupt_t interruptible
)
1169 return( lck_mtx_sleep(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
));
1173 thread_sleep_mutex_deadline_EXT(
1177 wait_interrupt_t interruptible
)
1179 return( lck_mtx_sleep_deadline(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
, deadline
));