Beispiel #1
0
        private static bool TryAcquireContended(Lock lck, Object obj, int millisecondsTimeout)
        {
#if FEATURE_GET_BLOCKING_OBJECTS
            int removeCookie = t_blockingObjects.Add(obj, ReasonForBlocking.OnCrst);
            try
            {
                return(lck.TryAcquire(millisecondsTimeout));
            }
            finally
            {
                t_blockingObjects.Remove(removeCookie);
            }
#else
            return(lck.TryAcquire(millisecondsTimeout));
#endif
        }
Beispiel #2
0
 internal static bool TryAcquireContended(Lock lck, object obj, int millisecondsTimeout)
 {
     using (new DebugBlockingScope(obj, DebugBlockingItemType.MonitorCriticalSection, millisecondsTimeout, out _))
     {
         return(lck.TryAcquire(millisecondsTimeout, trackContentions: true));
     }
 }
Beispiel #3
0
        public static void TryEnter(Object obj, int millisecondsTimeout, ref bool lockTaken)
        {
            if (lockTaken)
            {
                throw new ArgumentException(SR.Argument_MustBeFalse, "lockTaken");
            }
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout", SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
            }

#if CORERT
            // CORERT-TODO locks
            lockTaken = true;
            return;
#else
            Lock lck = GetLock(obj);
            if (lck.TryAcquire(0))
            {
                lockTaken = true;
                return;
            }
            lockTaken = TryAcquireContended(lck, obj, millisecondsTimeout);
            return;
#endif
        }
Beispiel #4
0
        public static void TryEnter(Object obj, TimeSpan timeout, ref bool lockTaken)
        {
            if (lockTaken)
            {
                throw new ArgumentException(SR.Argument_MustBeFalse, "lockTaken");
            }
            long tm = (long)timeout.TotalMilliseconds;

            if (tm < -1 || tm > (long)Int32.MaxValue)
            {
                throw new ArgumentOutOfRangeException("timeout", SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
            }
            int millisecondsTimeout = (int)tm;

#if CORERT
            // CORERT-TODO locks
            lockTaken = true;
            return;
#else
            Lock lck = GetLock(obj);
            if (lck.TryAcquire(0))
            {
                lockTaken = true;
                return;
            }

            lockTaken = TryAcquireContended(lck, obj, millisecondsTimeout);
            return;
#endif
        }
Beispiel #5
0
        internal static bool TryAcquireContended(Lock lck, Object obj, int millisecondsTimeout)
        {
            DebugBlockingItem blockingItem;

            using (new DebugBlockingScope(obj, DebugBlockingItemType.MonitorCriticalSection, millisecondsTimeout, out blockingItem))
            {
                return(lck.TryAcquire(millisecondsTimeout));
            }
        }
Beispiel #6
0
        public static bool TryEnter(Object obj, TimeSpan timeout)
        {
            long tm = (long)timeout.TotalMilliseconds;

            if (tm < -1 || tm > (long)Int32.MaxValue)
            {
                throw new ArgumentOutOfRangeException("timeout", SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
            }
            int millisecondsTimeout = (int)tm;

            Lock lck = GetLock(obj);

            if (lck.TryAcquire(0))
            {
                return(true);
            }
            return(lck.TryAcquire(millisecondsTimeout));
        }
        public static void Enter(object obj)
        {
            Lock lck = GetLock(obj);

            if (lck.TryAcquire(0))
            {
                return;
            }
            TryAcquireContended(lck, obj, Timeout.Infinite);
        }
Beispiel #8
0
        private static bool TryAcquireContended(Lock lck, Object obj, int millisecondsTimeout)
        {
            int removeCookie = t_blockingObjects.Add(obj, ReasonForBlocking.OnCrst);

            try
            {
                return(lck.TryAcquire(millisecondsTimeout));
            }
            finally
            {
                t_blockingObjects.Remove(removeCookie);
            }
        }
Beispiel #9
0
        public static void Enter(Object obj)
        {
#if CORERT
            // CORERT-TODO locks
            return;
#else
            Lock lck = GetLock(obj);
            if (lck.TryAcquire(0))
            {
                return;
            }
            TryAcquireContended(lck, obj, Timeout.Infinite);
            return;
#endif
        }
        public static bool TryEnter(object obj, int millisecondsTimeout)
        {
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
            }

            Lock lck = GetLock(obj);

            if (lck.TryAcquire(0))
            {
                return(true);
            }
            return(TryAcquireContended(lck, obj, millisecondsTimeout));
        }
        public static void Enter(object obj, ref bool lockTaken)
        {
            if (lockTaken)
            {
                throw new ArgumentException(SR.Argument_MustBeFalse, nameof(lockTaken));
            }

            Lock lck = GetLock(obj);

            if (lck.TryAcquire(0))
            {
                lockTaken = true;
                return;
            }
            TryAcquireContended(lck, obj, Timeout.Infinite);
            lockTaken = true;
        }
Beispiel #12
0
        public static bool TryEnter(Object obj, int millisecondsTimeout)
        {
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout", SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
            }

#if CORERT
            // CORERT-TODO locks
            return(true);
#else
            Lock lck = GetLock(obj);
            if (lck.TryAcquire(0))
            {
                return(true);
            }
            return(TryAcquireContended(lck, obj, millisecondsTimeout));
#endif
        }
        public static void TryEnter(object obj, int millisecondsTimeout, ref bool lockTaken)
        {
            if (lockTaken)
            {
                throw new ArgumentException(SR.Argument_MustBeFalse, nameof(lockTaken));
            }
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
            }

            Lock lck = GetLock(obj);

            if (lck.TryAcquire(0))
            {
                lockTaken = true;
                return;
            }
            lockTaken = TryAcquireContended(lck, obj, millisecondsTimeout);
        }
Beispiel #14
0
        private void PerformCallback(bool timedOut)
        {
            bool lockAcquired;
            var  spinner = new SpinWait();

            // Prevent the race condition with Unregister and the previous PerformCallback call, which may still be
            // holding the _lock.
            while (!(lockAcquired = _lock.TryAcquire(0)) && !Volatile.Read(ref _unregistering))
            {
                spinner.SpinOnce();
            }

            // If another thread is running Unregister, no need to restart the timer or clean up
            if (lockAcquired)
            {
                try
                {
                    if (!_unregistering)
                    {
                        if (_repeating)
                        {
                            // Allow this wait to fire again. Restart the timer before executing the callback.
                            RestartWait();
                        }
                        else
                        {
                            // This wait will not be fired again. Free the GC handle to allow the GC to collect this object.
                            Debug.Assert(_gcHandle.IsAllocated);
                            _gcHandle.Free();
                        }
                    }
                }
                finally
                {
                    _lock.Release();
                }
            }

            _ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(_callbackHelper, timedOut);
        }
Beispiel #15
0
        public static void Enter(Object obj, ref bool lockTaken)
        {
            if (lockTaken)
            {
                throw new ArgumentException(SR.Argument_MustBeFalse, "lockTaken");
            }
#if CORERT
            // CORERT-TODO locks
            lockTaken = true;
            return;
#else
            Lock lck = GetLock(obj);
            if (lck.TryAcquire(0))
            {
                lockTaken = true;
                return;
            }
            TryAcquireContended(lck, obj, Timeout.Infinite);
            lockTaken = true;
            return;
#endif
        }
Beispiel #16
0
 private static bool TryAcquireContended(Lock lck, Object obj, int millisecondsTimeout)
 {
     int removeCookie = t_blockingObjects.Add(obj, ReasonForBlocking.OnCrst);
     try
     {
         return lck.TryAcquire(millisecondsTimeout);
     }
     finally
     {
         t_blockingObjects.Remove(removeCookie);
     }
 }
Beispiel #17
0
        private static bool TryAcquireContended(Lock lck, Object obj, int millisecondsTimeout)
        {
#if FEATURE_GET_BLOCKING_OBJECTS
            int removeCookie = t_blockingObjects.Add(obj, ReasonForBlocking.OnCrst);
            try
            {
                return lck.TryAcquire(millisecondsTimeout);
            }
            finally
            {
                t_blockingObjects.Remove(removeCookie);
            }
#else
            return lck.TryAcquire(millisecondsTimeout);
#endif
        }