Add support for intercepting threadpool work.
authorRolf Bjarne Kvinge <rolf@xamarin.com>
Fri, 8 Jan 2016 18:49:06 +0000 (19:49 +0100)
committerMarek Safar <marek.safar@gmail.com>
Tue, 3 May 2016 09:42:39 +0000 (11:42 +0200)
This is used to Xamarin.iOS to wrap threadpool work in an NSAutoreleasePool.

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();
         }
     }