2 /******************************************************************************
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, R. Byron Moore
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME ("exmutex")
52 /* Local prototypes */
56 union acpi_operand_object
*obj_desc
,
57 struct acpi_thread_state
*thread
);
60 /*******************************************************************************
62 * FUNCTION: acpi_ex_unlink_mutex
64 * PARAMETERS: obj_desc - The mutex to be unlinked
68 * DESCRIPTION: Remove a mutex from the "acquired_mutex" list
70 ******************************************************************************/
73 acpi_ex_unlink_mutex (
74 union acpi_operand_object
*obj_desc
)
76 struct acpi_thread_state
*thread
= obj_desc
->mutex
.owner_thread
;
83 /* Doubly linked list */
85 if (obj_desc
->mutex
.next
) {
86 (obj_desc
->mutex
.next
)->mutex
.prev
= obj_desc
->mutex
.prev
;
89 if (obj_desc
->mutex
.prev
) {
90 (obj_desc
->mutex
.prev
)->mutex
.next
= obj_desc
->mutex
.next
;
93 thread
->acquired_mutex_list
= obj_desc
->mutex
.next
;
98 /*******************************************************************************
100 * FUNCTION: acpi_ex_link_mutex
102 * PARAMETERS: obj_desc - The mutex to be linked
103 * Thread - Current executing thread object
107 * DESCRIPTION: Add a mutex to the "acquired_mutex" list for this walk
109 ******************************************************************************/
113 union acpi_operand_object
*obj_desc
,
114 struct acpi_thread_state
*thread
)
116 union acpi_operand_object
*list_head
;
119 list_head
= thread
->acquired_mutex_list
;
121 /* This object will be the first object in the list */
123 obj_desc
->mutex
.prev
= NULL
;
124 obj_desc
->mutex
.next
= list_head
;
126 /* Update old first object to point back to this object */
129 list_head
->mutex
.prev
= obj_desc
;
132 /* Update list head */
134 thread
->acquired_mutex_list
= obj_desc
;
138 /*******************************************************************************
140 * FUNCTION: acpi_ex_acquire_mutex
142 * PARAMETERS: time_desc - Timeout integer
143 * obj_desc - Mutex object
144 * walk_state - Current method execution state
148 * DESCRIPTION: Acquire an AML mutex
150 ******************************************************************************/
153 acpi_ex_acquire_mutex (
154 union acpi_operand_object
*time_desc
,
155 union acpi_operand_object
*obj_desc
,
156 struct acpi_walk_state
*walk_state
)
161 ACPI_FUNCTION_TRACE_PTR ("ex_acquire_mutex", obj_desc
);
165 return_ACPI_STATUS (AE_BAD_PARAMETER
);
168 /* Sanity check -- we must have a valid thread ID */
170 if (!walk_state
->thread
) {
171 ACPI_REPORT_ERROR (("Cannot acquire Mutex [%4.4s], null thread info\n",
172 acpi_ut_get_node_name (obj_desc
->mutex
.node
)));
173 return_ACPI_STATUS (AE_AML_INTERNAL
);
177 * Current Sync must be less than or equal to the sync level of the
178 * mutex. This mechanism provides some deadlock prevention
180 if (walk_state
->thread
->current_sync_level
> obj_desc
->mutex
.sync_level
) {
182 "Cannot acquire Mutex [%4.4s], incorrect sync_level\n",
183 acpi_ut_get_node_name (obj_desc
->mutex
.node
)));
184 return_ACPI_STATUS (AE_AML_MUTEX_ORDER
);
187 /* Support for multiple acquires by the owning thread */
189 if (obj_desc
->mutex
.owner_thread
) {
190 /* Special case for Global Lock, allow all threads */
192 if ((obj_desc
->mutex
.owner_thread
->thread_id
==
193 walk_state
->thread
->thread_id
) ||
194 (obj_desc
->mutex
.semaphore
==
195 acpi_gbl_global_lock_semaphore
)) {
197 * The mutex is already owned by this thread,
198 * just increment the acquisition depth
200 obj_desc
->mutex
.acquisition_depth
++;
201 return_ACPI_STATUS (AE_OK
);
205 /* Acquire the mutex, wait if necessary */
207 status
= acpi_ex_system_acquire_mutex (time_desc
, obj_desc
);
208 if (ACPI_FAILURE (status
)) {
209 /* Includes failure from a timeout on time_desc */
211 return_ACPI_STATUS (status
);
214 /* Have the mutex: update mutex and walk info and save the sync_level */
216 obj_desc
->mutex
.owner_thread
= walk_state
->thread
;
217 obj_desc
->mutex
.acquisition_depth
= 1;
218 obj_desc
->mutex
.original_sync_level
= walk_state
->thread
->current_sync_level
;
220 walk_state
->thread
->current_sync_level
= obj_desc
->mutex
.sync_level
;
222 /* Link the mutex to the current thread for force-unlock at method exit */
224 acpi_ex_link_mutex (obj_desc
, walk_state
->thread
);
226 return_ACPI_STATUS (AE_OK
);
230 /*******************************************************************************
232 * FUNCTION: acpi_ex_release_mutex
234 * PARAMETERS: obj_desc - The object descriptor for this op
235 * walk_state - Current method execution state
239 * DESCRIPTION: Release a previously acquired Mutex.
241 ******************************************************************************/
244 acpi_ex_release_mutex (
245 union acpi_operand_object
*obj_desc
,
246 struct acpi_walk_state
*walk_state
)
251 ACPI_FUNCTION_TRACE ("ex_release_mutex");
255 return_ACPI_STATUS (AE_BAD_PARAMETER
);
258 /* The mutex must have been previously acquired in order to release it */
260 if (!obj_desc
->mutex
.owner_thread
) {
261 ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], not acquired\n",
262 acpi_ut_get_node_name (obj_desc
->mutex
.node
)));
263 return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED
);
266 /* Sanity check -- we must have a valid thread ID */
268 if (!walk_state
->thread
) {
269 ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], null thread info\n",
270 acpi_ut_get_node_name (obj_desc
->mutex
.node
)));
271 return_ACPI_STATUS (AE_AML_INTERNAL
);
275 * The Mutex is owned, but this thread must be the owner.
276 * Special case for Global Lock, any thread can release
278 if ((obj_desc
->mutex
.owner_thread
->thread_id
!= walk_state
->thread
->thread_id
) &&
279 (obj_desc
->mutex
.semaphore
!= acpi_gbl_global_lock_semaphore
)) {
281 "Thread %X cannot release Mutex [%4.4s] acquired by thread %X\n",
282 walk_state
->thread
->thread_id
,
283 acpi_ut_get_node_name (obj_desc
->mutex
.node
),
284 obj_desc
->mutex
.owner_thread
->thread_id
));
285 return_ACPI_STATUS (AE_AML_NOT_OWNER
);
289 * The sync level of the mutex must be less than or
290 * equal to the current sync level
292 if (obj_desc
->mutex
.sync_level
> walk_state
->thread
->current_sync_level
) {
294 "Cannot release Mutex [%4.4s], incorrect sync_level\n",
295 acpi_ut_get_node_name (obj_desc
->mutex
.node
)));
296 return_ACPI_STATUS (AE_AML_MUTEX_ORDER
);
299 /* Match multiple Acquires with multiple Releases */
301 obj_desc
->mutex
.acquisition_depth
--;
302 if (obj_desc
->mutex
.acquisition_depth
!= 0) {
303 /* Just decrement the depth and return */
305 return_ACPI_STATUS (AE_OK
);
308 /* Unlink the mutex from the owner's list */
310 acpi_ex_unlink_mutex (obj_desc
);
312 /* Release the mutex */
314 status
= acpi_ex_system_release_mutex (obj_desc
);
316 /* Update the mutex and walk state, restore sync_level before acquire */
318 obj_desc
->mutex
.owner_thread
= NULL
;
319 walk_state
->thread
->current_sync_level
= obj_desc
->mutex
.original_sync_level
;
321 return_ACPI_STATUS (status
);
325 /*******************************************************************************
327 * FUNCTION: acpi_ex_release_all_mutexes
329 * PARAMETERS: Thread - Current executing thread object
333 * DESCRIPTION: Release all mutexes held by this thread
335 ******************************************************************************/
338 acpi_ex_release_all_mutexes (
339 struct acpi_thread_state
*thread
)
341 union acpi_operand_object
*next
= thread
->acquired_mutex_list
;
342 union acpi_operand_object
*this;
346 ACPI_FUNCTION_ENTRY ();
349 /* Traverse the list of owned mutexes, releasing each one */
353 next
= this->mutex
.next
;
355 this->mutex
.acquisition_depth
= 1;
356 this->mutex
.prev
= NULL
;
357 this->mutex
.next
= NULL
;
359 /* Release the mutex */
361 status
= acpi_ex_system_release_mutex (this);
362 if (ACPI_FAILURE (status
)) {
366 /* Mark mutex unowned */
368 this->mutex
.owner_thread
= NULL
;
370 /* Update Thread sync_level (Last mutex is the important one) */
372 thread
->current_sync_level
= this->mutex
.original_sync_level
;