Use compare_exchange_weak() in AllocateGlobalMutexImpl()

Speculative fix of the crash.

New logic will ensure that `currentMutex` is not `nullptr` when returned
from `AllocateGlobalMutexImpl()`

Also removed explicit memory order for safety (no actual difference in
generated code on ARM64/x86-64, but ARM generates less instructions).

Bug: chromium:1457915
Change-Id: I8d932ee499f9d8ee4e38ab2173f4f1cefd0aa294
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4650794
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
This commit is contained in:
Igor Nazarov
2023-06-29 15:51:55 +03:00
committed by Angle LUCI CQ
parent 20cc4a9bc2
commit 25ce3dfec6

View File

@@ -141,17 +141,19 @@ priv::GlobalMutex *AllocateGlobalMutexImpl()
{
priv::GlobalMutex *currentMutex = nullptr;
std::unique_ptr<priv::GlobalMutex> newMutex(new priv::GlobalMutex());
if (g_Mutex.compare_exchange_strong(currentMutex, newMutex.get(), std::memory_order_release,
std::memory_order_acquire))
do
{
return newMutex.release();
}
if (g_Mutex.compare_exchange_weak(currentMutex, newMutex.get()))
{
return newMutex.release();
}
} while (currentMutex == nullptr);
return currentMutex;
}
priv::GlobalMutex *GetGlobalMutex()
{
priv::GlobalMutex *mutex = g_Mutex.load(std::memory_order_acquire);
priv::GlobalMutex *mutex = g_Mutex.load();
return mutex != nullptr ? mutex : AllocateGlobalMutexImpl();
}
#endif