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
< BASEPRI_DEFAULT
)
565 priority
= BASEPRI_DEFAULT
;
568 if (mutex
->lck_mtx_pri
== 0)
569 holder
->promotions
++;
570 holder
->sched_mode
|= TH_MODE_PROMOTED
;
571 if ( mutex
->lck_mtx_pri
< priority
&&
572 holder
->sched_pri
< priority
) {
573 KERNEL_DEBUG_CONSTANT(
574 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
575 holder
->sched_pri
, priority
, (int)holder
, (int)lck
, 0);
577 set_sched_pri(holder
, priority
);
579 thread_unlock(holder
);
582 if (mutex
->lck_mtx_pri
< priority
)
583 mutex
->lck_mtx_pri
= priority
;
584 if (self
->pending_promoter
[self
->pending_promoter_index
] == NULL
) {
585 self
->pending_promoter
[self
->pending_promoter_index
] = mutex
;
586 mutex
->lck_mtx_waiters
++;
589 if (self
->pending_promoter
[self
->pending_promoter_index
] != mutex
) {
590 self
->pending_promoter
[++self
->pending_promoter_index
] = mutex
;
591 mutex
->lck_mtx_waiters
++;
594 assert_wait((event_t
)(((unsigned int*)lck
)+((sizeof(lck_mtx_t
)-1)/sizeof(unsigned int))), THREAD_UNINT
);
595 lck_mtx_ilk_unlock(mutex
);
597 thread_block(THREAD_CONTINUE_NULL
);
599 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
602 * Record the Dtrace lockstat probe for blocking, block time
603 * measured from when we were entered.
606 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
) {
607 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_BLOCK
, lck
,
608 mach_absolute_time() - sleep_start
);
610 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_BLOCK
, lck
,
611 mach_absolute_time() - sleep_start
);
618 * Routine: lck_mtx_lock_acquire
620 * Invoked on acquiring the mutex when there is
623 * Returns the current number of waiters.
625 * Called with the interlock locked.
628 lck_mtx_lock_acquire(
631 thread_t thread
= current_thread();
634 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
637 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
639 if (thread
->pending_promoter
[thread
->pending_promoter_index
] == mutex
) {
640 thread
->pending_promoter
[thread
->pending_promoter_index
] = NULL
;
641 if (thread
->pending_promoter_index
> 0)
642 thread
->pending_promoter_index
--;
643 mutex
->lck_mtx_waiters
--;
646 if (mutex
->lck_mtx_waiters
> 0) {
647 integer_t priority
= mutex
->lck_mtx_pri
;
648 spl_t s
= splsched();
651 thread
->promotions
++;
652 thread
->sched_mode
|= TH_MODE_PROMOTED
;
653 if (thread
->sched_pri
< priority
) {
654 KERNEL_DEBUG_CONSTANT(
655 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
656 thread
->sched_pri
, priority
, 0, (int)lck
, 0);
658 set_sched_pri(thread
, priority
);
660 thread_unlock(thread
);
664 mutex
->lck_mtx_pri
= 0;
666 return (mutex
->lck_mtx_waiters
);
670 * Routine: lck_mtx_unlock_wakeup
672 * Invoked on unlock when there is contention.
674 * Called with the interlock locked.
677 lck_mtx_unlock_wakeup (
681 thread_t thread
= current_thread();
684 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
687 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
690 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_START
, (int)lck
, (int)holder
, 0, 0, 0);
692 if (thread
!= holder
)
693 panic("lck_mtx_unlock_wakeup: mutex %p holder %p\n", mutex
, holder
);
695 if (thread
->promotions
> 0) {
696 spl_t s
= splsched();
699 if ( --thread
->promotions
== 0 &&
700 (thread
->sched_mode
& TH_MODE_PROMOTED
) ) {
701 thread
->sched_mode
&= ~TH_MODE_PROMOTED
;
702 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
703 KERNEL_DEBUG_CONSTANT(
704 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) | DBG_FUNC_NONE
,
705 thread
->sched_pri
, DEPRESSPRI
, 0, (int)lck
, 0);
707 set_sched_pri(thread
, DEPRESSPRI
);
710 if (thread
->priority
< thread
->sched_pri
) {
711 KERNEL_DEBUG_CONSTANT(
712 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) |
714 thread
->sched_pri
, thread
->priority
,
718 compute_priority(thread
, FALSE
);
721 thread_unlock(thread
);
724 assert(mutex
->lck_mtx_waiters
> 0);
725 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
727 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
731 lck_mtx_unlockspin_wakeup (
734 assert(lck
->lck_mtx_waiters
> 0);
735 thread_wakeup_one((event_t
)(((unsigned int*)lck
)+(sizeof(lck_mtx_t
)-1)/sizeof(unsigned int)));
737 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_NONE
, (int)lck
, 0, 0, 1, 0);
740 * When there are waiters, we skip the hot-patch spot in the
741 * fastpath, so we record it here.
743 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE
, lck
, 0);
749 * Routine: mutex_pause
751 * Called by former callers of simple_lock_pause().
753 #define MAX_COLLISION_COUNTS 32
754 #define MAX_COLLISION 8
756 unsigned int max_collision_count
[MAX_COLLISION_COUNTS
];
758 uint32_t collision_backoffs
[MAX_COLLISION
] = {
759 10, 50, 100, 200, 400, 600, 800, 1000
764 mutex_pause(uint32_t collisions
)
766 wait_result_t wait_result
;
769 if (collisions
>= MAX_COLLISION_COUNTS
)
770 collisions
= MAX_COLLISION_COUNTS
- 1;
771 max_collision_count
[collisions
]++;
773 if (collisions
>= MAX_COLLISION
)
774 collisions
= MAX_COLLISION
- 1;
775 back_off
= collision_backoffs
[collisions
];
777 wait_result
= assert_wait_timeout((event_t
)mutex_pause
, THREAD_UNINT
, back_off
, NSEC_PER_USEC
);
778 assert(wait_result
== THREAD_WAITING
);
780 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
781 assert(wait_result
== THREAD_TIMED_OUT
);
785 unsigned int mutex_yield_wait
= 0;
786 unsigned int mutex_yield_no_wait
= 0;
795 _mutex_assert(mutex
, MA_OWNED
);
798 lck
= (lck_mtx_t
*) mutex
;
799 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
)
800 lck
= &lck
->lck_mtx_ptr
->lck_mtx
;
802 if (! lck
->lck_mtx_waiters
) {
803 mutex_yield_no_wait
++;
814 * Routine: lck_rw_sleep
819 lck_sleep_action_t lck_sleep_action
,
821 wait_interrupt_t interruptible
)
824 lck_rw_type_t lck_rw_type
;
826 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
827 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
829 res
= assert_wait(event
, interruptible
);
830 if (res
== THREAD_WAITING
) {
831 lck_rw_type
= lck_rw_done(lck
);
832 res
= thread_block(THREAD_CONTINUE_NULL
);
833 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
834 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
835 lck_rw_lock(lck
, lck_rw_type
);
836 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
837 lck_rw_lock_exclusive(lck
);
839 lck_rw_lock_shared(lck
);
843 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
844 (void)lck_rw_done(lck
);
851 * Routine: lck_rw_sleep_deadline
854 lck_rw_sleep_deadline(
856 lck_sleep_action_t lck_sleep_action
,
858 wait_interrupt_t interruptible
,
862 lck_rw_type_t lck_rw_type
;
864 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
865 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
867 res
= assert_wait_deadline(event
, interruptible
, deadline
);
868 if (res
== THREAD_WAITING
) {
869 lck_rw_type
= lck_rw_done(lck
);
870 res
= thread_block(THREAD_CONTINUE_NULL
);
871 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
872 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
873 lck_rw_lock(lck
, lck_rw_type
);
874 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
875 lck_rw_lock_exclusive(lck
);
877 lck_rw_lock_shared(lck
);
881 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
882 (void)lck_rw_done(lck
);
890 lockgroup_info_array_t
*lockgroup_infop
,
891 mach_msg_type_number_t
*lockgroup_infoCntp
)
893 lockgroup_info_t
*lockgroup_info_base
;
894 lockgroup_info_t
*lockgroup_info
;
895 vm_offset_t lockgroup_info_addr
;
896 vm_size_t lockgroup_info_size
;
903 if (host
== HOST_NULL
)
904 return KERN_INVALID_HOST
;
906 mutex_lock(&lck_grp_lock
);
908 lockgroup_info_size
= round_page(lck_grp_cnt
* sizeof *lockgroup_info
);
909 kr
= kmem_alloc_pageable(ipc_kernel_map
,
910 &lockgroup_info_addr
, lockgroup_info_size
);
911 if (kr
!= KERN_SUCCESS
) {
912 mutex_unlock(&lck_grp_lock
);
916 lockgroup_info_base
= (lockgroup_info_t
*) lockgroup_info_addr
;
917 lck_grp
= (lck_grp_t
*)queue_first(&lck_grp_queue
);
918 lockgroup_info
= lockgroup_info_base
;
920 for (i
= 0; i
< lck_grp_cnt
; i
++) {
922 lockgroup_info
->lock_spin_cnt
= lck_grp
->lck_grp_spincnt
;
923 lockgroup_info
->lock_spin_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_util_cnt
;
924 lockgroup_info
->lock_spin_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cnt
;
925 lockgroup_info
->lock_spin_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_miss_cnt
;
926 lockgroup_info
->lock_spin_held_max
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_max
;
927 lockgroup_info
->lock_spin_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cum
;
929 lockgroup_info
->lock_mtx_cnt
= lck_grp
->lck_grp_mtxcnt
;
930 lockgroup_info
->lock_mtx_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_util_cnt
;
931 lockgroup_info
->lock_mtx_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cnt
;
932 lockgroup_info
->lock_mtx_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_miss_cnt
;
933 lockgroup_info
->lock_mtx_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cnt
;
934 lockgroup_info
->lock_mtx_held_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_max
;
935 lockgroup_info
->lock_mtx_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cum
;
936 lockgroup_info
->lock_mtx_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_max
;
937 lockgroup_info
->lock_mtx_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cum
;
939 lockgroup_info
->lock_rw_cnt
= lck_grp
->lck_grp_rwcnt
;
940 lockgroup_info
->lock_rw_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
;
941 lockgroup_info
->lock_rw_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cnt
;
942 lockgroup_info
->lock_rw_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
;
943 lockgroup_info
->lock_rw_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
;
944 lockgroup_info
->lock_rw_held_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_max
;
945 lockgroup_info
->lock_rw_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cum
;
946 lockgroup_info
->lock_rw_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_max
;
947 lockgroup_info
->lock_rw_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cum
;
949 (void) strncpy(lockgroup_info
->lockgroup_name
,lck_grp
->lck_grp_name
, LOCKGROUP_MAX_NAME
);
951 lck_grp
= (lck_grp_t
*)(queue_next((queue_entry_t
)(lck_grp
)));
955 *lockgroup_infoCntp
= lck_grp_cnt
;
956 mutex_unlock(&lck_grp_lock
);
958 used
= (*lockgroup_infoCntp
) * sizeof *lockgroup_info
;
960 if (used
!= lockgroup_info_size
)
961 bzero((char *) lockgroup_info
, lockgroup_info_size
- used
);
963 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)lockgroup_info_addr
,
964 (vm_map_size_t
)lockgroup_info_size
, TRUE
, ©
);
965 assert(kr
== KERN_SUCCESS
);
967 *lockgroup_infop
= (lockgroup_info_t
*) copy
;
969 return(KERN_SUCCESS
);
973 * Compatibility module
976 extern lck_rw_t
*lock_alloc_EXT( boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
977 extern void lock_done_EXT(lck_rw_t
*lock
);
978 extern void lock_free_EXT(lck_rw_t
*lock
);
979 extern void lock_init_EXT(lck_rw_t
*lock
, boolean_t can_sleep
, unsigned short tag0
, unsigned short tag1
);
980 extern void lock_read_EXT(lck_rw_t
*lock
);
981 extern boolean_t
lock_read_to_write_EXT(lck_rw_t
*lock
);
982 extern void lock_write_EXT(lck_rw_t
*lock
);
983 extern void lock_write_to_read_EXT(lck_rw_t
*lock
);
984 extern wait_result_t
thread_sleep_lock_write_EXT(
985 event_t event
, lck_rw_t
*lock
, wait_interrupt_t interruptible
);
987 extern lck_mtx_t
*mutex_alloc_EXT(unsigned short tag
);
988 extern void mutex_free_EXT(lck_mtx_t
*mutex
);
989 extern void mutex_init_EXT(lck_mtx_t
*mutex
, unsigned short tag
);
990 extern void mutex_lock_EXT(lck_mtx_t
*mutex
);
991 extern boolean_t
mutex_try_EXT(lck_mtx_t
*mutex
);
992 extern void mutex_unlock_EXT(lck_mtx_t
*mutex
);
993 extern wait_result_t
thread_sleep_mutex_EXT(
994 event_t event
, lck_mtx_t
*mutex
, wait_interrupt_t interruptible
);
995 extern wait_result_t
thread_sleep_mutex_deadline_EXT(
996 event_t event
, lck_mtx_t
*mutex
, uint64_t deadline
, wait_interrupt_t interruptible
);
998 extern void usimple_lock_EXT(lck_spin_t
*lock
);
999 extern void usimple_lock_init_EXT(lck_spin_t
*lock
, unsigned short tag
);
1000 extern unsigned int usimple_lock_try_EXT(lck_spin_t
*lock
);
1001 extern void usimple_unlock_EXT(lck_spin_t
*lock
);
1002 extern wait_result_t
thread_sleep_usimple_lock_EXT(event_t event
, lck_spin_t
*lock
, wait_interrupt_t interruptible
);
1006 __unused boolean_t can_sleep
,
1007 __unused
unsigned short tag0
,
1008 __unused
unsigned short tag1
)
1010 return( lck_rw_alloc_init( &LockCompatGroup
, LCK_ATTR_NULL
));
1017 (void) lck_rw_done(lock
);
1024 lck_rw_free(lock
, &LockCompatGroup
);
1030 __unused boolean_t can_sleep
,
1031 __unused
unsigned short tag0
,
1032 __unused
unsigned short tag1
)
1034 lck_rw_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1041 lck_rw_lock_shared( lock
);
1045 lock_read_to_write_EXT(
1048 return( lck_rw_lock_shared_to_exclusive(lock
));
1055 lck_rw_lock_exclusive(lock
);
1059 lock_write_to_read_EXT(
1062 lck_rw_lock_exclusive_to_shared(lock
);
1066 thread_sleep_lock_write_EXT(
1069 wait_interrupt_t interruptible
)
1071 return( lck_rw_sleep(lock
, LCK_SLEEP_EXCLUSIVE
, event
, interruptible
));
1076 __unused
unsigned short tag
)
1078 return(lck_mtx_alloc_init(&LockCompatGroup
, LCK_ATTR_NULL
));
1085 lck_mtx_free(mutex
, &LockCompatGroup
);
1091 __unused
unsigned short tag
)
1093 lck_mtx_init(mutex
, &LockCompatGroup
, LCK_ATTR_NULL
);
1100 lck_mtx_lock(mutex
);
1107 return(lck_mtx_try_lock(mutex
));
1114 lck_mtx_unlock(mutex
);
1118 thread_sleep_mutex_EXT(
1121 wait_interrupt_t interruptible
)
1123 return( lck_mtx_sleep(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
));
1127 thread_sleep_mutex_deadline_EXT(
1131 wait_interrupt_t interruptible
)
1133 return( lck_mtx_sleep_deadline(mutex
, LCK_SLEEP_DEFAULT
, event
, interruptible
, deadline
));
1140 lck_spin_lock(lock
);
1144 usimple_lock_init_EXT(
1146 __unused
unsigned short tag
)
1148 lck_spin_init(lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
1152 usimple_lock_try_EXT(
1155 return(lck_spin_try_lock(lock
));
1162 lck_spin_unlock(lock
);
1166 thread_sleep_usimple_lock_EXT(
1169 wait_interrupt_t interruptible
)
1171 return( lck_spin_sleep(lock
, LCK_SLEEP_DEFAULT
, event
, interruptible
));