1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.util.ThreadInterruptedException;
21 import java.io.IOException;
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>
32 * @see Directory#makeLock(String)
34 public abstract class Lock {
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;
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;
44 /** Attempts to obtain exclusive access and immediately return
45 * upon success or failure.
46 * @return true iff exclusive access is obtained
48 public abstract boolean obtain() throws IOException;
51 * If a lock obtain called, this failureReason may be set
52 * with the "root cause" Exception as to why the lock was
55 protected Throwable failureReason;
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
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
68 * @throws IOException if obtain() throws IOException
70 public boolean obtain(long lockWaitTimeout) throws LockObtainFailedException, IOException {
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 + ")");
76 long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL;
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;
84 LockObtainFailedException e = new LockObtainFailedException(reason);
85 if (failureReason != null) {
86 e.initCause(failureReason);
91 Thread.sleep(LOCK_POLL_INTERVAL);
92 } catch (InterruptedException ie) {
93 throw new ThreadInterruptedException(ie);
100 /** Releases exclusive access. */
101 public abstract void release() throws IOException;
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;
108 /** Utility class for executing code with exclusive access. */
109 public abstract static class With {
111 private long lockWaitTimeout;
114 /** Constructs an executor that will grab the named lock. */
115 public With(Lock lock, long lockWaitTimeout) {
117 this.lockWaitTimeout = lockWaitTimeout;
120 /** Code to execute with exclusive access. */
121 protected abstract Object doBody() throws IOException;
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
129 * @throws IOException if {@link Lock#obtain} throws IOException
131 public Object run() throws LockObtainFailedException, IOException {
132 boolean locked = false;
134 locked = lock.obtain(lockWaitTimeout);