[interp] Remove unreachable code (#12411)
[mono-project.git] / mono / tests / bug-10127.cs
blob389616e4088c0929a40d3abbf37bb2515f18094c
1 using System;
2 using System.Threading;
3 using System.Collections.Generic;
4 using System.Runtime.InteropServices;
6 namespace WeakReferenceTest
8 public static class Cache {
9 static GCHandle[] table = new GCHandle[1024 * 1024];
11 public static T Probe<T>(int hc) where T : class
13 int index = hc & (table.Length - 1);
14 lock (table)
16 var wr = table[index];
17 if (!wr.IsAllocated)
18 return null;
19 return wr.Target as T;
23 public static T Add<T>(T obj, int hc) where T : class
25 int index = hc & (table.Length - 1);
26 lock (table)
28 table[index] = GCHandle.Alloc (obj, GCHandleType.Weak);
30 return obj;
35 public class Tester {
36 public static readonly int seed = unchecked(DateTime.Now.Ticks.GetHashCode());
38 Random rand = new Random(seed);
40 bool alive;
41 Thread thread;
43 public void Start()
45 alive = true;
46 thread = new Thread(new ThreadStart(Work));
47 thread.Start();
50 void Work()
52 do {
54 var item = rand.Next ();
55 var probed = Cache.Probe<object>(item.GetHashCode());
57 if (probed == null) {
58 Cache.Add<object>(item, item.GetHashCode());
61 if (rand.NextDouble() <= 0.1) {
62 GC.Collect();
65 } while (alive);
68 public void Stop ()
70 alive = false;
75 static class RandHelper {
76 public static string RandString(this Random rand, int len)
78 char[] table = new char[len];
79 for (int idx = 0; idx < len; idx++) {
80 table[idx] = (char) ('a' + idx);
82 return new string(table, 0, len);
86 class MainClass
88 public static void Main (string[] args)
90 Console.WriteLine("Starting cache testers");
91 Console.WriteLine("Thread seed: " + Tester.seed);
92 List<Tester> testers = new List<Tester>();
93 for (int count = 0; count < 10; count++) {
94 testers.Add(new Tester());
97 foreach (var tester in testers) {
98 tester.Start();
101 for (int i = 0; i < 4; ++i)
103 Thread.Sleep(TimeSpan.FromSeconds(1));
106 foreach (var tester in testers)
108 tester.Stop ();