utmutex.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmutex - local mutex support
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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. #define _COMPONENT ACPI_UTILITIES
  44. ACPI_MODULE_NAME("utmutex")
  45. /* Local prototypes */
  46. static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id);
  47. static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id);
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ut_mutex_initialize
  51. *
  52. * PARAMETERS: None.
  53. *
  54. * RETURN: Status
  55. *
  56. * DESCRIPTION: Create the system mutex objects.
  57. *
  58. ******************************************************************************/
  59. acpi_status acpi_ut_mutex_initialize(void)
  60. {
  61. u32 i;
  62. acpi_status status;
  63. ACPI_FUNCTION_TRACE("ut_mutex_initialize");
  64. /*
  65. * Create each of the predefined mutex objects
  66. */
  67. for (i = 0; i < NUM_MUTEX; i++) {
  68. status = acpi_ut_create_mutex(i);
  69. if (ACPI_FAILURE(status)) {
  70. return_ACPI_STATUS(status);
  71. }
  72. }
  73. status = acpi_os_create_lock(&acpi_gbl_gpe_lock);
  74. return_ACPI_STATUS(status);
  75. }
  76. /*******************************************************************************
  77. *
  78. * FUNCTION: acpi_ut_mutex_terminate
  79. *
  80. * PARAMETERS: None.
  81. *
  82. * RETURN: None.
  83. *
  84. * DESCRIPTION: Delete all of the system mutex objects.
  85. *
  86. ******************************************************************************/
  87. void acpi_ut_mutex_terminate(void)
  88. {
  89. u32 i;
  90. ACPI_FUNCTION_TRACE("ut_mutex_terminate");
  91. /*
  92. * Delete each predefined mutex object
  93. */
  94. for (i = 0; i < NUM_MUTEX; i++) {
  95. (void)acpi_ut_delete_mutex(i);
  96. }
  97. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  98. return_VOID;
  99. }
  100. /*******************************************************************************
  101. *
  102. * FUNCTION: acpi_ut_create_mutex
  103. *
  104. * PARAMETERS: mutex_iD - ID of the mutex to be created
  105. *
  106. * RETURN: Status
  107. *
  108. * DESCRIPTION: Create a mutex object.
  109. *
  110. ******************************************************************************/
  111. static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id)
  112. {
  113. acpi_status status = AE_OK;
  114. ACPI_FUNCTION_TRACE_U32("ut_create_mutex", mutex_id);
  115. if (mutex_id > MAX_MUTEX) {
  116. return_ACPI_STATUS(AE_BAD_PARAMETER);
  117. }
  118. if (!acpi_gbl_mutex_info[mutex_id].mutex) {
  119. status = acpi_os_create_semaphore(1, 1,
  120. &acpi_gbl_mutex_info
  121. [mutex_id].mutex);
  122. acpi_gbl_mutex_info[mutex_id].thread_id =
  123. ACPI_MUTEX_NOT_ACQUIRED;
  124. acpi_gbl_mutex_info[mutex_id].use_count = 0;
  125. }
  126. return_ACPI_STATUS(status);
  127. }
  128. /*******************************************************************************
  129. *
  130. * FUNCTION: acpi_ut_delete_mutex
  131. *
  132. * PARAMETERS: mutex_iD - ID of the mutex to be deleted
  133. *
  134. * RETURN: Status
  135. *
  136. * DESCRIPTION: Delete a mutex object.
  137. *
  138. ******************************************************************************/
  139. static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id)
  140. {
  141. acpi_status status;
  142. ACPI_FUNCTION_TRACE_U32("ut_delete_mutex", mutex_id);
  143. if (mutex_id > MAX_MUTEX) {
  144. return_ACPI_STATUS(AE_BAD_PARAMETER);
  145. }
  146. status = acpi_os_delete_semaphore(acpi_gbl_mutex_info[mutex_id].mutex);
  147. acpi_gbl_mutex_info[mutex_id].mutex = NULL;
  148. acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
  149. return_ACPI_STATUS(status);
  150. }
  151. /*******************************************************************************
  152. *
  153. * FUNCTION: acpi_ut_acquire_mutex
  154. *
  155. * PARAMETERS: mutex_iD - ID of the mutex to be acquired
  156. *
  157. * RETURN: Status
  158. *
  159. * DESCRIPTION: Acquire a mutex object.
  160. *
  161. ******************************************************************************/
  162. acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
  163. {
  164. acpi_status status;
  165. u32 this_thread_id;
  166. ACPI_FUNCTION_NAME("ut_acquire_mutex");
  167. if (mutex_id > MAX_MUTEX) {
  168. return (AE_BAD_PARAMETER);
  169. }
  170. this_thread_id = acpi_os_get_thread_id();
  171. #ifdef ACPI_MUTEX_DEBUG
  172. {
  173. u32 i;
  174. /*
  175. * Mutex debug code, for internal debugging only.
  176. *
  177. * Deadlock prevention. Check if this thread owns any mutexes of value
  178. * greater than or equal to this one. If so, the thread has violated
  179. * the mutex ordering rule. This indicates a coding error somewhere in
  180. * the ACPI subsystem code.
  181. */
  182. for (i = mutex_id; i < MAX_MUTEX; i++) {
  183. if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
  184. if (i == mutex_id) {
  185. ACPI_ERROR((AE_INFO,
  186. "Mutex [%s] already acquired by this thread [%X]",
  187. acpi_ut_get_mutex_name
  188. (mutex_id),
  189. this_thread_id));
  190. return (AE_ALREADY_ACQUIRED);
  191. }
  192. ACPI_ERROR((AE_INFO,
  193. "Invalid acquire order: Thread %X owns [%s], wants [%s]",
  194. this_thread_id,
  195. acpi_ut_get_mutex_name(i),
  196. acpi_ut_get_mutex_name(mutex_id)));
  197. return (AE_ACQUIRE_DEADLOCK);
  198. }
  199. }
  200. }
  201. #endif
  202. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  203. "Thread %X attempting to acquire Mutex [%s]\n",
  204. this_thread_id, acpi_ut_get_mutex_name(mutex_id)));
  205. status = acpi_os_wait_semaphore(acpi_gbl_mutex_info[mutex_id].mutex,
  206. 1, ACPI_WAIT_FOREVER);
  207. if (ACPI_SUCCESS(status)) {
  208. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  209. "Thread %X acquired Mutex [%s]\n",
  210. this_thread_id,
  211. acpi_ut_get_mutex_name(mutex_id)));
  212. acpi_gbl_mutex_info[mutex_id].use_count++;
  213. acpi_gbl_mutex_info[mutex_id].thread_id = this_thread_id;
  214. } else {
  215. ACPI_EXCEPTION((AE_INFO, status,
  216. "Thread %X could not acquire Mutex [%X]",
  217. this_thread_id, mutex_id));
  218. }
  219. return (status);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: acpi_ut_release_mutex
  224. *
  225. * PARAMETERS: mutex_iD - ID of the mutex to be released
  226. *
  227. * RETURN: Status
  228. *
  229. * DESCRIPTION: Release a mutex object.
  230. *
  231. ******************************************************************************/
  232. acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
  233. {
  234. acpi_status status;
  235. u32 this_thread_id;
  236. ACPI_FUNCTION_NAME("ut_release_mutex");
  237. this_thread_id = acpi_os_get_thread_id();
  238. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  239. "Thread %X releasing Mutex [%s]\n", this_thread_id,
  240. acpi_ut_get_mutex_name(mutex_id)));
  241. if (mutex_id > MAX_MUTEX) {
  242. return (AE_BAD_PARAMETER);
  243. }
  244. /*
  245. * Mutex must be acquired in order to release it!
  246. */
  247. if (acpi_gbl_mutex_info[mutex_id].thread_id == ACPI_MUTEX_NOT_ACQUIRED) {
  248. ACPI_ERROR((AE_INFO,
  249. "Mutex [%X] is not acquired, cannot release",
  250. mutex_id));
  251. return (AE_NOT_ACQUIRED);
  252. }
  253. #ifdef ACPI_MUTEX_DEBUG
  254. {
  255. u32 i;
  256. /*
  257. * Mutex debug code, for internal debugging only.
  258. *
  259. * Deadlock prevention. Check if this thread owns any mutexes of value
  260. * greater than this one. If so, the thread has violated the mutex
  261. * ordering rule. This indicates a coding error somewhere in
  262. * the ACPI subsystem code.
  263. */
  264. for (i = mutex_id; i < MAX_MUTEX; i++) {
  265. if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
  266. if (i == mutex_id) {
  267. continue;
  268. }
  269. ACPI_ERROR((AE_INFO,
  270. "Invalid release order: owns [%s], releasing [%s]",
  271. acpi_ut_get_mutex_name(i),
  272. acpi_ut_get_mutex_name(mutex_id)));
  273. return (AE_RELEASE_DEADLOCK);
  274. }
  275. }
  276. }
  277. #endif
  278. /* Mark unlocked FIRST */
  279. acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
  280. status =
  281. acpi_os_signal_semaphore(acpi_gbl_mutex_info[mutex_id].mutex, 1);
  282. if (ACPI_FAILURE(status)) {
  283. ACPI_EXCEPTION((AE_INFO, status,
  284. "Thread %X could not release Mutex [%X]",
  285. this_thread_id, mutex_id));
  286. } else {
  287. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  288. "Thread %X released Mutex [%s]\n",
  289. this_thread_id,
  290. acpi_ut_get_mutex_name(mutex_id)));
  291. }
  292. return (status);
  293. }