2 * Copyright (c) 1998-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 #ifndef __IOKIT_IOLOCKS_H
35 #define __IOKIT_IOLOCKS_H
38 #error IOLocks.h is for kernel use only
41 #include <sys/appleapiopts.h>
43 #include <IOKit/system.h>
45 #include <IOKit/IOReturn.h>
46 #include <IOKit/IOTypes.h>
52 #include <libkern/locks.h>
53 #include <machine/machine_routines.h>
55 extern lck_grp_t
*IOLockGroup
;
58 * Mutex lock operations
61 #ifdef XNU_KERNEL_PRIVATE
62 typedef lck_mtx_t IOLock
;
64 typedef struct _IOLock IOLock
;
65 #endif /* XNU_KERNEL_PRIVATE */
68 /*! @function IOLockAlloc
69 @abstract Allocates and initializes a mutex.
70 @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.
71 @result Pointer to the allocated lock, or zero on failure. */
73 IOLock
* IOLockAlloc( void );
75 /*! @function IOLockFree
76 @abstract Frees a mutex.
77 @discussion Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be woken.
78 @param lock Pointer to the allocated lock. */
80 void IOLockFree( IOLock
* lock
);
82 /*! @function IOLockGetMachLock
83 @abstract Accessor to a Mach mutex.
84 @discussion Accessor to the Mach mutex.
85 @param lock Pointer to the allocated lock. */
87 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
);
89 /*! @function IOLockLock
90 @abstract Lock a mutex.
91 @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.
92 @param lock Pointer to the allocated lock. */
94 #ifdef XNU_KERNEL_PRIVATE
97 void IOLockLock( IOLock
* lock
)
102 void IOLockLock( IOLock
* lock
);
103 #endif /* !IOLOCKS_CPP */
105 void IOLockLock( IOLock
* lock
);
106 #endif /* XNU_KERNEL_PRIVATE */
108 /*! @function IOLockTryLock
109 @abstract Attempt to lock a mutex.
110 @discussion Lock the mutex if it is currently unlocked, and return true. If the lock is held by any thread, return false.
111 @param lock Pointer to the allocated lock.
112 @result True if the mutex was unlocked and is now locked by the caller, otherwise false. */
114 #ifdef XNU_KERNEL_PRIVATE
117 boolean_t
IOLockTryLock( IOLock
* lock
)
119 return(lck_mtx_try_lock(lock
));
122 boolean_t
IOLockTryLock( IOLock
* lock
);
123 #endif /* !IOLOCKS_CPP */
125 boolean_t
IOLockTryLock( IOLock
* lock
);
126 #endif /* XNU_KERNEL_PRIVATE */
128 /*! @function IOLockUnlock
129 @abstract Unlock a mutex.
130 @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.
131 @param lock Pointer to the allocated lock. */
133 #ifdef XNU_KERNEL_PRIVATE
136 void IOLockUnlock( IOLock
* lock
)
138 lck_mtx_unlock(lock
);
141 void IOLockUnlock( IOLock
* lock
);
142 #endif /* !IOLOCKS_CPP */
144 void IOLockUnlock( IOLock
* lock
);
145 #endif /* XNU_KERNEL_PRIVATE */
147 /*! @function IOLockSleep
148 @abstract Sleep with mutex unlock and relock
149 @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.
150 @param lock Pointer to the locked lock.
151 @param event The event to sleep on.
152 @param interType How can the sleep be interrupted.
153 @result The wait-result value indicating how the thread was awakened.*/
154 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
);
156 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
157 AbsoluteTime deadline
, UInt32 interType
);
159 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
);
161 #ifdef __APPLE_API_OBSOLETE
163 /* The following API is deprecated */
166 kIOLockStateUnlocked
= 0,
167 kIOLockStateLocked
= 1
170 void IOLockInitWithState( IOLock
* lock
, IOLockState state
);
171 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
173 static __inline__
void IOTakeLock( IOLock
* lock
) { IOLockLock(lock
); }
174 static __inline__ boolean_t
IOTryLock( IOLock
* lock
) { return(IOLockTryLock(lock
)); }
175 static __inline__
void IOUnlock( IOLock
* lock
) { IOLockUnlock(lock
); }
177 #endif /* __APPLE_API_OBSOLETE */
180 * Recursive lock operations
183 typedef struct _IORecursiveLock IORecursiveLock
;
185 /*! @function IORecursiveLockAlloc
186 @abstract Allocates and initializes an recursive lock.
187 @discussion Allocates a recursive lock in general purpose memory, and initilizes it. Recursive locks function identically to mutexes but allow one thread to lock more than once, with balanced unlocks.
188 @result Pointer to the allocated lock, or zero on failure. */
190 IORecursiveLock
* IORecursiveLockAlloc( void );
192 /*! @function IORecursiveLockFree
193 @abstract Frees a recursive lock.
194 @discussion Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be woken.
195 @param lock Pointer to the allocated lock. */
197 void IORecursiveLockFree( IORecursiveLock
* lock
);
199 /*! @function IORecursiveLockGetMachLock
200 @abstract Accessor to a Mach mutex.
201 @discussion Accessor to the Mach mutex.
202 @param lock Pointer to the allocated lock. */
204 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
);
206 /*! @function IORecursiveLockLock
207 @abstract Lock a recursive lock.
208 @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.
209 @param lock Pointer to the allocated lock. */
211 void IORecursiveLockLock( IORecursiveLock
* lock
);
213 /*! @function IORecursiveLockTryLock
214 @abstract Attempt to lock a recursive lock.
215 @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.
216 @param lock Pointer to the allocated lock.
217 @result True if the lock is now locked by the caller, otherwise false. */
219 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* lock
);
221 /*! @function IORecursiveLockUnlock
222 @abstract Unlock a recursive lock.
223 @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.
224 @param lock Pointer to the allocated lock. */
226 void IORecursiveLockUnlock( IORecursiveLock
* lock
);
228 /*! @function IORecursiveLockHaveLock
229 @abstract Check if a recursive lock is held by the calling thread.
230 @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.
231 @param lock Pointer to the allocated lock.
232 @result True if the calling thread holds the lock otherwise false. */
234 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* lock
);
236 extern int IORecursiveLockSleep( IORecursiveLock
*_lock
,
237 void *event
, UInt32 interType
);
238 extern void IORecursiveLockWakeup( IORecursiveLock
*_lock
,
239 void *event
, bool oneThread
);
242 * Complex (read/write) lock operations
245 #ifdef XNU_KERNEL_PRIVATE
246 typedef lck_rw_t IORWLock
;
248 typedef struct _IORWLock IORWLock
;
249 #endif /* XNU_KERNEL_PRIVATE */
251 /*! @function IORWLockAlloc
252 @abstract Allocates and initializes a read/write lock.
253 @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.
254 @result Pointer to the allocated lock, or zero on failure. */
256 IORWLock
* IORWLockAlloc( void );
258 /*! @function IORWLockFree
259 @abstract Frees a read/write lock.
260 @discussion Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be woken.
261 @param lock Pointer to the allocated lock. */
263 void IORWLockFree( IORWLock
* lock
);
265 /*! @function IORWLockGetMachLock
266 @abstract Accessor to a Mach read/write lock.
267 @discussion Accessor to the Mach read/write lock.
268 @param lock Pointer to the allocated lock. */
270 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
);
272 /*! @function IORWLockRead
273 @abstract Lock a read/write lock for read.
274 @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.
275 @param lock Pointer to the allocated lock. */
277 #ifdef XNU_KERNEL_PRIVATE
280 void IORWLockRead( IORWLock
* lock
)
282 lck_rw_lock_shared( lock
);
285 void IORWLockRead( IORWLock
* lock
);
286 #endif /* !IOLOCKS_CPP */
288 void IORWLockRead( IORWLock
* lock
);
289 #endif /* XNU_KERNEL_PRIVATE */
291 /*! @function IORWLockWrite
292 @abstract Lock a read/write lock for write.
293 @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.
294 @param lock Pointer to the allocated lock. */
296 #ifdef XNU_KERNEL_PRIVATE
299 void IORWLockWrite( IORWLock
* lock
)
301 lck_rw_lock_exclusive( lock
);
304 void IORWLockWrite( IORWLock
* lock
);
305 #endif /* !IOLOCKS_CPP */
307 void IORWLockWrite( IORWLock
* lock
);
308 #endif /* XNU_KERNEL_PRIVATE */
310 /*! @function IORWLockUnlock
311 @abstract Unlock a read/write lock.
312 @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.
313 @param lock Pointer to the allocated lock. */
315 #ifdef XNU_KERNEL_PRIVATE
318 void IORWLockUnlock( IORWLock
* lock
)
323 void IORWLockUnlock( IORWLock
* lock
);
324 #endif /* !IOLOCKS_CPP */
326 void IORWLockUnlock( IORWLock
* lock
);
327 #endif /* XNU_KERNEL_PRIVATE */
329 #ifdef __APPLE_API_OBSOLETE
331 /* The following API is deprecated */
333 static __inline__
void IOReadLock( IORWLock
* lock
) { IORWLockRead(lock
); }
334 static __inline__
void IOWriteLock( IORWLock
* lock
) { IORWLockWrite(lock
); }
335 static __inline__
void IORWUnlock( IORWLock
* lock
) { IORWLockUnlock(lock
); }
337 #endif /* __APPLE_API_OBSOLETE */
341 * Simple locks. Cannot block while holding a simple lock.
344 #ifdef KERNEL_PRIVATE
345 typedef lck_spin_t IOSimpleLock
;
347 typedef struct _IOSimpleLock IOSimpleLock
;
348 #endif /* XNU_KERNEL_PRIVATE */
350 /*! @function IOSimpleLockAlloc
351 @abstract Allocates and initializes a spin lock.
352 @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.
353 @result Pointer to the allocated lock, or zero on failure. */
355 IOSimpleLock
* IOSimpleLockAlloc( void );
357 /*! @function IOSimpleLockFree
358 @abstract Frees a spin lock.
359 @discussion Frees a lock allocated with IOSimpleLockAlloc.
360 @param lock Pointer to the lock. */
362 void IOSimpleLockFree( IOSimpleLock
* lock
);
364 /*! @function IOSimpleLockGetMachLock
365 @abstract Accessor to a Mach spin lock.
366 @discussion Accessor to the Mach spin lock.
367 @param lock Pointer to the allocated lock. */
369 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
);
371 /*! @function IOSimpleLockInit
372 @abstract Initialize a spin lock.
373 @discussion Initialize an embedded spin lock, to the unlocked state.
374 @param lock Pointer to the lock. */
376 void IOSimpleLockInit( IOSimpleLock
* lock
);
378 /*! @function IOSimpleLockLock
379 @abstract Lock a spin lock.
380 @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.
381 @param lock Pointer to the lock. */
383 #ifdef XNU_KERNEL_PRIVATE
386 void IOSimpleLockLock( IOSimpleLock
* lock
)
388 lck_spin_lock( lock
);
391 void IOSimpleLockLock( IOSimpleLock
* lock
);
392 #endif /* !IOLOCKS_CPP */
394 void IOSimpleLockLock( IOSimpleLock
* lock
);
395 #endif /* XNU_KERNEL_PRIVATE */
397 /*! @function IOSimpleLockTryLock
398 @abstract Attempt to lock a spin lock.
399 @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.
400 @param lock Pointer to the lock.
401 @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
403 #ifdef XNU_KERNEL_PRIVATE
406 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
)
408 return( lck_spin_try_lock( lock
) );
411 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
);
412 #endif /* !IOLOCKS_CPP */
414 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
);
415 #endif /* XNU_KERNEL_PRIVATE */
417 /*! @function IOSimpleLockUnlock
418 @abstract Unlock a spin lock.
419 @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
420 @param lock Pointer to the lock. */
422 #ifdef XNU_KERNEL_PRIVATE
425 void IOSimpleLockUnlock( IOSimpleLock
* lock
)
427 lck_spin_unlock( lock
);
430 void IOSimpleLockUnlock( IOSimpleLock
* lock
);
431 #endif /* !IOLOCKS_CPP */
433 void IOSimpleLockUnlock( IOSimpleLock
* lock
);
434 #endif /* XNU_KERNEL_PRIVATE */
436 typedef long int IOInterruptState
;
438 /*! @function IOSimpleLockLockDisableInterrupt
439 @abstract Lock a spin lock.
440 @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.
441 @param lock Pointer to the lock. */
444 IOInterruptState
IOSimpleLockLockDisableInterrupt( IOSimpleLock
* lock
)
446 IOInterruptState state
= ml_set_interrupts_enabled( false );
447 IOSimpleLockLock( lock
);
451 /*! @function IOSimpleLockUnlockEnableInterrupt
452 @abstract Unlock a spin lock, and restore interrupt state.
453 @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.
454 @param lock Pointer to the lock.
455 @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
458 void IOSimpleLockUnlockEnableInterrupt( IOSimpleLock
* lock
,
459 IOInterruptState state
)
461 IOSimpleLockUnlock( lock
);
462 ml_set_interrupts_enabled( state
);
469 #endif /* !__IOKIT_IOLOCKS_H */