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