2 * Copyright (c) 1998-2000 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 #ifndef IOKIT_DEPRECATED
34 #define IOKIT_DEPRECATED 1
37 #include <IOKit/system.h>
39 #include <IOKit/IOReturn.h>
40 #include <IOKit/IOTypes.h>
46 #include <kern/lock.h>
47 #include <kern/simple_lock.h>
48 #include <machine/machine_routines.h>
51 * Mutex lock operations
54 typedef mutex_t IOLock
;
56 /*! @function IOLockAlloc
57 @abstract Allocates and initializes an osfmk mutex.
58 @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.
59 @result Pointer to the allocated lock, or zero on failure. */
61 IOLock
* IOLockAlloc( void );
63 /*! @function IOLockFree
64 @abstract Frees an osfmk mutex.
65 @discussion Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be woken.
66 @param lock Pointer to the allocated lock. */
68 void IOLockFree( IOLock
* lock
);
70 /*! @function IOLockLock
71 @abstract Lock an osfmk mutex.
72 @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.
73 @param lock Pointer to the allocated lock. */
76 void IOLockLock( IOLock
* lock
)
81 /*! @function IOLockTryLock
82 @abstract Attempt to lock an osfmk mutex.
83 @discussion Lock the mutex if it is currently unlocked, and return true. If the lock is held by any thread, return false.
84 @param lock Pointer to the allocated lock.
85 @result True if the mutex was unlocked and is now locked by the caller, otherwise false. */
88 boolean_t
IOLockTryLock( IOLock
* lock
)
90 return(_mutex_try(lock
));
93 /*! @function IOLockUnlock
94 @abstract Unlock an osfmk mutex.
95 @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.
96 @param lock Pointer to the allocated lock. */
99 void IOLockUnlock( IOLock
* lock
)
106 /* The following API is deprecated */
109 kIOLockStateUnlocked
= 0,
110 kIOLockStateLocked
= 1,
113 void IOLockInitWithState( IOLock
* lock
, IOLockState state
);
114 #define IOLockInit( l ) IOLockInitWithState( l, kIOLockStateUnlocked);
116 static __inline__
void IOTakeLock( IOLock
* lock
) { IOLockLock(lock
); }
117 static __inline__ boolean_t
IOTryLock( IOLock
* lock
) { return(IOLockTryLock(lock
)); }
118 static __inline__
void IOUnlock( IOLock
* lock
) { IOLockUnlock(lock
); }
120 #endif /* IOKIT_DEPRECATED */
123 * Recursive lock operations
126 typedef struct _IORecursiveLock IORecursiveLock
;
128 /*! @function IORecursiveLockAlloc
129 @abstract Allocates and initializes an recursive lock.
130 @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.
131 @result Pointer to the allocated lock, or zero on failure. */
133 IORecursiveLock
* IORecursiveLockAlloc( void );
135 /*! @function IORecursiveLockFree
136 @abstract Frees a recursive lock.
137 @discussion Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be woken.
138 @param lock Pointer to the allocated lock. */
140 void IORecursiveLockFree( IORecursiveLock
* lock
);
142 /*! @function IORecursiveLockLock
143 @abstract Lock a recursive lock.
144 @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.
145 @param lock Pointer to the allocated lock. */
147 void IORecursiveLockLock( IORecursiveLock
* lock
);
149 /*! @function IORecursiveLockTryLock
150 @abstract Attempt to lock a recursive lock.
151 @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.
152 @param lock Pointer to the allocated lock.
153 @result True if the lock is now locked by the caller, otherwise false. */
155 boolean_t
IORecursiveLockTryLock( IORecursiveLock
* lock
);
157 /*! @function IORecursiveLockUnlock
158 @abstract Unlock a recursive lock.
159 @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.
160 @param lock Pointer to the allocated lock. */
162 void IORecursiveLockUnlock( IORecursiveLock
* lock
);
164 /*! @function IORecursiveLockHaveLock
165 @abstract Check if a recursive lock is held by the calling thread.
166 @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.
167 @param lock Pointer to the allocated lock.
168 @result True if the calling thread holds the lock otherwise false. */
170 boolean_t
IORecursiveLockHaveLock( const IORecursiveLock
* lock
);
172 extern int IORecursiveLockSleep( IORecursiveLock
*_lock
,
173 void *event
, UInt32 interType
);
174 extern void IORecursiveLockWakeup( IORecursiveLock
*_lock
,
175 void *event
, bool oneThread
);
178 * Complex (read/write) lock operations
181 typedef lock_t IORWLock
;
183 /*! @function IORWLockAlloc
184 @abstract Allocates and initializes an osfmk general (read/write) lock.
185 @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.
186 @result Pointer to the allocated lock, or zero on failure. */
188 IORWLock
* IORWLockAlloc( void );
190 /*! @function IORWLockFree
191 @abstract Frees an osfmk general (read/write) lock.
192 @discussion Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be woken.
193 @param lock Pointer to the allocated lock. */
195 void IORWLockFree( IORWLock
* lock
);
197 /*! @function IORWLockRead
198 @abstract Lock an osfmk lock for read.
199 @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.
200 @param lock Pointer to the allocated lock. */
203 void IORWLockRead( IORWLock
* lock
)
208 /*! @function IORWLockWrite
209 @abstract Lock an osfmk lock for write.
210 @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.
211 @param lock Pointer to the allocated lock. */
214 void IORWLockWrite( IORWLock
* lock
)
219 /*! @function IORWLockUnlock
220 @abstract Unlock an osfmk lock.
221 @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.
222 @param lock Pointer to the allocated lock. */
225 void IORWLockUnlock( IORWLock
* lock
)
232 /* The following API is deprecated */
234 static __inline__
void IOReadLock( IORWLock
* lock
) { IORWLockRead(lock
); }
235 static __inline__
void IOWriteLock( IORWLock
* lock
) { IORWLockWrite(lock
); }
236 static __inline__
void IORWUnlock( IORWLock
* lock
) { IORWLockUnlock(lock
); }
238 #endif /* IOKIT_DEPRECATED */
242 * Simple locks. Cannot block while holding a simple lock.
245 typedef simple_lock_data_t IOSimpleLock
;
247 /*! @function IOSimpleLockAlloc
248 @abstract Allocates and initializes an osfmk simple (spin) lock.
249 @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.
250 @result Pointer to the allocated lock, or zero on failure. */
252 IOSimpleLock
* IOSimpleLockAlloc( void );
254 /*! @function IOSimpleLockFree
255 @abstract Frees an osfmk simple (spin) lock.
256 @discussion Frees a lock allocated with IOSimpleLockAlloc.
257 @param lock Pointer to the lock. */
259 void IOSimpleLockFree( IOSimpleLock
* lock
);
261 /*! @function IOSimpleLockInit
262 @abstract Initialize an osfmk simple (spin) lock.
263 @discussion Initialize an embedded osfmk simple lock, to the unlocked state.
264 @param lock Pointer to the lock. */
266 void IOSimpleLockInit( IOSimpleLock
* lock
);
268 /*! @function IOSimpleLockLock
269 @abstract Lock an osfmk simple lock.
270 @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.
271 @param lock Pointer to the lock. */
274 void IOSimpleLockLock( IOSimpleLock
* lock
)
279 /*! @function IOSimpleLockTryLock
280 @abstract Attempt to lock an osfmk simple lock.
281 @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.
282 @param lock Pointer to the lock.
283 @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
286 boolean_t
IOSimpleLockTryLock( IOSimpleLock
* lock
)
288 return( simple_lock_try( lock
) );
291 /*! @function IOSimpleLockUnlock
292 @abstract Unlock an osfmk simple lock.
293 @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
294 @param lock Pointer to the lock. */
297 void IOSimpleLockUnlock( IOSimpleLock
* lock
)
299 simple_unlock( lock
);
302 typedef long int IOInterruptState
;
304 /*! @function IOSimpleLockLockDisableInterrupt
305 @abstract Lock an osfmk simple lock.
306 @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.
307 @param lock Pointer to the lock. */
310 IOInterruptState
IOSimpleLockLockDisableInterrupt( IOSimpleLock
* lock
)
312 IOInterruptState state
= ml_set_interrupts_enabled( false );
317 /*! @function IOSimpleLockUnlockEnableInterrupt
318 @abstract Unlock an osfmk simple lock, and restore interrupt state.
319 @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.
320 @param lock Pointer to the lock.
321 @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
324 void IOSimpleLockUnlockEnableInterrupt( IOSimpleLock
* lock
,
325 IOInterruptState state
)
327 simple_unlock( lock
);
328 ml_set_interrupts_enabled( state
);
335 #endif /* !__IOKIT_IOLOCKS_H */