Hash :
25ce3dfe
Author :
Date :
2023-06-29T15:51:55
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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// GlobalMutex.cpp: Defines Global Mutex and utilities.
#include "libANGLE/GlobalMutex.h"
#include <atomic>
#include "common/debug.h"
#include "common/system_utils.h"
namespace egl
{
namespace priv
{
using GlobalMutexType = std::mutex;
#if !defined(ANGLE_ENABLE_ASSERTS) && !defined(ANGLE_ENABLE_GLOBAL_MUTEX_RECURSION)
// Default version.
class GlobalMutex final : angle::NonCopyable
{
public:
ANGLE_INLINE void lock() { mMutex.lock(); }
ANGLE_INLINE void unlock() { mMutex.unlock(); }
protected:
GlobalMutexType mMutex;
};
#endif
#if defined(ANGLE_ENABLE_ASSERTS) && !defined(ANGLE_ENABLE_GLOBAL_MUTEX_RECURSION)
// Debug version.
class GlobalMutex final : angle::NonCopyable
{
public:
ANGLE_INLINE void lock()
{
const angle::ThreadId threadId = angle::GetCurrentThreadId();
ASSERT(getOwnerThreadId() != threadId);
mMutex.lock();
ASSERT(getOwnerThreadId() == angle::InvalidThreadId());
mOwnerThreadId.store(threadId, std::memory_order_relaxed);
}
ANGLE_INLINE void unlock()
{
ASSERT(getOwnerThreadId() == angle::GetCurrentThreadId());
mOwnerThreadId.store(angle::InvalidThreadId(), std::memory_order_relaxed);
mMutex.unlock();
}
private:
ANGLE_INLINE angle::ThreadId getOwnerThreadId() const
{
return mOwnerThreadId.load(std::memory_order_relaxed);
}
GlobalMutexType mMutex;
std::atomic<angle::ThreadId> mOwnerThreadId{angle::InvalidThreadId()};
};
#endif // defined(ANGLE_ENABLE_ASSERTS) && !defined(ANGLE_ENABLE_GLOBAL_MUTEX_RECURSION)
#if defined(ANGLE_ENABLE_GLOBAL_MUTEX_RECURSION)
// Recursive version.
class GlobalMutex final : angle::NonCopyable
{
public:
ANGLE_INLINE void lock()
{
const angle::ThreadId threadId = angle::GetCurrentThreadId();
if (ANGLE_UNLIKELY(!mMutex.try_lock()))
{
if (ANGLE_UNLIKELY(getOwnerThreadId() == threadId))
{
ASSERT(mLockLevel > 0);
++mLockLevel;
return;
}
mMutex.lock();
}
ASSERT(getOwnerThreadId() == angle::InvalidThreadId());
ASSERT(mLockLevel == 0);
mOwnerThreadId.store(threadId, std::memory_order_relaxed);
mLockLevel = 1;
}
ANGLE_INLINE void unlock()
{
ASSERT(getOwnerThreadId() == angle::GetCurrentThreadId());
ASSERT(mLockLevel > 0);
if (ANGLE_LIKELY(--mLockLevel == 0))
{
mOwnerThreadId.store(angle::InvalidThreadId(), std::memory_order_relaxed);
mMutex.unlock();
}
}
private:
ANGLE_INLINE angle::ThreadId getOwnerThreadId() const
{
return mOwnerThreadId.load(std::memory_order_relaxed);
}
GlobalMutexType mMutex;
std::atomic<angle::ThreadId> mOwnerThreadId{angle::InvalidThreadId()};
uint32_t mLockLevel = 0;
};
#endif // defined(ANGLE_ENABLE_GLOBAL_MUTEX_RECURSION)
} // namespace priv
namespace
{
#if defined(ANGLE_ENABLE_GLOBAL_MUTEX_LOAD_TIME_ALLOCATE)
# if !ANGLE_HAS_ATTRIBUTE_CONSTRUCTOR || !ANGLE_HAS_ATTRIBUTE_DESTRUCTOR
# error \
"'angle_enable_global_mutex_load_time_allocate' " \
"requires constructor/destructor compiler atributes."
# endif
priv::GlobalMutex *g_MutexPtr = nullptr;
void ANGLE_CONSTRUCTOR AllocateGlobalMutex()
{
ASSERT(g_MutexPtr == nullptr);
g_MutexPtr = new priv::GlobalMutex();
}
void ANGLE_DESTRUCTOR DeallocateGlobalMutex()
{
SafeDelete(g_MutexPtr);
}
#else
ANGLE_REQUIRE_CONSTANT_INIT std::atomic<priv::GlobalMutex *> g_Mutex(nullptr);
static_assert(std::is_trivially_destructible<decltype(g_Mutex)>::value,
"global mutex is not trivially destructible");
priv::GlobalMutex *AllocateGlobalMutexImpl()
{
priv::GlobalMutex *currentMutex = nullptr;
std::unique_ptr<priv::GlobalMutex> newMutex(new priv::GlobalMutex());
do
{
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();
return mutex != nullptr ? mutex : AllocateGlobalMutexImpl();
}
#endif
} // anonymous namespace
// ScopedGlobalMutexLock implementation.
#if defined(ANGLE_ENABLE_GLOBAL_MUTEX_LOAD_TIME_ALLOCATE)
ScopedGlobalMutexLock::ScopedGlobalMutexLock()
{
g_MutexPtr->lock();
}
ScopedGlobalMutexLock::~ScopedGlobalMutexLock()
{
g_MutexPtr->unlock();
}
#else
ScopedGlobalMutexLock::ScopedGlobalMutexLock() : mMutex(*GetGlobalMutex())
{
mMutex.lock();
}
ScopedGlobalMutexLock::~ScopedGlobalMutexLock()
{
mMutex.unlock();
}
#endif
// ScopedOptionalGlobalMutexLock implementation.
ScopedOptionalGlobalMutexLock::ScopedOptionalGlobalMutexLock(bool enabled)
{
if (enabled)
{
#if defined(ANGLE_ENABLE_GLOBAL_MUTEX_LOAD_TIME_ALLOCATE)
mMutex = g_MutexPtr;
#else
mMutex = GetGlobalMutex();
#endif
mMutex->lock();
}
else
{
mMutex = nullptr;
}
}
ScopedOptionalGlobalMutexLock::~ScopedOptionalGlobalMutexLock()
{
if (mMutex != nullptr)
{
mMutex->unlock();
}
}
// Global functions.
#if defined(ANGLE_PLATFORM_WINDOWS) && !defined(ANGLE_STATIC)
# if defined(ANGLE_ENABLE_GLOBAL_MUTEX_LOAD_TIME_ALLOCATE)
# error "'angle_enable_global_mutex_load_time_allocate' is not supported in Windows DLL."
# endif
void AllocateGlobalMutex()
{
(void)AllocateGlobalMutexImpl();
}
void DeallocateGlobalMutex()
{
priv::GlobalMutex *mutex = g_Mutex.exchange(nullptr);
if (mutex != nullptr)
{
{
// Wait for the mutex to become released by other threads before deleting.
std::lock_guard<priv::GlobalMutex> lock(*mutex);
}
delete mutex;
}
}
#endif
} // namespace egl