Пример #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
        }
Пример #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));
     }
 }
Пример #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
        }
Пример #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
        }
Пример #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));
            }
        }
Пример #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));
        }
Пример #7
0
        public static void Enter(object obj)
        {
            Lock lck = GetLock(obj);

            if (lck.TryAcquire(0))
            {
                return;
            }
            TryAcquireContended(lck, obj, Timeout.Infinite);
        }
Пример #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);
            }
        }
Пример #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
        }
Пример #10
0
        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));
        }
Пример #11
0
        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;
        }
Пример #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
        }
Пример #13
0
        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);
        }
Пример #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);
        }
Пример #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
        }
Пример #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);
     }
 }
Пример #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
        }