Skip to content

Commit

Permalink
Revert: Use ReentrantLock i.o. Thread#yield
Browse files Browse the repository at this point in the history
Reverting to be able to make a clear pull request to add this
functionality as we've tested it to improve CPU.

#enhancement/reentrant-lock-io-yield
  • Loading branch information
smcvb committed Oct 21, 2024
1 parent eebb047 commit 8f915e2
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.grpc.stub.ClientCallStreamObserver;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;

/**
* Lock-based synchronized implementation of a {@link ClientCallStreamObserver}. Acts as a wrapper of another {@code
Expand All @@ -31,7 +30,7 @@
public class SynchronizedRequestStream<T> extends ClientCallStreamObserver<T> {

private final ClientCallStreamObserver<T> delegate;
private final ReentrantLock lock = new ReentrantLock();
private final AtomicBoolean lock = new AtomicBoolean(false);
private final AtomicBoolean halfClosed = new AtomicBoolean(false);

/**
Expand Down Expand Up @@ -101,13 +100,15 @@ public void onCompleted() {
}

private void inLock(Runnable action) {
while (!lock.compareAndSet(false, true)) {
Thread.yield();
}
try {
lock.lock();
if (!halfClosed.get()) {
action.run();
}
} finally {
lock.unlock();
lock.set(false);
}
}
}

0 comments on commit 8f915e2

Please sign in to comment.