Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Store / Lock.cs
blobf52ead3dc0995037f5579eddd9c646473abe05fd
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexWriter = Lucene.Net.Index.IndexWriter;
18 namespace Lucene.Net.Store
21 /// <summary>An interprocess mutex lock.
22 /// <p>Typical use might look like:<pre>
23 /// new Lock.With(directory.makeLock("my.lock")) {
24 /// public Object doBody() {
25 /// <i>... code to execute while locked ...</i>
26 /// }
27 /// }.run();
28 /// </pre>
29 ///
30 /// </summary>
31 /// <author> Doug Cutting
32 /// </author>
33 /// <version> $Id: Lock.cs,v 1.2 2005/01/17 19:54:31 joeshaw Exp $
34 /// </version>
35 /// <seealso cref="Directory#MakeLock(String)">
36 /// </seealso>
37 public abstract class Lock
39 public static long LOCK_POLL_INTERVAL = 1000;
41 /// <summary>Attempts to obtain exclusive access and immediately return
42 /// upon success or failure.
43 /// </summary>
44 /// <returns> true iff exclusive access is obtained
45 /// </returns>
46 public abstract bool Obtain();
48 /// <summary>Attempts to obtain an exclusive lock within amount
49 /// of time given. Currently polls once per second until
50 /// lockWaitTimeout is passed.
51 /// </summary>
52 /// <param name="lockWaitTimeout">length of time to wait in ms
53 /// </param>
54 /// <returns> true if lock was obtained
55 /// </returns>
56 /// <throws> IOException if lock wait times out or obtain() throws an IOException </throws>
57 public virtual bool Obtain(long lockWaitTimeout)
59 bool locked = Obtain();
60 int maxSleepCount = (int) (lockWaitTimeout / LOCK_POLL_INTERVAL);
61 int sleepCount = 0;
63 // FIXED trow@ximian.com 2004 May 8
64 // We shouldn't just fail right away if lockWaitTimeout < LOCK_POLL_INTERVAL.
65 maxSleepCount = Math.Max (maxSleepCount, 1);
67 while (!locked)
69 // FIXED trow@ximian.com 2004 May 8
70 // Lock would time out before first sleep if maxSleepCount == 1
71 if (sleepCount == maxSleepCount)
73 throw new System.IO.IOException("Lock obtain timed out: " + this.ToString());
75 ++sleepCount;
77 try
79 System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * LOCK_POLL_INTERVAL));
81 catch (System.Threading.ThreadInterruptedException e)
83 throw new System.IO.IOException(e.ToString());
85 locked = Obtain();
87 return locked;
90 /// <summary>Releases exclusive access. </summary>
91 public abstract void Release();
93 /// <summary>Returns true if the resource is currently locked. Note that one must
94 /// still call {@link #Obtain()} before using the resource.
95 /// </summary>
96 public abstract bool IsLocked();
99 /// <summary>Utility class for executing code with exclusive access. </summary>
100 public abstract class With
102 private Lock lock_Renamed;
103 private long lockWaitTimeout;
105 /// <summary>Constructs an executor that will grab the named lock.
106 /// Defaults lockWaitTimeout to Lock.COMMIT_LOCK_TIMEOUT.
107 /// </summary>
108 /// <deprecated> Kept only to avoid breaking existing code.
109 /// </deprecated>
110 public With(Lock lock_Renamed):this(lock_Renamed, IndexWriter.COMMIT_LOCK_TIMEOUT)
114 /// <summary>Constructs an executor that will grab the named lock. </summary>
115 public With(Lock lock_Renamed, long lockWaitTimeout)
117 this.lock_Renamed = lock_Renamed;
118 this.lockWaitTimeout = lockWaitTimeout;
121 /// <summary>Code to execute with exclusive access. </summary>
122 public abstract System.Object DoBody();
124 /// <summary>Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
125 /// cannot be obtained immediately. Retries to obtain lock once per second
126 /// until it is obtained, or until it has tried ten times. Lock is released when
127 /// {@link #doBody} exits.
128 /// </summary>
129 public virtual System.Object run()
131 bool locked = false;
134 locked = lock_Renamed.Obtain(lockWaitTimeout);
135 return DoBody();
137 finally
139 if (locked)
140 lock_Renamed.Release();