Merge "OFPGC_ADD_OR_MOD support in openflowplugin"
[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 import javax.annotation.Nullable;
14
15 /**
16  * Key based semaphore provider.
17  * For the same key there is always only one semaphore available. Unused semaphores are garbage-collect.
18  * @param <K> key type
19  */
20
21 public interface SemaphoreKeeper<K> {
22     /**
23      * Create or load semaphore for key from cache.
24      * @param key semaphore identifier
25      * @return new or existing semaphore for given key, for one key there is always only one semaphore available
26      */
27     Semaphore summonGuard(@Nonnull final K key);
28
29     /**
30      * Get guard and lock for key.
31      * @param key for which guard should be created and acquired
32      * @return semaphore guard
33      */
34     Semaphore summonGuardAndAcquire(@Nonnull final K key);
35
36     /**
37      * Unlock and release guard.
38      * @param guard semaphore guard which should be released
39      */
40     void releaseGuard(@Nullable final Semaphore guard);
41 }