public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            CheckDisposed();
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout");
            }
            cancellationToken.ThrowIfCancellationRequested();
            GC.KeepAlive(cancellationToken.WaitHandle);
            if (millisecondsTimeout == -1)
            {
                ThreadingHelper.SpinWaitRelativeSet(ref _count, 1);
                return(true);
            }
            var start     = ThreadingHelper.TicksNow();
            var remaining = millisecondsTimeout;

            while (_event.Wait(remaining, cancellationToken))
            {
                // The thread is not allowed here unless there is room in the semaphore
                if (TryEnter())
                {
                    return(true);
                }
                remaining = (int)(millisecondsTimeout - ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow() - start));
                if (remaining <= 0)
                {
                    break;
                }
            }
            // Time out
            return(false);
        }
 public void DisposedConditional(Action whenDisposed, Action whenNotDisposed)
 {
     if (_status == -1)
     {
         if (whenDisposed != null)
         {
             whenDisposed.Invoke();
         }
     }
     else
     {
         if (whenNotDisposed != null)
         {
             if (ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1))
             {
                 try
                 {
                     whenNotDisposed.Invoke();
                 }
                 finally
                 {
                     System.Threading.Interlocked.Decrement(ref _status);
                 }
             }
             else
             {
                 if (whenDisposed != null)
                 {
                     whenDisposed.Invoke();
                 }
             }
         }
     }
 }
 public TReturn DisposedConditional <TReturn>(Func <TReturn> whenDisposed, Func <TReturn> whenNotDisposed)
 {
     if (_status == -1)
     {
         if (whenDisposed == null)
         {
             return(default(TReturn));
         }
         return(whenDisposed.Invoke());
     }
     if (whenNotDisposed == null)
     {
         return(default(TReturn));
     }
     if (ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1))
     {
         try
         {
             return(whenNotDisposed.Invoke());
         }
         finally
         {
             System.Threading.Interlocked.Decrement(ref _status);
         }
     }
     if (whenDisposed == null)
     {
         return(default(TReturn));
     }
     return(whenDisposed.Invoke());
 }
Exemple #4
0
 private void WriteTarget(object target)
 {
     if (_status == -1 || !ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1))
     {
         ReleaseExtracted();
         _handle = GCHandle.Alloc(target, _trackResurrection ? GCHandleType.Weak : GCHandleType.WeakTrackResurrection);
         if (Interlocked.CompareExchange(ref _managedDisposal, 0, 1) == 1)
         {
             GC.ReRegisterForFinalize(this);
         }
         UnDispose();
     }
     else
     {
         try
         {
             var oldHandle = _handle;
             if (oldHandle.IsAllocated)
             {
                 try
                 {
                     oldHandle.Target = target;
                     return;
                 }
                 catch (InvalidOperationException)
                 {
                     _handle = GCHandle.Alloc(target, _trackResurrection ? GCHandleType.Weak : GCHandleType.WeakTrackResurrection);
                 }
             }
             else
             {
                 _handle = GCHandle.Alloc(target, _trackResurrection ? GCHandleType.Weak : GCHandleType.WeakTrackResurrection);
             }
             if (oldHandle.IsAllocated)
             {
                 oldHandle.Free();
                 try
                 {
                     oldHandle.Free();
                 }
                 catch (InvalidOperationException)
                 {
                     // Empty
                 }
             }
         }
         finally
         {
             Interlocked.Decrement(ref _status);
         }
     }
 }