8185bb0b29a0b8aedcec952534eede9a21e2c624
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / SemaphoreKeeper.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.frsync;
10
11 import java.util.concurrent.Semaphore;
12 import javax.annotation.Nonnull;
13
14 /**
15  * Proposal for how a key based semaphore provider should look like.
16  * <ul>
17  * <li>thread safe</li>
18  * <li>garbage-collect unused semaphores</li>
19  * <li>for the same key there must be always only one semaphore available</li>
20  * </ul>
21  *
22  *
23  * usage:
24  * <pre>
25  * final Semaphore guard = semaphoreKeeper.summonGuard(key);
26  * guard.acquire();
27  * // guard protected logic ...
28  * guard.release();
29  * </pre>
30  *
31  * @param <K> key type
32  */
33
34 public interface SemaphoreKeeper<K> {
35     /**
36      * @param key semaphore identifier
37      * @return new or existing semaphore for given key, for one key there is always only one semaphore available
38      */
39     Semaphore summonGuard(@Nonnull K key);
40 }