Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / sgen-domain-unload-2.cs
1 using System;
2 using System.Threading;
3
4 /*
5 This test stresses what happens when root domain threads are allocating into the nursery
6 while a domain is cleaned up.
7
8 This is a regression test for a crash in the domain object cleaner code that did not
9 stop-the-world before walking the heap.
10 */
11 class Driver {
12         static void AllocStuff ()
13         {
14                 var x = new object ();
15                 for (int i = 0; i < 300; ++i)
16                         x = new byte [i];
17         }
18
19         static void BackgroundNoise ()
20         {
21                 while (true)
22                         AllocStuff ();
23         }
24
25         static void Main () {
26                 for (int i = 0; i < Math.Max (1, Environment.ProcessorCount / 2); ++i) {
27                 // for (int i = 0; i < 4; ++i) {
28                         var t = new Thread (BackgroundNoise);
29                         t.IsBackground = true;
30                         t.Start ();
31                 }
32
33                 int iterations = 0;
34
35                 for (TestTimeout timeout = TestTimeout.Start(TimeSpan.FromSeconds(TestTimeout.IsStressTest ? 120 : 5)); timeout.HaveTimeLeft;) {
36                         var ad = AppDomain.CreateDomain ("domain_" + iterations);
37                         ad.DoCallBack (new CrossAppDomainDelegate (AllocStuff));
38                         AppDomain.Unload (ad);
39
40                         Console.Write (".");
41                         if ((++iterations) % 20 == 0) Console.WriteLine ();
42                 }
43                 Console.WriteLine ($"\ndone {iterations} iterations");
44         }
45 }