Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import java.time.Duration; |
| 19 | import java.util.Optional; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | |
| 22 | import org.onosproject.store.primitives.DefaultDistributedLock; |
| 23 | |
| 24 | /** |
| 25 | * Asynchronous lock primitive. |
| 26 | */ |
| 27 | public interface AsyncDistributedLock extends DistributedPrimitive { |
| 28 | @Override |
| 29 | default Type primitiveType() { |
| 30 | return Type.LOCK; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Acquires the lock, blocking until it's available. |
| 35 | * |
| 36 | * @return future to be completed once the lock has been acquired |
| 37 | */ |
| 38 | CompletableFuture<Version> lock(); |
| 39 | |
| 40 | /** |
| 41 | * Attempts to acquire the lock. |
| 42 | * |
| 43 | * @return future to be completed with a boolean indicating whether the lock was acquired |
| 44 | */ |
| 45 | CompletableFuture<Optional<Version>> tryLock(); |
| 46 | |
| 47 | /** |
| 48 | * Attempts to acquire the lock for a specified amount of time. |
| 49 | * |
| 50 | * @param timeout the timeout after which to give up attempting to acquire the lock |
| 51 | * @return future to be completed with a boolean indicating whether the lock was acquired |
| 52 | */ |
| 53 | CompletableFuture<Optional<Version>> tryLock(Duration timeout); |
| 54 | |
| 55 | /** |
| 56 | * Unlocks the lock. |
| 57 | * |
| 58 | * @return future to be completed once the lock has been released |
| 59 | */ |
| 60 | CompletableFuture<Void> unlock(); |
| 61 | |
| 62 | default DistributedLock asLock() { |
| 63 | return asLock(-1); |
| 64 | } |
| 65 | |
| 66 | default DistributedLock asLock(long timeoutMillis) { |
| 67 | return new DefaultDistributedLock(this, timeoutMillis); |
| 68 | } |
| 69 | } |