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