blob: 6b9ea8ec5c739e5ff2d695bd439e3b11a9447d53 [file] [log] [blame]
Brian O'Connorabafb502014-12-02 22:26:20 -08001package org.onosproject.store.service;
Madan Jampanif73fb042014-11-11 10:49:05 -08002
Madan Jampani1d3494e2014-11-20 11:24:22 -08003import java.util.concurrent.CompletableFuture;
Madan Jampani1769a1a2014-11-19 21:51:44 -08004
Madan Jampanif73fb042014-11-11 10:49:05 -08005/**
6 * A lock is a tool for controlling access to a shared resource by multiple processes.
7 * Commonly, a lock provides exclusive access to a resource such as a network device
8 * or exclusive permission to a controller to perform a particular role such as serve
9 * as the master controller for a device.
10 * At any given time one and only process can acquire the lock.
11 */
12public interface Lock {
13
Madan Jampani23af4fc2014-11-12 00:54:18 -080014 /**
15 * Returns the path this lock will be used to guard from concurrent access.
16 * @return path.
17 */
18 String path();
Madan Jampani12390c12014-11-12 00:35:56 -080019
Madan Jampanif73fb042014-11-11 10:49:05 -080020 /**
21 * Acquires the lock.
22 * If the lock is not available then the caller thread becomes
23 * disabled for thread scheduling purposes and lies dormant until
24 * the lock has been acquired.
25 * <p>
26 * Locks are reentrant. A thread invoking this method multiple times
27 * without an intervening unlock or lease expiration must invoke unlock()
28 * the same number of times before the lock is released (unless the lease expires).
29 * When this method is invoked for a lock that is already acquired,
30 * the lease time will be set to the maximum of the remaining lease time
31 * from the previous invocation, or leaseDurationMillis.
32 * @param leaseDurationMillis the number of milliseconds to hold the
33 * lock after granting it, before automatically releasing it if it hasn't
34 * already been released by invoking unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080035 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampani71582ed2014-11-18 10:06:01 -080036 * @throws InterruptedException if the thread is interrupted while waiting
Madan Jampanif73fb042014-11-11 10:49:05 -080037 */
Madan Jampani71582ed2014-11-18 10:06:01 -080038 void lock(int leaseDurationMillis) throws InterruptedException;
Madan Jampanif73fb042014-11-11 10:49:05 -080039
40 /**
Madan Jampani1769a1a2014-11-19 21:51:44 -080041 * Acquires the lock asynchronously.
42 * @param leaseDurationMillis leaseDurationMillis the number of milliseconds the lock
43 * will be reserved before it becomes available for others.
44 * @return Future that can be used for blocking until lock is acquired.
45 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080046 CompletableFuture<Void> lockAsync(int leaseDurationMillis);
Madan Jampani1769a1a2014-11-19 21:51:44 -080047
48 /**
Madan Jampanif73fb042014-11-11 10:49:05 -080049 * Acquires the lock only if it is free at the time of invocation.
50 * @param leaseDurationMillis the number of milliseconds the must be
51 * locked after it is granted, before automatically releasing it if it hasn't
52 * already been released by an invocation of unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080053 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080054 * @return true if the lock was acquired and false otherwise
55 */
Madan Jampani12390c12014-11-12 00:35:56 -080056 boolean tryLock(int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080057
58 /**
59 * Acquires the lock if it is free within the given waiting
60 * time and the current thread has not been interrupted.
61 * @param waitTimeMillis the maximum time (in milliseconds) to wait for the lock
62 * @param leaseDurationMillis the number of milliseconds to hold the
63 * lock after granting it, before automatically releasing it if it hasn't
64 * already been released by invoking unlock(Object). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080065 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080066 * @return true if the lock was acquired and false if the waiting time
67 * elapsed before the lock was acquired
Madan Jampani71582ed2014-11-18 10:06:01 -080068 * @throws InterruptedException if the thread is interrupted while waiting
Madan Jampanif73fb042014-11-11 10:49:05 -080069 */
Madan Jampani1769a1a2014-11-19 21:51:44 -080070 boolean tryLock(int waitTimeMillis, int leaseDurationMillis) throws InterruptedException;
Madan Jampanif73fb042014-11-11 10:49:05 -080071
72 /**
73 * Returns true if this Lock instance currently holds the lock.
74 * @return true if this instance is the owner of the lock.
75 */
76 boolean isLocked();
77
78 /**
Madan Jampani1ee91782014-11-20 20:24:24 -080079 * Returns the epoch for this lock.
80 * If this lock is currently locked i.e. isLocked() returns true, epoch signifies the logical time
81 * when the lock was acquired. The concept of epoch lets one come up with a global ordering for all
82 * lock acquisition events
83 * @return epoch
84 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080085 long epoch();
Madan Jampani1ee91782014-11-20 20:24:24 -080086
87 /**
Madan Jampanif73fb042014-11-11 10:49:05 -080088 * Releases the lock.
89 */
90 void unlock();
91
92 /**
Madan Jampani32fe7802014-11-11 11:16:47 -080093 * Extends the expiration time for a lock that is currently owned
94 * by a specified duration. The new expiration time is computed
95 * by adding the specified duration to the current time. If this point
96 * in time is earlier than the existing expiration time then this method
97 * has no effect.
98 * @param leaseDurationMillis extension duration.
99 * @return true if successfully extended expiration, false if attempt to
100 * extend expiration fails or if the path is currently not locked by this instance.
Madan Jampanif73fb042014-11-11 10:49:05 -0800101 */
Madan Jampani12390c12014-11-12 00:35:56 -0800102 boolean extendExpiration(int leaseDurationMillis);
Madan Jampani23af4fc2014-11-12 00:54:18 -0800103}