xref: /xnu-10002.61.3/iokit/IOKit/IOLocks.h (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 1998-2012 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  *
30  */
31 
32 #ifndef __IOKIT_IOLOCKS_H
33 #define __IOKIT_IOLOCKS_H
34 
35 #ifndef KERNEL
36 #error IOLocks.h is for kernel use only
37 #endif
38 
39 #include <sys/appleapiopts.h>
40 #include <sys/cdefs.h>
41 
42 #include <IOKit/system.h>
43 
44 #include <IOKit/IOReturn.h>
45 #include <IOKit/IOTypes.h>
46 #include <machine/machine_routines.h>
47 #include <libkern/locks.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /*! @var IOLockGroup
54  *   Global lock group used by all IOKit locks.  To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
55  */
56 extern lck_grp_t        *IOLockGroup;
57 
58 #if defined(XNU_KERNEL_PRIVATE)
59 #define IOLOCKS_INLINE  1
60 #endif
61 
62 /*
63  * Mutex lock operations
64  */
65 
66 #ifdef  IOLOCKS_INLINE
67 typedef lck_mtx_t       IOLock;
68 #else
69 typedef struct _IOLock  IOLock;
70 #endif  /* IOLOCKS_INLINE */
71 
72 
73 /*! @function IOLockAlloc
74  *   @abstract Allocates and initializes a mutex.
75  *   @discussion Allocates a mutex in general purpose memory, and initializes it. Mutexes are general purpose blocking mutual exclusion locks, supplied by libkern/locks.h. This function may block and so should not be called from interrupt level or while a spin lock is held.  IOLocks use the global IOKit lock group, IOLockGroup.  To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
76  *   @result Pointer to the allocated lock, or zero on failure. */
77 
78 IOLock * IOLockAlloc( void );
79 
80 /*! @function IOLockFree
81  *   @abstract Frees a mutex.
82  *   @discussion Frees a lock allocated with IOLockAlloc. Mutex should be unlocked with no waiters.
83  *   @param lock Pointer to the allocated lock. */
84 
85 void    IOLockFree( IOLock * lock);
86 
87 /*! @function IOLockGetMachLock
88  *   @abstract Accessor to a Mach mutex.
89  *   @discussion Accessor to the Mach mutex.
90  *   @param lock Pointer to the allocated lock. */
91 
92 lck_mtx_t * IOLockGetMachLock( IOLock * lock);
93 
94 /*! @function IOLockLock
95  *   @abstract Lock a mutex.
96  *   @discussion Lock the mutex. If the lock is held by any thread, block waiting for its unlock. This function may block and so should not be called from interrupt level or while a spin lock is held. Locking the mutex recursively from one thread will result in deadlock.
97  *   @param lock Pointer to the allocated lock. */
98 
99 #ifdef  IOLOCKS_INLINE
100 #define IOLockLock(l)   lck_mtx_lock(l)
101 #else
102 void    IOLockLock( IOLock * lock);
103 #endif  /* !IOLOCKS_INLINE */
104 
105 /*! @function IOLockTryLock
106  *   @abstract Attempt to lock a mutex.
107  *   @discussion Lock the mutex if it is currently unlocked, and return true. If the lock is held by any thread, return false.
108  *   @param lock Pointer to the allocated lock.
109  *   @result True if the mutex was unlocked and is now locked by the caller, otherwise false. */
110 
111 #ifdef  IOLOCKS_INLINE
112 #define IOLockTryLock(l)        lck_mtx_try_lock(l)
113 #else
114 boolean_t IOLockTryLock( IOLock * lock);
115 #endif  /* !IOLOCKS_INLINE */
116 
117 /*! @function IOLockUnlock
118  *   @abstract Unlock a mutex.
119  *  @discussion Unlock the mutex and wake any blocked waiters. Results are undefined if the caller has not locked the mutex. This function may block and so should not be called from interrupt level or while a spin lock is held.
120  *   @param lock Pointer to the allocated lock. */
121 
122 #ifdef  IOLOCKS_INLINE
123 #define IOLockUnlock(l) lck_mtx_unlock(l)
124 #else
125 void    IOLockUnlock( IOLock * lock);
126 #endif  /* !IOLOCKS_INLINE */
127 
128 /*! @function IOLockSleep
129  *   @abstract Sleep with mutex unlock and relock
130  *  @discussion Prepare to sleep,unlock the mutex, and re-acquire it on wakeup. Results are undefined if the caller has not locked the mutex. This function may block and so should not be called from interrupt level or while a spin lock is held.
131  *   @param lock Pointer to the locked lock.
132  *   @param event The event to sleep on. Must be non-NULL.
133  *   @param interType How can the sleep be interrupted.
134  *       @result The wait-result value indicating how the thread was awakened.*/
135 int     IOLockSleep( IOLock * lock, void *event, UInt32 interType) __DARWIN14_ALIAS(IOLockSleep);
136 
137 int     IOLockSleepDeadline( IOLock * lock, void *event,
138     AbsoluteTime deadline, UInt32 interType) __DARWIN14_ALIAS(IOLockSleepDeadline);
139 
140 void    IOLockWakeup(IOLock * lock, void *event, bool oneThread) __DARWIN14_ALIAS(IOLockWakeup);
141 
142 #ifdef XNU_KERNEL_PRIVATE
143 /*! @enum     IOLockAssertState
144  *  @abstract Used with IOLockAssert to assert the state of a lock.
145  */
146 typedef enum {
147 	kIOLockAssertOwned    = LCK_ASSERT_OWNED,
148 	kIOLockAssertNotOwned = LCK_ASSERT_NOTOWNED
149 } IOLockAssertState;
150 
151 #ifdef IOLOCKS_INLINE
152 #define IOLockAssert(l, type) LCK_MTX_ASSERT(l, type)
153 
154 /*! @function   IOLockInlineInit()
155  * @abstract    Initializes an inline lock.
156  */
157 void    IOLockInlineInit(IOLock *);
158 
159 /*! @function   IOLockInlineDestroy()
160  * @abstract    Destroys an inline lock.
161  */
162 void    IOLockInlineDestroy(IOLock *);
163 
164 #else
165 /*! @function   IOLockAssert
166  *  @abstract   Assert that lock is either held or not held by current thread.
167  *  @discussion Call with either kIOLockAssertOwned or kIOLockAssertNotOwned.
168  *  Panics the kernel if the lock is not owned if called with kIOLockAssertOwned,
169  *  and vice-versa.
170  */
171 void    IOLockAssert(IOLock * lock, IOLockAssertState type);
172 #endif /* !IOLOCKS_INLINE */
173 #endif /* !XNU_KERNEL_PRIVATE */
174 
175 #ifdef __APPLE_API_OBSOLETE
176 
177 /* The following API is deprecated */
178 
179 typedef enum {
180 	kIOLockStateUnlocked        = 0,
181 	kIOLockStateLocked          = 1
182 } IOLockState;
183 
184 void    IOLockInitWithState( IOLock * lock, IOLockState state);
185 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
186 
187 static __inline__ void
IOTakeLock(IOLock * lock)188 IOTakeLock( IOLock * lock)
189 {
190 	IOLockLock(lock);
191 }
192 static __inline__ boolean_t
IOTryLock(IOLock * lock)193 IOTryLock(  IOLock * lock)
194 {
195 	return IOLockTryLock(lock);
196 }
197 static __inline__ void
IOUnlock(IOLock * lock)198 IOUnlock(   IOLock * lock)
199 {
200 	IOLockUnlock(lock);
201 }
202 
203 #endif /* __APPLE_API_OBSOLETE */
204 
205 /*
206  * Recursive lock operations
207  */
208 
209 typedef struct _IORecursiveLock IORecursiveLock;
210 
211 /*! @function IORecursiveLockAlloc
212  *   @abstract Allocates and initializes an recursive lock.
213  *   @discussion Allocates a recursive lock in general purpose memory, and initializes it. Recursive locks function identically to mutexes but allow one thread to lock more than once, with balanced unlocks.  IORecursiveLocks use the global IOKit lock group, IOLockGroup.  To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
214  *   @result Pointer to the allocated lock, or zero on failure. */
215 
216 IORecursiveLock * IORecursiveLockAlloc( void );
217 
218 /*! @function IORecursiveLockFree
219  *   @abstract Frees a recursive lock.
220  *   @discussion Frees a lock allocated with IORecursiveLockAlloc. Lock should be unlocked with no waiters.
221  *   @param lock Pointer to the allocated lock. */
222 
223 void            IORecursiveLockFree( IORecursiveLock * lock);
224 
225 /*! @function IORecursiveLockGetMachLock
226  *   @abstract Accessor to a Mach mutex.
227  *   @discussion Accessor to the Mach mutex.
228  *   @param lock Pointer to the allocated lock. */
229 
230 lck_mtx_t * IORecursiveLockGetMachLock( IORecursiveLock * lock);
231 
232 /*! @function IORecursiveLockLock
233  *   @abstract Lock a recursive lock.
234  *   @discussion Lock the recursive lock. If the lock is held by another thread, block waiting for its unlock. This function may block and so should not be called from interrupt level or while a spin lock is held. The lock may be taken recursively by the same thread, with a balanced number of calls to IORecursiveLockUnlock.
235  *   @param lock Pointer to the allocated lock. */
236 
237 void            IORecursiveLockLock( IORecursiveLock * lock);
238 
239 /*! @function IORecursiveLockTryLock
240  *   @abstract Attempt to lock a recursive lock.
241  *   @discussion Lock the lock if it is currently unlocked, or held by the calling thread, and return true. If the lock is held by another thread, return false. Successful calls to IORecursiveLockTryLock should be balanced with calls to IORecursiveLockUnlock.
242  *   @param lock Pointer to the allocated lock.
243  *   @result True if the lock is now locked by the caller, otherwise false. */
244 
245 boolean_t       IORecursiveLockTryLock( IORecursiveLock * lock);
246 
247 /*! @function IORecursiveLockUnlock
248  *   @abstract Unlock a recursive lock.
249  *  @discussion Undo one call to IORecursiveLockLock, if the lock is now unlocked wake any blocked waiters. Results are undefined if the caller does not balance calls to IORecursiveLockLock with IORecursiveLockUnlock. This function may block and so should not be called from interrupt level or while a spin lock is held.
250  *   @param lock Pointer to the allocated lock. */
251 
252 void            IORecursiveLockUnlock( IORecursiveLock * lock);
253 
254 /*! @function IORecursiveLockHaveLock
255  *   @abstract Check if a recursive lock is held by the calling thread.
256  *   @discussion If the lock is held by the calling thread, return true, otherwise the lock is unlocked, or held by another thread and false is returned.
257  *   @param lock Pointer to the allocated lock.
258  *   @result True if the calling thread holds the lock otherwise false. */
259 
260 boolean_t       IORecursiveLockHaveLock( const IORecursiveLock * lock);
261 
262 extern int      IORecursiveLockSleep( IORecursiveLock *_lock,
263     void *event, UInt32 interType);
264 extern int      IORecursiveLockSleepDeadline( IORecursiveLock * _lock, void *event,
265     AbsoluteTime deadline, UInt32 interType);
266 extern void     IORecursiveLockWakeup( IORecursiveLock *_lock,
267     void *event, bool oneThread);
268 
269 /*
270  * Complex (read/write) lock operations
271  */
272 
273 #ifdef  IOLOCKS_INLINE
274 typedef lck_rw_t                IORWLock;
275 #else
276 typedef struct _IORWLock        IORWLock;
277 #endif  /* IOLOCKS_INLINE */
278 
279 /*! @function IORWLockAlloc
280  *   @abstract Allocates and initializes a read/write lock.
281  *   @discussion Allocates and initializes a read/write lock in general purpose memory. Read/write locks provide for multiple readers, one exclusive writer, and are supplied by libkern/locks.h. This function may block and so should not be called from interrupt level or while a spin lock is held.  IORWLocks use the global IOKit lock group, IOLockGroup.  To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
282  *   @result Pointer to the allocated lock, or zero on failure. */
283 
284 IORWLock * IORWLockAlloc( void );
285 
286 /*! @function IORWLockFree
287  *  @abstract Frees a read/write lock.
288  *  @discussion Frees a lock allocated with IORWLockAlloc. Lock should be unlocked with no waiters.
289  *   @param lock Pointer to the allocated lock. */
290 
291 void    IORWLockFree( IORWLock * lock);
292 
293 /*! @function IORWLockGetMachLock
294  *   @abstract Accessor to a Mach read/write lock.
295  *   @discussion Accessor to the Mach read/write lock.
296  *   @param lock Pointer to the allocated lock. */
297 
298 lck_rw_t * IORWLockGetMachLock( IORWLock * lock);
299 
300 /*! @function IORWLockRead
301  *   @abstract Lock a read/write lock for read.
302  *  @discussion Lock the lock for read, allowing multiple readers when there are no writers. If the lock is held for write, block waiting for its unlock. This function may block and so should not be called from interrupt level or while a spin lock is held. Locking the lock recursively from one thread, for read or write, can result in deadlock.
303  *   @param lock Pointer to the allocated lock. */
304 
305 #ifdef  IOLOCKS_INLINE
306 #define IORWLockRead(l) lck_rw_lock_shared(l)
307 #else
308 void    IORWLockRead(IORWLock * lock);
309 #endif  /* !IOLOCKS_INLINE */
310 
311 /*! @function IORWLockTryRead
312  *   @abstract Attempt to lock a read/write lock for read.
313  *  @discussion Lock the lock for read, allowing multiple readers when there are no writers. If the lock is held for write, return false. Return true otherwise.
314  *   @param lock Pointer to the allocated lock. */
315 
316 #ifdef  IOLOCKS_INLINE
317 #define IORWLockTryRead(l)        lck_rw_try_lock_shared(l)
318 #else
319 void    IORWLockTryRead( IORWLock * lock);
320 #endif  /* !IOLOCKS_INLINE */
321 
322 /*! @function IORWLockWrite
323  *   @abstract Lock a read/write lock for write.
324  *   @discussion Lock the lock for write, allowing one writer exlusive access. If the lock is held for read or write, block waiting for its unlock. This function may block and so should not be called from interrupt level or while a spin lock is held. Locking the lock recursively from one thread, for read or write, can result in deadlock.
325  *   @param lock Pointer to the allocated lock. */
326 
327 #ifdef  IOLOCKS_INLINE
328 #define IORWLockWrite(l)        lck_rw_lock_exclusive(l)
329 #else
330 void    IORWLockWrite( IORWLock * lock);
331 #endif  /* !IOLOCKS_INLINE */
332 
333 /*! @function IORWLockTryWrite
334  *   @abstract Attempt to lock a read/write lock for write.
335  *   @discussion Lock the lock for write, allowing one writer exlusive access. If the lock is held for read or write, return false. Return true otherwise.
336  *   @param lock Pointer to the allocated lock. */
337 
338 #ifdef  IOLOCKS_INLINE
339 #define IORWLockTryWrite(l)        lck_rw_try_lock_exclusive(l)
340 #else
341 void    IORWLockTryWrite( IORWLock * lock);
342 #endif  /* !IOLOCKS_INLINE */
343 
344 /*! @function IORWLockUnlock
345  *   @abstract Unlock a read/write lock.
346  *   @discussion Undo one call to IORWLockRead or IORWLockWrite. Results are undefined if the caller has not locked the lock. This function may block and so should not be called from interrupt level or while a spin lock is held.
347  *   @param lock Pointer to the allocated lock. */
348 
349 #ifdef  IOLOCKS_INLINE
350 #define IORWLockUnlock(l)       lck_rw_done(l)
351 #else
352 void    IORWLockUnlock( IORWLock * lock);
353 #endif  /* !IOLOCKS_INLINE */
354 
355 #ifdef XNU_KERNEL_PRIVATE
356 /*! @enum     IORWLockAssertState
357  *  @abstract Used with IORWLockAssert to assert the state of a lock.
358  */
359 typedef enum {
360 	kIORWLockAssertRead    = LCK_RW_ASSERT_SHARED,
361 	kIORWLockAssertWrite   = LCK_RW_ASSERT_EXCLUSIVE,
362 	kIORWLockAssertHeld    = LCK_RW_ASSERT_HELD,
363 	kIORWLockAssertNotHeld = LCK_RW_ASSERT_NOTHELD
364 } IORWLockAssertState;
365 
366 #ifdef IOLOCKS_INLINE
367 #define IORWLockAssert(l, type) LCK_RW_ASSERT(l, type)
368 
369 /*! @function   IORWLockInlineInit()
370  * @abstract    Initializes an inline lock.
371  */
372 void    IORWLockInlineInit(IORWLock *);
373 
374 /*! @function   IORWLockInlineDestroy()
375  * @abstract    Destroys an inline lock.
376  */
377 void    IORWLockInlineDestroy(IORWLock *);
378 
379 #else
380 /*! @function   IORWLockAssert
381  *  @abstract   Assert that a reader-writer lock is either held or not held
382  *  by the current thread.
383  *  @discussion Call with a value defined by the IORWLockAssertState type.
384  *  If the specified lock is not in the state specified by the type argument,
385  *  then the kernel will panic.
386  */
387 void    IORWLockAssert(IORWLock * lock, IORWLockAssertState type);
388 #endif /* !IOLOCKS_INLINE */
389 #endif /* !XNU_KERNEL_PRIVATE */
390 
391 #ifdef __APPLE_API_OBSOLETE
392 
393 /* The following API is deprecated */
394 
395 static __inline__ void
IOReadLock(IORWLock * lock)396 IOReadLock( IORWLock * lock)
397 {
398 	IORWLockRead(lock);
399 }
400 static __inline__ void
IOWriteLock(IORWLock * lock)401 IOWriteLock(  IORWLock * lock)
402 {
403 	IORWLockWrite(lock);
404 }
405 static __inline__ void
IORWUnlock(IORWLock * lock)406 IORWUnlock(   IORWLock * lock)
407 {
408 	IORWLockUnlock(lock);
409 }
410 
411 #endif /* __APPLE_API_OBSOLETE */
412 
413 
414 /*
415  * Simple locks. Cannot block while holding a simple lock.
416  */
417 
418 #ifdef  IOLOCKS_INLINE
419 typedef lck_spin_t              IOSimpleLock;
420 #else
421 typedef struct _IOSimpleLock    IOSimpleLock;
422 #endif  /* IOLOCKS_INLINE */
423 
424 /*! @function IOSimpleLockAlloc
425  *   @abstract Allocates and initializes a spin lock.
426  *   @discussion Allocates and initializes a spin lock in general purpose memory. Spin locks provide non-blocking mutual exclusion for synchronization between thread context and interrupt context, or for multiprocessor synchronization, and are supplied by libkern/locks.h. This function may block and so should not be called from interrupt level or while a spin lock is held.  IOSimpleLocks use the global IOKit lock group, IOLockGroup.  To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
427  *   @result Pointer to the allocated lock, or zero on failure. */
428 
429 IOSimpleLock * IOSimpleLockAlloc( void );
430 
431 /*! @function IOSimpleLockFree
432  *   @abstract Frees a spin lock.
433  *   @discussion Frees a lock allocated with IOSimpleLockAlloc.
434  *   @param lock Pointer to the lock. */
435 
436 void IOSimpleLockFree( IOSimpleLock * lock );
437 
438 /*! @function IOSimpleLockGetMachLock
439  *   @abstract Accessor to a Mach spin lock.
440  *   @discussion Accessor to the Mach spin lock.
441  *   @param lock Pointer to the allocated lock. */
442 
443 lck_spin_t * IOSimpleLockGetMachLock( IOSimpleLock * lock);
444 
445 /*! @function IOSimpleLockInit
446  *   @abstract Initialize a spin lock.
447  *   @discussion Initialize a non heap allocated spin lock to the unlocked state. Use this function when your lock is, for example, a member variable. You will need to call IOSimpleLockDestroy when you are finished with the lock to avoid lock group refcount leaks.
448  *   @param lock Pointer to the lock. */
449 
450 void IOSimpleLockInit( IOSimpleLock * lock );
451 
452 /*! @function IOSimpleLockDestroy
453  *   @abstract De-initializes (destroys) a spin lock initialized with IOSimpleLockInit
454  *   @discussion Destroy / De-initialize a non heap allocated spin lock, releasing any system resources such as lock group refcounts.
455  *   @param lock Pointer to the lock. */
456 
457 void IOSimpleLockDestroy( IOSimpleLock * lock );
458 
459 /*! @function IOSimpleLockLock
460  *   @abstract Lock a spin lock.
461  *  @discussion Lock the spin lock. If the lock is held, spin waiting for its unlock. Spin locks disable preemption, cannot be held across any blocking operation, and should be held for very short periods. When used to synchronize between interrupt context and thread context they should be locked with interrupts disabled - IOSimpleLockLockDisableInterrupt() will do both. Locking the lock recursively from one thread will result in deadlock.
462  *   @param lock Pointer to the lock. */
463 
464 #ifdef  IOLOCKS_INLINE
465 #define IOSimpleLockLock(l)     lck_spin_lock(l)
466 #else
467 void IOSimpleLockLock( IOSimpleLock * lock );
468 #endif  /* !IOLOCKS_INLINE */
469 
470 
471 /*! @function IOSimpleLockTryLock
472  *   @abstract Attempt to lock a spin lock.
473  *  @discussion Lock the spin lock if it is currently unlocked, and return true. If the lock is held, return false. Successful calls to IOSimpleLockTryLock should be balanced with calls to IOSimpleLockUnlock.
474  *   @param lock Pointer to the lock.
475  *   @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
476 
477 #ifdef  IOLOCKS_INLINE
478 #define IOSimpleLockTryLock(l)  lck_spin_try_lock(l)
479 #else
480 boolean_t IOSimpleLockTryLock( IOSimpleLock * lock );
481 #endif  /* !IOLOCKS_INLINE */
482 
483 /*! @function IOSimpleLockUnlock
484  *   @abstract Unlock a spin lock.
485  *   @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
486  *   @param lock Pointer to the lock. */
487 
488 #ifdef  IOLOCKS_INLINE
489 #define IOSimpleLockUnlock(l)   lck_spin_unlock(l)
490 #else
491 void IOSimpleLockUnlock( IOSimpleLock * lock );
492 #endif  /* !IOLOCKS_INLINE */
493 
494 #ifdef XNU_KERNEL_PRIVATE
495 /*! @enum     IOSimpleLockAssertState
496  *  @abstract Used with IOSimpleLockAssert to assert the state of a lock.
497  */
498 typedef enum {
499 	kIOSimpleLockAssertOwned    = LCK_ASSERT_OWNED,
500 	kIOSimpleLockAssertNotOwned = LCK_ASSERT_NOTOWNED
501 } IOSimpleLockAssertState;
502 
503 #ifdef IOLOCKS_INLINE
504 #define IOSimpleLockAssert(l, type) LCK_SPIN_ASSERT(l, type)
505 #else
506 /*! @function   IOSimpleLockAssert
507  *  @abstract   Assert that spinlock is either held or not held by current thread.
508  *  @discussion Call with either kIOSimpleLockAssertOwned or kIOSimpleLockAssertNotOwned.
509  *  Panics the kernel if the lock is not owned if called with
510  *  kIOSimpleLockAssertOwned and vice-versa.
511  */
512 void    IOSimpleLockAssert(IOSimpleLock *lock, IOSimpleLockAssertState type);
513 #endif /* !IOLOCKS_INLINE */
514 #endif /* !XNU_KERNEL_PRIVATE */
515 
516 #if __LP64__
517 typedef boolean_t IOInterruptState;
518 #else
519 typedef long int IOInterruptState;
520 #endif
521 
522 /*! @function IOSimpleLockLockDisableInterrupt
523  *   @abstract Lock a spin lock.
524  *   @discussion Lock the spin lock. If the lock is held, spin waiting for its unlock. Simple locks disable preemption, cannot be held across any blocking operation, and should be held for very short periods. When used to synchronize between interrupt context and thread context they should be locked with interrupts disabled - IOSimpleLockLockDisableInterrupt() will do both. Locking the lock recursively from one thread will result in deadlock.
525  *   @param lock Pointer to the lock. */
526 
527 static __inline__
528 IOInterruptState
IOSimpleLockLockDisableInterrupt(IOSimpleLock * lock)529 IOSimpleLockLockDisableInterrupt( IOSimpleLock * lock )
530 {
531 	IOInterruptState    state = ml_set_interrupts_enabled( false );
532 	IOSimpleLockLock( lock );
533 	return state;
534 }
535 
536 /*! @function IOSimpleLockUnlockEnableInterrupt
537  *   @abstract Unlock a spin lock, and restore interrupt state.
538  *   @discussion Unlock the lock, and restore preemption and interrupts to the state as they were when the lock was taken. Results are undefined if the caller has not locked the lock.
539  *   @param lock Pointer to the lock.
540  *   @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
541 
542 static __inline__
543 void
IOSimpleLockUnlockEnableInterrupt(IOSimpleLock * lock,IOInterruptState state)544 IOSimpleLockUnlockEnableInterrupt( IOSimpleLock * lock,
545     IOInterruptState state )
546 {
547 	IOSimpleLockUnlock( lock );
548 	ml_set_interrupts_enabled( state );
549 }
550 
551 #ifdef __cplusplus
552 } /* extern "C" */
553 #endif
554 
555 #endif /* !__IOKIT_IOLOCKS_H */
556