/// <summary> /// Attempts to obtain an exclusive lock within amount of /// time given. Polls once per <see cref="LOCK_POLL_INTERVAL"/> /// (currently 1000) milliseconds until <paramref name="lockWaitTimeout"/> is /// passed. /// </summary> /// <param name="lockWaitTimeout"> length of time to wait in /// milliseconds or /// <see cref="LOCK_OBTAIN_WAIT_FOREVER"/> to retry forever </param> /// <returns> <c>true</c> if lock was obtained </returns> /// <exception cref="LockObtainFailedException"> if lock wait times out </exception> /// <exception cref="ArgumentException"> if <paramref name="lockWaitTimeout"/> is /// out of bounds </exception> /// <exception cref="IOException"> if <see cref="Obtain()"/> throws <see cref="IOException"/> </exception> public bool Obtain(long lockWaitTimeout) { FailureReason = null; bool locked = Obtain(); if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER) { throw new 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) { string reason = "Lock obtain timed out: " + this.ToString(); if (FailureReason != null) { reason += ": " + FailureReason; } LockObtainFailedException e = FailureReason != null ? new LockObtainFailedException(reason, FailureReason) : new LockObtainFailedException(reason); throw e; } Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL)); // LUCENENET NOTE: No need to catch and rethrow same excepton type ThreadInterruptedException locked = Obtain(); } return(locked); }
/// <summary> /// Attempts to obtain an exclusive lock within amount of /// time given. Polls once per <see cref="LOCK_POLL_INTERVAL"/> /// (currently 1000) milliseconds until <paramref name="lockWaitTimeout"/> is /// passed. /// </summary> /// <param name="lockWaitTimeout"> length of time to wait in /// milliseconds or /// <see cref="LOCK_OBTAIN_WAIT_FOREVER"/> to retry forever </param> /// <returns> <c>true</c> if lock was obtained </returns> /// <exception cref="LockObtainFailedException"> if lock wait times out </exception> /// <exception cref="ArgumentOutOfRangeException"> if <paramref name="lockWaitTimeout"/> is /// out of bounds </exception> /// <exception cref="IOException"> if <see cref="Obtain()"/> throws <see cref="IOException"/> </exception> public bool Obtain(long lockWaitTimeout) { FailureReason = null; bool locked = Obtain(); if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER) { throw new ArgumentOutOfRangeException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) } long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL; long sleepCount = 0; while (!locked) { if (lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER && sleepCount++ >= maxSleepCount) { string reason = "Lock obtain timed out: " + this.ToString(); if (FailureReason != null) { reason += ": " + FailureReason; } LockObtainFailedException e = FailureReason != null ? new LockObtainFailedException(reason, FailureReason) : new LockObtainFailedException(reason); throw e; } try { Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL)); } catch (Exception ie) when(ie.IsInterruptedException()) { throw new Util.ThreadInterruptedException(ie); } locked = Obtain(); } return(locked); }