Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
Lock.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Support;
20 
21 namespace Lucene.Net.Store
22 {
23 
34  public abstract class Lock
35  {
36 
40  public static long LOCK_POLL_INTERVAL = 1000;
41 
45  public const long LOCK_OBTAIN_WAIT_FOREVER = - 1;
46 
52  public abstract bool Obtain();
53 
58  protected internal System.Exception failureReason;
59 
76  public virtual bool Obtain(long lockWaitTimeout)
77  {
78  failureReason = null;
79  bool locked = Obtain();
80  if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER)
81  throw new System.ArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")");
82 
83  long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL;
84  long sleepCount = 0;
85  while (!locked)
86  {
87  if (lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER && sleepCount++ >= maxSleepCount)
88  {
89  System.String reason = "Lock obtain timed out: " + this.ToString();
90  if (failureReason != null)
91  {
92  reason += (": " + failureReason);
93  }
94  var e = failureReason != null
95  ? new LockObtainFailedException(reason, failureReason)
96  : new LockObtainFailedException(reason);
97  throw e;
98  }
99  try
100  {
101  System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL));
102  }
103  catch (System.Threading.ThreadInterruptedException)
104  {
105  throw;
106  }
107  locked = Obtain();
108  }
109  return locked;
110  }
111 
113  public abstract void Release();
114 
118  public abstract bool IsLocked();
119 
120 
122  public abstract class With
123  {
124  private Lock lock_Renamed;
125  private long lockWaitTimeout;
126 
127 
129  protected With(Lock lock_Renamed, long lockWaitTimeout)
130  {
131  this.lock_Renamed = lock_Renamed;
132  this.lockWaitTimeout = lockWaitTimeout;
133  }
134 
136  protected internal abstract System.Object DoBody();
137 
147  public virtual System.Object run()
148  {
149  bool locked = false;
150  try
151  {
152  locked = lock_Renamed.Obtain(lockWaitTimeout);
153  return DoBody();
154  }
155  finally
156  {
157  if (locked)
158  lock_Renamed.Release();
159  }
160  }
161  }
162  }
163 }