зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1601024 - ThreadSafeWeakReference::tryDetach must acquire its lock r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D60037 --HG-- extra : moz-landing-system : lando
This commit is contained in:
Родитель
ab5a99dd15
Коммит
c635cee728
|
@ -111,10 +111,16 @@ class ReadWriteSpinLock {
|
|||
// Decrement the counter to remove a read lock.
|
||||
void readUnlock() { mCounter--; }
|
||||
|
||||
// Try to acquire the write lock, but only if there are no readers.
|
||||
// If successful, sets the counter to a negative value.
|
||||
bool tryWriteLock() {
|
||||
return mCounter.compareExchange(0, std::numeric_limits<CounterType>::min());
|
||||
// Spins until a write lock is acquired. This can only occur if there are no
|
||||
// readers or writers. Once it is acquired, the counter is set to a negative
|
||||
// value.
|
||||
void writeLock() {
|
||||
while (true) {
|
||||
if (mCounter.compareExchange(0,
|
||||
std::numeric_limits<CounterType>::min())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the counter to 0.
|
||||
|
@ -169,13 +175,12 @@ class ThreadSafeWeakReference
|
|||
// to check the refcount and verify that this is the last reference to
|
||||
// the tracked object, so the weak reference can be safely detached.
|
||||
void tryDetach(const SupportsThreadSafeWeakPtr<T>* aOwner) {
|
||||
if (mLock.tryWriteLock()) {
|
||||
mLock.writeLock();
|
||||
if (aOwner->hasOneRef()) {
|
||||
mPtr = nullptr;
|
||||
}
|
||||
mLock.writeUnlock();
|
||||
}
|
||||
}
|
||||
|
||||
ReadWriteSpinLock mLock;
|
||||
Atomic<T*> mPtr;
|
||||
|
|
Загрузка…
Ссылка в новой задаче