discord-jadx/app/src/main/java/org/webrtc/RefCountDelegate.java

43 lines
1.3 KiB
Java

package org.webrtc;
import androidx.annotation.Nullable;
import java.util.concurrent.atomic.AtomicInteger;
public class RefCountDelegate implements RefCounted {
private final AtomicInteger refCount = new AtomicInteger(1);
@Nullable
private final Runnable releaseCallback;
public RefCountDelegate(@Nullable Runnable runnable) {
this.releaseCallback = runnable;
}
@Override // org.webrtc.RefCounted
public void release() {
Runnable runnable;
int decrementAndGet = this.refCount.decrementAndGet();
if (decrementAndGet < 0) {
throw new IllegalStateException("release() called on an object with refcount < 1");
} else if (decrementAndGet == 0 && (runnable = this.releaseCallback) != null) {
runnable.run();
}
}
@Override // org.webrtc.RefCounted
public void retain() {
if (this.refCount.incrementAndGet() < 2) {
throw new IllegalStateException("retain() called on an object with refcount < 1");
}
}
public boolean safeRetain() {
int i = this.refCount.get();
while (i != 0) {
if (this.refCount.weakCompareAndSet(i, i + 1)) {
return true;
}
i = this.refCount.get();
}
return false;
}
}