[coreclr] Support for NSAutoreleasePools has now been implemented for background threads for both MonoVM and CoreCLR. Fixes #11256. (#11749)

Fixes https://github.com/xamarin/xamarin-macios/issues/11256.
This commit is contained in:
Rolf Bjarne Kvinge 2021-06-02 07:41:44 +02:00 коммит произвёл GitHub
Родитель abb7d1f6b6
Коммит 071ac6463e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 29 добавлений и 2 удалений

Просмотреть файл

@ -375,8 +375,7 @@ xamarin_bridge_vm_initialize (int propertyCount, const char **propertyKeys, cons
void void
xamarin_install_nsautoreleasepool_hooks () xamarin_install_nsautoreleasepool_hooks ()
{ {
// https://github.com/xamarin/xamarin-macios/issues/11256 // No need to do anything here for CoreCLR.
fprintf (stderr, "TODO: add support for wrapping all threads with NSAutoreleasePools.\n");
} }
void void

Просмотреть файл

@ -489,6 +489,34 @@ namespace MonoTouchFixtures.ObjCRuntime {
obj.Dispose (); obj.Dispose ();
} }
[Test]
public void NSAutoreleasePoolInThread ()
{
var count = 10;
var threads = new Thread [count];
var obj = new NSObject ();
for (int i = 0; i < count; i++) {
threads [i] = new Thread ((v) => {
obj.DangerousRetain ().DangerousAutorelease ();
}) {
Name = $"NSAutoreleasePoolInThread #{i}",
IsBackground = true,
};
threads [i].Start ();
}
for (var i = 0; i < count; i++) {
Assert.IsTrue (threads [i].Join (TimeSpan.FromSeconds (1)), $"Thread #{i}");
}
// Strangely enough there seems to be a race condition here, not all threads will necessarily
// have completed the autorelease by this point. Some should have though, so assert that the object
// was released on at least half the threads.
Assert.That ((int) obj.RetainCount, Is.LessThan (count / 2), "RC");
obj.Dispose ();
}
class ResurrectedObjectsDisposedTestClass : NSObject { class ResurrectedObjectsDisposedTestClass : NSObject {
[Export ("invokeMe:wait:")] [Export ("invokeMe:wait:")]