blob: 0d0579ebeb684929cf137e130af2ae90d8c6c2af [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service;
Madan Jampanif73fb042014-11-11 10:49:05 -080017
Madan Jampani1d3494e2014-11-20 11:24:22 -080018import java.util.concurrent.CompletableFuture;
Madan Jampani1769a1a2014-11-19 21:51:44 -080019
Madan Jampanif73fb042014-11-11 10:49:05 -080020/**
21 * A lock is a tool for controlling access to a shared resource by multiple processes.
22 * Commonly, a lock provides exclusive access to a resource such as a network device
23 * or exclusive permission to a controller to perform a particular role such as serve
24 * as the master controller for a device.
25 * At any given time one and only process can acquire the lock.
26 */
27public interface Lock {
28
Madan Jampani23af4fc2014-11-12 00:54:18 -080029 /**
30 * Returns the path this lock will be used to guard from concurrent access.
31 * @return path.
32 */
33 String path();
Madan Jampani12390c12014-11-12 00:35:56 -080034
Madan Jampanif73fb042014-11-11 10:49:05 -080035 /**
36 * Acquires the lock.
37 * If the lock is not available then the caller thread becomes
38 * disabled for thread scheduling purposes and lies dormant until
39 * the lock has been acquired.
40 * <p>
41 * Locks are reentrant. A thread invoking this method multiple times
42 * without an intervening unlock or lease expiration must invoke unlock()
43 * the same number of times before the lock is released (unless the lease expires).
44 * When this method is invoked for a lock that is already acquired,
45 * the lease time will be set to the maximum of the remaining lease time
46 * from the previous invocation, or leaseDurationMillis.
47 * @param leaseDurationMillis the number of milliseconds to hold the
48 * lock after granting it, before automatically releasing it if it hasn't
49 * already been released by invoking unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080050 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampani71582ed2014-11-18 10:06:01 -080051 * @throws InterruptedException if the thread is interrupted while waiting
Madan Jampanif73fb042014-11-11 10:49:05 -080052 */
Madan Jampani71582ed2014-11-18 10:06:01 -080053 void lock(int leaseDurationMillis) throws InterruptedException;
Madan Jampanif73fb042014-11-11 10:49:05 -080054
55 /**
Madan Jampani1769a1a2014-11-19 21:51:44 -080056 * Acquires the lock asynchronously.
57 * @param leaseDurationMillis leaseDurationMillis the number of milliseconds the lock
58 * will be reserved before it becomes available for others.
59 * @return Future that can be used for blocking until lock is acquired.
60 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080061 CompletableFuture<Void> lockAsync(int leaseDurationMillis);
Madan Jampani1769a1a2014-11-19 21:51:44 -080062
63 /**
Madan Jampanif73fb042014-11-11 10:49:05 -080064 * Acquires the lock only if it is free at the time of invocation.
65 * @param leaseDurationMillis the number of milliseconds the must be
66 * locked after it is granted, before automatically releasing it if it hasn't
67 * already been released by an invocation of unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080068 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080069 * @return true if the lock was acquired and false otherwise
70 */
Madan Jampani12390c12014-11-12 00:35:56 -080071 boolean tryLock(int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080072
73 /**
74 * Acquires the lock if it is free within the given waiting
75 * time and the current thread has not been interrupted.
76 * @param waitTimeMillis the maximum time (in milliseconds) to wait for the lock
77 * @param leaseDurationMillis the number of milliseconds to hold the
78 * lock after granting it, before automatically releasing it if it hasn't
79 * already been released by invoking unlock(Object). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080080 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080081 * @return true if the lock was acquired and false if the waiting time
82 * elapsed before the lock was acquired
Madan Jampani71582ed2014-11-18 10:06:01 -080083 * @throws InterruptedException if the thread is interrupted while waiting
Madan Jampanif73fb042014-11-11 10:49:05 -080084 */
Madan Jampani1769a1a2014-11-19 21:51:44 -080085 boolean tryLock(int waitTimeMillis, int leaseDurationMillis) throws InterruptedException;
Madan Jampanif73fb042014-11-11 10:49:05 -080086
87 /**
88 * Returns true if this Lock instance currently holds the lock.
89 * @return true if this instance is the owner of the lock.
90 */
91 boolean isLocked();
92
93 /**
Madan Jampani1ee91782014-11-20 20:24:24 -080094 * Returns the epoch for this lock.
95 * If this lock is currently locked i.e. isLocked() returns true, epoch signifies the logical time
96 * when the lock was acquired. The concept of epoch lets one come up with a global ordering for all
97 * lock acquisition events
98 * @return epoch
99 */
Brian O'Connor72a034c2014-11-26 18:24:23 -0800100 long epoch();
Madan Jampani1ee91782014-11-20 20:24:24 -0800101
102 /**
Madan Jampanif73fb042014-11-11 10:49:05 -0800103 * Releases the lock.
104 */
105 void unlock();
106
107 /**
Madan Jampani32fe7802014-11-11 11:16:47 -0800108 * Extends the expiration time for a lock that is currently owned
109 * by a specified duration. The new expiration time is computed
110 * by adding the specified duration to the current time. If this point
111 * in time is earlier than the existing expiration time then this method
112 * has no effect.
113 * @param leaseDurationMillis extension duration.
114 * @return true if successfully extended expiration, false if attempt to
115 * extend expiration fails or if the path is currently not locked by this instance.
Madan Jampanif73fb042014-11-11 10:49:05 -0800116 */
Madan Jampani12390c12014-11-12 00:35:56 -0800117 boolean extendExpiration(int leaseDurationMillis);
Madan Jampani23af4fc2014-11-12 00:54:18 -0800118}