1 package org.apache.lucene.store;
2
3 /**
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 import org.apache.lucene.util.ThreadInterruptedException;
21 import java.io.IOException;
22
23 /** An interprocess mutex lock.
24 * <p>Typical use might look like:<pre>
25 * new Lock.With(directory.makeLock("my.lock")) {
26 * public Object doBody() {
27 * <i>... code to execute while locked ...</i>
28 * }
29 * }.run();
30 * </pre>
31 *
32 * @see Directory#makeLock(String)
33 */
34 public abstract class Lock {
35
36 /** How long {@link #obtain(long)} waits, in milliseconds,
37 * in between attempts to acquire the lock. */
38 public static long LOCK_POLL_INTERVAL = 1000;
39
40 /** Pass this value to {@link #obtain(long)} to try
41 * forever to obtain the lock. */
42 public static final long LOCK_OBTAIN_WAIT_FOREVER = -1;
43
44 /** Attempts to obtain exclusive access and immediately return
45 * upon success or failure.
46 * @return true iff exclusive access is obtained
47 */
48 public abstract boolean obtain() throws IOException;
49
50 /**
51 * If a lock obtain called, this failureReason may be set
52 * with the "root cause" Exception as to why the lock was
53 * not obtained.
54 */
55 protected Throwable failureReason;
56
57 /** Attempts to obtain an exclusive lock within amount of
58 * time given. Polls once per {@link #LOCK_POLL_INTERVAL}
59 * (currently 1000) milliseconds until lockWaitTimeout is
60 * passed.
61 * @param lockWaitTimeout length of time to wait in
62 * milliseconds or {@link
63 * #LOCK_OBTAIN_WAIT_FOREVER} to retry forever
64 * @return true if lock was obtained
65 * @throws LockObtainFailedException if lock wait times out
66 * @throws IllegalArgumentException if lockWaitTimeout is
67 * out of bounds
68 * @throws IOException if obtain() throws IOException
69 */
70 public boolean obtain(long lockWaitTimeout) throws LockObtainFailedException, IOException {
71 failureReason = null;
72 boolean locked = obtain();
73 if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER)
74 throw new IllegalArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")");
75
76 long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL;
77 long sleepCount = 0;
78 while (!locked) {
79 if (lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER && sleepCount++ >= maxSleepCount) {
80 String reason = "Lock obtain timed out: " + this.toString();
81 if (failureReason != null) {
82 reason += ": " + failureReason;
83 }
84 LockObtainFailedException e = new LockObtainFailedException(reason);
85 if (failureReason != null) {
86 e.initCause(failureReason);
87 }
88 throw e;
89 }
90 try {
91 Thread.sleep(LOCK_POLL_INTERVAL);
92 } catch (InterruptedException ie) {
93 throw new ThreadInterruptedException(ie);
94 }
95 locked = obtain();
96 }
97 return locked;
98 }
99
100 /** Releases exclusive access. */
101 public abstract void release() throws IOException;
102
103 /** Returns true if the resource is currently locked. Note that one must
104 * still call {@link #obtain()} before using the resource. */
105 public abstract boolean isLocked() throws IOException;
106
107
108 /** Utility class for executing code with exclusive access. */
109 public abstract static class With {
110 private Lock lock;
111 private long lockWaitTimeout;
112
113
114 /** Constructs an executor that will grab the named lock. */
115 public With(Lock lock, long lockWaitTimeout) {
116 this.lock = lock;
117 this.lockWaitTimeout = lockWaitTimeout;
118 }
119
120 /** Code to execute with exclusive access. */
121 protected abstract Object doBody() throws IOException;
122
123 /** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
124 * cannot be obtained immediately. Retries to obtain lock once per second
125 * until it is obtained, or until it has tried ten times. Lock is released when
126 * {@link #doBody} exits.
127 * @throws LockObtainFailedException if lock could not
128 * be obtained
129 * @throws IOException if {@link Lock#obtain} throws IOException
130 */
131 public Object run() throws LockObtainFailedException, IOException {
132 boolean locked = false;
133 try {
134 locked = lock.obtain(lockWaitTimeout);
135 return doBody();
136 } finally {
137 if (locked)
138 lock.release();
139 }
140 }
141 }
142
143 }