Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/i386/include/IOKit/IOLocks.h

1/*
2 * Copyright (c) 1998-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 *
30 */
31
32#ifndef __IOKIT_IOLOCKS_H
33#define __IOKIT_IOLOCKS_H
34
35
36#include <sys/appleapiopts.h>
37
38#include <IOKit/system.h>
39
40#include <IOKit/IOReturn.h>
41#include <IOKit/IOTypes.h>
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47#include <libkern/locks.h>
48#include <machine/machine_routines.h>
49
50/*! @var IOLockGroup
51 Global lock group used by all IOKit locks. To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
52*/
53extern lck_grp_t*IOLockGroup;
54
55
56/*
57 * Mutex lock operations
58 */
59
60#ifdefIOLOCKS_INLINE
61typedef lck_mtx_tIOLock;
62#else
63typedef struct _IOLockIOLock;
64#endif/* IOLOCKS_INLINE */
65
66
67/*! @function IOLockAlloc
68 @abstract Allocates and initializes a mutex.
69 @discussion Allocates a mutex in general purpose memory, and initializes 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. IOLocks use the global IOKit lock group, IOLockGroup. To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
70 @result Pointer to the allocated lock, or zero on failure. */
71
72IOLock * IOLockAlloc( void );
73
74/*! @function IOLockFree
75 @abstract Frees a mutex.
76 @discussion Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be woken.
77 @param lock Pointer to the allocated lock. */
78
79voidIOLockFree( IOLock * lock);
80
81/*! @function IOLockGetMachLock
82 @abstract Accessor to a Mach mutex.
83 @discussion Accessor to the Mach mutex.
84 @param lock Pointer to the allocated lock. */
85
86lck_mtx_t * IOLockGetMachLock( IOLock * lock);
87
88/*! @function IOLockLock
89 @abstract Lock a mutex.
90 @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.
91 @param lock Pointer to the allocated lock. */
92
93#ifdefIOLOCKS_INLINE
94#define IOLockLock(l)lck_mtx_lock(l)
95#else
96voidIOLockLock( IOLock * lock);
97#endif/* !IOLOCKS_INLINE */
98
99/*! @function IOLockTryLock
100 @abstract Attempt to lock a mutex.
101 @discussion Lock the mutex if it is currently unlocked, and return true. If the lock is held by any thread, return false.
102 @param lock Pointer to the allocated lock.
103 @result True if the mutex was unlocked and is now locked by the caller, otherwise false. */
104
105#ifdefIOLOCKS_INLINE
106#define IOLockTryLock(l)lck_mtx_try_lock(l)
107#else
108boolean_t IOLockTryLock( IOLock * lock);
109#endif/* !IOLOCKS_INLINE */
110
111/*! @function IOLockUnlock
112 @abstract Unlock a mutex.
113@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.
114 @param lock Pointer to the allocated lock. */
115
116#ifdefIOLOCKS_INLINE
117#define IOLockUnlock(l)lck_mtx_unlock(l)
118#else
119#ifdefined(__i386__)
120voidIOLockUnlock( IOLock * lock) __DARWIN10_ALIAS(IOLockUnlock);
121#else/* !__i386__ */
122voidIOLockUnlock( IOLock * lock);
123#endif/* __i386__ */
124#endif/* !IOLOCKS_INLINE */
125
126/*! @function IOLockSleep
127 @abstract Sleep with mutex unlock and relock
128@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.
129 @param lock Pointer to the locked lock.
130 @param event The event to sleep on.
131 @param interType How can the sleep be interrupted.
132@result The wait-result value indicating how the thread was awakened.*/
133intIOLockSleep( IOLock * lock, void *event, UInt32 interType);
134
135intIOLockSleepDeadline( IOLock * lock, void *event,
136AbsoluteTime deadline, UInt32 interType);
137
138voidIOLockWakeup(IOLock * lock, void *event, bool oneThread);
139
140#ifdef __APPLE_API_OBSOLETE
141
142/* The following API is deprecated */
143
144typedef enum {
145 kIOLockStateUnlocked= 0,
146 kIOLockStateLocked= 1
147} IOLockState;
148
149voidIOLockInitWithState( IOLock * lock, IOLockState state);
150#defineIOLockInit( l )IOLockInitWithState( l, kIOLockStateUnlocked);
151
152static __inline__ void IOTakeLock( IOLock * lock) { IOLockLock(lock); }
153static __inline__ boolean_t IOTryLock( IOLock * lock) { return(IOLockTryLock(lock)); }
154static __inline__ void IOUnlock( IOLock * lock) { IOLockUnlock(lock); }
155
156#endif /* __APPLE_API_OBSOLETE */
157
158/*
159 * Recursive lock operations
160 */
161
162typedef struct _IORecursiveLock IORecursiveLock;
163
164/*! @function IORecursiveLockAlloc
165 @abstract Allocates and initializes an recursive lock.
166 @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. IORecursiveLocks use the global IOKit lock group, IOLockGroup. To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
167 @result Pointer to the allocated lock, or zero on failure. */
168
169IORecursiveLock * IORecursiveLockAlloc( void );
170
171/*! @function IORecursiveLockFree
172 @abstract Frees a recursive lock.
173 @discussion Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be woken.
174 @param lock Pointer to the allocated lock. */
175
176voidIORecursiveLockFree( IORecursiveLock * lock);
177
178/*! @function IORecursiveLockGetMachLock
179 @abstract Accessor to a Mach mutex.
180 @discussion Accessor to the Mach mutex.
181 @param lock Pointer to the allocated lock. */
182
183lck_mtx_t * IORecursiveLockGetMachLock( IORecursiveLock * lock);
184
185/*! @function IORecursiveLockLock
186 @abstract Lock a recursive lock.
187 @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.
188 @param lock Pointer to the allocated lock. */
189
190voidIORecursiveLockLock( IORecursiveLock * lock);
191
192/*! @function IORecursiveLockTryLock
193 @abstract Attempt to lock a recursive lock.
194 @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.
195 @param lock Pointer to the allocated lock.
196 @result True if the lock is now locked by the caller, otherwise false. */
197
198boolean_tIORecursiveLockTryLock( IORecursiveLock * lock);
199
200/*! @function IORecursiveLockUnlock
201 @abstract Unlock a recursive lock.
202@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.
203 @param lock Pointer to the allocated lock. */
204
205voidIORecursiveLockUnlock( IORecursiveLock * lock);
206
207/*! @function IORecursiveLockHaveLock
208 @abstract Check if a recursive lock is held by the calling thread.
209 @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.
210 @param lock Pointer to the allocated lock.
211 @result True if the calling thread holds the lock otherwise false. */
212
213boolean_tIORecursiveLockHaveLock( const IORecursiveLock * lock);
214
215extern intIORecursiveLockSleep( IORecursiveLock *_lock,
216 void *event, UInt32 interType);
217extern intIORecursiveLockSleepDeadline( IORecursiveLock * _lock, void *event,
218AbsoluteTime deadline, UInt32 interType);
219extern voidIORecursiveLockWakeup( IORecursiveLock *_lock,
220 void *event, bool oneThread);
221
222/*
223 * Complex (read/write) lock operations
224 */
225
226#ifdefIOLOCKS_INLINE
227typedef lck_rw_tIORWLock;
228#else
229typedef struct _IORWLockIORWLock;
230#endif/* IOLOCKS_INLINE */
231
232/*! @function IORWLockAlloc
233 @abstract Allocates and initializes a read/write lock.
234 @discussion Allocates and initializes a read/write lock in general purpose memory. 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. IORWLocks use the global IOKit lock group, IOLockGroup. To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
235 @result Pointer to the allocated lock, or zero on failure. */
236
237IORWLock * IORWLockAlloc( void );
238
239/*! @function IORWLockFree
240 @abstract Frees a read/write lock.
241 @discussion Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be woken.
242 @param lock Pointer to the allocated lock. */
243
244voidIORWLockFree( IORWLock * lock);
245
246/*! @function IORWLockGetMachLock
247 @abstract Accessor to a Mach read/write lock.
248 @discussion Accessor to the Mach read/write lock.
249 @param lock Pointer to the allocated lock. */
250
251lck_rw_t * IORWLockGetMachLock( IORWLock * lock);
252
253/*! @function IORWLockRead
254 @abstract Lock a read/write lock for read.
255@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.
256 @param lock Pointer to the allocated lock. */
257
258#ifdefIOLOCKS_INLINE
259#define IORWLockRead(l)lck_rw_lock_shared(l)
260#else
261voidIORWLockRead(IORWLock * lock);
262#endif/* !IOLOCKS_INLINE */
263
264/*! @function IORWLockWrite
265 @abstract Lock a read/write lock for write.
266 @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.
267 @param lock Pointer to the allocated lock. */
268
269#ifdefIOLOCKS_INLINE
270#define IORWLockWrite(l)lck_rw_lock_exclusive(l)
271#else
272voidIORWLockWrite( IORWLock * lock);
273#endif/* !IOLOCKS_INLINE */
274
275/*! @function IORWLockUnlock
276 @abstract Unlock a read/write lock.
277 @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.
278 @param lock Pointer to the allocated lock. */
279
280#ifdefIOLOCKS_INLINE
281#define IORWLockUnlock(l)lck_rw_done(l)
282#else
283voidIORWLockUnlock( IORWLock * lock);
284#endif/* !IOLOCKS_INLINE */
285
286
287#ifdef __APPLE_API_OBSOLETE
288
289/* The following API is deprecated */
290
291static __inline__ void IOReadLock( IORWLock * lock) { IORWLockRead(lock); }
292static __inline__ void IOWriteLock( IORWLock * lock) { IORWLockWrite(lock); }
293static __inline__ void IORWUnlock( IORWLock * lock) { IORWLockUnlock(lock); }
294
295#endif /* __APPLE_API_OBSOLETE */
296
297
298/*
299 * Simple locks. Cannot block while holding a simple lock.
300 */
301
302#ifdefIOLOCKS_INLINE
303typedef lck_spin_tIOSimpleLock;
304#else
305typedef struct _IOSimpleLockIOSimpleLock;
306#endif/* IOLOCKS_INLINE */
307
308/*! @function IOSimpleLockAlloc
309 @abstract Allocates and initializes a spin lock.
310 @discussion Allocates and initializes a spin lock in general purpose memory. 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. IOSimpleLocks use the global IOKit lock group, IOLockGroup. To simplify kext debugging and lock-heat analysis, consider using lck_* locks with a per-driver lock group, as defined in kern/locks.h.
311 @result Pointer to the allocated lock, or zero on failure. */
312
313IOSimpleLock * IOSimpleLockAlloc( void );
314
315/*! @function IOSimpleLockFree
316 @abstract Frees a spin lock.
317 @discussion Frees a lock allocated with IOSimpleLockAlloc.
318 @param lock Pointer to the lock. */
319
320void IOSimpleLockFree( IOSimpleLock * lock );
321
322/*! @function IOSimpleLockGetMachLock
323 @abstract Accessor to a Mach spin lock.
324 @discussion Accessor to the Mach spin lock.
325 @param lock Pointer to the allocated lock. */
326
327lck_spin_t * IOSimpleLockGetMachLock( IOSimpleLock * lock);
328
329/*! @function IOSimpleLockInit
330 @abstract Initialize a spin lock.
331 @discussion Initialize an embedded spin lock, to the unlocked state.
332 @param lock Pointer to the lock. */
333
334void IOSimpleLockInit( IOSimpleLock * lock );
335
336/*! @function IOSimpleLockLock
337 @abstract Lock a spin lock.
338@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.
339 @param lock Pointer to the lock. */
340
341#ifdefIOLOCKS_INLINE
342#define IOSimpleLockLock(l)lck_spin_lock(l)
343#else
344void IOSimpleLockLock( IOSimpleLock * lock );
345#endif/* !IOLOCKS_INLINE */
346
347
348/*! @function IOSimpleLockTryLock
349 @abstract Attempt to lock a spin lock.
350@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.
351 @param lock Pointer to the lock.
352 @result True if the lock was unlocked and is now locked by the caller, otherwise false. */
353
354#ifdefIOLOCKS_INLINE
355#define IOSimpleLockTryLock(l)lck_spin_try_lock(l)
356#else
357boolean_t IOSimpleLockTryLock( IOSimpleLock * lock );
358#endif/* !IOLOCKS_INLINE */
359
360/*! @function IOSimpleLockUnlock
361 @abstract Unlock a spin lock.
362 @discussion Unlock the lock, and restore preemption. Results are undefined if the caller has not locked the lock.
363 @param lock Pointer to the lock. */
364
365#ifdefIOLOCKS_INLINE
366#define IOSimpleLockUnlock(l)lck_spin_unlock(l)
367#else
368void IOSimpleLockUnlock( IOSimpleLock * lock );
369#endif/* !IOLOCKS_INLINE */
370
371#if __LP64__
372typedef boolean_t IOInterruptState;
373#else
374typedef long int IOInterruptState;
375#endif
376
377/*! @function IOSimpleLockLockDisableInterrupt
378 @abstract Lock a spin lock.
379 @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.
380 @param lock Pointer to the lock. */
381
382static __inline__
383IOInterruptState IOSimpleLockLockDisableInterrupt( IOSimpleLock * lock )
384{
385 IOInterruptStatestate = ml_set_interrupts_enabled( false );
386 IOSimpleLockLock( lock );
387 return( state );
388}
389
390/*! @function IOSimpleLockUnlockEnableInterrupt
391 @abstract Unlock a spin lock, and restore interrupt state.
392 @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.
393 @param lock Pointer to the lock.
394 @param state The interrupt state returned by IOSimpleLockLockDisableInterrupt() */
395
396static __inline__
397void IOSimpleLockUnlockEnableInterrupt( IOSimpleLock * lock,
398IOInterruptState state )
399{
400 IOSimpleLockUnlock( lock );
401 ml_set_interrupts_enabled( state );
402}
403
404#ifdef __cplusplus
405} /* extern "C" */
406#endif
407
408#endif /* !__IOKIT_IOLOCKS_H */
409
410

Archive Download this file

Revision: 2225