blob: ed9a9e6bf6261ad97364a3aea3a4a9891354c376 [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 Jampani23af4fc2014-11-12 00:54:18 -080012 /**
13 * Returns the path this lock will be used to guard from concurrent access.
14 * @return path.
15 */
16 String path();
Madan Jampani12390c12014-11-12 00:35:56 -080017
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 Jampani71582ed2014-11-18 10:06:01 -080034 * @throws InterruptedException if the thread is interrupted while waiting
Madan Jampanif73fb042014-11-11 10:49:05 -080035 */
Madan Jampani71582ed2014-11-18 10:06:01 -080036 void lock(int leaseDurationMillis) throws InterruptedException;
Madan Jampanif73fb042014-11-11 10:49:05 -080037
38 /**
39 * Acquires the lock only if it is free at the time of invocation.
40 * @param leaseDurationMillis the number of milliseconds the must be
41 * locked after it is granted, before automatically releasing it if it hasn't
42 * already been released by an invocation of unlock(). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080043 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080044 * @return true if the lock was acquired and false otherwise
45 */
Madan Jampani12390c12014-11-12 00:35:56 -080046 boolean tryLock(int leaseDurationMillis);
Madan Jampanif73fb042014-11-11 10:49:05 -080047
48 /**
49 * Acquires the lock if it is free within the given waiting
50 * time and the current thread has not been interrupted.
51 * @param waitTimeMillis the maximum time (in milliseconds) to wait for the lock
52 * @param leaseDurationMillis the number of milliseconds to hold the
53 * lock after granting it, before automatically releasing it if it hasn't
54 * already been released by invoking unlock(Object). Must be in the range
Madan Jampani32fe7802014-11-11 11:16:47 -080055 * (0, LockManager.MAX_LEASE_MILLIS]
Madan Jampanif73fb042014-11-11 10:49:05 -080056 * @return true if the lock was acquired and false if the waiting time
57 * elapsed before the lock was acquired
Madan Jampani71582ed2014-11-18 10:06:01 -080058 * @throws InterruptedException if the thread is interrupted while waiting
Madan Jampanif73fb042014-11-11 10:49:05 -080059 */
Madan Jampani71582ed2014-11-18 10:06:01 -080060 boolean tryLock(long waitTimeMillis, int leaseDurationMillis) throws InterruptedException;
Madan Jampanif73fb042014-11-11 10:49:05 -080061
62 /**
63 * Returns true if this Lock instance currently holds the lock.
64 * @return true if this instance is the owner of the lock.
65 */
66 boolean isLocked();
67
68 /**
69 * Releases the lock.
70 */
71 void unlock();
72
73 /**
Madan Jampani32fe7802014-11-11 11:16:47 -080074 * Extends the expiration time for a lock that is currently owned
75 * by a specified duration. The new expiration time is computed
76 * by adding the specified duration to the current time. If this point
77 * in time is earlier than the existing expiration time then this method
78 * has no effect.
79 * @param leaseDurationMillis extension duration.
80 * @return true if successfully extended expiration, false if attempt to
81 * extend expiration fails or if the path is currently not locked by this instance.
Madan Jampanif73fb042014-11-11 10:49:05 -080082 */
Madan Jampani12390c12014-11-12 00:35:56 -080083 boolean extendExpiration(int leaseDurationMillis);
Madan Jampani23af4fc2014-11-12 00:54:18 -080084}