2 * Copyright (c) 1998-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 #ifndef __IOKIT_IOLOCKS_H
30 #define __IOKIT_IOLOCKS_H
33 #error IOLocks.h is for kernel use only
36 #include <sys/appleapiopts.h>
38 #include <IOKit/system.h>
40 #include <IOKit/IOReturn.h>
41 #include <IOKit/IOTypes.h>
47 #include <kern/lock.h>
48 #include <kern/simple_lock.h>
49 #include <kern/sched_prim.h>
50 #include <machine/machine_routines.h>
53 * Mutex lock operations
56 typedef mutex_t IOLock
;
58 /*! @function IOLockAlloc
59 @abstract Allocates and initializes an osfmk mutex.
60 @discussion Allocates an osfmk mutex in general purpose memory, and initilizes it. Mutexes are general purpose blocking mutual exclusion locks, supplied by osfmk/kern/lock.h. This function may block and so should not be called from interrupt level or while a simple lock is held.
61 @result Pointer to the allocated lock, or zero on failure. */
63 IOLock
* IOLockAlloc( void );
65 /*! @function IOLockFree
66 @abstract Frees an osfmk mutex.
67 @discussion Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be woken.
68 @param lock Pointer to the allocated lock. */
70 void IOLockFree( IOLock
* lock
);
72 /*! @function IOLockLock
73 @abstract Lock an osfmk mutex.
74 @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 simple lock is held. Locking the mutex recursively from one thread will result in deadlock.
75 @param lock Pointer to the allocated lock. */
78 void IOLockLock( IOLock
* lock
)
83 /*! @function IOLockTryLock
84 @abstract Attempt to lock an osfmk mutex.
85 @discussion Lock the mutex if it is currently unlocked, and return true. If the lock is held by any thread, return false.
86 @param lock Pointer to the allocated lock.
87 @result True if the mutex was unlocked and is now locked by the caller, otherwise false. */
90 boolean_t
IOLockTryLock( IOLock
* lock
)
92 return(mutex_try(lock
));
95 /*! @function IOLockUnlock
96 @abstract Unlock an osfmk mutex.
97 @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 simple lock is held.
98 @param lock Pointer to the allocated lock. */
101 void IOLockUnlock( IOLock
* lock
)
106 /*! @function IOLockSleep
107 @abstract Sleep with mutex unlock and relock
108 @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 simple lock is held.
109 @param lock Pointer to the locked lock.
110 @param event The event to sleep on.
111 @param interType How can the sleep be interrupted.
112 @result The wait-result value indicating how the thread was awakened.*/
114 int IOLockSleep( IOLock
* lock
, void *event
, UInt32 interType
)
116 return thread_sleep_mutex((event_t
) event
, lock
, (int) interType
);
120 int IOLockSleepDeadline( IOLock
* lock
, void *event
,
121 AbsoluteTime deadline
, UInt32 interType
)
123 return thread_sleep_mutex_deadline((event_t
) event
, lock
,
124 __OSAbsoluteTime(deadline
), (int) interType
);
128 void IOLockWakeup(IOLock
* lock
, void *event
, bool oneThread
)
130 thread_wakeup_prim((event_t
) event
, oneThread
, THREAD_AWAKENED
);
133 #ifdef __APPLE_API_OBSOLETE
135 /* The following API is deprecated */
138 kIOLockStateUnlocked
= 0,
139 kIOLockStateLocked
= 1,
142 void IOLockInitWithState( IOLock
* lock
, IOLockState state
);
143 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
145 static __inline__
void IOTakeLock( IOLock
* lock
) { IOLockLock(lock
); }
146 static __inline__ boolean_t
IOTryLock( IOLock
* lock
) { return(IOLockTryLock(lock
)); }
147 static __inline__
void IOUnlock( IOLock
* lock
) { IOLockUnlock(lock
); }
149 #endif /* __APPLE_API_OBSOLETE */
152 * Recursive lock operations
155 typedef struct _IORecursiveLock IORecursiveLock
;
157 /*! @function IORecursiveLockAlloc
158 @abstract Allocates and initializes an recursive lock.
159 @discussion Allocates a recursive lock in general purpose memory, and initilizes it. Recursive locks function identically to osfmk mutexes but allow one thread to lock more than once, with balanced unlocks.
160 @result Pointer to the allocated lock, or zero on failure. */
162 IORecursiveLock
* IORecursiveLockAlloc( void );
164 /*! @function IORecursiveLockFree
165 @abstract Frees a recursive lock.
166 @discussion Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be woken.
167 @param lock Pointer to the allocated lock. */
169 void IORecursiveLockFree( IORecursiveLock
* lock
);
171 /*! @function IORecursiveLockLock
172 @abstract Lock a recursive lock.
173 @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 simple lock is held. The lock may be taken recursively by the same thread, with a balanced number of calls to IORecursiveLockUnlock.
174 @param lock Pointer to the allocated lock. */
176 void IORecursiveLockLock( IORecursiveLock
* lock
);
178 /*! @function IORecursiveLockTryLock
179 @abstract Attempt to lock a recursive lock.
180 @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.
181 @param lock Pointer to the allocated lock.
182 @result True if the lock is now locked by the caller, otherwise false. */
184 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* lock
);
186 /*! @function IORecursiveLockUnlock
187 @abstract Unlock a recursive lock.
188 @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 simple lock is held.
189 @param lock Pointer to the allocated lock. */
191 void IORecursiveLockUnlock( IORecursiveLock
* lock
);
193 /*! @function IORecursiveLockHaveLock
194 @abstract Check if a recursive lock is held by the calling thread.
195 @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.
196 @param lock Pointer to the allocated lock.
197 @result True if the calling thread holds the lock otherwise false. */
199 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* lock
);
201 extern int IORecursiveLockSleep( IORecursiveLock
*_lock
,
202 void *event
, UInt32 interType
);
203 extern void IORecursiveLockWakeup( IORecursiveLock
*_lock
,
204 void *event
, bool oneThread
);
207 * Complex (read/write) lock operations
210 typedef lock_t IORWLock
;
212 /*! @function IORWLockAlloc
213 @abstract Allocates and initializes an osfmk general (read/write) lock.
214 @discussion Allocates an initializes an osfmk lock_t in general purpose memory, and initilizes it. Read/write locks provide for multiple readers, one exclusive writer, and are supplied by osfmk/kern/lock.h. This function may block and so should not be called from interrupt level or while a simple lock is held.
215 @result Pointer to the allocated lock, or zero on failure. */
217 IORWLock
* IORWLockAlloc( void );
219 /*! @function IORWLockFree
220 @abstract Frees an osfmk general (read/write) lock.
221 @discussion Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be woken.
222 @param lock Pointer to the allocated lock. */
224 void IORWLockFree( IORWLock
* lock
);
226 /*! @function IORWLockRead
227 @abstract Lock an osfmk lock for read.
228 @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 simple lock is held. Locking the lock recursively from one thread, for read or write, can result in deadlock.
229 @param lock Pointer to the allocated lock. */
232 void IORWLockRead( IORWLock
* lock
)
237 /*! @function IORWLockWrite
238 @abstract Lock an osfmk lock for write.
239 @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 simple lock is held. Locking the lock recursively from one thread, for read or write, can result in deadlock.
240 @param lock Pointer to the allocated lock. */
243 void IORWLockWrite( IORWLock
* lock
)
248 /*! @function IORWLockUnlock
249 @abstract Unlock an osfmk lock.
250 @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 simple lock is held.
251 @param lock Pointer to the allocated lock. */
254 void IORWLockUnlock( IORWLock
* lock
)
259 #ifdef __APPLE_API_OBSOLETE
261 /* The following API is deprecated */
263 static __inline__
void IOReadLock( IORWLock
* lock
) { IORWLockRead(lock
); }
264 static __inline__
void IOWriteLock( IORWLock
* lock
) { IORWLockWrite(lock
); }
265 static __inline__
void IORWUnlock( IORWLock
* lock
) { IORWLockUnlock(lock
); }
267 #endif /* __APPLE_API_OBSOLETE */
271 * Simple locks. Cannot block while holding a simple lock.
274 typedef simple_lock_data_t IOSimpleLock
;
276 /*! @function IOSimpleLockAlloc
277 @abstract Allocates and initializes an osfmk simple (spin) lock.
278 @discussion Allocates an initializes an osfmk simple lock in general purpose memory, and initilizes it. Simple locks provide non-blocking mutual exclusion for synchronization between thread context and interrupt context, or for multiprocessor synchronization, and are supplied by osfmk/kern/simple_lock.h. This function may block and so should not be called from interrupt level or while a simple lock is held.
279 @result Pointer to the allocated lock, or zero on failure. */
281 IOSimpleLock
* IOSimpleLockAlloc( void );
283 /*! @function IOSimpleLockFree
284 @abstract Frees an osfmk simple (spin) lock.
285 @discussion Frees a lock allocated with IOSimpleLockAlloc.
286 @param lock Pointer to the lock. */
288 void IOSimpleLockFree( IOSimpleLock
* lock
);
290 /*! @function IOSimpleLockInit
291 @abstract Initialize an osfmk simple (spin) lock.
292 @discussion Initialize an embedded osfmk simple lock, to the unlocked state.
293 @param lock Pointer to the lock. */
295 void IOSimpleLockInit( IOSimpleLock
* lock
);
297 /*! @function IOSimpleLockLock
298 @abstract Lock an osfmk simple lock.
299 @discussion Lock the simple 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.
300 @param lock Pointer to the lock. */
303 void IOSimpleLockLock( IOSimpleLock
* lock
)
305 usimple_lock( lock
);
308 /*! @function IOSimpleLockTryLock
309 @abstract Attempt to lock an osfmk simple lock.
310 @discussion Lock the simple 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.
311 @param lock Pointer to the lock.
312 @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
315 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
)
317 return( usimple_lock_try( lock
) );
320 /*! @function IOSimpleLockUnlock
321 @abstract Unlock an osfmk simple lock.
322 @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
323 @param lock Pointer to the lock. */
326 void IOSimpleLockUnlock( IOSimpleLock
* lock
)
328 usimple_unlock( lock
);
331 typedef long int IOInterruptState
;
333 /*! @function IOSimpleLockLockDisableInterrupt
334 @abstract Lock an osfmk simple lock.
335 @discussion Lock the simple 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.
336 @param lock Pointer to the lock. */
339 IOInterruptState
IOSimpleLockLockDisableInterrupt( IOSimpleLock
* lock
)
341 IOInterruptState state
= ml_set_interrupts_enabled( false );
342 usimple_lock( lock
);
346 /*! @function IOSimpleLockUnlockEnableInterrupt
347 @abstract Unlock an osfmk simple lock, and restore interrupt state.
348 @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.
349 @param lock Pointer to the lock.
350 @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
353 void IOSimpleLockUnlockEnableInterrupt( IOSimpleLock
* lock
,
354 IOInterruptState state
)
356 usimple_unlock( lock
);
357 ml_set_interrupts_enabled( state
);
364 #endif /* !__IOKIT_IOLOCKS_H */