Merge pull request #725 from knocte/threadpool_init
[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
13         static void AllocStuff ()
14         {
15                 var x = new object ();
16                 for (int i = 0; i < 300; ++i)
17                         x = new byte [i];
18         }
19
20         static void BackgroundNoise ()
21         {
22                 while (true)
23                         AllocStuff ();
24         }
25
26         static void Main () {
27                 for (int i = 0; i < Math.Max (1, Environment.ProcessorCount / 2); ++i) {
28                 // for (int i = 0; i < 4; ++i) {
29                         var t = new Thread (BackgroundNoise);
30                         t.IsBackground = true;
31                         t.Start ();
32                 }
33                 
34                 for (int i = 0; i < 100; ++i) {
35                         var ad = AppDomain.CreateDomain ("domain_" + i);
36                         ad.DoCallBack (new CrossAppDomainDelegate (AllocStuff));
37                         AppDomain.Unload (ad);
38                         Console.Write (".");
39                         if (i > 0 && i % 20 == 0) Console.WriteLine ();
40                 }
41                 Console.WriteLine ("\ndone");
42         }
43 }