utlock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /******************************************************************************
  2. *
  3. * Module Name: utlock - Reader/Writer lock interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utlock")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ut_create_rw_lock
  49. * acpi_ut_delete_rw_lock
  50. *
  51. * PARAMETERS: lock - Pointer to a valid RW lock
  52. *
  53. * RETURN: Status
  54. *
  55. * DESCRIPTION: Reader/writer lock creation and deletion interfaces.
  56. *
  57. ******************************************************************************/
  58. acpi_status acpi_ut_create_rw_lock(struct acpi_rw_lock *lock)
  59. {
  60. acpi_status status;
  61. lock->num_readers = 0;
  62. status = acpi_os_create_mutex(&lock->reader_mutex);
  63. if (ACPI_FAILURE(status)) {
  64. return (status);
  65. }
  66. status = acpi_os_create_mutex(&lock->writer_mutex);
  67. return (status);
  68. }
  69. void acpi_ut_delete_rw_lock(struct acpi_rw_lock *lock)
  70. {
  71. acpi_os_delete_mutex(lock->reader_mutex);
  72. acpi_os_delete_mutex(lock->writer_mutex);
  73. lock->num_readers = 0;
  74. lock->reader_mutex = NULL;
  75. lock->writer_mutex = NULL;
  76. }
  77. /*******************************************************************************
  78. *
  79. * FUNCTION: acpi_ut_acquire_read_lock
  80. * acpi_ut_release_read_lock
  81. *
  82. * PARAMETERS: lock - Pointer to a valid RW lock
  83. *
  84. * RETURN: Status
  85. *
  86. * DESCRIPTION: Reader interfaces for reader/writer locks. On acquisition,
  87. * only the first reader acquires the write mutex. On release,
  88. * only the last reader releases the write mutex. Although this
  89. * algorithm can in theory starve writers, this should not be a
  90. * problem with ACPICA since the subsystem is infrequently used
  91. * in comparison to (for example) an I/O system.
  92. *
  93. ******************************************************************************/
  94. acpi_status acpi_ut_acquire_read_lock(struct acpi_rw_lock *lock)
  95. {
  96. acpi_status status;
  97. status = acpi_os_acquire_mutex(lock->reader_mutex, ACPI_WAIT_FOREVER);
  98. if (ACPI_FAILURE(status)) {
  99. return (status);
  100. }
  101. /* Acquire the write lock only for the first reader */
  102. lock->num_readers++;
  103. if (lock->num_readers == 1) {
  104. status =
  105. acpi_os_acquire_mutex(lock->writer_mutex,
  106. ACPI_WAIT_FOREVER);
  107. }
  108. acpi_os_release_mutex(lock->reader_mutex);
  109. return (status);
  110. }
  111. acpi_status acpi_ut_release_read_lock(struct acpi_rw_lock *lock)
  112. {
  113. acpi_status status;
  114. status = acpi_os_acquire_mutex(lock->reader_mutex, ACPI_WAIT_FOREVER);
  115. if (ACPI_FAILURE(status)) {
  116. return (status);
  117. }
  118. /* Release the write lock only for the very last reader */
  119. lock->num_readers--;
  120. if (lock->num_readers == 0) {
  121. acpi_os_release_mutex(lock->writer_mutex);
  122. }
  123. acpi_os_release_mutex(lock->reader_mutex);
  124. return (status);
  125. }
  126. /*******************************************************************************
  127. *
  128. * FUNCTION: acpi_ut_acquire_write_lock
  129. * acpi_ut_release_write_lock
  130. *
  131. * PARAMETERS: lock - Pointer to a valid RW lock
  132. *
  133. * RETURN: Status
  134. *
  135. * DESCRIPTION: Writer interfaces for reader/writer locks. Simply acquire or
  136. * release the writer mutex associated with the lock. Acquisition
  137. * of the lock is fully exclusive and will block all readers and
  138. * writers until it is released.
  139. *
  140. ******************************************************************************/
  141. acpi_status acpi_ut_acquire_write_lock(struct acpi_rw_lock *lock)
  142. {
  143. acpi_status status;
  144. status = acpi_os_acquire_mutex(lock->writer_mutex, ACPI_WAIT_FOREVER);
  145. return (status);
  146. }
  147. void acpi_ut_release_write_lock(struct acpi_rw_lock *lock)
  148. {
  149. acpi_os_release_mutex(lock->writer_mutex);
  150. }