xref: /xnu-11215/iokit/IOKit/IOLocks.h (revision 8d741a5d)
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 int     IOLockSleepWithInheritor( IOLock * lock, UInt32 lck_sleep_action,
152     void *event, thread_t inheritor, UInt32 interType, uint64_t deadline);
153 
154 void    IOLockWakeupAllWithInheritor(IOLock * lock, void *event);
155 
156 #ifdef IOLOCKS_INLINE
157 #define IOLockAssert(l, type) LCK_MTX_ASSERT(l, type)
158 
159 /*! @function   IOLockInlineInit()
160  * @abstract    Initializes an inline lock.
161  */
162 void    IOLockInlineInit(IOLock *);
163 
164 /*! @function   IOLockInlineDestroy()
165  * @abstract    Destroys an inline lock.
166  */
167 void    IOLockInlineDestroy(IOLock *);
168 
169 #else
170 /*! @function   IOLockAssert
171  *  @abstract   Assert that lock is either held or not held by current thread.
172  *  @discussion Call with either kIOLockAssertOwned or kIOLockAssertNotOwned.
173  *  Panics the kernel if the lock is not owned if called with kIOLockAssertOwned,
174  *  and vice-versa.
175  */
176 void    IOLockAssert(IOLock * lock, IOLockAssertState type);
177 #endif /* !IOLOCKS_INLINE */
178 #endif /* !XNU_KERNEL_PRIVATE */
179 
180 #ifdef __APPLE_API_OBSOLETE
181 
182 /* The following API is deprecated */
183 
184 typedef enum {
185 	kIOLockStateUnlocked        = 0,
186 	kIOLockStateLocked          = 1
187 } IOLockState;
188 
189 void    IOLockInitWithState( IOLock * lock, IOLockState state);
190 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
191 
192 static __inline__ void
IOTakeLock(IOLock * lock)193 IOTakeLock( IOLock * lock)
194 {
195 	IOLockLock(lock);
196 }
197 static __inline__ boolean_t
IOTryLock(IOLock * lock)198 IOTryLock(  IOLock * lock)
199 {
200 	return IOLockTryLock(lock);
201 }
202 static __inline__ void
IOUnlock(IOLock * lock)203 IOUnlock(   IOLock * lock)
204 {
205 	IOLockUnlock(lock);
206 }
207 
208 #endif /* __APPLE_API_OBSOLETE */
209 
210 /*
211  * Recursive lock operations
212  */
213 
214 typedef struct _IORecursiveLock IORecursiveLock;
215 
216 /*! @function IORecursiveLockAlloc
217  *   @abstract Allocates and initializes an recursive lock.
218  *   @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.
219  *   @result Pointer to the allocated lock, or zero on failure. */
220 
221 IORecursiveLock * IORecursiveLockAlloc( void );
222 
223 /*! @function IORecursiveLockFree
224  *   @abstract Frees a recursive lock.
225  *   @discussion Frees a lock allocated with IORecursiveLockAlloc. Lock should be unlocked with no waiters.
226  *   @param lock Pointer to the allocated lock. */
227 
228 void            IORecursiveLockFree( IORecursiveLock * lock);
229 
230 /*! @function IORecursiveLockGetMachLock
231  *   @abstract Accessor to a Mach mutex.
232  *   @discussion Accessor to the Mach mutex.
233  *   @param lock Pointer to the allocated lock. */
234 
235 lck_mtx_t * IORecursiveLockGetMachLock( IORecursiveLock * lock);
236 
237 /*! @function IORecursiveLockLock
238  *   @abstract Lock a recursive lock.
239  *   @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.
240  *   @param lock Pointer to the allocated lock. */
241 
242 void            IORecursiveLockLock( IORecursiveLock * lock);
243 
244 /*! @function IORecursiveLockTryLock
245  *   @abstract Attempt to lock a recursive lock.
246  *   @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.
247  *   @param lock Pointer to the allocated lock.
248  *   @result True if the lock is now locked by the caller, otherwise false. */
249 
250 boolean_t       IORecursiveLockTryLock( IORecursiveLock * lock);
251 
252 /*! @function IORecursiveLockUnlock
253  *   @abstract Unlock a recursive lock.
254  *  @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.
255  *   @param lock Pointer to the allocated lock. */
256 
257 void            IORecursiveLockUnlock( IORecursiveLock * lock);
258 
259 /*! @function IORecursiveLockHaveLock
260  *   @abstract Check if a recursive lock is held by the calling thread.
261  *   @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.
262  *   @param lock Pointer to the allocated lock.
263  *   @result True if the calling thread holds the lock otherwise false. */
264 
265 boolean_t       IORecursiveLockHaveLock( const IORecursiveLock * lock);
266 
267 extern int      IORecursiveLockSleep( IORecursiveLock *_lock,
268     void *event, UInt32 interType);
269 extern int      IORecursiveLockSleepDeadline( IORecursiveLock * _lock, void *event,
270     AbsoluteTime deadline, UInt32 interType);
271 extern void     IORecursiveLockWakeup( IORecursiveLock *_lock,
272     void *event, bool oneThread);
273 
274 /*
275  * Complex (read/write) lock operations
276  */
277 
278 #ifdef  IOLOCKS_INLINE
279 typedef lck_rw_t                IORWLock;
280 #else
281 typedef struct _IORWLock        IORWLock;
282 #endif  /* IOLOCKS_INLINE */
283 
284 /*! @function IORWLockAlloc
285  *   @abstract Allocates and initializes a read/write lock.
286  *   @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.
287  *   @result Pointer to the allocated lock, or zero on failure. */
288 
289 IORWLock * IORWLockAlloc( void );
290 
291 /*! @function IORWLockFree
292  *  @abstract Frees a read/write lock.
293  *  @discussion Frees a lock allocated with IORWLockAlloc. Lock should be unlocked with no waiters.
294  *   @param lock Pointer to the allocated lock. */
295 
296 void    IORWLockFree( IORWLock * lock);
297 
298 /*! @function IORWLockGetMachLock
299  *   @abstract Accessor to a Mach read/write lock.
300  *   @discussion Accessor to the Mach read/write lock.
301  *   @param lock Pointer to the allocated lock. */
302 
303 lck_rw_t * IORWLockGetMachLock( IORWLock * lock);
304 
305 /*! @function IORWLockRead
306  *   @abstract Lock a read/write lock for read.
307  *  @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.
308  *   @param lock Pointer to the allocated lock. */
309 
310 #ifdef  IOLOCKS_INLINE
311 #define IORWLockRead(l) lck_rw_lock_shared(l)
312 #else
313 void    IORWLockRead(IORWLock * lock);
314 #endif  /* !IOLOCKS_INLINE */
315 
316 /*! @function IORWLockTryRead
317  *   @abstract Attempt to lock a read/write lock for read.
318  *  @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.
319  *   @param lock Pointer to the allocated lock. */
320 
321 #ifdef  IOLOCKS_INLINE
322 #define IORWLockTryRead(l)        lck_rw_try_lock_shared(l)
323 #else
324 void    IORWLockTryRead( IORWLock * lock);
325 #endif  /* !IOLOCKS_INLINE */
326 
327 /*! @function IORWLockWrite
328  *   @abstract Lock a read/write lock for write.
329  *   @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.
330  *   @param lock Pointer to the allocated lock. */
331 
332 #ifdef  IOLOCKS_INLINE
333 #define IORWLockWrite(l)        lck_rw_lock_exclusive(l)
334 #else
335 void    IORWLockWrite( IORWLock * lock);
336 #endif  /* !IOLOCKS_INLINE */
337 
338 /*! @function IORWLockTryWrite
339  *   @abstract Attempt to lock a read/write lock for write.
340  *   @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.
341  *   @param lock Pointer to the allocated lock. */
342 
343 #ifdef  IOLOCKS_INLINE
344 #define IORWLockTryWrite(l)        lck_rw_try_lock_exclusive(l)
345 #else
346 void    IORWLockTryWrite( IORWLock * lock);
347 #endif  /* !IOLOCKS_INLINE */
348 
349 /*! @function IORWLockUnlock
350  *   @abstract Unlock a read/write lock.
351  *   @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.
352  *   @param lock Pointer to the allocated lock. */
353 
354 #ifdef  IOLOCKS_INLINE
355 #define IORWLockUnlock(l)       lck_rw_done(l)
356 #else
357 void    IORWLockUnlock( IORWLock * lock);
358 #endif  /* !IOLOCKS_INLINE */
359 
360 #ifdef XNU_KERNEL_PRIVATE
361 /*! @enum     IORWLockAssertState
362  *  @abstract Used with IORWLockAssert to assert the state of a lock.
363  */
364 typedef enum {
365 	kIORWLockAssertRead    = LCK_RW_ASSERT_SHARED,
366 	kIORWLockAssertWrite   = LCK_RW_ASSERT_EXCLUSIVE,
367 	kIORWLockAssertHeld    = LCK_RW_ASSERT_HELD,
368 	kIORWLockAssertNotHeld = LCK_RW_ASSERT_NOTHELD
369 } IORWLockAssertState;
370 
371 #ifdef IOLOCKS_INLINE
372 #define IORWLockAssert(l, type) LCK_RW_ASSERT(l, type)
373 
374 /*! @function   IORWLockInlineInit()
375  * @abstract    Initializes an inline lock.
376  */
377 void    IORWLockInlineInit(IORWLock *);
378 
379 /*! @function   IORWLockInlineDestroy()
380  * @abstract    Destroys an inline lock.
381  */
382 void    IORWLockInlineDestroy(IORWLock *);
383 
384 #else
385 /*! @function   IORWLockAssert
386  *  @abstract   Assert that a reader-writer lock is either held or not held
387  *  by the current thread.
388  *  @discussion Call with a value defined by the IORWLockAssertState type.
389  *  If the specified lock is not in the state specified by the type argument,
390  *  then the kernel will panic.
391  */
392 void    IORWLockAssert(IORWLock * lock, IORWLockAssertState type);
393 #endif /* !IOLOCKS_INLINE */
394 #endif /* !XNU_KERNEL_PRIVATE */
395 
396 #ifdef __APPLE_API_OBSOLETE
397 
398 /* The following API is deprecated */
399 
400 static __inline__ void
IOReadLock(IORWLock * lock)401 IOReadLock( IORWLock * lock)
402 {
403 	IORWLockRead(lock);
404 }
405 static __inline__ void
IOWriteLock(IORWLock * lock)406 IOWriteLock(  IORWLock * lock)
407 {
408 	IORWLockWrite(lock);
409 }
410 static __inline__ void
IORWUnlock(IORWLock * lock)411 IORWUnlock(   IORWLock * lock)
412 {
413 	IORWLockUnlock(lock);
414 }
415 
416 #endif /* __APPLE_API_OBSOLETE */
417 
418 
419 /*
420  * Simple locks. Cannot block while holding a simple lock.
421  */
422 
423 #ifdef  IOLOCKS_INLINE
424 typedef lck_spin_t              IOSimpleLock;
425 #else
426 typedef struct _IOSimpleLock    IOSimpleLock;
427 #endif  /* IOLOCKS_INLINE */
428 
429 /*! @function IOSimpleLockAlloc
430  *   @abstract Allocates and initializes a spin lock.
431  *   @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.
432  *   @result Pointer to the allocated lock, or zero on failure. */
433 
434 IOSimpleLock * IOSimpleLockAlloc( void );
435 
436 /*! @function IOSimpleLockFree
437  *   @abstract Frees a spin lock.
438  *   @discussion Frees a lock allocated with IOSimpleLockAlloc.
439  *   @param lock Pointer to the lock. */
440 
441 void IOSimpleLockFree( IOSimpleLock * lock );
442 
443 /*! @function IOSimpleLockGetMachLock
444  *   @abstract Accessor to a Mach spin lock.
445  *   @discussion Accessor to the Mach spin lock.
446  *   @param lock Pointer to the allocated lock. */
447 
448 lck_spin_t * IOSimpleLockGetMachLock( IOSimpleLock * lock);
449 
450 /*! @function IOSimpleLockInit
451  *   @abstract Initialize a spin lock.
452  *   @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.
453  *   @param lock Pointer to the lock. */
454 
455 void IOSimpleLockInit( IOSimpleLock * lock );
456 
457 /*! @function IOSimpleLockDestroy
458  *   @abstract De-initializes (destroys) a spin lock initialized with IOSimpleLockInit
459  *   @discussion Destroy / De-initialize a non heap allocated spin lock, releasing any system resources such as lock group refcounts.
460  *   @param lock Pointer to the lock. */
461 
462 void IOSimpleLockDestroy( IOSimpleLock * lock );
463 
464 /*! @function IOSimpleLockLock
465  *   @abstract Lock a spin lock.
466  *  @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.
467  *   @param lock Pointer to the lock. */
468 
469 #ifdef  IOLOCKS_INLINE
470 #define IOSimpleLockLock(l)     lck_spin_lock(l)
471 #else
472 void IOSimpleLockLock( IOSimpleLock * lock );
473 #endif  /* !IOLOCKS_INLINE */
474 
475 
476 /*! @function IOSimpleLockTryLock
477  *   @abstract Attempt to lock a spin lock.
478  *  @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.
479  *   @param lock Pointer to the lock.
480  *   @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
481 
482 #ifdef  IOLOCKS_INLINE
483 #define IOSimpleLockTryLock(l)  lck_spin_try_lock(l)
484 #else
485 boolean_t IOSimpleLockTryLock( IOSimpleLock * lock );
486 #endif  /* !IOLOCKS_INLINE */
487 
488 /*! @function IOSimpleLockUnlock
489  *   @abstract Unlock a spin lock.
490  *   @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
491  *   @param lock Pointer to the lock. */
492 
493 #ifdef  IOLOCKS_INLINE
494 #define IOSimpleLockUnlock(l)   lck_spin_unlock(l)
495 #else
496 void IOSimpleLockUnlock( IOSimpleLock * lock );
497 #endif  /* !IOLOCKS_INLINE */
498 
499 #ifdef XNU_KERNEL_PRIVATE
500 /*! @enum     IOSimpleLockAssertState
501  *  @abstract Used with IOSimpleLockAssert to assert the state of a lock.
502  */
503 typedef enum {
504 	kIOSimpleLockAssertOwned    = LCK_ASSERT_OWNED,
505 	kIOSimpleLockAssertNotOwned = LCK_ASSERT_NOTOWNED
506 } IOSimpleLockAssertState;
507 
508 #ifdef IOLOCKS_INLINE
509 #define IOSimpleLockAssert(l, type) LCK_SPIN_ASSERT(l, type)
510 #else
511 /*! @function   IOSimpleLockAssert
512  *  @abstract   Assert that spinlock is either held or not held by current thread.
513  *  @discussion Call with either kIOSimpleLockAssertOwned or kIOSimpleLockAssertNotOwned.
514  *  Panics the kernel if the lock is not owned if called with
515  *  kIOSimpleLockAssertOwned and vice-versa.
516  */
517 void    IOSimpleLockAssert(IOSimpleLock *lock, IOSimpleLockAssertState type);
518 #endif /* !IOLOCKS_INLINE */
519 #endif /* !XNU_KERNEL_PRIVATE */
520 
521 #if __LP64__
522 typedef boolean_t IOInterruptState;
523 #else
524 typedef long int IOInterruptState;
525 #endif
526 
527 /*! @function IOSimpleLockLockDisableInterrupt
528  *   @abstract Lock a spin lock.
529  *   @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.
530  *   @param lock Pointer to the lock. */
531 
532 static __inline__
533 IOInterruptState
IOSimpleLockLockDisableInterrupt(IOSimpleLock * lock)534 IOSimpleLockLockDisableInterrupt( IOSimpleLock * lock )
535 {
536 	IOInterruptState    state = ml_set_interrupts_enabled( false );
537 	IOSimpleLockLock( lock );
538 	return state;
539 }
540 
541 /*! @function IOSimpleLockUnlockEnableInterrupt
542  *   @abstract Unlock a spin lock, and restore interrupt state.
543  *   @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.
544  *   @param lock Pointer to the lock.
545  *   @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
546 
547 static __inline__
548 void
IOSimpleLockUnlockEnableInterrupt(IOSimpleLock * lock,IOInterruptState state)549 IOSimpleLockUnlockEnableInterrupt( IOSimpleLock * lock,
550     IOInterruptState state )
551 {
552 	IOSimpleLockUnlock( lock );
553 	ml_set_interrupts_enabled( state );
554 }
555 
556 #ifdef __cplusplus
557 } /* extern "C" */
558 #endif
559 
560 #endif /* !__IOKIT_IOLOCKS_H */
561