[tests] Don't starve the main thread by doing GCs
[mono.git] / mono / tests / thread-suspend-selfsuspended.cs
index c9681dffed266dd47043952cc88277f433298ecb..02a63a4bb39acc2047dfa40ac05bda0be3e442be 100644 (file)
@@ -7,12 +7,15 @@ class Driver
        public static void Main ()
        {
                bool finished = false;
+               int can_gc = 0;
 
                Thread t1 = Thread.CurrentThread;
 
                Thread t2 = new Thread (() => {
                        while (!finished) {
-                               GC.Collect ();
+                               int local_can_gc = can_gc;
+                               if (local_can_gc > 0 && Interlocked.CompareExchange (ref can_gc, local_can_gc - 1, local_can_gc) == local_can_gc)
+                                       GC.Collect ();
 
                                try {
                                        t1.Resume ();
@@ -27,7 +30,8 @@ class Driver
 
                Thread.Sleep (10);
 
-               for (int i = 0; i < 50 * 40 * 20; ++i) {
+               for (int i = 0; i < 50 * 40 * 5; ++i) {
+                       Interlocked.Increment (ref can_gc);
                        Thread.CurrentThread.Suspend ();
                        if ((i + 1) % (50) == 0)
                                Console.Write (".");