2 * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #ifndef __IOKIT_IOLOCKS_H
33 #define __IOKIT_IOLOCKS_H
36 #error IOLocks.h is for kernel use only
39 #include <sys/appleapiopts.h>
41 #include <IOKit/system.h>
43 #include <IOKit/IOReturn.h>
44 #include <IOKit/IOTypes.h>
50 #include <libkern/locks.h>
51 #include <machine/machine_routines.h>
53 extern lck_grp_t
*IOLockGroup
;
56 * Mutex lock operations
59 #ifdef XNU_KERNEL_PRIVATE
60 typedef lck_mtx_t IOLock
;
62 typedef struct _IOLock IOLock
;
63 #endif /* XNU_KERNEL_PRIVATE */
66 /*! @function IOLockAlloc
67 @abstract Allocates and initializes a mutex.
68 @discussion Allocates a mutex in general purpose memory, and initilizes 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.
69 @result Pointer to the allocated lock, or zero on failure. */
71 IOLock
* IOLockAlloc( void );
73 /*! @function IOLockFree
74 @abstract Frees a mutex.
75 @discussion Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be woken.
76 @param lock Pointer to the allocated lock. */
78 void IOLockFree( IOLock
* lock
);
80 /*! @function IOLockGetMachLock
81 @abstract Accessor to a Mach mutex.
82 @discussion Accessor to the Mach mutex.
83 @param lock Pointer to the allocated lock. */
85 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
);
87 /*! @function IOLockLock
88 @abstract Lock a mutex.
89 @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.
90 @param lock Pointer to the allocated lock. */
92 #ifdef XNU_KERNEL_PRIVATE
95 void IOLockLock( IOLock
* lock
)
100 void IOLockLock( IOLock
* lock
);
101 #endif /* !IOLOCKS_CPP */
103 void IOLockLock( IOLock
* lock
);
104 #endif /* XNU_KERNEL_PRIVATE */
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. */
112 #ifdef XNU_KERNEL_PRIVATE
115 boolean_t
IOLockTryLock( IOLock
* lock
)
117 return(lck_mtx_try_lock(lock
));
120 boolean_t
IOLockTryLock( IOLock
* lock
);
121 #endif /* !IOLOCKS_CPP */
123 boolean_t
IOLockTryLock( IOLock
* lock
);
124 #endif /* XNU_KERNEL_PRIVATE */
126 /*! @function IOLockUnlock
127 @abstract Unlock a mutex.
128 @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.
129 @param lock Pointer to the allocated lock. */
131 #ifdef XNU_KERNEL_PRIVATE
134 void IOLockUnlock( IOLock
* lock
)
136 lck_mtx_unlock(lock
);
139 void IOLockUnlock( IOLock
* lock
);
140 #endif /* !IOLOCKS_CPP */
142 void IOLockUnlock( IOLock
* lock
);
143 #endif /* XNU_KERNEL_PRIVATE */
145 /*! @function IOLockSleep
146 @abstract Sleep with mutex unlock and relock
147 @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.
148 @param lock Pointer to the locked lock.
149 @param event The event to sleep on.
150 @param interType How can the sleep be interrupted.
151 @result The wait-result value indicating how the thread was awakened.*/
152 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
);
154 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
155 AbsoluteTime deadline
, UInt32 interType
);
157 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
);
159 #ifdef __APPLE_API_OBSOLETE
161 /* The following API is deprecated */
164 kIOLockStateUnlocked
= 0,
165 kIOLockStateLocked
= 1
168 void IOLockInitWithState( IOLock
* lock
, IOLockState state
);
169 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
171 static __inline__
void IOTakeLock( IOLock
* lock
) { IOLockLock(lock
); }
172 static __inline__ boolean_t
IOTryLock( IOLock
* lock
) { return(IOLockTryLock(lock
)); }
173 static __inline__
void IOUnlock( IOLock
* lock
) { IOLockUnlock(lock
); }
175 #endif /* __APPLE_API_OBSOLETE */
178 * Recursive lock operations
181 typedef struct _IORecursiveLock IORecursiveLock
;
183 /*! @function IORecursiveLockAlloc
184 @abstract Allocates and initializes an recursive lock.
185 @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.
186 @result Pointer to the allocated lock, or zero on failure. */
188 IORecursiveLock
* IORecursiveLockAlloc( void );
190 /*! @function IORecursiveLockFree
191 @abstract Frees a recursive lock.
192 @discussion Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be woken.
193 @param lock Pointer to the allocated lock. */
195 void IORecursiveLockFree( IORecursiveLock
* lock
);
197 /*! @function IORecursiveLockGetMachLock
198 @abstract Accessor to a Mach mutex.
199 @discussion Accessor to the Mach mutex.
200 @param lock Pointer to the allocated lock. */
202 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
);
204 /*! @function IORecursiveLockLock
205 @abstract Lock a recursive lock.
206 @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.
207 @param lock Pointer to the allocated lock. */
209 void IORecursiveLockLock( IORecursiveLock
* lock
);
211 /*! @function IORecursiveLockTryLock
212 @abstract Attempt to lock a recursive lock.
213 @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.
214 @param lock Pointer to the allocated lock.
215 @result True if the lock is now locked by the caller, otherwise false. */
217 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* lock
);
219 /*! @function IORecursiveLockUnlock
220 @abstract Unlock a recursive lock.
221 @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.
222 @param lock Pointer to the allocated lock. */
224 void IORecursiveLockUnlock( IORecursiveLock
* lock
);
226 /*! @function IORecursiveLockHaveLock
227 @abstract Check if a recursive lock is held by the calling thread.
228 @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.
229 @param lock Pointer to the allocated lock.
230 @result True if the calling thread holds the lock otherwise false. */
232 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* lock
);
234 extern int IORecursiveLockSleep( IORecursiveLock
*_lock
,
235 void *event
, UInt32 interType
);
236 extern void IORecursiveLockWakeup( IORecursiveLock
*_lock
,
237 void *event
, bool oneThread
);
240 * Complex (read/write) lock operations
243 #ifdef XNU_KERNEL_PRIVATE
244 typedef lck_rw_t IORWLock
;
246 typedef struct _IORWLock IORWLock
;
247 #endif /* XNU_KERNEL_PRIVATE */
249 /*! @function IORWLockAlloc
250 @abstract Allocates and initializes a read/write lock.
251 @discussion Allocates and initializes a read/write lock in general purpose memory, and initilizes it. 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.
252 @result Pointer to the allocated lock, or zero on failure. */
254 IORWLock
* IORWLockAlloc( void );
256 /*! @function IORWLockFree
257 @abstract Frees a read/write lock.
258 @discussion Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be woken.
259 @param lock Pointer to the allocated lock. */
261 void IORWLockFree( IORWLock
* lock
);
263 /*! @function IORWLockGetMachLock
264 @abstract Accessor to a Mach read/write lock.
265 @discussion Accessor to the Mach read/write lock.
266 @param lock Pointer to the allocated lock. */
268 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
);
270 /*! @function IORWLockRead
271 @abstract Lock a read/write lock for read.
272 @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.
273 @param lock Pointer to the allocated lock. */
275 #ifdef XNU_KERNEL_PRIVATE
278 void IORWLockRead( IORWLock
* lock
)
280 lck_rw_lock_shared( lock
);
283 void IORWLockRead( IORWLock
* lock
);
284 #endif /* !IOLOCKS_CPP */
286 void IORWLockRead( IORWLock
* lock
);
287 #endif /* XNU_KERNEL_PRIVATE */
289 /*! @function IORWLockWrite
290 @abstract Lock a read/write lock for write.
291 @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.
292 @param lock Pointer to the allocated lock. */
294 #ifdef XNU_KERNEL_PRIVATE
297 void IORWLockWrite( IORWLock
* lock
)
299 lck_rw_lock_exclusive( lock
);
302 void IORWLockWrite( IORWLock
* lock
);
303 #endif /* !IOLOCKS_CPP */
305 void IORWLockWrite( IORWLock
* lock
);
306 #endif /* XNU_KERNEL_PRIVATE */
308 /*! @function IORWLockUnlock
309 @abstract Unlock a read/write lock.
310 @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.
311 @param lock Pointer to the allocated lock. */
313 #ifdef XNU_KERNEL_PRIVATE
316 void IORWLockUnlock( IORWLock
* lock
)
321 void IORWLockUnlock( IORWLock
* lock
);
322 #endif /* !IOLOCKS_CPP */
324 void IORWLockUnlock( IORWLock
* lock
);
325 #endif /* XNU_KERNEL_PRIVATE */
327 #ifdef __APPLE_API_OBSOLETE
329 /* The following API is deprecated */
331 static __inline__
void IOReadLock( IORWLock
* lock
) { IORWLockRead(lock
); }
332 static __inline__
void IOWriteLock( IORWLock
* lock
) { IORWLockWrite(lock
); }
333 static __inline__
void IORWUnlock( IORWLock
* lock
) { IORWLockUnlock(lock
); }
335 #endif /* __APPLE_API_OBSOLETE */
339 * Simple locks. Cannot block while holding a simple lock.
342 #ifdef KERNEL_PRIVATE
343 typedef lck_spin_t IOSimpleLock
;
345 typedef struct _IOSimpleLock IOSimpleLock
;
346 #endif /* XNU_KERNEL_PRIVATE */
348 /*! @function IOSimpleLockAlloc
349 @abstract Allocates and initializes a spin lock.
350 @discussion Allocates an initializes a spin lock in general purpose memory, and initilizes it. 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.
351 @result Pointer to the allocated lock, or zero on failure. */
353 IOSimpleLock
* IOSimpleLockAlloc( void );
355 /*! @function IOSimpleLockFree
356 @abstract Frees a spin lock.
357 @discussion Frees a lock allocated with IOSimpleLockAlloc.
358 @param lock Pointer to the lock. */
360 void IOSimpleLockFree( IOSimpleLock
* lock
);
362 /*! @function IOSimpleLockGetMachLock
363 @abstract Accessor to a Mach spin lock.
364 @discussion Accessor to the Mach spin lock.
365 @param lock Pointer to the allocated lock. */
367 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
);
369 /*! @function IOSimpleLockInit
370 @abstract Initialize a spin lock.
371 @discussion Initialize an embedded spin lock, to the unlocked state.
372 @param lock Pointer to the lock. */
374 void IOSimpleLockInit( IOSimpleLock
* lock
);
376 /*! @function IOSimpleLockLock
377 @abstract Lock a spin lock.
378 @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.
379 @param lock Pointer to the lock. */
381 #ifdef XNU_KERNEL_PRIVATE
384 void IOSimpleLockLock( IOSimpleLock
* lock
)
386 lck_spin_lock( lock
);
389 void IOSimpleLockLock( IOSimpleLock
* lock
);
390 #endif /* !IOLOCKS_CPP */
392 void IOSimpleLockLock( IOSimpleLock
* lock
);
393 #endif /* XNU_KERNEL_PRIVATE */
395 /*! @function IOSimpleLockTryLock
396 @abstract Attempt to lock a spin lock.
397 @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.
398 @param lock Pointer to the lock.
399 @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
401 #ifdef XNU_KERNEL_PRIVATE
404 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
)
406 return( lck_spin_try_lock( lock
) );
409 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
);
410 #endif /* !IOLOCKS_CPP */
412 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
);
413 #endif /* XNU_KERNEL_PRIVATE */
415 /*! @function IOSimpleLockUnlock
416 @abstract Unlock a spin lock.
417 @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
418 @param lock Pointer to the lock. */
420 #ifdef XNU_KERNEL_PRIVATE
423 void IOSimpleLockUnlock( IOSimpleLock
* lock
)
425 lck_spin_unlock( lock
);
428 void IOSimpleLockUnlock( IOSimpleLock
* lock
);
429 #endif /* !IOLOCKS_CPP */
431 void IOSimpleLockUnlock( IOSimpleLock
* lock
);
432 #endif /* XNU_KERNEL_PRIVATE */
434 typedef long int IOInterruptState
;
436 /*! @function IOSimpleLockLockDisableInterrupt
437 @abstract Lock a spin lock.
438 @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.
439 @param lock Pointer to the lock. */
442 IOInterruptState
IOSimpleLockLockDisableInterrupt( IOSimpleLock
* lock
)
444 IOInterruptState state
= ml_set_interrupts_enabled( false );
445 IOSimpleLockLock( lock
);
449 /*! @function IOSimpleLockUnlockEnableInterrupt
450 @abstract Unlock a spin lock, and restore interrupt state.
451 @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.
452 @param lock Pointer to the lock.
453 @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
456 void IOSimpleLockUnlockEnableInterrupt( IOSimpleLock
* lock
,
457 IOInterruptState state
)
459 IOSimpleLockUnlock( lock
);
460 ml_set_interrupts_enabled( state
);
467 #endif /* !__IOKIT_IOLOCKS_H */