blob: 510df82e2b8d3b869c82d69752567309406326df [file] [log] [blame]
Madan Jampanif73fb042014-11-11 10:49:05 -08001package org.onlab.onos.store.service;
2
3/**
4 * A lock is a tool for controlling access to a shared resource by multiple processes.
5 * Commonly, a lock provides exclusive access to a resource such as a network device
6 * or exclusive permission to a controller to perform a particular role such as serve
7 * as the master controller for a device.
8 * At any given time one and only process can acquire the lock.
9 */
10public interface Lock {
11
Madan Jampani12390c12014-11-12 00:35:56 -080012 /**
13 * Returns the path this lock will be used to guard from concurrent access.
14 * @return path.
15 */
16 String path();
17
Madan Jampanif73fb042014-11-11 10:49:05 -080018 /**
19 * Acquires the lock.
20 * If the lock is not available then the caller thread becomes
21 * disabled for thread scheduling purposes and lies dormant until
22 * the lock has been acquired.
23 * <p>
24 * Locks are reentrant. A thread invoking this method multiple times
25 * without an intervening unlock or lease expiration must invoke unlock()
26 * the same number of times before the lock is released (unless the lease expires).
27 * When this method is invoked for a lock that is already acquired,
28 * the lease time will be set to the maximum of the remaining lease time
29 * from the previous invocation, or leaseDurationMillis.
30 * @param leaseDurationMillis the number of milliseconds to hold the
31 * lock after granting it, before automatically releasing it if it hasn't
32 * already been released by invoking unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080033 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080034 */
Madan Jampani12390c12014-11-12 00:35:56 -080035 void lock(int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080036
37 /**
38 * Acquires the lock only if it is free at the time of invocation.
39 * @param leaseDurationMillis the number of milliseconds the must be
40 * locked after it is granted, before automatically releasing it if it hasn't
41 * already been released by an invocation of unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080042 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080043 * @return true if the lock was acquired and false otherwise
44 */
Madan Jampani12390c12014-11-12 00:35:56 -080045 boolean tryLock(int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080046
47 /**
48 * Acquires the lock if it is free within the given waiting
49 * time and the current thread has not been interrupted.
50 * @param waitTimeMillis the maximum time (in milliseconds) to wait for the lock
51 * @param leaseDurationMillis the number of milliseconds to hold the
52 * lock after granting it, before automatically releasing it if it hasn't
53 * already been released by invoking unlock(Object). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080054 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080055 * @return true if the lock was acquired and false if the waiting time
56 * elapsed before the lock was acquired
57 */
Madan Jampani12390c12014-11-12 00:35:56 -080058 boolean tryLock(long waitTimeMillis, int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080059
60 /**
61 * Returns true if this Lock instance currently holds the lock.
62 * @return true if this instance is the owner of the lock.
63 */
64 boolean isLocked();
65
66 /**
67 * Releases the lock.
68 */
69 void unlock();
70
71 /**
Madan Jampani32fe7802014-11-11 11:16:47 -080072 * Extends the expiration time for a lock that is currently owned
73 * by a specified duration. The new expiration time is computed
74 * by adding the specified duration to the current time. If this point
75 * in time is earlier than the existing expiration time then this method
76 * has no effect.
77 * @param leaseDurationMillis extension duration.
78 * @return true if successfully extended expiration, false if attempt to
79 * extend expiration fails or if the path is currently not locked by this instance.
Madan Jampanif73fb042014-11-11 10:49:05 -080080 */
Madan Jampani12390c12014-11-12 00:35:56 -080081 boolean extendExpiration(int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080082}