Add support for intercepting threadpool work.
[mono.git] / mcs / class / referencesource / mscorlib / system / threading / threadpool.cs
index 851996e18d9894e5803542ab9ae7b8881e345ddd..13e1e31a9767216974fb004458de09cd2d167fff 100644 (file)
@@ -1185,9 +1185,27 @@ namespace System.Threading
     //
     internal static class _ThreadPoolWaitCallback
     {
+#if FEATURE_INTERCEPTABLE_THREADPOOL_CALLBACK
+        // This feature is used by Xamarin.iOS to use an NSAutoreleasePool
+        // for every task done by the threadpool.
+        static Func<Func<bool>, bool> dispatcher;
+
+        internal static void SetDispatcher (Func<Func<bool>, bool> value)
+        {
+            dispatcher = value;
+        }
+#endif
+
         [System.Security.SecurityCritical]
         static internal bool PerformWaitCallback()
         {
+#if FEATURE_INTERCEPTABLE_THREADPOOL_CALLBACK
+            // store locally first to ensure another thread doesn't clear the field between checking for null and using it.
+            var dispatcher = _ThreadPoolWaitCallback.dispatcher;
+            if (dispatcher != null)
+                return dispatcher (ThreadPoolWorkQueue.Dispatch);
+#endif
+
             return ThreadPoolWorkQueue.Dispatch();
         }
     }