Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 1 | package 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 | */ |
| 10 | public interface Lock { |
| 11 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 12 | /** |
| 13 | * Returns the path this lock will be used to guard from concurrent access. |
| 14 | * @return path. |
| 15 | */ |
| 16 | String path(); |
| 17 | |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 18 | /** |
| 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 Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 33 | * (0, LockManager.MAX_LEASE_MILLIS] |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 34 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 35 | void lock(int leaseDurationMillis); |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 36 | |
| 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 Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 42 | * (0, LockManager.MAX_LEASE_MILLIS] |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 43 | * @return true if the lock was acquired and false otherwise |
| 44 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 45 | boolean tryLock(int leaseDurationMillis); |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 46 | |
| 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 Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 54 | * (0, LockManager.MAX_LEASE_MILLIS] |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 55 | * @return true if the lock was acquired and false if the waiting time |
| 56 | * elapsed before the lock was acquired |
| 57 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 58 | boolean tryLock(long waitTimeMillis, int leaseDurationMillis); |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 59 | |
| 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 Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 72 | * 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 Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 80 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 81 | boolean extendExpiration(int leaseDurationMillis); |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 82 | } |