Hash :
4b724130
Author :
Date :
2023-06-27T18:08:27
Rename SharedContexMutex into ContexMutex
Follow up after:
Replace (Single/Shared)ContextMutex classed with ContextMutex
Renamed build option:
angle_enable_shared_context_mutex -> angle_enable_context_mutex
Renamed because there is no more SharedContexMutex class and
ContextMutex is now used for both Shared and not Shared Contexts.
Bug: angleproject:8226
Change-Id: I68eea84aa59441d9c5b19870910b2bb499311e08
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4650350
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Igor Nazarov <i.nazarov@samsung.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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
//
// 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.
//
// ContextMutex.cpp: Classes for protecting Context access and EGLImage siblings.
#include "libANGLE/ContextMutex.h"
#include "common/system_utils.h"
#include "libANGLE/Context.h"
namespace egl
{
namespace
{
[[maybe_unused]] bool CheckThreadIdCurrent(const std::atomic<angle::ThreadId> &threadId,
angle::ThreadId *currentThreadIdOut)
{
*currentThreadIdOut = angle::GetCurrentThreadId();
return (threadId.load(std::memory_order_relaxed) == *currentThreadIdOut);
}
[[maybe_unused]] bool TryUpdateThreadId(std::atomic<angle::ThreadId> *threadId,
angle::ThreadId oldThreadId,
angle::ThreadId newThreadId)
{
const bool ok = (threadId->load(std::memory_order_relaxed) == oldThreadId);
if (ok)
{
threadId->store(newThreadId, std::memory_order_relaxed);
}
return ok;
}
} // namespace
// ScopedContextMutexAddRefLock
void ScopedContextMutexAddRefLock::lock(ContextMutex *mutex)
{
ASSERT(mutex != nullptr);
ASSERT(mMutex == nullptr);
// lock() before addRef() - using mMutex as synchronization
mutex->lock();
// Take the "root" mutex after the lock.
mMutex = mutex->getRoot();
ASSERT(mMutex->isReferenced());
mMutex->addRef();
}
// ContextMutex
ContextMutex::ContextMutex(ContextMutex *root)
: mRoot(this), mOwnerThreadId(angle::InvalidThreadId()), mLockLevel(0), mRefCount(0), mRank(0)
{
if (root != nullptr)
{
setNewRoot(root);
}
}
ContextMutex::~ContextMutex()
{
ASSERT(mLockLevel == 0);
ASSERT(mRefCount == 0);
ASSERT(mLeaves.empty());
ContextMutex *const root = getRoot();
if (this == root)
{
ASSERT(mOldRoots.empty());
}
else
{
for (ContextMutex *oldRoot : mOldRoots)
{
ASSERT(oldRoot->getRoot() == root);
ASSERT(oldRoot->mLeaves.empty());
oldRoot->release();
}
root->removeLeaf(this);
root->release();
}
}
void ContextMutex::Merge(ContextMutex *lockedMutex, ContextMutex *otherMutex)
{
ASSERT(lockedMutex != nullptr);
ASSERT(otherMutex != nullptr);
// Since lockedMutex is locked, its "root" pointer is stable.
ContextMutex *lockedRoot = lockedMutex->getRoot();
ContextMutex *otherLockedRoot = nullptr;
// Mutex merging will update the structure of both mutexes, therefore both mutexes must be
// locked before continuing. First mutex is already locked, need to lock the other mutex.
// Because other thread may perform merge with same mutexes reversed, we can't simply lock
// otherMutex - this may cause a deadlock. Additionally, otherMutex may have same "root" (same
// mutex or already merged), not only merging is unnecessary, but locking otherMutex will
// guarantee a deadlock.
for (;;)
{
// First, check that "root" of otherMutex is the same as "root" of lockedMutex.
// lockedRoot is stable by definition and it is safe to compare with "unstable root".
ContextMutex *otherRoot = otherMutex->getRoot();
if (otherRoot == lockedRoot)
{
// Do nothing if two mutexes are the same/merged.
return;
}
// Second, try to lock otherMutex "root" (can't use lock()/lockImpl(), see above comment).
if (otherRoot->tryLockImpl())
{
otherLockedRoot = otherRoot->getRoot();
// otherMutex "root" can't become lockedMutex "root". For that to happen, lockedMutex
// must be locked from some other thread first, which is impossible, since it is already
// locked by this thread.
ASSERT(otherLockedRoot != lockedRoot);
// Lock is successful. Both mutexes are locked - can proceed with the merge...
break;
}
// Lock was unsuccessful - unlock and retry...
// May use "unlockImpl()" because lockedRoot is a "stable root" mutex.
// Note: lock will be preserved in case of the recursive lock.
lockedRoot->unlockImpl();
// Sleep random amount to allow one of the thread acquire the lock next time...
std::this_thread::sleep_for(std::chrono::microseconds(rand() % 91 + 10));
// Because lockedMutex was unlocked, its "root" might have been changed. Below line will
// reacquire the lock and update lockedRoot pointer.
lockedMutex->lock();
lockedRoot = lockedMutex->getRoot();
}
// Decide the new "root". See mRank comment for more details...
ContextMutex *oldRoot = otherLockedRoot;
ContextMutex *newRoot = lockedRoot;
if (oldRoot->mRank > newRoot->mRank)
{
std::swap(oldRoot, newRoot);
}
else if (oldRoot->mRank == newRoot->mRank)
{
++newRoot->mRank;
}
ASSERT(newRoot->isReferenced());
// Update the structure
for (ContextMutex *const leaf : oldRoot->mLeaves)
{
ASSERT(leaf->getRoot() == oldRoot);
leaf->setNewRoot(newRoot);
}
oldRoot->mLeaves.clear();
oldRoot->setNewRoot(newRoot);
// Leave only the "merged" mutex locked. "oldRoot" already merged, need to use "unlockImpl()"
oldRoot->unlockImpl();
// Merge from recursive lock is unexpected. Handle such cases anyway to be safe.
while (oldRoot->mLockLevel > 0)
{
newRoot->lockImpl();
oldRoot->unlockImpl();
}
}
void ContextMutex::setNewRoot(ContextMutex *newRoot)
{
ContextMutex *const oldRoot = getRoot();
ASSERT(newRoot != oldRoot);
mRoot.store(newRoot, std::memory_order_relaxed);
newRoot->addRef();
newRoot->addLeaf(this);
if (oldRoot != this)
{
mOldRoots.emplace_back(oldRoot);
}
}
void ContextMutex::addLeaf(ContextMutex *leaf)
{
ASSERT(this == getRoot());
ASSERT(leaf->getRoot() == this);
ASSERT(leaf->mLeaves.empty());
ASSERT(mLeaves.count(leaf) == 0);
mLeaves.emplace(leaf);
}
void ContextMutex::removeLeaf(ContextMutex *leaf)
{
ASSERT(this == getRoot());
ASSERT(leaf->getRoot() == this);
ASSERT(leaf->mLeaves.empty());
ASSERT(mLeaves.count(leaf) == 1);
mLeaves.erase(leaf);
}
void ContextMutex::release(UnlockBehaviour unlockBehaviour)
{
ASSERT(isReferenced());
const bool needDelete = (--mRefCount == 0);
if (unlockBehaviour == UnlockBehaviour::kUnlock)
{
ASSERT(this == getRoot());
unlockImpl();
}
if (needDelete)
{
delete this;
}
}
bool ContextMutex::try_lock()
{
return getRoot()->tryLockImpl();
}
void ContextMutex::lock()
{
getRoot()->lockImpl();
}
void ContextMutex::unlock()
{
ContextMutex *const root = getRoot();
// "root" is currently locked so "root->getRoot()" will return stable result.
ASSERT(root == root->getRoot());
root->unlockImpl();
}
#if defined(ANGLE_ENABLE_CONTEXT_MUTEX_RECURSION)
bool ContextMutex::tryLockImpl()
{
const angle::ThreadId threadId = angle::GetCurrentThreadId();
if (ANGLE_UNLIKELY(!mMutex.try_lock()))
{
if (ANGLE_UNLIKELY(mOwnerThreadId.load(std::memory_order_relaxed) == threadId))
{
ASSERT(this == getRoot());
ASSERT(mLockLevel > 0);
++mLockLevel;
return true;
}
return false;
}
ASSERT(mOwnerThreadId.load(std::memory_order_relaxed) == angle::InvalidThreadId());
ASSERT(mLockLevel == 0);
ContextMutex *const root = getRoot();
if (ANGLE_UNLIKELY(this != root))
{
// Unlock, so only the "stable root" mutex remains locked
mMutex.unlock();
return root->tryLockImpl();
}
mOwnerThreadId.store(threadId, std::memory_order_relaxed);
mLockLevel = 1;
return true;
}
void ContextMutex::lockImpl()
{
const angle::ThreadId threadId = angle::GetCurrentThreadId();
if (ANGLE_UNLIKELY(!mMutex.try_lock()))
{
if (ANGLE_UNLIKELY(mOwnerThreadId.load(std::memory_order_relaxed) == threadId))
{
ASSERT(this == getRoot());
ASSERT(mLockLevel > 0);
++mLockLevel;
return;
}
mMutex.lock();
}
ASSERT(mOwnerThreadId.load(std::memory_order_relaxed) == angle::InvalidThreadId());
ASSERT(mLockLevel == 0);
ContextMutex *const root = getRoot();
if (ANGLE_UNLIKELY(this != root))
{
// Unlock, so only the "stable root" mutex remains locked
mMutex.unlock();
root->lockImpl();
}
else
{
mOwnerThreadId.store(threadId, std::memory_order_relaxed);
mLockLevel = 1;
}
}
void ContextMutex::unlockImpl()
{
ASSERT(mOwnerThreadId.load(std::memory_order_relaxed) == angle::GetCurrentThreadId());
ASSERT(mLockLevel > 0);
if (ANGLE_LIKELY(--mLockLevel == 0))
{
mOwnerThreadId.store(angle::InvalidThreadId(), std::memory_order_relaxed);
mMutex.unlock();
}
}
#else
bool ContextMutex::tryLockImpl()
{
angle::ThreadId currentThreadId;
ASSERT(!CheckThreadIdCurrent(mOwnerThreadId, ¤tThreadId));
if (mMutex.try_lock())
{
ContextMutex *const root = getRoot();
if (ANGLE_UNLIKELY(this != root))
{
// Unlock, so only the "stable root" mutex remains locked
mMutex.unlock();
return root->tryLockImpl();
}
ASSERT(TryUpdateThreadId(&mOwnerThreadId, angle::InvalidThreadId(), currentThreadId));
return true;
}
return false;
}
void ContextMutex::lockImpl()
{
angle::ThreadId currentThreadId;
ASSERT(!CheckThreadIdCurrent(mOwnerThreadId, ¤tThreadId));
mMutex.lock();
ContextMutex *const root = getRoot();
if (ANGLE_UNLIKELY(this != root))
{
// Unlock, so only the "stable root" mutex remains locked
mMutex.unlock();
root->lockImpl();
}
else
{
ASSERT(TryUpdateThreadId(&mOwnerThreadId, angle::InvalidThreadId(), currentThreadId));
}
}
void ContextMutex::unlockImpl()
{
ASSERT(
TryUpdateThreadId(&mOwnerThreadId, angle::GetCurrentThreadId(), angle::InvalidThreadId()));
mMutex.unlock();
}
#endif
} // namespace egl