Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Generate unique class name for MonoDeadLetter #89956

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/mono/mono/utils/mono-threads-mach-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,13 @@ mono_threads_init_dead_letter (void)
setObjectForKey = sel_registerName ("setObject:forKey:");
objectForKey = sel_registerName ("objectForKey:");

// define the dead letter class
mono_dead_letter_class = objc_allocateClassPair (nsobject, "MonoDeadLetter", 0);
char *class_name = g_strdup_printf ("MonoDeadLetter%p", &"MonoDeadLetter");

// Define the dead letter class
// The class name needs to be unique in the event different runtimes are loaded into the same process.
mono_dead_letter_class = objc_allocateClassPair (nsobject, class_name, 0);
g_free (class_name);

class_addMethod (mono_dead_letter_class, dealloc, (IMP)mono_dead_letter_dealloc, "v@:");
objc_registerClassPair (mono_dead_letter_class);

Expand Down
19 changes: 5 additions & 14 deletions src/native/libs/System.Native/pal_autoreleasepool.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@

#include "pal_autoreleasepool.h"
#include <Foundation/Foundation.h>

@interface PlaceholderObject : NSObject
- (void)noop:(id)_;
@end

@implementation PlaceholderObject : NSObject
- (void)noop:(id)_
{
[self release];
}
@end
#include <objc/runtime.h>

void EnsureNSThreadIsMultiThreaded(void)
{
Expand All @@ -22,12 +12,13 @@ void EnsureNSThreadIsMultiThreaded(void)
// Start another no-op thread with the NSThread APIs to get NSThread into multithreaded mode.
// The NSAutoReleasePool APIs can't be used on secondary threads until NSThread is in multithreaded mode.
// See https://developer.apple.com/documentation/foundation/nsautoreleasepool for more information.
PlaceholderObject* placeholderObject = [[PlaceholderObject alloc] init];

//
// We need to use detachNewThreadSelector to put NSThread into multithreaded mode.
// We can't use detachNewThreadWithBlock since it doesn't change NSThread into multithreaded mode for some reason.
// See https://developer.apple.com/documentation/foundation/nswillbecomemultithreadednotification for more information.
[NSThread detachNewThreadSelector:@selector(noop:) toTarget:placeholderObject withObject:nil];
id placeholderObject = [[NSMutableString alloc] init];
[NSThread detachNewThreadSelector:@selector(appendString:) toTarget:placeholderObject withObject:@""];
[placeholderObject release];
}
assert([NSThread isMultiThreaded]);
}
Expand Down