Esempio n. 1
0
        /// <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;
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL));
                // LUCENENET NOTE: No need to catch and rethrow same excepton type ThreadInterruptedException

                locked = Obtain();
            }
            return(locked);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <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 = new LockObtainFailedException(reason);
                    e = FailureReason != null
                                        ? new LockObtainFailedException(reason, FailureReason)
                                        : new LockObtainFailedException(reason);
                    throw e;
                }

//#if FEATURE_THREAD_INTERRUPT
//                try
//                {
//#endif
                Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL));
//#if FEATURE_THREAD_INTERRUPT // LUCENENET NOTE: Senseless to catch and rethrow the same exception type
//                }
//                catch (ThreadInterruptedException ie)
//                {
//                    throw new ThreadInterruptedException(ie.ToString(), ie);
//                }
//#endif
                locked = Obtain();
            }
            return(locked);
        }
Esempio n. 4
0
        /// <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="System.IO.IOException"> if <see cref="Obtain()"/> throws <see cref="System.IO.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 = new LockObtainFailedException(reason);
                    e = FailureReason != null
                                        ? new LockObtainFailedException(reason, FailureReason)
                                        : new LockObtainFailedException(reason);
                    throw e;
                }

#if !NETSTANDARD
                try
                {
#endif
                Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL));
#if !NETSTANDARD
            }
            catch (ThreadInterruptedException ie)
            {
                throw new ThreadInterruptedException(ie.ToString(), ie);
            }
#endif
                locked = Obtain();
            }
            return(locked);
        }
Esempio n. 5
0
        /// <summary>
        /// Attempts to obtain an exclusive lock within amount of
        ///  time given. Polls once per <seealso cref="#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>
        /// <exception cref="LockObtainFailedException"> if lock wait times out </exception>
        /// <exception cref="IllegalArgumentException"> if lockWaitTimeout is
        ///         out of bounds </exception>
        /// <exception cref="System.IO.IOException"> if obtain() throws System.IO.IOException </exception>
        public 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)
                {
                    string reason = "Lock obtain timed out: " + this.ToString();
                    if (FailureReason != null)
                    {
                        reason += ": " + FailureReason;
                    }
                    LockObtainFailedException e = new LockObtainFailedException(reason);
                    e = FailureReason != null
                                        ? new LockObtainFailedException(reason, FailureReason)
                                        : new LockObtainFailedException(reason);
                    throw e;
                }
                try
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(LOCK_POLL_INTERVAL));
                }
                catch (ThreadInterruptedException ie)
                {
                    throw new ThreadInterruptedException(ie);
                }
                locked = Obtain();
            }
            return locked;
        }
Esempio n. 6
0
		/// <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)
				{
					throw new System.IO.IOException(e.ToString());
				}
				locked = Obtain();
			}
			return locked;
		}