/// <summary>Attempts to obtain an exclusive lock within amount of /// time given. Polls once per {@link #LOCK_POLL_INTERVAL} /// (currently 1000) milliseconds until lockWaitTimeout is /// passed. /// </summary> /// <param name="lockWaitTimeout">length of time to wait in /// milliseconds or {@link /// #LOCK_OBTAIN_WAIT_FOREVER} to retry forever /// </param> /// <returns> true if lock was obtained /// </returns> /// <throws> LockObtainFailedException if lock wait times out </throws> /// <throws> IllegalArgumentException if lockWaitTimeout is </throws> /// <summary> out of bounds /// </summary> /// <throws> IOException if obtain() throws IOException </throws> public virtual bool Obtain(long lockWaitTimeout) { failureReason = null; bool locked = Obtain(); if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER) { throw new System.ArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")"); } long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL; long sleepCount = 0; while (!locked) { if (lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER && sleepCount++ >= maxSleepCount) { System.String reason = "Lock obtain timed out: " + this.ToString(); if (failureReason != null) { reason += (": " + failureReason); } LockObtainFailedException e; if (failureReason != null) { e = new LockObtainFailedException(reason, failureReason); } else { e = new LockObtainFailedException(reason); } throw e; } try { System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * LOCK_POLL_INTERVAL)); } catch (System.Threading.ThreadInterruptedException e) { // In 3.0 we will change this to throw // InterruptedException instead SupportClass.ThreadClass.Current().Interrupt(); throw new System.IO.IOException(e.ToString()); } locked = Obtain(); } return(locked); }
/// <summary>Attempts to obtain an exclusive lock within amount of /// time given. Polls once per {@link #LOCK_POLL_INTERVAL} /// (currently 1000) milliseconds until lockWaitTimeout is /// passed. /// </summary> /// <param name="lockWaitTimeout">length of time to wait in /// milliseconds or {@link /// #LOCK_OBTAIN_WAIT_FOREVER} to retry forever /// </param> /// <returns> true if lock was obtained /// </returns> /// <throws> LockObtainFailedException if lock wait times out </throws> /// <throws> IllegalArgumentException if lockWaitTimeout is </throws> /// <summary> out of bounds /// </summary> /// <throws> IOException if obtain() throws IOException </throws> public virtual bool Obtain(long lockWaitTimeout) { failureReason = null; bool locked = Obtain(); if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER) throw new System.ArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")"); long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL; long sleepCount = 0; while (!locked) { if (lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER && sleepCount++ >= maxSleepCount) { System.String reason = "Lock obtain timed out: " + this.ToString(); if (failureReason != null) { reason += (": " + failureReason); } LockObtainFailedException e; if (failureReason != null) { e = new LockObtainFailedException(reason, failureReason); } else { e = new LockObtainFailedException(reason); } throw e; } try { System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * LOCK_POLL_INTERVAL)); } catch (System.Threading.ThreadInterruptedException e) { // In 3.0 we will change this to throw // InterruptedException instead SupportClass.ThreadClass.Current().Interrupt(); throw new System.IO.IOException(e.ToString()); } locked = Obtain(); } return locked; }