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_mutex_data(static,lck_grp_lock
)
97 lck_grp_attr_t LockDefaultGroupAttr
;
98 lck_grp_t LockCompatGroup
;
99 lck_attr_t LockDefaultLckAttr
;
102 * Routine: lck_mod_init
109 queue_init(&lck_grp_queue
);
110 mutex_init(&lck_grp_lock
, 0);
112 lck_grp_attr_setdefault( &LockDefaultGroupAttr
);
113 lck_grp_init( &LockCompatGroup
, "Compatibility APIs", LCK_GRP_ATTR_NULL
);
114 lck_attr_setdefault(&LockDefaultLckAttr
);
118 * Routine: lck_grp_attr_alloc_init
122 lck_grp_attr_alloc_init(
125 lck_grp_attr_t
*attr
;
127 if ((attr
= (lck_grp_attr_t
*)kalloc(sizeof(lck_grp_attr_t
))) != 0)
128 lck_grp_attr_setdefault(attr
);
135 * Routine: lck_grp_attr_setdefault
139 lck_grp_attr_setdefault(
140 lck_grp_attr_t
*attr
)
142 if (LcksOpts
& enaLkStat
)
143 attr
->grp_attr_val
= LCK_GRP_ATTR_STAT
;
145 attr
->grp_attr_val
= 0;
150 * Routine: lck_grp_attr_setstat
154 lck_grp_attr_setstat(
155 lck_grp_attr_t
*attr
)
157 (void)hw_atomic_or(&attr
->grp_attr_val
, LCK_GRP_ATTR_STAT
);
162 * Routine: lck_grp_attr_free
167 lck_grp_attr_t
*attr
)
169 kfree(attr
, sizeof(lck_grp_attr_t
));
174 * Routine: lck_grp_alloc_init
179 const char* grp_name
,
180 lck_grp_attr_t
*attr
)
184 if ((grp
= (lck_grp_t
*)kalloc(sizeof(lck_grp_t
))) != 0)
185 lck_grp_init(grp
, grp_name
, attr
);
192 * Routine: lck_grp_init
198 const char* grp_name
,
199 lck_grp_attr_t
*attr
)
201 bzero((void *)grp
, sizeof(lck_grp_t
));
203 (void) strncpy(grp
->lck_grp_name
, grp_name
, LCK_GRP_MAX_NAME
);
205 if (attr
!= LCK_GRP_ATTR_NULL
)
206 grp
->lck_grp_attr
= attr
->grp_attr_val
;
207 else if (LcksOpts
& enaLkStat
)
208 grp
->lck_grp_attr
= LCK_GRP_ATTR_STAT
;
210 grp
->lck_grp_attr
= LCK_ATTR_NONE
;
212 grp
->lck_grp_refcnt
= 1;
214 mutex_lock(&lck_grp_lock
);
215 enqueue_tail(&lck_grp_queue
, (queue_entry_t
)grp
);
217 mutex_unlock(&lck_grp_lock
);
223 * Routine: lck_grp_free
230 mutex_lock(&lck_grp_lock
);
232 (void)remque((queue_entry_t
)grp
);
233 mutex_unlock(&lck_grp_lock
);
234 lck_grp_deallocate(grp
);
239 * Routine: lck_grp_reference
246 (void)hw_atomic_add(&grp
->lck_grp_refcnt
, 1);
251 * Routine: lck_grp_deallocate
258 if (hw_atomic_sub(&grp
->lck_grp_refcnt
, 1) == 0)
259 kfree(grp
, sizeof(lck_grp_t
));
263 * Routine: lck_grp_lckcnt_incr
271 unsigned int *lckcnt
;
275 lckcnt
= &grp
->lck_grp_spincnt
;
278 lckcnt
= &grp
->lck_grp_mtxcnt
;
281 lckcnt
= &grp
->lck_grp_rwcnt
;
284 return panic("lck_grp_lckcnt_incr(): invalid lock type: %d\n", lck_type
);
287 (void)hw_atomic_add(lckcnt
, 1);
291 * Routine: lck_grp_lckcnt_decr
299 unsigned int *lckcnt
;
303 lckcnt
= &grp
->lck_grp_spincnt
;
306 lckcnt
= &grp
->lck_grp_mtxcnt
;
309 lckcnt
= &grp
->lck_grp_rwcnt
;
312 return panic("lck_grp_lckcnt_decr(): invalid lock type: %d\n", lck_type
);
315 (void)hw_atomic_sub(lckcnt
, 1);
319 * Routine: lck_attr_alloc_init
328 if ((attr
= (lck_attr_t
*)kalloc(sizeof(lck_attr_t
))) != 0)
329 lck_attr_setdefault(attr
);
336 * Routine: lck_attr_setdefault
344 if (LcksOpts
& enaLkDeb
)
345 attr
->lck_attr_val
= LCK_ATTR_DEBUG
;
347 attr
->lck_attr_val
= LCK_ATTR_NONE
;
349 attr
->lck_attr_val
= LCK_ATTR_DEBUG
;
355 * Routine: lck_attr_setdebug
361 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_DEBUG
);
365 * Routine: lck_attr_setdebug
371 (void)hw_atomic_and(&attr
->lck_attr_val
, ~LCK_ATTR_DEBUG
);
376 * Routine: lck_attr_rw_shared_priority
379 lck_attr_rw_shared_priority(
382 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_RW_SHARED_PRIORITY
);
387 * Routine: lck_attr_free
393 kfree(attr
, sizeof(lck_attr_t
));
398 * Routine: lck_spin_sleep
403 lck_sleep_action_t lck_sleep_action
,
405 wait_interrupt_t interruptible
)
409 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
410 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
412 res
= assert_wait(event
, interruptible
);
413 if (res
== THREAD_WAITING
) {
414 lck_spin_unlock(lck
);
415 res
= thread_block(THREAD_CONTINUE_NULL
);
416 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
420 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
421 lck_spin_unlock(lck
);
428 * Routine: lck_spin_sleep_deadline
431 lck_spin_sleep_deadline(
433 lck_sleep_action_t lck_sleep_action
,
435 wait_interrupt_t interruptible
,
440 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
441 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
443 res
= assert_wait_deadline(event
, interruptible
, deadline
);
444 if (res
== THREAD_WAITING
) {
445 lck_spin_unlock(lck
);
446 res
= thread_block(THREAD_CONTINUE_NULL
);
447 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
451 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
452 lck_spin_unlock(lck
);
459 * Routine: lck_mtx_sleep
464 lck_sleep_action_t lck_sleep_action
,
466 wait_interrupt_t interruptible
)
470 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_START
,
471 (int)lck
, (int)lck_sleep_action
, (int)event
, (int)interruptible
, 0);
473 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
474 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
476 res
= assert_wait(event
, interruptible
);
477 if (res
== THREAD_WAITING
) {
479 res
= thread_block(THREAD_CONTINUE_NULL
);
480 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
484 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
487 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
494 * Routine: lck_mtx_sleep_deadline
497 lck_mtx_sleep_deadline(
499 lck_sleep_action_t lck_sleep_action
,
501 wait_interrupt_t interruptible
,
506 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_START
,
507 (int)lck
, (int)lck_sleep_action
, (int)event
, (int)interruptible
, 0);
509 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
510 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
512 res
= assert_wait_deadline(event
, interruptible
, deadline
);
513 if (res
== THREAD_WAITING
) {
515 res
= thread_block(THREAD_CONTINUE_NULL
);
516 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
520 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
523 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
529 * Routine: lck_mtx_lock_wait
531 * Invoked in order to wait on contention.
533 * Called with the interlock locked and
534 * returns it unlocked.
541 thread_t self
= current_thread();
544 spl_t s
= splsched();
546 uint64_t sleep_start
= 0;
548 if (lockstat_probemap
[LS_LCK_MTX_LOCK_BLOCK
] || lockstat_probemap
[LS_LCK_MTX_EXT_LOCK_BLOCK
]) {
549 sleep_start
= mach_absolute_time();
553 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
556 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
558 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
560 priority
= self
->sched_pri
;
561 if (priority
< self
->priority
)
562 priority
= self
->priority
;
563 if (priority
< BASEPRI_DEFAULT
)
564 priority
= BASEPRI_DEFAULT
;
567 if (mutex
->lck_mtx_pri
== 0)
568 holder
->promotions
++;
569 holder
->sched_mode
|= TH_MODE_PROMOTED
;
570 if ( mutex
->lck_mtx_pri
< priority
&&
571 holder
->sched_pri
< priority
) {
572 KERNEL_DEBUG_CONSTANT(
573 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
574 holder
->sched_pri
, priority
, (int)holder
, (int)lck
, 0);
576 set_sched_pri(holder
, priority
);
578 thread_unlock(holder
);
581 if (mutex
->lck_mtx_pri
< priority
)
582 mutex
->lck_mtx_pri
= priority
;
583 if (self
->pending_promoter
[self
->pending_promoter_index
] == NULL
) {
584 self
->pending_promoter
[self
->pending_promoter_index
] = mutex
;
585 mutex
->lck_mtx_waiters
++;
588 if (self
->pending_promoter
[self
->pending_promoter_index
] != mutex
) {
589 self
->pending_promoter
[++self
->pending_promoter_index
] = mutex
;
590 mutex
->lck_mtx_waiters
++;
593 assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_mtx_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
594 lck_mtx_ilk_unlock(mutex
);
596 thread_block(THREAD_CONTINUE_NULL
);
598 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
601 * Record the Dtrace lockstat probe for blocking, block time
602 * measured from when we were entered.
605 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
) {
606 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_BLOCK
, lck
,
607 mach_absolute_time() - sleep_start
);
609 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_BLOCK
, lck
,
610 mach_absolute_time() - sleep_start
);
617 * Routine: lck_mtx_lock_acquire
619 * Invoked on acquiring the mutex when there is
622 * Returns the current number of waiters.
624 * Called with the interlock locked.
627 lck_mtx_lock_acquire(
630 thread_t thread
= current_thread();
633 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
636 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
638 if (thread
->pending_promoter
[thread
->pending_promoter_index
] == mutex
) {
639 thread
->pending_promoter
[thread
->pending_promoter_index
] = NULL
;
640 if (thread
->pending_promoter_index
> 0)
641 thread
->pending_promoter_index
--;
642 mutex
->lck_mtx_waiters
--;
645 if (mutex
->lck_mtx_waiters
> 0) {
646 integer_t priority
= mutex
->lck_mtx_pri
;
647 spl_t s
= splsched();
650 thread
->promotions
++;
651 thread
->sched_mode
|= TH_MODE_PROMOTED
;
652 if (thread
->sched_pri
< priority
) {
653 KERNEL_DEBUG_CONSTANT(
654 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
655 thread
->sched_pri
, priority
, 0, (int)lck
, 0);
657 set_sched_pri(thread
, priority
);
659 thread_unlock(thread
);
663 mutex
->lck_mtx_pri
= 0;
665 return (mutex
->lck_mtx_waiters
);
669 * Routine: lck_mtx_unlock_wakeup
671 * Invoked on unlock when there is contention.
673 * Called with the interlock locked.
676 lck_mtx_unlock_wakeup (
680 thread_t thread
= current_thread();
683 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
686 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
689 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
691 if (thread
!= holder
)
692 panic("lck_mtx_unlock_wakeup: mutex %p holder %p\n", mutex
, holder
);
694 if (thread
->promotions
> 0) {
695 spl_t s
= splsched();
698 if ( --thread
->promotions
== 0 &&
699 (thread
->sched_mode
& TH_MODE_PROMOTED
) ) {
700 thread
->sched_mode
&= ~TH_MODE_PROMOTED
;
701 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
702 KERNEL_DEBUG_CONSTANT(
703 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) | DBG_FUNC_NONE
,
704 thread
->sched_pri
, DEPRESSPRI
, 0, (int)lck
, 0);
706 set_sched_pri(thread
, DEPRESSPRI
);
709 if (thread
->priority
< thread
->sched_pri
) {
710 KERNEL_DEBUG_CONSTANT(
711 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) |
713 thread
->sched_pri
, thread
->priority
,
717 compute_priority(thread
, FALSE
);
720 thread_unlock(thread
);
723 assert(mutex
->lck_mtx_waiters
> 0);
724 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
726 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
730 lck_mtx_unlockspin_wakeup (
733 assert(lck
->lck_mtx_waiters
> 0);
734 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
736 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_NONE
, (int)lck
, 0, 0, 1, 0);
739 * When there are waiters, we skip the hot-patch spot in the
740 * fastpath, so we record it here.
742 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE
, lck
, 0);
748 * Routine: mutex_pause
750 * Called by former callers of simple_lock_pause().
752 #define MAX_COLLISION_COUNTS 32
753 #define MAX_COLLISION 8
755 unsigned int max_collision_count
[MAX_COLLISION_COUNTS
];
757 uint32_t collision_backoffs
[MAX_COLLISION
] = {
758 10, 50, 100, 200, 400, 600, 800, 1000
763 mutex_pause(uint32_t collisions
)
765 wait_result_t wait_result
;
768 if (collisions
>= MAX_COLLISION_COUNTS
)
769 collisions
= MAX_COLLISION_COUNTS
- 1;
770 max_collision_count
[collisions
]++;
772 if (collisions
>= MAX_COLLISION
)
773 collisions
= MAX_COLLISION
- 1;
774 back_off
= collision_backoffs
[collisions
];
776 wait_result
= assert_wait_timeout((event_t
)mutex_pause
, THREAD_UNINT
, back_off
, NSEC_PER_USEC
);
777 assert(wait_result
== THREAD_WAITING
);
779 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
780 assert(wait_result
== THREAD_TIMED_OUT
);
784 unsigned int mutex_yield_wait
= 0;
785 unsigned int mutex_yield_no_wait
= 0;
794 _mutex_assert(mutex
, MA_OWNED
);
797 lck
= (lck_mtx_t
*) mutex
;
798 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
)
799 lck
= &lck
->lck_mtx_ptr
->lck_mtx
;
801 if (! lck
->lck_mtx_waiters
) {
802 mutex_yield_no_wait
++;
813 * Routine: lck_rw_sleep
818 lck_sleep_action_t lck_sleep_action
,
820 wait_interrupt_t interruptible
)
823 lck_rw_type_t lck_rw_type
;
825 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
826 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
828 res
= assert_wait(event
, interruptible
);
829 if (res
== THREAD_WAITING
) {
830 lck_rw_type
= lck_rw_done(lck
);
831 res
= thread_block(THREAD_CONTINUE_NULL
);
832 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
833 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
834 lck_rw_lock(lck
, lck_rw_type
);
835 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
836 lck_rw_lock_exclusive(lck
);
838 lck_rw_lock_shared(lck
);
842 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
843 (void)lck_rw_done(lck
);
850 * Routine: lck_rw_sleep_deadline
853 lck_rw_sleep_deadline(
855 lck_sleep_action_t lck_sleep_action
,
857 wait_interrupt_t interruptible
,
861 lck_rw_type_t lck_rw_type
;
863 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
864 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
866 res
= assert_wait_deadline(event
, interruptible
, deadline
);
867 if (res
== THREAD_WAITING
) {
868 lck_rw_type
= lck_rw_done(lck
);
869 res
= thread_block(THREAD_CONTINUE_NULL
);
870 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
871 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
872 lck_rw_lock(lck
, lck_rw_type
);
873 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
874 lck_rw_lock_exclusive(lck
);
876 lck_rw_lock_shared(lck
);
880 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
881 (void)lck_rw_done(lck
);
889 lockgroup_info_array_t
*lockgroup_infop
,
890 mach_msg_type_number_t
*lockgroup_infoCntp
)
892 lockgroup_info_t
*lockgroup_info_base
;
893 lockgroup_info_t
*lockgroup_info
;
894 vm_offset_t lockgroup_info_addr
;
895 vm_size_t lockgroup_info_size
;
902 if (host
== HOST_NULL
)
903 return KERN_INVALID_HOST
;
905 mutex_lock(&lck_grp_lock
);
907 lockgroup_info_size
= round_page(lck_grp_cnt
* sizeof *lockgroup_info
);
908 kr
= kmem_alloc_pageable(ipc_kernel_map
,
909 &lockgroup_info_addr
, lockgroup_info_size
);
910 if (kr
!= KERN_SUCCESS
) {
911 mutex_unlock(&lck_grp_lock
);
915 lockgroup_info_base
= (lockgroup_info_t
*) lockgroup_info_addr
;
916 lck_grp
= (lck_grp_t
*)queue_first(&lck_grp_queue
);
917 lockgroup_info
= lockgroup_info_base
;
919 for (i
= 0; i
< lck_grp_cnt
; i
++) {
921 lockgroup_info
->lock_spin_cnt
= lck_grp
->lck_grp_spincnt
;
922 lockgroup_info
->lock_spin_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_util_cnt
;
923 lockgroup_info
->lock_spin_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cnt
;
924 lockgroup_info
->lock_spin_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_miss_cnt
;
925 lockgroup_info
->lock_spin_held_max
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_max
;
926 lockgroup_info
->lock_spin_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cum
;
928 lockgroup_info
->lock_mtx_cnt
= lck_grp
->lck_grp_mtxcnt
;
929 lockgroup_info
->lock_mtx_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_util_cnt
;
930 lockgroup_info
->lock_mtx_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cnt
;
931 lockgroup_info
->lock_mtx_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_miss_cnt
;
932 lockgroup_info
->lock_mtx_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cnt
;
933 lockgroup_info
->lock_mtx_held_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_max
;
934 lockgroup_info
->lock_mtx_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cum
;
935 lockgroup_info
->lock_mtx_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_max
;
936 lockgroup_info
->lock_mtx_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cum
;
938 lockgroup_info
->lock_rw_cnt
= lck_grp
->lck_grp_rwcnt
;
939 lockgroup_info
->lock_rw_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
;
940 lockgroup_info
->lock_rw_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cnt
;
941 lockgroup_info
->lock_rw_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
;
942 lockgroup_info
->lock_rw_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
;
943 lockgroup_info
->lock_rw_held_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_max
;
944 lockgroup_info
->lock_rw_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cum
;
945 lockgroup_info
->lock_rw_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_max
;
946 lockgroup_info
->lock_rw_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cum
;
948 (void) strncpy(lockgroup_info
->lockgroup_name
,lck_grp
->lck_grp_name
, LOCKGROUP_MAX_NAME
);
950 lck_grp
= (lck_grp_t
*)(queue_next((queue_entry_t
)(lck_grp
)));
954 *lockgroup_infoCntp
= lck_grp_cnt
;
955 mutex_unlock(&lck_grp_lock
);
957 used
= (*lockgroup_infoCntp
) * sizeof *lockgroup_info
;
959 if (used
!= lockgroup_info_size
)
960 bzero((char *) lockgroup_info
, lockgroup_info_size
- used
);
962 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)lockgroup_info_addr
,
963 (vm_map_size_t
)lockgroup_info_size
, TRUE
, ©
);
964 assert(kr
== KERN_SUCCESS
);
966 *lockgroup_infop
= (lockgroup_info_t
*) copy
;
968 return(KERN_SUCCESS
);
972 * Compatibility module
975 extern lck_rw_t
*lock_alloc_EXT( boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
976 extern void lock_done_EXT(lck_rw_t
*lock
);
977 extern void lock_free_EXT(lck_rw_t
*lock
);
978 extern void lock_init_EXT(lck_rw_t
*lock
, boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
979 extern void lock_read_EXT(lck_rw_t
*lock
);
980 extern boolean_t
lock_read_to_write_EXT(lck_rw_t
*lock
);
981 extern void lock_write_EXT(lck_rw_t
*lock
);
982 extern void lock_write_to_read_EXT(lck_rw_t
*lock
);
983 extern wait_result_t
thread_sleep_lock_write_EXT(
984 event_t event
, lck_rw_t
*lock
, wait_interrupt_t interruptible
);
986 extern lck_mtx_t
*mutex_alloc_EXT(unsigned short tag
);
987 extern void mutex_free_EXT(lck_mtx_t
*mutex
);
988 extern void mutex_init_EXT(lck_mtx_t
*mutex
, unsigned short tag
);
989 extern void mutex_lock_EXT(lck_mtx_t
*mutex
);
990 extern boolean_t
mutex_try_EXT(lck_mtx_t
*mutex
);
991 extern void mutex_unlock_EXT(lck_mtx_t
*mutex
);
992 extern wait_result_t
thread_sleep_mutex_EXT(
993 event_t event
, lck_mtx_t
*mutex
, wait_interrupt_t interruptible
);
994 extern wait_result_t
thread_sleep_mutex_deadline_EXT(
995 event_t event
, lck_mtx_t
*mutex
, uint64_t deadline
, wait_interrupt_t interruptible
);
997 extern void usimple_lock_EXT(lck_spin_t
*lock
);
998 extern void usimple_lock_init_EXT(lck_spin_t
*lock
, unsigned short tag
);
999 extern unsigned int usimple_lock_try_EXT(lck_spin_t
*lock
);
1000 extern void usimple_unlock_EXT(lck_spin_t
*lock
);
1001 extern wait_result_t
thread_sleep_usimple_lock_EXT(event_t event
, lck_spin_t
*lock
, wait_interrupt_t interruptible
);
1005 __unused boolean_t can_sleep
,
1006 __unused
unsigned short tag0
,
1007 __unused
unsigned short tag1
)
1009 return( lck_rw_alloc_init( &LockCompatGroup
, LCK_ATTR_NULL
));
1016 (void) lck_rw_done(lock
);
1023 lck_rw_free(lock
, &LockCompatGroup
);
1029 __unused boolean_t can_sleep
,
1030 __unused
unsigned short tag0
,
1031 __unused
unsigned short tag1
)
1033 lck_rw_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1040 lck_rw_lock_shared( lock
);
1044 lock_read_to_write_EXT(
1047 return( lck_rw_lock_shared_to_exclusive(lock
));
1054 lck_rw_lock_exclusive(lock
);
1058 lock_write_to_read_EXT(
1061 lck_rw_lock_exclusive_to_shared(lock
);
1065 thread_sleep_lock_write_EXT(
1068 wait_interrupt_t interruptible
)
1070 return( lck_rw_sleep(lock
, LCK_SLEEP_EXCLUSIVE
, event
, interruptible
));
1075 __unused
unsigned short tag
)
1077 return(lck_mtx_alloc_init(&LockCompatGroup
, LCK_ATTR_NULL
));
1084 lck_mtx_free(mutex
, &LockCompatGroup
);
1090 __unused
unsigned short tag
)
1092 lck_mtx_init(mutex
, &LockCompatGroup
, LCK_ATTR_NULL
);
1099 lck_mtx_lock(mutex
);
1106 return(lck_mtx_try_lock(mutex
));
1113 lck_mtx_unlock(mutex
);
1117 thread_sleep_mutex_EXT(
1120 wait_interrupt_t interruptible
)
1122 return( lck_mtx_sleep(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
));
1126 thread_sleep_mutex_deadline_EXT(
1130 wait_interrupt_t interruptible
)
1132 return( lck_mtx_sleep_deadline(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
, deadline
));
1139 lck_spin_lock(lock
);
1143 usimple_lock_init_EXT(
1145 __unused
unsigned short tag
)
1147 lck_spin_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1151 usimple_lock_try_EXT(
1154 return(lck_spin_try_lock(lock
));
1161 lck_spin_unlock(lock
);
1165 thread_sleep_usimple_lock_EXT(
1168 wait_interrupt_t interruptible
)
1170 return( lck_spin_sleep(lock
, LCK_SLEEP_DEFAULT
, event
, interruptible
));