2 * Copyright (c) 1998-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 #ifndef __IOKIT_IOLOCKS_H
27 #define __IOKIT_IOLOCKS_H
30 #error IOLocks.h is for kernel use only
33 #include <sys/appleapiopts.h>
35 #include <IOKit/system.h>
37 #include <IOKit/IOReturn.h>
38 #include <IOKit/IOTypes.h>
44 #include <libkern/locks.h>
45 #include <machine/machine_routines.h>
47 extern lck_grp_t
*IOLockGroup
;
50 * Mutex lock operations
53 #ifdef XNU_KERNEL_PRIVATE
54 typedef lck_mtx_t IOLock
;
56 typedef struct _IOLock IOLock
;
57 #endif /* XNU_KERNEL_PRIVATE */
60 /*! @function IOLockAlloc
61 @abstract Allocates and initializes a mutex.
62 @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.
63 @result Pointer to the allocated lock, or zero on failure. */
65 IOLock
* IOLockAlloc( void );
67 /*! @function IOLockFree
68 @abstract Frees a mutex.
69 @discussion Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be woken.
70 @param lock Pointer to the allocated lock. */
72 void IOLockFree( IOLock
* lock
);
74 /*! @function IOLockGetMachLock
75 @abstract Accessor to a Mach mutex.
76 @discussion Accessor to the Mach mutex.
77 @param lock Pointer to the allocated lock. */
79 lck_mtx_t
* IOLockGetMachLock( IOLock
* lock
);
81 /*! @function IOLockLock
82 @abstract Lock a mutex.
83 @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.
84 @param lock Pointer to the allocated lock. */
86 #ifdef XNU_KERNEL_PRIVATE
89 void IOLockLock( IOLock
* lock
)
94 void IOLockLock( IOLock
* lock
);
95 #endif /* !IOLOCKS_CPP */
97 void IOLockLock( IOLock
* lock
);
98 #endif /* XNU_KERNEL_PRIVATE */
100 /*! @function IOLockTryLock
101 @abstract Attempt to lock a mutex.
102 @discussion Lock the mutex if it is currently unlocked, and return true. If the lock is held by any thread, return false.
103 @param lock Pointer to the allocated lock.
104 @result True if the mutex was unlocked and is now locked by the caller, otherwise false. */
106 #ifdef XNU_KERNEL_PRIVATE
109 boolean_t
IOLockTryLock( IOLock
* lock
)
111 return(lck_mtx_try_lock(lock
));
114 boolean_t
IOLockTryLock( IOLock
* lock
);
115 #endif /* !IOLOCKS_CPP */
117 boolean_t
IOLockTryLock( IOLock
* lock
);
118 #endif /* XNU_KERNEL_PRIVATE */
120 /*! @function IOLockUnlock
121 @abstract Unlock a mutex.
122 @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.
123 @param lock Pointer to the allocated lock. */
125 #ifdef XNU_KERNEL_PRIVATE
128 void IOLockUnlock( IOLock
* lock
)
130 lck_mtx_unlock(lock
);
133 void IOLockUnlock( IOLock
* lock
);
134 #endif /* !IOLOCKS_CPP */
136 void IOLockUnlock( IOLock
* lock
);
137 #endif /* XNU_KERNEL_PRIVATE */
139 /*! @function IOLockSleep
140 @abstract Sleep with mutex unlock and relock
141 @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.
142 @param lock Pointer to the locked lock.
143 @param event The event to sleep on.
144 @param interType How can the sleep be interrupted.
145 @result The wait-result value indicating how the thread was awakened.*/
146 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
);
148 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
149 AbsoluteTime deadline
, UInt32 interType
);
151 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
);
153 #ifdef __APPLE_API_OBSOLETE
155 /* The following API is deprecated */
158 kIOLockStateUnlocked
= 0,
159 kIOLockStateLocked
= 1
162 void IOLockInitWithState( IOLock
* lock
, IOLockState state
);
163 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
165 static __inline__
void IOTakeLock( IOLock
* lock
) { IOLockLock(lock
); }
166 static __inline__ boolean_t
IOTryLock( IOLock
* lock
) { return(IOLockTryLock(lock
)); }
167 static __inline__
void IOUnlock( IOLock
* lock
) { IOLockUnlock(lock
); }
169 #endif /* __APPLE_API_OBSOLETE */
172 * Recursive lock operations
175 typedef struct _IORecursiveLock IORecursiveLock
;
177 /*! @function IORecursiveLockAlloc
178 @abstract Allocates and initializes an recursive lock.
179 @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.
180 @result Pointer to the allocated lock, or zero on failure. */
182 IORecursiveLock
* IORecursiveLockAlloc( void );
184 /*! @function IORecursiveLockFree
185 @abstract Frees a recursive lock.
186 @discussion Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be woken.
187 @param lock Pointer to the allocated lock. */
189 void IORecursiveLockFree( IORecursiveLock
* lock
);
191 /*! @function IORecursiveLockGetMachLock
192 @abstract Accessor to a Mach mutex.
193 @discussion Accessor to the Mach mutex.
194 @param lock Pointer to the allocated lock. */
196 lck_mtx_t
* IORecursiveLockGetMachLock( IORecursiveLock
* lock
);
198 /*! @function IORecursiveLockLock
199 @abstract Lock a recursive lock.
200 @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.
201 @param lock Pointer to the allocated lock. */
203 void IORecursiveLockLock( IORecursiveLock
* lock
);
205 /*! @function IORecursiveLockTryLock
206 @abstract Attempt to lock a recursive lock.
207 @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.
208 @param lock Pointer to the allocated lock.
209 @result True if the lock is now locked by the caller, otherwise false. */
211 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* lock
);
213 /*! @function IORecursiveLockUnlock
214 @abstract Unlock a recursive lock.
215 @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.
216 @param lock Pointer to the allocated lock. */
218 void IORecursiveLockUnlock( IORecursiveLock
* lock
);
220 /*! @function IORecursiveLockHaveLock
221 @abstract Check if a recursive lock is held by the calling thread.
222 @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.
223 @param lock Pointer to the allocated lock.
224 @result True if the calling thread holds the lock otherwise false. */
226 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* lock
);
228 extern int IORecursiveLockSleep( IORecursiveLock
*_lock
,
229 void *event
, UInt32 interType
);
230 extern void IORecursiveLockWakeup( IORecursiveLock
*_lock
,
231 void *event
, bool oneThread
);
234 * Complex (read/write) lock operations
237 #ifdef XNU_KERNEL_PRIVATE
238 typedef lck_rw_t IORWLock
;
240 typedef struct _IORWLock IORWLock
;
241 #endif /* XNU_KERNEL_PRIVATE */
243 /*! @function IORWLockAlloc
244 @abstract Allocates and initializes a read/write lock.
245 @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.
246 @result Pointer to the allocated lock, or zero on failure. */
248 IORWLock
* IORWLockAlloc( void );
250 /*! @function IORWLockFree
251 @abstract Frees a read/write lock.
252 @discussion Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be woken.
253 @param lock Pointer to the allocated lock. */
255 void IORWLockFree( IORWLock
* lock
);
257 /*! @function IORWLockGetMachLock
258 @abstract Accessor to a Mach read/write lock.
259 @discussion Accessor to the Mach read/write lock.
260 @param lock Pointer to the allocated lock. */
262 lck_rw_t
* IORWLockGetMachLock( IORWLock
* lock
);
264 /*! @function IORWLockRead
265 @abstract Lock a read/write lock for read.
266 @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.
267 @param lock Pointer to the allocated lock. */
269 #ifdef XNU_KERNEL_PRIVATE
272 void IORWLockRead( IORWLock
* lock
)
274 lck_rw_lock_shared( lock
);
277 void IORWLockRead( IORWLock
* lock
);
278 #endif /* !IOLOCKS_CPP */
280 void IORWLockRead( IORWLock
* lock
);
281 #endif /* XNU_KERNEL_PRIVATE */
283 /*! @function IORWLockWrite
284 @abstract Lock a read/write lock for write.
285 @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.
286 @param lock Pointer to the allocated lock. */
288 #ifdef XNU_KERNEL_PRIVATE
291 void IORWLockWrite( IORWLock
* lock
)
293 lck_rw_lock_exclusive( lock
);
296 void IORWLockWrite( IORWLock
* lock
);
297 #endif /* !IOLOCKS_CPP */
299 void IORWLockWrite( IORWLock
* lock
);
300 #endif /* XNU_KERNEL_PRIVATE */
302 /*! @function IORWLockUnlock
303 @abstract Unlock a read/write lock.
304 @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.
305 @param lock Pointer to the allocated lock. */
307 #ifdef XNU_KERNEL_PRIVATE
310 void IORWLockUnlock( IORWLock
* lock
)
315 void IORWLockUnlock( IORWLock
* lock
);
316 #endif /* !IOLOCKS_CPP */
318 void IORWLockUnlock( IORWLock
* lock
);
319 #endif /* XNU_KERNEL_PRIVATE */
321 #ifdef __APPLE_API_OBSOLETE
323 /* The following API is deprecated */
325 static __inline__
void IOReadLock( IORWLock
* lock
) { IORWLockRead(lock
); }
326 static __inline__
void IOWriteLock( IORWLock
* lock
) { IORWLockWrite(lock
); }
327 static __inline__
void IORWUnlock( IORWLock
* lock
) { IORWLockUnlock(lock
); }
329 #endif /* __APPLE_API_OBSOLETE */
333 * Simple locks. Cannot block while holding a simple lock.
336 #ifdef KERNEL_PRIVATE
337 typedef lck_spin_t IOSimpleLock
;
339 typedef struct _IOSimpleLock IOSimpleLock
;
340 #endif /* XNU_KERNEL_PRIVATE */
342 /*! @function IOSimpleLockAlloc
343 @abstract Allocates and initializes a spin lock.
344 @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.
345 @result Pointer to the allocated lock, or zero on failure. */
347 IOSimpleLock
* IOSimpleLockAlloc( void );
349 /*! @function IOSimpleLockFree
350 @abstract Frees a spin lock.
351 @discussion Frees a lock allocated with IOSimpleLockAlloc.
352 @param lock Pointer to the lock. */
354 void IOSimpleLockFree( IOSimpleLock
* lock
);
356 /*! @function IOSimpleLockGetMachLock
357 @abstract Accessor to a Mach spin lock.
358 @discussion Accessor to the Mach spin lock.
359 @param lock Pointer to the allocated lock. */
361 lck_spin_t
* IOSimpleLockGetMachLock( IOSimpleLock
* lock
);
363 /*! @function IOSimpleLockInit
364 @abstract Initialize a spin lock.
365 @discussion Initialize an embedded spin lock, to the unlocked state.
366 @param lock Pointer to the lock. */
368 void IOSimpleLockInit( IOSimpleLock
* lock
);
370 /*! @function IOSimpleLockLock
371 @abstract Lock a spin lock.
372 @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.
373 @param lock Pointer to the lock. */
375 #ifdef XNU_KERNEL_PRIVATE
378 void IOSimpleLockLock( IOSimpleLock
* lock
)
380 lck_spin_lock( lock
);
383 void IOSimpleLockLock( IOSimpleLock
* lock
);
384 #endif /* !IOLOCKS_CPP */
386 void IOSimpleLockLock( IOSimpleLock
* lock
);
387 #endif /* XNU_KERNEL_PRIVATE */
389 /*! @function IOSimpleLockTryLock
390 @abstract Attempt to lock a spin lock.
391 @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.
392 @param lock Pointer to the lock.
393 @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
395 #ifdef XNU_KERNEL_PRIVATE
398 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
)
400 return( lck_spin_try_lock( lock
) );
403 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
);
404 #endif /* !IOLOCKS_CPP */
406 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
);
407 #endif /* XNU_KERNEL_PRIVATE */
409 /*! @function IOSimpleLockUnlock
410 @abstract Unlock a spin lock.
411 @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
412 @param lock Pointer to the lock. */
414 #ifdef XNU_KERNEL_PRIVATE
417 void IOSimpleLockUnlock( IOSimpleLock
* lock
)
419 lck_spin_unlock( lock
);
422 void IOSimpleLockUnlock( IOSimpleLock
* lock
);
423 #endif /* !IOLOCKS_CPP */
425 void IOSimpleLockUnlock( IOSimpleLock
* lock
);
426 #endif /* XNU_KERNEL_PRIVATE */
428 typedef long int IOInterruptState
;
430 /*! @function IOSimpleLockLockDisableInterrupt
431 @abstract Lock a spin lock.
432 @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.
433 @param lock Pointer to the lock. */
436 IOInterruptState
IOSimpleLockLockDisableInterrupt( IOSimpleLock
* lock
)
438 IOInterruptState state
= ml_set_interrupts_enabled( false );
439 IOSimpleLockLock( lock
);
443 /*! @function IOSimpleLockUnlockEnableInterrupt
444 @abstract Unlock a spin lock, and restore interrupt state.
445 @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.
446 @param lock Pointer to the lock.
447 @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
450 void IOSimpleLockUnlockEnableInterrupt( IOSimpleLock
* lock
,
451 IOInterruptState state
)
453 IOSimpleLockUnlock( lock
);
454 ml_set_interrupts_enabled( state
);
461 #endif /* !__IOKIT_IOLOCKS_H */