ADC Home > Reference Library > Reference > Darwin > Kernel Framework Reference

 


IOLocks.h

Includes:
<sys/appleapiopts.h>
<IOKit/system.h>
<IOKit/IOReturn.h>
<IOKit/IOTypes.h>
<libkern/locks.h>
<machine/machine_routines.h>

Overview

This header contains definitions of functions that implement various types of lock operations, such as mutex locks, recursive locks, and simple locks.



Functions

IOLockAlloc

Allocates and initializes a mutex.

IOLockFree

Frees a mutex.

IOLockGetMachLock

Gets a Mach mutex.

IOLockLock

Locks a mutex.

IOLockSleep

Sleeps with mutex unlocked and relocks on wakeup.

IOLockSleepDeadline

Sleeps with mutex unlocked and relocks on wakeup when the specified event occurs or the deadline passes.

IOLockTryLock

Attempts to lock a mutex.

IOLockUnlock

Unlocks a mutex.

IOLockWakeup

Wakes up the thread holding the specified lock.

IORecursiveLockAlloc

Allocates and initializes an recursive lock.

IORecursiveLockFree

Frees a recursive lock.

IORecursiveLockGetMachLock

Gets a Mach mutex.

IORecursiveLockHaveLock

Checks if a recursive lock is held by the calling thread.

IORecursiveLockLock

Locks a recursive lock.

IORecursiveLockSleep

Sleeps with the recursive lock unlocked and relocks on wakeup.

IORecursiveLockTryLock

Attempts to lock a recursive lock.

IORecursiveLockUnlock

Unlocks a recursive lock.

IORecursiveLockWakeup

Wakes up the thread holding the specified recursive lock.

IORWLockAlloc

Allocates and initializes a read/write lock.

IORWLockFree

Frees a read/write lock.

IORWLockGetMachLock

Gets a Mach read/write lock.

IORWLockRead

Locks a read/write lock for read.

IORWLockUnlock

Unlocks a read/write lock.

IORWLockWrite

Locks a read/write lock for write.

IOSimpleLockAlloc

Allocates and initializes a spin lock.

IOSimpleLockFree

Frees a spin lock.

IOSimpleLockGetMachLock

Gets a Mach spin lock.

IOSimpleLockInit

Initializes a spin lock.

IOSimpleLockLock

Locks a spin lock.

IOSimpleLockLockDisableInterrupt

Locks a spin lock, disabling interrupts.

IOSimpleLockTryLock

Attempts to lock a spin lock.

IOSimpleLockUnlock

Unlocks a spin lock.

IOSimpleLockUnlockEnableInterrupt

Unlocks a spin lock and restores interrupt state.


IOLockAlloc


Allocates and initializes a mutex.

IOLock * IOLockAlloc(
    void );  
Return Value

Pointer to the allocated lock, or zero on failure.

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.


IOLockFree


Frees a mutex.

void IOLockFree(
    IOLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Frees a lock allocated with IOLockAlloc. Any blocked waiters will not be awakened.


IOLockGetMachLock


Gets a Mach mutex.

lck_mtx_t * IOLockGetMachLock(
    IOLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

This is an accessor to a Mach mutex.


IOLockLock


Locks a mutex.

void IOLockLock(
    IOLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Locks 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.


IOLockSleep


Sleeps with mutex unlocked and relocks on wakeup.

int IOLockSleep(
    IOLock *lock,
    void *event,
    UInt32 interType);  
Parameters
lock

Pointer to the locked lock.

event

The event to sleep on.

interType

How the sleep can be interrupted. Possible values are:

  • THREAD_UNINT Not interruptible
  • THREAD_INTERRUPTIBLE Interruptible, but not guaranteed to be restartable
  • THREAD_ABORTSAFE Safely abortable
Return Value

The wait-result value indicating how the thread was awakened. Possible values are:

Discussion

Prepare to sleep, unlocks the mutex, and re-acquires 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.


IOLockSleepDeadline


Sleeps with mutex unlocked and relocks on wakeup when the specified event occurs or the deadline passes.

int IOLockSleepDeadline(
    IOLock *lock,
    void *event, 
    AbsoluteTime deadline,
    UInt32 interType);  
Parameters
lock

Pointer to the locked lock.

event

The event to sleep on.

deadline

The time by which to wake up.

interType

How the sleep can be interrupted. Possible values are:

  • THREAD_UNINT Not interruptible
  • THREAD_INTERRUPTIBLE Interruptible, but not guaranteed to be restartable
  • THREAD_ABORTSAFE Safely abortable
Return Value

The wait-result value indicating how the thread was awakened. Possible values are:

Discussion

Prepare to sleep until the event occurs or the deadline passes, 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.


IOLockTryLock


Attempts to lock a mutex.

boolean_t IOLockTryLock(
    IOLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Return Value

True if the mutex was unlocked and is now locked by the caller, otherwise false.

Discussion

Locks the mutex if it is currently unlocked and returns true. If the lock is held by any thread, this function returns false.


IOLockUnlock


Unlocks a mutex.

void IOLockUnlock(
    IOLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Unlocks the mutex and wakes 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.


IOLockWakeup


Wakes up the thread holding the specified lock.

void IOLockWakeup(
    IOLock *lock,
    void *event,
    bool oneThread);  
Parameters
lock

Pointer to the locked lock.

event

The event being waited on.

oneThread

A Boolean value indicating whether only one thread should be awakened (true) or all (false).

Discussion

Wakes up the thread holding the specified lock and waiting on the specified event.


IORecursiveLockAlloc


Allocates and initializes an recursive lock.

IORecursiveLock * IORecursiveLockAlloc(
    void );  
Return Value

Pointer to the allocated lock, or zero on failure.

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.


IORecursiveLockFree


Frees a recursive lock.

void IORecursiveLockFree(
    IORecursiveLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Frees a lock allocated with IORecursiveLockAlloc. Any blocked waiters will not be awakened.


IORecursiveLockGetMachLock


Gets a Mach mutex.

lck_mtx_t * IORecursiveLockGetMachLock(
    IORecursiveLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Accessor to a Mach mutex.


IORecursiveLockHaveLock


Checks if a recursive lock is held by the calling thread.

boolean_t IORecursiveLockHaveLock(
    const IORecursiveLock * lock);  
Parameters
lock

Pointer to the allocated lock.

Return Value

True if the calling thread holds the lock, otherwise false.

Discussion

If the lock is held by the calling thread returns true, otherwise the lock is unlocked or held by another thread and false is returned.


IORecursiveLockLock


Locks a recursive lock.

void IORecursiveLockLock(
    IORecursiveLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

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.


IORecursiveLockSleep


Sleeps with the recursive lock unlocked and relocks on wakeup.

extern int IORecursiveLockSleep(
    IORecursiveLock *lock, 
    void *event,
    UInt32 interType);  
Parameters
lock

Pointer to the allocated lock.

event

The event to sleep on.

interType

How the sleep can be interrupted. Possible values are:

  • THREAD_UNINT Not interruptible
  • THREAD_INTERRUPTIBLE Interruptible, but not guaranteed to be restartable
  • THREAD_ABORTSAFE Safely abortable
Return Value

The wait-result value indicating how the thread was awakened. Possible values are:

Discussion

Prepare to sleep, unlocks the lock, and re-establishes it on wakeup. 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.


IORecursiveLockTryLock


Attempts to lock a recursive lock.

boolean_t IORecursiveLockTryLock(
    IORecursiveLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Return Value

True if the lock is now locked by the caller, otherwise false.

Discussion

Locks the lock if it is currently unlocked or held by the calling thread, and returns true. If the lock is held by another thread, this function returns false. Successful calls to IORecursiveLockTryLock should be balanced with calls to IORecursiveLockUnlock.


IORecursiveLockUnlock


Unlocks a recursive lock.

void IORecursiveLockUnlock(
    IORecursiveLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Undoes 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.


IORecursiveLockWakeup


Wakes up the thread holding the specified recursive lock.

extern void IORecursiveLockWakeup(
    IORecursiveLock *lock, 
    void *event,
    bool oneThread);  
Parameters
lock

Pointer to the allocated lock.

event

The event being waited on.

oneThread

A Boolean value indicating whether only one thread should be awakened (true) or all (false).

Discussion

Wakes up the thread holding the specified recursive lock and waiting on the specified event.


IORWLockAlloc


Allocates and initializes a read/write lock.

IORWLock * IORWLockAlloc(
    void );  
Return Value

Pointer to the allocated lock, or zero on failure.

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.


IORWLockFree


Frees a read/write lock.

void IORWLockFree(
    IORWLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Frees a lock allocated with IORWLockAlloc. Any blocked waiters will not be awakened.


IORWLockGetMachLock


Gets a Mach read/write lock.

lck_rw_t * IORWLockGetMachLock(
    IORWLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

This is an accessor to a Mach read/write lock.


IORWLockRead


Locks a read/write lock for read.

void IORWLockRead(
    IORWLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Locks 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.


IORWLockUnlock


Unlocks a read/write lock.

void IORWLockUnlock(
    IORWLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Undoes 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.


IORWLockWrite


Locks a read/write lock for write.

void IORWLockWrite(
    IORWLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

Locks the lock for write, allowing one writer exclusive 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.


IOSimpleLockAlloc


Allocates and initializes a spin lock.

IOSimpleLock * IOSimpleLockAlloc(
    void );  
Return Value

Pointer to the allocated lock, or zero on failure.

Discussion

Allocates an initializes a spin lock in general purpose memory, and initializes 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.


IOSimpleLockFree


Frees a spin lock.

void IOSimpleLockFree(
    IOSimpleLock *lock );  
Parameters
lock

Pointer to the lock.

Discussion

Frees a lock allocated with IOSimpleLockAlloc.


IOSimpleLockGetMachLock


Gets a Mach spin lock.

lck_spin_t * IOSimpleLockGetMachLock(
    IOSimpleLock *lock);  
Parameters
lock

Pointer to the allocated lock.

Discussion

This is an accessor to a Mach spin lock.


IOSimpleLockInit


Initializes a spin lock.

void IOSimpleLockInit(
    IOSimpleLock *lock );  
Parameters
lock

Pointer to the lock.

Discussion

Initializes an embedded spin lock, to the unlocked state.


IOSimpleLockLock


Locks a spin lock.

void IOSimpleLockLock(
    IOSimpleLock *lock );  
Parameters
lock

Pointer to the lock.

Discussion

Locks 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.


IOSimpleLockLockDisableInterrupt


Locks a spin lock, disabling interrupts.

static __inline__ IOInterruptState IOSimpleLockLockDisableInterrupt(
    IOSimpleLock *lock ) 
Parameters
lock

Pointer to the lock.

Return Value

True if interrupts are enabled, false if interrupts are disabled.

Discussion

Locks the spin lock, disabling interrupts. 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 does both. Locking the lock recursively from one thread will result in deadlock.


IOSimpleLockTryLock


Attempts to lock a spin lock.

boolean_t IOSimpleLockTryLock(
    IOSimpleLock *lock );  
Parameters
lock

Pointer to the lock.

Return Value

True if the lock was unlocked and is now locked by the caller, otherwise false.

Discussion

Locks the spin lock if it is currently unlocked, and returns true. If the lock is held, this function returns false. Successful calls to IOSimpleLockTryLock should be balanced with calls to IOSimpleLockUnlock.


IOSimpleLockUnlock


Unlocks a spin lock.

void IOSimpleLockUnlock(
    IOSimpleLock *lock );  
Parameters
lock

Pointer to the lock.

Discussion

Unlocks the lock, and restores preemption. Results are undefined if the caller has not locked the lock.


IOSimpleLockUnlockEnableInterrupt


Unlocks a spin lock and restores interrupt state.

static __inline__ void IOSimpleLockUnlockEnableInterrupt(
    IOSimpleLock *lock, 
    IOInterruptState state ) 
Parameters
lock

Pointer to the lock.

state

The interrupt state returned by IOSimpleLockLockDisableInterrupt.

Discussion

Unlocks the lock, and restores preemption and interrupts to the state they were in when the lock was taken. Results are undefined if the caller has not locked the lock.


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.

 

Last Updated: 2008-12-19