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
;
356 * Routine: lck_attr_setdebug
362 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_DEBUG
);
366 * Routine: lck_attr_setdebug
372 (void)hw_atomic_and(&attr
->lck_attr_val
, ~LCK_ATTR_DEBUG
);
377 * Routine: lck_attr_rw_shared_priority
380 lck_attr_rw_shared_priority(
383 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_RW_SHARED_PRIORITY
);
388 * Routine: lck_attr_free
394 kfree(attr
, sizeof(lck_attr_t
));
399 * Routine: lck_spin_sleep
404 lck_sleep_action_t lck_sleep_action
,
406 wait_interrupt_t interruptible
)
410 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
411 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
413 res
= assert_wait(event
, interruptible
);
414 if (res
== THREAD_WAITING
) {
415 lck_spin_unlock(lck
);
416 res
= thread_block(THREAD_CONTINUE_NULL
);
417 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
421 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
422 lck_spin_unlock(lck
);
429 * Routine: lck_spin_sleep_deadline
432 lck_spin_sleep_deadline(
434 lck_sleep_action_t lck_sleep_action
,
436 wait_interrupt_t interruptible
,
441 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
442 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
444 res
= assert_wait_deadline(event
, interruptible
, deadline
);
445 if (res
== THREAD_WAITING
) {
446 lck_spin_unlock(lck
);
447 res
= thread_block(THREAD_CONTINUE_NULL
);
448 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
452 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
453 lck_spin_unlock(lck
);
460 * Routine: lck_mtx_sleep
465 lck_sleep_action_t lck_sleep_action
,
467 wait_interrupt_t interruptible
)
471 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_START
,
472 (int)lck
, (int)lck_sleep_action
, (int)event
, (int)interruptible
, 0);
474 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
475 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
477 res
= assert_wait(event
, interruptible
);
478 if (res
== THREAD_WAITING
) {
480 res
= thread_block(THREAD_CONTINUE_NULL
);
481 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
485 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
488 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
495 * Routine: lck_mtx_sleep_deadline
498 lck_mtx_sleep_deadline(
500 lck_sleep_action_t lck_sleep_action
,
502 wait_interrupt_t interruptible
,
507 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_START
,
508 (int)lck
, (int)lck_sleep_action
, (int)event
, (int)interruptible
, 0);
510 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
511 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
513 res
= assert_wait_deadline(event
, interruptible
, deadline
);
514 if (res
== THREAD_WAITING
) {
516 res
= thread_block(THREAD_CONTINUE_NULL
);
517 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
521 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
524 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
530 * Routine: lck_mtx_lock_wait
532 * Invoked in order to wait on contention.
534 * Called with the interlock locked and
535 * returns it unlocked.
542 thread_t self
= current_thread();
545 spl_t s
= splsched();
547 uint64_t sleep_start
= 0;
549 if (lockstat_probemap
[LS_LCK_MTX_LOCK_BLOCK
] || lockstat_probemap
[LS_LCK_MTX_EXT_LOCK_BLOCK
]) {
550 sleep_start
= mach_absolute_time();
554 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
557 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
559 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
561 priority
= self
->sched_pri
;
562 if (priority
< self
->priority
)
563 priority
= self
->priority
;
564 if (priority
> MINPRI_KERNEL
)
565 priority
= MINPRI_KERNEL
;
567 if (priority
< BASEPRI_DEFAULT
)
568 priority
= BASEPRI_DEFAULT
;
571 if (mutex
->lck_mtx_pri
== 0)
572 holder
->promotions
++;
573 if (holder
->priority
< MINPRI_KERNEL
) {
574 holder
->sched_mode
|= TH_MODE_PROMOTED
;
575 if ( mutex
->lck_mtx_pri
< priority
&&
576 holder
->sched_pri
< priority
) {
577 KERNEL_DEBUG_CONSTANT(
578 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
579 holder
->sched_pri
, priority
, (int)holder
, (int)lck
, 0);
581 set_sched_pri(holder
, priority
);
584 thread_unlock(holder
);
587 if (mutex
->lck_mtx_pri
< priority
)
588 mutex
->lck_mtx_pri
= priority
;
589 if (self
->pending_promoter
[self
->pending_promoter_index
] == NULL
) {
590 self
->pending_promoter
[self
->pending_promoter_index
] = mutex
;
591 mutex
->lck_mtx_waiters
++;
594 if (self
->pending_promoter
[self
->pending_promoter_index
] != mutex
) {
595 self
->pending_promoter
[++self
->pending_promoter_index
] = mutex
;
596 mutex
->lck_mtx_waiters
++;
599 assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_mtx_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
600 lck_mtx_ilk_unlock(mutex
);
602 thread_block(THREAD_CONTINUE_NULL
);
604 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
607 * Record the Dtrace lockstat probe for blocking, block time
608 * measured from when we were entered.
611 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
) {
612 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_BLOCK
, lck
,
613 mach_absolute_time() - sleep_start
);
615 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_BLOCK
, lck
,
616 mach_absolute_time() - sleep_start
);
623 * Routine: lck_mtx_lock_acquire
625 * Invoked on acquiring the mutex when there is
628 * Returns the current number of waiters.
630 * Called with the interlock locked.
633 lck_mtx_lock_acquire(
636 thread_t thread
= current_thread();
639 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
642 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
644 if (thread
->pending_promoter
[thread
->pending_promoter_index
] == mutex
) {
645 thread
->pending_promoter
[thread
->pending_promoter_index
] = NULL
;
646 if (thread
->pending_promoter_index
> 0)
647 thread
->pending_promoter_index
--;
648 mutex
->lck_mtx_waiters
--;
651 if (mutex
->lck_mtx_waiters
> 0) {
652 integer_t priority
= mutex
->lck_mtx_pri
;
653 spl_t s
= splsched();
656 thread
->promotions
++;
657 if (thread
->priority
< MINPRI_KERNEL
) {
658 thread
->sched_mode
|= TH_MODE_PROMOTED
;
659 if (thread
->sched_pri
< priority
) {
660 KERNEL_DEBUG_CONSTANT(
661 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
662 thread
->sched_pri
, priority
, 0, (int)lck
, 0);
664 set_sched_pri(thread
, priority
);
667 thread_unlock(thread
);
671 mutex
->lck_mtx_pri
= 0;
673 return (mutex
->lck_mtx_waiters
);
677 * Routine: lck_mtx_unlock_wakeup
679 * Invoked on unlock when there is contention.
681 * Called with the interlock locked.
684 lck_mtx_unlock_wakeup (
688 thread_t thread
= current_thread();
691 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
694 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
697 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
699 if (thread
!= holder
)
700 panic("lck_mtx_unlock_wakeup: mutex %p holder %p\n", mutex
, holder
);
702 if (thread
->promotions
> 0) {
703 spl_t s
= splsched();
706 if ( --thread
->promotions
== 0 &&
707 (thread
->sched_mode
& TH_MODE_PROMOTED
) ) {
708 thread
->sched_mode
&= ~TH_MODE_PROMOTED
;
709 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
710 KERNEL_DEBUG_CONSTANT(
711 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) | DBG_FUNC_NONE
,
712 thread
->sched_pri
, DEPRESSPRI
, 0, (int)lck
, 0);
714 set_sched_pri(thread
, DEPRESSPRI
);
717 if (thread
->priority
< thread
->sched_pri
) {
718 KERNEL_DEBUG_CONSTANT(
719 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) |
721 thread
->sched_pri
, thread
->priority
,
725 compute_priority(thread
, FALSE
);
728 thread_unlock(thread
);
731 assert(mutex
->lck_mtx_waiters
> 0);
732 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
734 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
738 lck_mtx_unlockspin_wakeup (
741 assert(lck
->lck_mtx_waiters
> 0);
742 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
744 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_NONE
, (int)lck
, 0, 0, 1, 0);
747 * When there are waiters, we skip the hot-patch spot in the
748 * fastpath, so we record it here.
750 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE
, lck
, 0);
756 * Routine: mutex_pause
758 * Called by former callers of simple_lock_pause().
760 #define MAX_COLLISION_COUNTS 32
761 #define MAX_COLLISION 8
763 unsigned int max_collision_count
[MAX_COLLISION_COUNTS
];
765 uint32_t collision_backoffs
[MAX_COLLISION
] = {
766 10, 50, 100, 200, 400, 600, 800, 1000
771 mutex_pause(uint32_t collisions
)
773 wait_result_t wait_result
;
776 if (collisions
>= MAX_COLLISION_COUNTS
)
777 collisions
= MAX_COLLISION_COUNTS
- 1;
778 max_collision_count
[collisions
]++;
780 if (collisions
>= MAX_COLLISION
)
781 collisions
= MAX_COLLISION
- 1;
782 back_off
= collision_backoffs
[collisions
];
784 wait_result
= assert_wait_timeout((event_t
)mutex_pause
, THREAD_UNINT
, back_off
, NSEC_PER_USEC
);
785 assert(wait_result
== THREAD_WAITING
);
787 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
788 assert(wait_result
== THREAD_TIMED_OUT
);
792 unsigned int mutex_yield_wait
= 0;
793 unsigned int mutex_yield_no_wait
= 0;
802 _mutex_assert(mutex
, MA_OWNED
);
805 lck
= (lck_mtx_t
*) mutex
;
806 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
)
807 lck
= &lck
->lck_mtx_ptr
->lck_mtx
;
809 if (! lck
->lck_mtx_waiters
) {
810 mutex_yield_no_wait
++;
821 * Routine: lck_rw_sleep
826 lck_sleep_action_t lck_sleep_action
,
828 wait_interrupt_t interruptible
)
831 lck_rw_type_t lck_rw_type
;
833 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
834 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
836 res
= assert_wait(event
, interruptible
);
837 if (res
== THREAD_WAITING
) {
838 lck_rw_type
= lck_rw_done(lck
);
839 res
= thread_block(THREAD_CONTINUE_NULL
);
840 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
841 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
842 lck_rw_lock(lck
, lck_rw_type
);
843 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
844 lck_rw_lock_exclusive(lck
);
846 lck_rw_lock_shared(lck
);
850 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
851 (void)lck_rw_done(lck
);
858 * Routine: lck_rw_sleep_deadline
861 lck_rw_sleep_deadline(
863 lck_sleep_action_t lck_sleep_action
,
865 wait_interrupt_t interruptible
,
869 lck_rw_type_t lck_rw_type
;
871 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
872 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
874 res
= assert_wait_deadline(event
, interruptible
, deadline
);
875 if (res
== THREAD_WAITING
) {
876 lck_rw_type
= lck_rw_done(lck
);
877 res
= thread_block(THREAD_CONTINUE_NULL
);
878 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
879 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
880 lck_rw_lock(lck
, lck_rw_type
);
881 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
882 lck_rw_lock_exclusive(lck
);
884 lck_rw_lock_shared(lck
);
888 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
889 (void)lck_rw_done(lck
);
897 lockgroup_info_array_t
*lockgroup_infop
,
898 mach_msg_type_number_t
*lockgroup_infoCntp
)
900 lockgroup_info_t
*lockgroup_info_base
;
901 lockgroup_info_t
*lockgroup_info
;
902 vm_offset_t lockgroup_info_addr
;
903 vm_size_t lockgroup_info_size
;
910 if (host
== HOST_NULL
)
911 return KERN_INVALID_HOST
;
913 mutex_lock(&lck_grp_lock
);
915 lockgroup_info_size
= round_page(lck_grp_cnt
* sizeof *lockgroup_info
);
916 kr
= kmem_alloc_pageable(ipc_kernel_map
,
917 &lockgroup_info_addr
, lockgroup_info_size
);
918 if (kr
!= KERN_SUCCESS
) {
919 mutex_unlock(&lck_grp_lock
);
923 lockgroup_info_base
= (lockgroup_info_t
*) lockgroup_info_addr
;
924 lck_grp
= (lck_grp_t
*)queue_first(&lck_grp_queue
);
925 lockgroup_info
= lockgroup_info_base
;
927 for (i
= 0; i
< lck_grp_cnt
; i
++) {
929 lockgroup_info
->lock_spin_cnt
= lck_grp
->lck_grp_spincnt
;
930 lockgroup_info
->lock_spin_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_util_cnt
;
931 lockgroup_info
->lock_spin_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cnt
;
932 lockgroup_info
->lock_spin_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_miss_cnt
;
933 lockgroup_info
->lock_spin_held_max
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_max
;
934 lockgroup_info
->lock_spin_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cum
;
936 lockgroup_info
->lock_mtx_cnt
= lck_grp
->lck_grp_mtxcnt
;
937 lockgroup_info
->lock_mtx_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_util_cnt
;
938 lockgroup_info
->lock_mtx_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cnt
;
939 lockgroup_info
->lock_mtx_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_miss_cnt
;
940 lockgroup_info
->lock_mtx_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cnt
;
941 lockgroup_info
->lock_mtx_held_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_max
;
942 lockgroup_info
->lock_mtx_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cum
;
943 lockgroup_info
->lock_mtx_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_max
;
944 lockgroup_info
->lock_mtx_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cum
;
946 lockgroup_info
->lock_rw_cnt
= lck_grp
->lck_grp_rwcnt
;
947 lockgroup_info
->lock_rw_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
;
948 lockgroup_info
->lock_rw_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cnt
;
949 lockgroup_info
->lock_rw_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
;
950 lockgroup_info
->lock_rw_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
;
951 lockgroup_info
->lock_rw_held_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_max
;
952 lockgroup_info
->lock_rw_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cum
;
953 lockgroup_info
->lock_rw_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_max
;
954 lockgroup_info
->lock_rw_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cum
;
956 (void) strncpy(lockgroup_info
->lockgroup_name
,lck_grp
->lck_grp_name
, LOCKGROUP_MAX_NAME
);
958 lck_grp
= (lck_grp_t
*)(queue_next((queue_entry_t
)(lck_grp
)));
962 *lockgroup_infoCntp
= lck_grp_cnt
;
963 mutex_unlock(&lck_grp_lock
);
965 used
= (*lockgroup_infoCntp
) * sizeof *lockgroup_info
;
967 if (used
!= lockgroup_info_size
)
968 bzero((char *) lockgroup_info
, lockgroup_info_size
- used
);
970 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)lockgroup_info_addr
,
971 (vm_map_size_t
)lockgroup_info_size
, TRUE
, ©
);
972 assert(kr
== KERN_SUCCESS
);
974 *lockgroup_infop
= (lockgroup_info_t
*) copy
;
976 return(KERN_SUCCESS
);
980 * Compatibility module
983 extern lck_rw_t
*lock_alloc_EXT( boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
984 extern void lock_done_EXT(lck_rw_t
*lock
);
985 extern void lock_free_EXT(lck_rw_t
*lock
);
986 extern void lock_init_EXT(lck_rw_t
*lock
, boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
987 extern void lock_read_EXT(lck_rw_t
*lock
);
988 extern boolean_t
lock_read_to_write_EXT(lck_rw_t
*lock
);
989 extern void lock_write_EXT(lck_rw_t
*lock
);
990 extern void lock_write_to_read_EXT(lck_rw_t
*lock
);
991 extern wait_result_t
thread_sleep_lock_write_EXT(
992 event_t event
, lck_rw_t
*lock
, wait_interrupt_t interruptible
);
994 extern lck_mtx_t
*mutex_alloc_EXT(unsigned short tag
);
995 extern void mutex_free_EXT(lck_mtx_t
*mutex
);
996 extern void mutex_init_EXT(lck_mtx_t
*mutex
, unsigned short tag
);
997 extern void mutex_lock_EXT(lck_mtx_t
*mutex
);
998 extern boolean_t
mutex_try_EXT(lck_mtx_t
*mutex
);
999 extern void mutex_unlock_EXT(lck_mtx_t
*mutex
);
1000 extern wait_result_t
thread_sleep_mutex_EXT(
1001 event_t event
, lck_mtx_t
*mutex
, wait_interrupt_t interruptible
);
1002 extern wait_result_t
thread_sleep_mutex_deadline_EXT(
1003 event_t event
, lck_mtx_t
*mutex
, uint64_t deadline
, wait_interrupt_t interruptible
);
1005 extern void usimple_lock_EXT(lck_spin_t
*lock
);
1006 extern void usimple_lock_init_EXT(lck_spin_t
*lock
, unsigned short tag
);
1007 extern unsigned int usimple_lock_try_EXT(lck_spin_t
*lock
);
1008 extern void usimple_unlock_EXT(lck_spin_t
*lock
);
1009 extern wait_result_t
thread_sleep_usimple_lock_EXT(event_t event
, lck_spin_t
*lock
, wait_interrupt_t interruptible
);
1013 __unused boolean_t can_sleep
,
1014 __unused
unsigned short tag0
,
1015 __unused
unsigned short tag1
)
1017 return( lck_rw_alloc_init( &LockCompatGroup
, LCK_ATTR_NULL
));
1024 (void) lck_rw_done(lock
);
1031 lck_rw_free(lock
, &LockCompatGroup
);
1037 __unused boolean_t can_sleep
,
1038 __unused
unsigned short tag0
,
1039 __unused
unsigned short tag1
)
1041 lck_rw_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1048 lck_rw_lock_shared( lock
);
1052 lock_read_to_write_EXT(
1055 return( lck_rw_lock_shared_to_exclusive(lock
));
1062 lck_rw_lock_exclusive(lock
);
1066 lock_write_to_read_EXT(
1069 lck_rw_lock_exclusive_to_shared(lock
);
1073 thread_sleep_lock_write_EXT(
1076 wait_interrupt_t interruptible
)
1078 return( lck_rw_sleep(lock
, LCK_SLEEP_EXCLUSIVE
, event
, interruptible
));
1083 __unused
unsigned short tag
)
1085 return(lck_mtx_alloc_init(&LockCompatGroup
, LCK_ATTR_NULL
));
1092 lck_mtx_free(mutex
, &LockCompatGroup
);
1098 __unused
unsigned short tag
)
1100 lck_mtx_init(mutex
, &LockCompatGroup
, LCK_ATTR_NULL
);
1107 lck_mtx_lock(mutex
);
1114 return(lck_mtx_try_lock(mutex
));
1121 lck_mtx_unlock(mutex
);
1125 thread_sleep_mutex_EXT(
1128 wait_interrupt_t interruptible
)
1130 return( lck_mtx_sleep(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
));
1134 thread_sleep_mutex_deadline_EXT(
1138 wait_interrupt_t interruptible
)
1140 return( lck_mtx_sleep_deadline(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
, deadline
));
1147 lck_spin_lock(lock
);
1151 usimple_lock_init_EXT(
1153 __unused
unsigned short tag
)
1155 lck_spin_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1159 usimple_lock_try_EXT(
1162 return(lck_spin_try_lock(lock
));
1169 lck_spin_unlock(lock
);
1173 thread_sleep_usimple_lock_EXT(
1176 wait_interrupt_t interruptible
)
1178 return( lck_spin_sleep(lock
, LCK_SLEEP_DEFAULT
, event
, interruptible
));