[tests] Don't starve the main thread by doing GCs
[mono.git] / mono / tests / thread-suspend-suspended.cs
index 994042b22875b17b0ff6ca3413174a06a66b20ff..eb681653e600e200d97b7ebec44140c3c2a253d3 100644 (file)
@@ -8,6 +8,7 @@ class Driver
        public static void Main ()
        {
                bool finished = false;
+               int can_gc = 0;
 
                Thread t1 = new Thread (() => {
                        while (!finished) {}
@@ -15,7 +16,9 @@ class Driver
 
                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 ();
                                Thread.Yield ();
                        }
                });
@@ -25,8 +28,9 @@ class Driver
 
                Thread.Sleep (10);
 
-               for (int i = 0; i < 50 * 40 * 20; ++i) {
+               for (int i = 0; i < 50 * 40 * 5; ++i) {
                        t1.Suspend ();
+                       Interlocked.Increment (ref can_gc);
                        Thread.Yield ();
                        t1.Resume ();
                        if ((i + 1) % (50) == 0)