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:
Perry Jiang 2020-01-21 13:33:04 +00:00
Родитель ab5a99dd15
Коммит c635cee728
1 изменённых файлов: 14 добавлений и 9 удалений

Просмотреть файл

@ -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,12 +175,11 @@ 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()) {
if (aOwner->hasOneRef()) {
mPtr = nullptr;
}
mLock.writeUnlock();
mLock.writeLock();
if (aOwner->hasOneRef()) {
mPtr = nullptr;
}
mLock.writeUnlock();
}
ReadWriteSpinLock mLock;