xref: /xnu-8020.140.41/osfmk/kern/lock_types.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 2021 Apple Computer, Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions  *
15*27b03b36SApple OSS Distributions  * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions  *
18*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions  * limitations under the License.
25*27b03b36SApple OSS Distributions  *
26*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions  */
28*27b03b36SApple OSS Distributions #ifndef _KERN_LOCK_TYPES_H
29*27b03b36SApple OSS Distributions #define _KERN_LOCK_TYPES_H
30*27b03b36SApple OSS Distributions 
31*27b03b36SApple OSS Distributions #include <kern/kern_types.h>
32*27b03b36SApple OSS Distributions 
33*27b03b36SApple OSS Distributions __BEGIN_DECLS
34*27b03b36SApple OSS Distributions 
35*27b03b36SApple OSS Distributions #ifdef  XNU_KERNEL_PRIVATE
36*27b03b36SApple OSS Distributions #if __x86_64__
37*27b03b36SApple OSS Distributions /*
38*27b03b36SApple OSS Distributions  * Extended mutexes are only implemented on x86_64
39*27b03b36SApple OSS Distributions  */
40*27b03b36SApple OSS Distributions #define HAS_EXT_MUTEXES 1
41*27b03b36SApple OSS Distributions #endif /* __x86_64__ */
42*27b03b36SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
43*27b03b36SApple OSS Distributions 
44*27b03b36SApple OSS Distributions /*!
45*27b03b36SApple OSS Distributions  * @enum lck_sleep_action_t
46*27b03b36SApple OSS Distributions  *
47*27b03b36SApple OSS Distributions  * @abstract
48*27b03b36SApple OSS Distributions  * An action to pass to the @c lck_*_sleep* family of functions.
49*27b03b36SApple OSS Distributions  */
50*27b03b36SApple OSS Distributions __options_decl(lck_sleep_action_t, unsigned int, {
51*27b03b36SApple OSS Distributions 	LCK_SLEEP_DEFAULT      = 0x00,    /**< Release the lock while waiting for the event, then reclaim */
52*27b03b36SApple OSS Distributions 	LCK_SLEEP_UNLOCK       = 0x01,    /**< Release the lock and return unheld */
53*27b03b36SApple OSS Distributions 	LCK_SLEEP_SHARED       = 0x02,    /**< Reclaim the lock in shared mode (RW only) */
54*27b03b36SApple OSS Distributions 	LCK_SLEEP_EXCLUSIVE    = 0x04,    /**< Reclaim the lock in exclusive mode (RW only) */
55*27b03b36SApple OSS Distributions 	LCK_SLEEP_SPIN         = 0x08,    /**< Reclaim the lock in spin mode (mutex only) */
56*27b03b36SApple OSS Distributions 	LCK_SLEEP_PROMOTED_PRI = 0x10,    /**< Sleep at a promoted priority */
57*27b03b36SApple OSS Distributions 	LCK_SLEEP_SPIN_ALWAYS  = 0x20,    /**< Reclaim the lock in spin-always mode (mutex only) */
58*27b03b36SApple OSS Distributions });
59*27b03b36SApple OSS Distributions 
60*27b03b36SApple OSS Distributions #define LCK_SLEEP_MASK           0x3f     /* Valid actions */
61*27b03b36SApple OSS Distributions 
62*27b03b36SApple OSS Distributions __options_decl(lck_wake_action_t, unsigned int, {
63*27b03b36SApple OSS Distributions 	LCK_WAKE_DEFAULT                = 0x00,  /* If waiters are present, transfer their push to the wokenup thread */
64*27b03b36SApple OSS Distributions 	LCK_WAKE_DO_NOT_TRANSFER_PUSH   = 0x01,  /* Do not transfer waiters push when waking up */
65*27b03b36SApple OSS Distributions });
66*27b03b36SApple OSS Distributions 
67*27b03b36SApple OSS Distributions #if MACH_KERNEL_PRIVATE
68*27b03b36SApple OSS Distributions 
69*27b03b36SApple OSS Distributions /*!
70*27b03b36SApple OSS Distributions  * @enum hw_lock_status_t
71*27b03b36SApple OSS Distributions  *
72*27b03b36SApple OSS Distributions  * @abstract
73*27b03b36SApple OSS Distributions  * Used to pass information about very low level locking primitives.
74*27b03b36SApple OSS Distributions  *
75*27b03b36SApple OSS Distributions  */
76*27b03b36SApple OSS Distributions __enum_closed_decl(hw_lock_status_t, int, {
77*27b03b36SApple OSS Distributions 	/**
78*27b03b36SApple OSS Distributions 	 * The lock was not taken because it is in an invalid state,
79*27b03b36SApple OSS Distributions 	 * or the memory was unmapped.
80*27b03b36SApple OSS Distributions 	 *
81*27b03b36SApple OSS Distributions 	 * This is only valid for @c *_allow_invalid() variants.
82*27b03b36SApple OSS Distributions 	 *
83*27b03b36SApple OSS Distributions 	 * Preemption is preserved to the caller level for all variants.
84*27b03b36SApple OSS Distributions 	 */
85*27b03b36SApple OSS Distributions 	HW_LOCK_INVALID    = -1,
86*27b03b36SApple OSS Distributions 
87*27b03b36SApple OSS Distributions 	/**
88*27b03b36SApple OSS Distributions 	 * the lock wasn't acquired and is contended / timed out.
89*27b03b36SApple OSS Distributions 	 *
90*27b03b36SApple OSS Distributions 	 * - @c *_nopreempt() variants: preemption level preserved
91*27b03b36SApple OSS Distributions 	 * - @c *_trylock() variants: preemption level preserved
92*27b03b36SApple OSS Distributions 	 * - other variants: preemption is disabled
93*27b03b36SApple OSS Distributions 	 */
94*27b03b36SApple OSS Distributions 	HW_LOCK_CONTENDED  =  0,
95*27b03b36SApple OSS Distributions 
96*27b03b36SApple OSS Distributions 	/**
97*27b03b36SApple OSS Distributions 	 * the lock was acquired successfully
98*27b03b36SApple OSS Distributions 	 *
99*27b03b36SApple OSS Distributions 	 * - @c *_nopreempt() variants: preemption level preserved
100*27b03b36SApple OSS Distributions 	 * - other variants: preemption is disabled
101*27b03b36SApple OSS Distributions 	 */
102*27b03b36SApple OSS Distributions 	HW_LOCK_ACQUIRED   =  1,
103*27b03b36SApple OSS Distributions });
104*27b03b36SApple OSS Distributions 
105*27b03b36SApple OSS Distributions /*!
106*27b03b36SApple OSS Distributions  * @enum hw_lock_timeout_status_t
107*27b03b36SApple OSS Distributions  *
108*27b03b36SApple OSS Distributions  * @abstract
109*27b03b36SApple OSS Distributions  * Used by spinlock timeout handlers.
110*27b03b36SApple OSS Distributions  *
111*27b03b36SApple OSS Distributions  * @const HW_LOCK_TIMEOUT_RETURN
112*27b03b36SApple OSS Distributions  * Tell the @c hw_lock*_to* caller to break out of the wait
113*27b03b36SApple OSS Distributions  * and return HW_LOCK_CONTENDED.
114*27b03b36SApple OSS Distributions  *
115*27b03b36SApple OSS Distributions  * @const HW_LOCK_TIMEOUT_CONTINUE
116*27b03b36SApple OSS Distributions  * Keep spinning for another "timeout".
117*27b03b36SApple OSS Distributions  */
118*27b03b36SApple OSS Distributions __enum_closed_decl(hw_lock_timeout_status_t, _Bool, {
119*27b03b36SApple OSS Distributions 	HW_LOCK_TIMEOUT_RETURN,         /**< return without taking the lock */
120*27b03b36SApple OSS Distributions 	HW_LOCK_TIMEOUT_CONTINUE,       /**< keep spinning                  */
121*27b03b36SApple OSS Distributions });
122*27b03b36SApple OSS Distributions 
123*27b03b36SApple OSS Distributions /*!
124*27b03b36SApple OSS Distributions  * @typedef hw_lock_timeout_handler_t
125*27b03b36SApple OSS Distributions  *
126*27b03b36SApple OSS Distributions  * @abstract
127*27b03b36SApple OSS Distributions  * The type of the timeout handlers for low level locking primitives.
128*27b03b36SApple OSS Distributions  *
129*27b03b36SApple OSS Distributions  * @discussion
130*27b03b36SApple OSS Distributions  * Typical handlers are written to just panic and not return
131*27b03b36SApple OSS Distributions  * unless some very specific conditions are met (debugging, ...).
132*27b03b36SApple OSS Distributions  */
133*27b03b36SApple OSS Distributions typedef hw_lock_timeout_status_t (*hw_lock_timeout_handler_t)(void *lock,
134*27b03b36SApple OSS Distributions     uint64_t timeout, uint64_t start, uint64_t now, uint64_t interrupt_time);
135*27b03b36SApple OSS Distributions 
136*27b03b36SApple OSS Distributions #endif /* MACH_KERNEL_PRIVATE */
137*27b03b36SApple OSS Distributions 
138*27b03b36SApple OSS Distributions __END_DECLS
139*27b03b36SApple OSS Distributions 
140*27b03b36SApple OSS Distributions #endif /* _KERN_LOCK_TYPES_H */
141