ACPI_STATUS AcpiExAcquireMutexObject ( UINT16 Timeout, ACPI_OPERAND_OBJECT *ObjDesc, ACPI_THREAD_ID ThreadId) { ACPI_STATUS Status; ACPI_FUNCTION_TRACE_PTR (ExAcquireMutexObject, ObjDesc); if (!ObjDesc) { return_ACPI_STATUS (AE_BAD_PARAMETER); } /* Support for multiple acquires by the owning thread */ if (ObjDesc->Mutex.ThreadId == ThreadId) { /* * The mutex is already owned by this thread, just increment the * acquisition depth */ ObjDesc->Mutex.AcquisitionDepth++; return_ACPI_STATUS (AE_OK); } /* Acquire the mutex, wait if necessary. Special case for Global Lock */ if (ObjDesc == AcpiGbl_GlobalLockMutex) { Status = AcpiEvAcquireGlobalLock (Timeout); } else { Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex, Timeout); } if (ACPI_FAILURE (Status)) { /* Includes failure from a timeout on TimeDesc */ return_ACPI_STATUS (Status); } /* Acquired the mutex: update mutex object */ ObjDesc->Mutex.ThreadId = ThreadId; ObjDesc->Mutex.AcquisitionDepth = 1; ObjDesc->Mutex.OriginalSyncLevel = 0; ObjDesc->Mutex.OwnerThread = NULL; /* Used only for AML Acquire() */ return_ACPI_STATUS (AE_OK); }
ACPI_STATUS AcpiDsBeginMethodExecution ( ACPI_NAMESPACE_NODE *MethodNode, ACPI_OPERAND_OBJECT *ObjDesc, ACPI_WALK_STATE *WalkState) { ACPI_STATUS Status = AE_OK; ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode); if (!MethodNode) { return_ACPI_STATUS (AE_NULL_ENTRY); } /* Prevent wraparound of thread count */ if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX) { ACPI_ERROR ((AE_INFO, "Method reached maximum reentrancy limit (255)")); return_ACPI_STATUS (AE_AML_METHOD_LIMIT); } /* * If this method is serialized, we need to acquire the method mutex. */ if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED) { /* * Create a mutex for the method if it is defined to be Serialized * and a mutex has not already been created. We defer the mutex creation * until a method is actually executed, to minimize the object count */ if (!ObjDesc->Method.Mutex) { Status = AcpiDsCreateMethodMutex (ObjDesc); if (ACPI_FAILURE (Status)) { return_ACPI_STATUS (Status); } } /* * The CurrentSyncLevel (per-thread) must be less than or equal to * the sync level of the method. This mechanism provides some * deadlock prevention * * Top-level method invocation has no walk state at this point */ if (WalkState && (WalkState->Thread->CurrentSyncLevel > ObjDesc->Method.Mutex->Mutex.SyncLevel)) { ACPI_ERROR ((AE_INFO, "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%u)", AcpiUtGetNodeName (MethodNode), WalkState->Thread->CurrentSyncLevel)); return_ACPI_STATUS (AE_AML_MUTEX_ORDER); } /* * Obtain the method mutex if necessary. Do not acquire mutex for a * recursive call. */ if (!WalkState || !ObjDesc->Method.Mutex->Mutex.ThreadId || (WalkState->Thread->ThreadId != ObjDesc->Method.Mutex->Mutex.ThreadId)) { /* * Acquire the method mutex. This releases the interpreter if we * block (and reacquires it before it returns) */ Status = AcpiExSystemWaitMutex (ObjDesc->Method.Mutex->Mutex.OsMutex, ACPI_WAIT_FOREVER); if (ACPI_FAILURE (Status)) { return_ACPI_STATUS (Status); } /* Update the mutex and walk info and save the original SyncLevel */ if (WalkState) { ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel = WalkState->Thread->CurrentSyncLevel; ObjDesc->Method.Mutex->Mutex.ThreadId = WalkState->Thread->ThreadId; WalkState->Thread->CurrentSyncLevel = ObjDesc->Method.SyncLevel; } else { ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel = ObjDesc->Method.Mutex->Mutex.SyncLevel; } } /* Always increase acquisition depth */ ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++; } /* * Allocate an Owner ID for this method, only if this is the first thread * to begin concurrent execution. We only need one OwnerId, even if the * method is invoked recursively. */ if (!ObjDesc->Method.OwnerId) { Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId); if (ACPI_FAILURE (Status)) { goto Cleanup; } } /* * Increment the method parse tree thread count since it has been * reentered one more time (even if it is the same thread) */ ObjDesc->Method.ThreadCount++; AcpiMethodCount++; return_ACPI_STATUS (Status); Cleanup: /* On error, must release the method mutex (if present) */ if (ObjDesc->Method.Mutex) { AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex); } return_ACPI_STATUS (Status); }
ACPI_STATUS AcpiEvAcquireGlobalLock ( UINT16 Timeout) { ACPI_CPU_FLAGS Flags; ACPI_STATUS Status; BOOLEAN Acquired = FALSE; ACPI_FUNCTION_TRACE (EvAcquireGlobalLock); /* * Only one thread can acquire the GL at a time, the GlobalLockMutex * enforces this. This interface releases the interpreter if we must wait. */ Status = AcpiExSystemWaitMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex, Timeout); if (ACPI_FAILURE (Status)) { return_ACPI_STATUS (Status); } /* * Update the global lock handle and check for wraparound. The handle is * only used for the external global lock interfaces, but it is updated * here to properly handle the case where a single thread may acquire the * lock via both the AML and the AcpiAcquireGlobalLock interfaces. The * handle is therefore updated on the first acquire from a given thread * regardless of where the acquisition request originated. */ AcpiGbl_GlobalLockHandle++; if (AcpiGbl_GlobalLockHandle == 0) { AcpiGbl_GlobalLockHandle = 1; } /* * Make sure that a global lock actually exists. If not, just * treat the lock as a standard mutex. */ if (!AcpiGbl_GlobalLockPresent) { AcpiGbl_GlobalLockAcquired = TRUE; return_ACPI_STATUS (AE_OK); } Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock); do { /* Attempt to acquire the actual hardware lock */ ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_FACS, Acquired); if (Acquired) { AcpiGbl_GlobalLockAcquired = TRUE; ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Acquired hardware Global Lock\n")); break; } /* * Did not get the lock. The pending bit was set above, and * we must now wait until we receive the global lock * released interrupt. */ AcpiGbl_GlobalLockPending = TRUE; AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags); ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Waiting for hardware Global Lock\n")); /* * Wait for handshake with the global lock interrupt handler. * This interface releases the interpreter if we must wait. */ Status = AcpiExSystemWaitSemaphore (AcpiGbl_GlobalLockSemaphore, ACPI_WAIT_FOREVER); Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock); } while (ACPI_SUCCESS (Status)); AcpiGbl_GlobalLockPending = FALSE; AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags); return_ACPI_STATUS (Status); }
ACPI_STATUS AcpiExAcquireMutex ( ACPI_OPERAND_OBJECT *TimeDesc, ACPI_OPERAND_OBJECT *ObjDesc, ACPI_WALK_STATE *WalkState) { ACPI_STATUS Status; ACPI_FUNCTION_TRACE_PTR (ExAcquireMutex, ObjDesc); if (!ObjDesc) { return_ACPI_STATUS (AE_BAD_PARAMETER); } /* Sanity check: we must have a valid thread ID */ if (!WalkState->Thread) { ACPI_ERROR ((AE_INFO, "Cannot acquire Mutex [%4.4s], null thread info", AcpiUtGetNodeName (ObjDesc->Mutex.Node))); return_ACPI_STATUS (AE_AML_INTERNAL); } /* * Current Sync must be less than or equal to the sync level of the * mutex. This mechanism provides some deadlock prevention */ if (WalkState->Thread->CurrentSyncLevel > ObjDesc->Mutex.SyncLevel) { ACPI_ERROR ((AE_INFO, "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)", AcpiUtGetNodeName (ObjDesc->Mutex.Node), WalkState->Thread->CurrentSyncLevel)); return_ACPI_STATUS (AE_AML_MUTEX_ORDER); } /* Support for multiple acquires by the owning thread */ if (ObjDesc->Mutex.OwnerThread) { if (ObjDesc->Mutex.OwnerThread->ThreadId == WalkState->Thread->ThreadId) { /* * The mutex is already owned by this thread, just increment the * acquisition depth */ ObjDesc->Mutex.AcquisitionDepth++; return_ACPI_STATUS (AE_OK); } } /* Acquire the mutex, wait if necessary. Special case for Global Lock */ if (ObjDesc->Mutex.OsMutex == AcpiGbl_GlobalLockMutex) { Status = AcpiEvAcquireGlobalLock ((UINT16) TimeDesc->Integer.Value); } else { Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex, (UINT16) TimeDesc->Integer.Value); } if (ACPI_FAILURE (Status)) { /* Includes failure from a timeout on TimeDesc */ return_ACPI_STATUS (Status); } /* Have the mutex: update mutex and walk info and save the SyncLevel */ ObjDesc->Mutex.OwnerThread = WalkState->Thread; ObjDesc->Mutex.AcquisitionDepth = 1; ObjDesc->Mutex.OriginalSyncLevel = WalkState->Thread->CurrentSyncLevel; WalkState->Thread->CurrentSyncLevel = ObjDesc->Mutex.SyncLevel; /* Link the mutex to the current thread for force-unlock at method exit */ AcpiExLinkMutex (ObjDesc, WalkState->Thread); return_ACPI_STATUS (AE_OK); }