Hash :
e5619a5c
Author :
Date :
2024-10-15T18:27:00
Use EGL sync global lock for all EGL*sync entrypoint calls To free EGL sync operations from waiting for other EGL calls to finish, we use a separate global lock for EGL*sync entrypoints. Below angle::SimpleMutex are added to protect resources that may have race condition due to being accessed by EGL*sync calls and non EGL*sync calls at the same time: 1. Display::mContextMapMutex that protects Display::mState.contextMap 2. static angle::base::NoDestructor<angle::SimpleMutex> anglePlatformDisplayMapMutex that protects static angle::base::NoDestructor<ANGLEPlatformDisplayMap> displays 3. static angle::base::NoDestructor<angle::SimpleMutex> devicePlatformDisplayMapMutex that protects static angle::base::NoDestructor<DevicePlatformDisplayMap> displays EGL_Terminate() entry point takes both global lock and global egl sync lock. This is to protect Display::mSyncMap, Display::mSyncPools, and Display::mSyncHandleAllocator being get cleared by thread 1 calling eglTerminate, while they are still accessed by thread 2 through a call such as eglCreateSync. So that we won't have thread 2 finish validating the sync object with syncID exists in Display::mSyncMap, but then find the mSyncMap.find(syncID) returns a nullptr due to the mSyncMap is cleared by thread 1. Same applies to EGL_LabelObjectKHR(), EGL_ReleaseThread(), ThreadCleanupCallback(). EGL_Initialize() writes to Display::mInitialized. This is read by EGL Sync API validation functions. EGL_Initialize() also takes both global lock and global sync lock to prevent race conditions between EGL_Initialize() and EGL Sync APIs. When ANGLE_CAPTURE_ENABLED is enabled, fall back to global lock, as the CaptureEGLCallToFrameCapture() touches many resources (e.g. mMaxAccessedResourceIDs, mFrameCalls) that could lead to race conditions without a global lock. Bug: b/362604439 Change-Id: Ic0d54a4cd66743bcd0f48f41f247dd223cff2f5e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5933570 Reviewed-by: Roman Lavrov <romanl@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Igor Nazarov <i.nazarov@samsung.com> Commit-Queue: Yuxin Hu <yuxinhu@google.com>
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
//
// 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
{
#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
GlobalMutex *g_MutexPtr = nullptr;
GlobalMutex *g_EGLSyncMutexPtr = nullptr;
void ANGLE_CONSTRUCTOR AllocateGlobalMutex()
{
ASSERT(g_MutexPtr == nullptr);
g_MutexPtr = new GlobalMutex();
ASSERT(g_EGLSyncMutexPtr == nullptr);
g_EGLSyncMutexPtr = new GlobalMutex();
}
void ANGLE_DESTRUCTOR DeallocateGlobalMutex()
{
SafeDelete(g_MutexPtr);
SafeDelete(g_EGLSyncMutexPtr);
}
#else
ANGLE_REQUIRE_CONSTANT_INIT std::atomic<GlobalMutex *> g_Mutex(nullptr);
ANGLE_REQUIRE_CONSTANT_INIT std::atomic<GlobalMutex *> g_EGLSyncMutex(nullptr);
static_assert(std::is_trivially_destructible<decltype(g_Mutex)>::value,
"global mutex is not trivially destructible");
static_assert(std::is_trivially_destructible<decltype(g_EGLSyncMutex)>::value,
"global EGL Sync mutex is not trivially destructible");
GlobalMutex *AllocateGlobalMutexImpl(std::atomic<GlobalMutex *> *globalMutex)
{
GlobalMutex *currentMutex = nullptr;
std::unique_ptr<GlobalMutex> newMutex(new GlobalMutex());
do
{
if (globalMutex->compare_exchange_weak(currentMutex, newMutex.get()))
{
return newMutex.release();
}
} while (currentMutex == nullptr);
return currentMutex;
}
GlobalMutex *GetGlobalMutex()
{
GlobalMutex *mutex = g_Mutex.load();
return mutex != nullptr ? mutex : AllocateGlobalMutexImpl(&g_Mutex);
}
GlobalMutex *GetGlobalEGLSyncObjectMutex()
{
GlobalMutex *mutex = g_EGLSyncMutex.load();
return mutex != nullptr ? mutex : AllocateGlobalMutexImpl(&g_EGLSyncMutex);
}
#endif
} // anonymous namespace
// ScopedGlobalMutexLock implementation.
#if defined(ANGLE_ENABLE_GLOBAL_MUTEX_LOAD_TIME_ALLOCATE)
template <GlobalMutexChoice mutexChoice>
ScopedGlobalMutexLock<mutexChoice>::ScopedGlobalMutexLock()
{
switch (mutexChoice)
{
case GlobalMutexChoice::EGL:
g_MutexPtr->lock();
break;
case GlobalMutexChoice::Sync:
g_EGLSyncMutexPtr->lock();
break;
default:
UNREACHABLE();
break;
}
}
template <GlobalMutexChoice mutexChoice>
ScopedGlobalMutexLock<mutexChoice>::~ScopedGlobalMutexLock()
{
switch (mutexChoice)
{
case GlobalMutexChoice::EGL:
g_MutexPtr->unlock();
break;
case GlobalMutexChoice::Sync:
g_EGLSyncMutexPtr->unlock();
break;
default:
UNREACHABLE();
break;
}
}
#else
template <GlobalMutexChoice mutexChoice>
ScopedGlobalMutexLock<mutexChoice>::ScopedGlobalMutexLock()
{
switch (mutexChoice)
{
case GlobalMutexChoice::EGL:
mMutex = GetGlobalMutex();
break;
case GlobalMutexChoice::Sync:
mMutex = GetGlobalEGLSyncObjectMutex();
break;
default:
UNREACHABLE();
break;
}
mMutex->lock();
}
template <GlobalMutexChoice mutexChoice>
ScopedGlobalMutexLock<mutexChoice>::~ScopedGlobalMutexLock()
{
mMutex->unlock();
}
#endif
template class ScopedGlobalMutexLock<GlobalMutexChoice::EGL>;
template class ScopedGlobalMutexLock<GlobalMutexChoice::Sync>;
} // namespace priv
// ScopedOptionalGlobalMutexLock implementation.
ScopedOptionalGlobalMutexLock::ScopedOptionalGlobalMutexLock(bool enabled)
{
if (enabled)
{
#if defined(ANGLE_ENABLE_GLOBAL_MUTEX_LOAD_TIME_ALLOCATE)
mMutex = priv::g_MutexPtr;
#else
mMutex = priv::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)priv::AllocateGlobalMutexImpl(&priv::g_Mutex);
(void)priv::AllocateGlobalMutexImpl(&priv::g_EGLSyncMutex);
}
void DeallocateGlobalMutexImpl(std::atomic<priv::GlobalMutex *> *globalMutex)
{
priv::GlobalMutex *mutex = globalMutex->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;
}
}
void DeallocateGlobalMutex()
{
DeallocateGlobalMutexImpl(&priv::g_Mutex);
DeallocateGlobalMutexImpl(&priv::g_EGLSyncMutex);
}
#endif
} // namespace egl