コード例 #1
0
ファイル: ThreadTests.cs プロジェクト: zielmicha/corefx
        private static void VerifyLocalDataSlot(LocalDataStoreSlot slot)
        {
            Assert.NotNull(slot);

            var waitForThreadArray      = new Action[2];
            var threadArray             = new Thread[2];
            var barrier                 = new Barrier(threadArray.Length);
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken       = cancellationTokenSource.Token;

            Func <bool> barrierSignalAndWait =
                () =>
            {
                try
                {
                    Assert.True(barrier.SignalAndWait(UnexpectedTimeoutMilliseconds, cancellationToken));
                }
                catch (OperationCanceledException)
                {
                    return(false);
                }
                return(true);
            };

            Action <int> threadMain =
                threadIndex =>
            {
                try
                {
                    Assert.Null(Thread.GetData(slot));
                    if (!barrierSignalAndWait())
                    {
                        return;
                    }

                    if (threadIndex == 0)
                    {
                        Thread.SetData(slot, threadIndex);
                    }
                    if (!barrierSignalAndWait())
                    {
                        return;
                    }

                    if (threadIndex == 0)
                    {
                        Assert.Equal(threadIndex, Thread.GetData(slot));
                    }
                    else
                    {
                        Assert.Null(Thread.GetData(slot));
                    }
                    if (!barrierSignalAndWait())
                    {
                        return;
                    }

                    if (threadIndex != 0)
                    {
                        Thread.SetData(slot, threadIndex);
                    }
                    if (!barrierSignalAndWait())
                    {
                        return;
                    }

                    Assert.Equal(threadIndex, Thread.GetData(slot));
                    if (!barrierSignalAndWait())
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    cancellationTokenSource.Cancel();
                    throw new TargetInvocationException(ex);
                }
            };

            for (int i = 0; i < threadArray.Length; ++i)
            {
                threadArray[i] = ThreadTestHelpers.CreateGuardedThread(out waitForThreadArray[i], () => threadMain(i));
                threadArray[i].IsBackground = true;
                threadArray[i].Start();
            }

            foreach (var waitForThread in waitForThreadArray)
            {
                waitForThread();
            }
        }
コード例 #2
0
ファイル: ThreadTests.cs プロジェクト: zielmicha/corefx
 public static void CurrentPrincipalTest_SkipOnDesktopFramework()
 {
     ThreadTestHelpers.RunTestInBackgroundThread(() => Assert.Null(Thread.CurrentPrincipal));
 }
コード例 #3
0
ファイル: ThreadTests.cs プロジェクト: zielmicha/corefx
 public static void ExecutionContextTest()
 {
     ThreadTestHelpers.RunTestInBackgroundThread(
         () => Assert.Equal(ExecutionContext.Capture(), Thread.CurrentThread.ExecutionContext));
 }
コード例 #4
0
    public static void CountTest()
    {
        RemoteExecutor.Invoke(() =>
        {
            const int TimersPerThread = 64;
            int processorCount        = Environment.ProcessorCount;
            int totalTimerCount       = processorCount * TimersPerThread;

            var timers = new List <Timer>(totalTimerCount);
            TimerCallback timerCallback   = _ => { };
            var startCreateTimerThreads   = new ManualResetEvent(false);
            Action createTimerThreadStart = () =>
            {
                startCreateTimerThreads.WaitOne();
                for (int i = 0; i < TimersPerThread; ++i)
                {
                    lock (timers)
                    {
                        timers.Add(
                            new Timer(
                                timerCallback,
                                null,
                                ThreadTestHelpers.UnexpectedTimeoutMilliseconds,
                                ThreadTestHelpers.UnexpectedTimeoutMilliseconds));
                        Assert.True(Timer.ActiveCount >= timers.Count);
                    }
                }
            };
            var waitsForThread = new Action[processorCount];
            for (int i = 0; i < processorCount; ++i)
            {
                Thread t       = ThreadTestHelpers.CreateGuardedThread(out waitsForThread[i], createTimerThreadStart);
                t.IsBackground = true;
                t.Start();
            }

            startCreateTimerThreads.Set();
            foreach (Action waitForThread in waitsForThread)
            {
                waitForThread();
            }

            // To leave some room for unknown timers to be scheduled and removed, remove a large number of timers at a time and
            // verify that the timer count has decreased
            while (timers.Count > 0)
            {
                long timerCountBeforeRemove = Timer.ActiveCount;
                int endIndex = timers.Count - processorCount * 8;
                for (int i = timers.Count - 1; i >= Math.Max(0, endIndex); --i)
                {
                    timers[i].Dispose();
                    timers.RemoveAt(i);
                }

                if (endIndex >= 0)
                {
                    Assert.True(Timer.ActiveCount < timerCountBeforeRemove);
                }
            }
        }).Dispose();
    }