コード例 #1
0
 private void Finish(object state)
 {
     GC.KeepAlive(state);
     ThreadingHelper.SpinWaitWhile(ref _status, _changing);
     if (Interlocked.CompareExchange(ref _status, _executing, _created) == _created)
     {
         var callback = Volatile.Read(ref Callback);
         if (callback != null)
         {
             callback.Invoke();
             Close();
             Volatile.Write(ref _status, _executed);
         }
     }
 }
コード例 #2
0
        private static void Initialize()
        {
            var check = Interlocked.CompareExchange(ref _status, _statusPending, _statusNotReady);

            switch (check)
            {
            case _statusNotReady:
                GC.KeepAlive(new GCProbe());
                Volatile.Write(ref _status, _statusReady);
                break;

            case _statusPending:
                ThreadingHelper.SpinWaitUntil(ref _status, _statusReady);
                break;
            }
        }
コード例 #3
0
 public TReturn DisposedConditional <TReturn>(Func <TReturn> whenDisposed, Func <TReturn> whenNotDisposed)
 {
     if (_status == -1)
     {
         if (ReferenceEquals(whenDisposed, null))
         {
             return(default(TReturn));
         }
         else
         {
             return(whenDisposed.Invoke());
         }
     }
     else
     {
         if (ReferenceEquals(whenNotDisposed, null))
         {
             return(default(TReturn));
         }
         else
         {
             if (ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1))
             {
                 try
                 {
                     return(whenNotDisposed.Invoke());
                 }
                 finally
                 {
                     global::System.Threading.Interlocked.Decrement(ref _status);
                 }
             }
             else
             {
                 if (ReferenceEquals(whenDisposed, null))
                 {
                     return(default(TReturn));
                 }
                 else
                 {
                     return(whenDisposed.Invoke());
                 }
             }
         }
     }
 }
コード例 #4
0
 public bool Change(long dueTime)
 {
     if (Interlocked.CompareExchange(ref _status, _changing, _created) == _created)
     {
         _startTime  = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow());
         _targetTime = _startTime + dueTime;
         var wrapped = Interlocked.CompareExchange(ref _wrapped, null, null);
         if (wrapped == null)
         {
             return(false);
         }
         wrapped.Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(-1));
         Volatile.Write(ref _status, _created);
         return(true);
     }
     return(false);
 }
コード例 #5
0
 private bool TakeDisposalExecution()
 {
     return(_status != -1 && ThreadingHelper.SpinWaitSetUnless(ref _status, -1, 0, -1));
 }
コード例 #6
0
 protected void Start(long dueTime)
 {
     _startTime  = ThreadingHelper.Milliseconds(ThreadingHelper.TicksNow());
     _targetTime = _startTime + dueTime;
     _wrapped    = new Timer(Finish, null, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(-1));
 }