Github user geomacy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/520#discussion_r97600990
--- Diff:
core/src/main/java/org/apache/brooklyn/core/sensor/ReleaseableLatch.java ---
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.sensor;
+
+import java.util.concurrent.Semaphore;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.util.core.task.DeferredSupplier;
+import org.apache.brooklyn.util.core.task.ImmediateSupplier;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
+
+// DeferredSupplier used as a marker interface to prevent coercion. When
resolved it must evaluate to {@code Boolean.TRUE}.
+public interface ReleaseableLatch extends DeferredSupplier<Boolean>,
ImmediateSupplier<Boolean> {
+ /**
+ * Increment usage count for the {@code caller} entity
+ */
+ void acquire(Entity caller);
+
+ /**
+ * Decrement usage count for the {@code caller} entity
+ */
+ void release(Entity caller);
+
+ static abstract class AbstractReleaseableLatch implements
ReleaseableLatch {
+ // Instances coerce to TRUE as they are non-null.
+ @Override public Boolean get() {return Boolean.TRUE;}
+ @Override public Maybe<Boolean> getImmediately() {return
Maybe.of(Boolean.TRUE);}
+ }
+
+ ReleaseableLatch NOP = new Factory.NopLatch();
+
+ static class Factory {
+ private static class NopLatch extends AbstractReleaseableLatch {
+ @Override public void acquire(Entity caller) {}
+ @Override public void release(Entity caller) {}
+ }
+
+ private static class MaxConcurrencyLatch extends
AbstractReleaseableLatch {
+ private int permits;
+ private transient final Semaphore sem;
+
+ public MaxConcurrencyLatch(int permits) {
+ this.permits = permits;
+ this.sem = new Semaphore(permits);
+ }
+
+ @Override
+ public void acquire(Entity caller) {
--- End diff --
Why is no use made of `caller` here and in `release` - by the description
of the contract above "Increment usage count for the {@code caller} entity"
surely it should? Reading that, I wouldn't expect that if I have two entities
and do `latch.acquire(entity1)`, I would then be able to do
`latch.release(entity2)`, or indeed `latch.release(null)`, and still release a
permit on the semaphore, but that's what will happen. It may be worth updating
the comment to indicate that implementations are free to ignore the entity if
that suits them.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---