Exemplo n.º 1
0
        public static void TimeoutConstructorZeroDueTime()
        {
            var value   = new[] { 0 };
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 0);

            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.AreEqual(1, value[0]);
        }
Exemplo n.º 2
0
        public static void TimeoutChange()
        {
            var now     = DateTime.Now;
            var value   = new[] { now };
            var timeout = new Timeout(() => value[0] = DateTime.Now, 100);

            timeout.Change(1000);                // race condition
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted); // race condition
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.Greater((value[0] - now).TotalMilliseconds, 100);
        }
Exemplo n.º 3
0
        public static void TimeoutFinishAndChange()
        {
            var value   = new[] { 0 };
            var timeout = RootedTimeout.Launch(() => value[0] = 1, 100);

            Assert.IsFalse(timeout.IsCanceled);
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsTrue(timeout.IsCompleted);
            Assert.AreEqual(1, value[0]);
            Assert.IsFalse(timeout.Change(1000));
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsTrue(timeout.IsCompleted);
        }
Exemplo n.º 4
0
        private static void Initialize()
        {
            switch (Interlocked.CompareExchange(ref _status, _statusPending, _statusNotReady))
            {
            case _statusNotReady:
                GC.KeepAlive(new GCProbe());
                Volatile.Write(ref _status, _statusReady);
                break;

            case _statusPending:
                ThreadingHelper.SpinWaitUntil(ref _status, _statusReady);
                break;
            }
        }
Exemplo n.º 5
0
        public static void TimeoutChange()
        {
again:
            var now = DateTime.Now;
            var value   = new[] { now };
            var timeout = RootedTimeout.Launch(() => value[0] = DateTime.Now, 100);

            if (!timeout.Change(1000))
            {
                goto again;
            }
            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.Greater((value[0] - now).TotalMilliseconds, 100);
        }
Exemplo n.º 6
0
        private static void Initialize()
        {
            var check = Interlocked.CompareExchange(ref _status, INT_StatusPending, INT_StatusNotReady);

            switch (check)
            {
            case INT_StatusNotReady:
                GC.KeepAlive(new GCProbe());
                Thread.VolatileWrite(ref _status, INT_StatusReady);
                break;

            case INT_StatusPending:
                ThreadingHelper.SpinWaitUntil(ref _status, INT_StatusReady);
                break;
            }
        }
Exemplo n.º 7
0
        public static void TimeoutChange()
        {
            RootedTimeout timeout;
            var           value = new DateTime[1];
            DateTime      now;

            do
            {
                now      = DateTime.Now;
                value[0] = now;
                timeout  = RootedTimeout.Launch(() => value[0] = DateTime.Now, 100);
            }while (!timeout.Change(1000));

            Assert.IsFalse(timeout.IsCanceled);
            Assert.IsFalse(timeout.IsCompleted);
            ThreadingHelper.SpinWaitUntil(() => timeout.IsCompleted);
            Assert.Greater((value[0] - now).TotalMilliseconds, 100);
        }