Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / process-unref-race.cs
1
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.Threading;
6 using System.Threading.Tasks;
7
8 class Driver
9 {
10         static IEnumerable<int> UntilTimeout (uint ms)
11         {
12                 DateTime start = DateTime.UtcNow;
13                 for (int i = 0; (DateTime.UtcNow - start).TotalMilliseconds < ms ; i++)
14                         yield return i;
15         }
16
17         public static void Main ()
18         {
19                 object count_lock = new object ();
20                 int count = 0;
21
22                 ParallelOptions options = new ParallelOptions {
23                         MaxDegreeOfParallelism = Environment.ProcessorCount * 4,
24                 };
25
26                 Thread t1 = new Thread (() => {
27                         Parallel.ForEach (UntilTimeout (15 * 1000), options, _ => {
28                                 using (Process p = Process.Start ("cat", "/dev/null")) {
29                                         p.WaitForExit ();
30                                 }
31
32                                 lock (count_lock) {
33                                         count += 1;
34
35                                         if (count % (10) == 0)
36                                                 Console.Write (".");
37                                         if (count % (10 * 50) == 0)
38                                                 Console.WriteLine ();
39                                 }
40                         });
41                 });
42
43                 t1.Start ();
44
45                 while (!t1.Join (0)) {
46                         try {
47                                 using (Process p = Process.GetProcessById (1));
48                         } catch (ArgumentException) {
49                         }
50                 }
51         }
52 }