utmutex.c 10 KB

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