FreeBSD/sys/contrib/dev/acpica/components/utilities/utlock.c
/*******************************************************************************
 *
 * FUNCTION:    AcpiUtAcquireReadLock
 *              AcpiUtReleaseReadLock
 *
 * PARAMETERS:  Lock                - Pointer to a valid RW lock
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Reader interfaces for reader/writer locks. On acquisition,
 *              only the first reader acquires the write mutex. On release,
 *              only the last reader releases the write mutex. Although this
 *              algorithm can in theory starve writers, this should not be a
 *              problem with ACPICA since the subsystem is infrequently used
 *              in comparison to (for example) an I/O system.
 *
 ******************************************************************************/


Lock                - Pointer to a valid RW lock

Status

Reader interfaces for reader/writer locks. On acquisition,
only the first reader acquires the write mutex. On release,
only the last reader releases the write mutex. Although this
algorithm can in theory starve writers, this should not be a
problem with ACPICA since the subsystem is infrequently used
in comparison to (for example) an I/O system.
Lock - Pointer to a valid RW lock Status Reader interfaces for reader / writer locks . On acquisition only the first reader acquires the write mutex . On release only the last reader releases the write mutex . Although this algorithm can in theory starve writers this should not be a problem with ACPICA since the subsystem is infrequently used in comparison to ( for example ) an I / O system .
ACPI_STATUS
AcpiUtAcquireReadLock (
    ACPI_RW_LOCK            *Lock)
{
    ACPI_STATUS             Status;
    Status = AcpiOsAcquireMutex (Lock->ReaderMutex, ACPI_WAIT_FOREVER);
    if (ACPI_FAILURE (Status))
    {
        return (Status);
    }
    Lock->NumReaders++;
    if (Lock->NumReaders == 1)
    {
        Status = AcpiOsAcquireMutex (Lock->WriterMutex, ACPI_WAIT_FOREVER);
    }
    AcpiOsReleaseMutex (Lock->ReaderMutex);
    return (Status);
}
ACPI_STATUS AcpiUtAcquireReadLock ( ACPI_RW_LOCK * Lock ) { ACPI_STATUS Status ; Status = AcpiOsAcquireMutex ( Lock -> ReaderMutex , ACPI_WAIT_FOREVER ) ; if ( ACPI_FAILURE ( Status ) ) { return ( Status ) ; } Lock -> NumReaders ++ ; if ( Lock -> NumReaders == 1 ) { Status = AcpiOsAcquireMutex ( Lock -> WriterMutex , ACPI_WAIT_FOREVER ) ; } AcpiOsReleaseMutex ( Lock -> ReaderMutex ) ; return ( Status ) ; }
FUNCTION:    AcpiUtAcquireReadLock
              AcpiUtReleaseReadLock
FUNCTION : AcpiUtAcquireReadLock AcpiUtReleaseReadLock
/* Acquire the write lock only for the first reader */