blob: 2f5f112fbaa963e7e9904afdd702e701ea5d6070 [file] [log] [blame]
Madan Jampanif73fb042014-11-11 10:49:05 -08001package org.onlab.onos.store.service;
2
Madan Jampani1769a1a2014-11-19 21:51:44 -08003import java.util.concurrent.Future;
4
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 */
46 Future<Void> lockAsync(int leaseDurationMillis);
47
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 /**
79 * Releases the lock.
80 */
81 void unlock();
82
83 /**
Madan Jampani32fe7802014-11-11 11:16:47 -080084 * Extends the expiration time for a lock that is currently owned
85 * by a specified duration. The new expiration time is computed
86 * by adding the specified duration to the current time. If this point
87 * in time is earlier than the existing expiration time then this method
88 * has no effect.
89 * @param leaseDurationMillis extension duration.
90 * @return true if successfully extended expiration, false if attempt to
91 * extend expiration fails or if the path is currently not locked by this instance.
Madan Jampanif73fb042014-11-11 10:49:05 -080092 */
Madan Jampani12390c12014-11-12 00:35:56 -080093 boolean extendExpiration(int leaseDurationMillis);
Madan Jampani23af4fc2014-11-12 00:54:18 -080094}