yashmayya commented on code in PR #14847:
URL: https://github.com/apache/pinot/pull/14847#discussion_r1927125996
##########
pinot-common/src/main/java/org/apache/pinot/common/concurrency/AdjustableSemaphore.java:
##########
@@ -20,32 +20,44 @@
import com.google.common.base.Preconditions;
import java.util.concurrent.Semaphore;
+import java.util.concurrent.atomic.AtomicInteger;
/**
* A semaphore that allows adjusting the number of permits in a non-blocking
way.
*/
public class AdjustableSemaphore extends Semaphore {
- private int _totalPermits;
+ private final AtomicInteger _totalPermits;
public AdjustableSemaphore(int permits) {
super(permits);
- _totalPermits = permits;
+ _totalPermits = new AtomicInteger(permits);
}
public AdjustableSemaphore(int permits, boolean fair) {
super(permits, fair);
- _totalPermits = permits;
+ _totalPermits = new AtomicInteger(permits);
}
+ /**
+ * Sets the total number of permits to the given value without blocking.
+ */
public void setPermits(int permits) {
Preconditions.checkArgument(permits > 0, "Permits must be a positive
integer");
- if (permits < _totalPermits) {
- reducePermits(_totalPermits - permits);
- } else if (permits > _totalPermits) {
- release(permits - _totalPermits);
+ if (permits < _totalPermits.get()) {
+ reducePermits(_totalPermits.get() - permits);
+ } else if (permits > _totalPermits.get()) {
+ release(permits - _totalPermits.get());
Review Comment:
Interesting, thanks for that info!
> The only actual issue is if that attribute ends up creating false sharing
with another, frequently written, not volatile attribute
Wouldn't the other attribute also need to be volatile?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]