2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.applications.frsync;
11 import java.util.concurrent.Semaphore;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
16 * Proposal for how a key based semaphore provider should look like.
18 * <li>thread safe</li>
19 * <li>garbage-collect unused semaphores</li>
20 * <li>for the same key there must be always only one semaphore available</li>
26 * final Semaphore guard = semaphoreKeeper.summonGuard(key);
28 * // guard protected logic ...
35 public interface SemaphoreKeeper<K> {
37 * @param key semaphore identifier
38 * @return new or existing semaphore for given key, for one key there is always only one semaphore available
40 Semaphore summonGuard(@Nonnull final K key);
43 * Get guard and lock for key.
44 * @param key for which guard should be created and acquired
45 * @return semaphore guard
47 Semaphore summonGuardAndAcquire(@Nonnull final K key);
50 * Unlock and release guard.
51 * @param guard semaphore guard which should be released
53 void releaseGuard(@Nullable final Semaphore guard);