The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/iokit/IOKit/IOLocks.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 356279c10772e8a81e9419cfc2d900fc


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.