Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: utlock - Reader/Writer lock interfaces
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #include <acpi/acpi.h>
0011 #include "accommon.h"
0012 
0013 #define _COMPONENT          ACPI_UTILITIES
0014 ACPI_MODULE_NAME("utlock")
0015 
0016 /*******************************************************************************
0017  *
0018  * FUNCTION:    acpi_ut_create_rw_lock
0019  *              acpi_ut_delete_rw_lock
0020  *
0021  * PARAMETERS:  lock                - Pointer to a valid RW lock
0022  *
0023  * RETURN:      Status
0024  *
0025  * DESCRIPTION: Reader/writer lock creation and deletion interfaces.
0026  *
0027  ******************************************************************************/
0028 acpi_status acpi_ut_create_rw_lock(struct acpi_rw_lock *lock)
0029 {
0030     acpi_status status;
0031 
0032     lock->num_readers = 0;
0033     status = acpi_os_create_mutex(&lock->reader_mutex);
0034     if (ACPI_FAILURE(status)) {
0035         return (status);
0036     }
0037 
0038     status = acpi_os_create_mutex(&lock->writer_mutex);
0039     return (status);
0040 }
0041 
0042 void acpi_ut_delete_rw_lock(struct acpi_rw_lock *lock)
0043 {
0044 
0045     acpi_os_delete_mutex(lock->reader_mutex);
0046     acpi_os_delete_mutex(lock->writer_mutex);
0047 
0048     lock->num_readers = 0;
0049     lock->reader_mutex = NULL;
0050     lock->writer_mutex = NULL;
0051 }
0052 
0053 /*******************************************************************************
0054  *
0055  * FUNCTION:    acpi_ut_acquire_read_lock
0056  *              acpi_ut_release_read_lock
0057  *
0058  * PARAMETERS:  lock                - Pointer to a valid RW lock
0059  *
0060  * RETURN:      Status
0061  *
0062  * DESCRIPTION: Reader interfaces for reader/writer locks. On acquisition,
0063  *              only the first reader acquires the write mutex. On release,
0064  *              only the last reader releases the write mutex. Although this
0065  *              algorithm can in theory starve writers, this should not be a
0066  *              problem with ACPICA since the subsystem is infrequently used
0067  *              in comparison to (for example) an I/O system.
0068  *
0069  ******************************************************************************/
0070 
0071 acpi_status acpi_ut_acquire_read_lock(struct acpi_rw_lock *lock)
0072 {
0073     acpi_status status;
0074 
0075     status = acpi_os_acquire_mutex(lock->reader_mutex, ACPI_WAIT_FOREVER);
0076     if (ACPI_FAILURE(status)) {
0077         return (status);
0078     }
0079 
0080     /* Acquire the write lock only for the first reader */
0081 
0082     lock->num_readers++;
0083     if (lock->num_readers == 1) {
0084         status =
0085             acpi_os_acquire_mutex(lock->writer_mutex,
0086                       ACPI_WAIT_FOREVER);
0087     }
0088 
0089     acpi_os_release_mutex(lock->reader_mutex);
0090     return (status);
0091 }
0092 
0093 acpi_status acpi_ut_release_read_lock(struct acpi_rw_lock *lock)
0094 {
0095     acpi_status status;
0096 
0097     status = acpi_os_acquire_mutex(lock->reader_mutex, ACPI_WAIT_FOREVER);
0098     if (ACPI_FAILURE(status)) {
0099         return (status);
0100     }
0101 
0102     /* Release the write lock only for the very last reader */
0103 
0104     lock->num_readers--;
0105     if (lock->num_readers == 0) {
0106         acpi_os_release_mutex(lock->writer_mutex);
0107     }
0108 
0109     acpi_os_release_mutex(lock->reader_mutex);
0110     return (status);
0111 }
0112 
0113 /*******************************************************************************
0114  *
0115  * FUNCTION:    acpi_ut_acquire_write_lock
0116  *              acpi_ut_release_write_lock
0117  *
0118  * PARAMETERS:  lock                - Pointer to a valid RW lock
0119  *
0120  * RETURN:      Status
0121  *
0122  * DESCRIPTION: Writer interfaces for reader/writer locks. Simply acquire or
0123  *              release the writer mutex associated with the lock. Acquisition
0124  *              of the lock is fully exclusive and will block all readers and
0125  *              writers until it is released.
0126  *
0127  ******************************************************************************/
0128 
0129 acpi_status acpi_ut_acquire_write_lock(struct acpi_rw_lock *lock)
0130 {
0131     acpi_status status;
0132 
0133     status = acpi_os_acquire_mutex(lock->writer_mutex, ACPI_WAIT_FOREVER);
0134     return (status);
0135 }
0136 
0137 void acpi_ut_release_write_lock(struct acpi_rw_lock *lock)
0138 {
0139 
0140     acpi_os_release_mutex(lock->writer_mutex);
0141 }