Merge pull request #1048 from DavidKarlas/cacheThreadsAndBulkFetch
[mono.git] / mono / utils / mono-threads-mach-helper.c
1 /*
2  * mono-threads-mach-helper.c: ObjectiveC hacks to improve our changes with thread shutdown
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2014 Xamarin Inc
8  */
9
10 #include "config.h"
11
12 #if defined(__MACH__)
13
14 #include <objc/runtime.h>
15 #include <objc/message.h>
16 #include <mono/utils/mono-compiler.h>
17
18 /*
19  * We cannot include mono-threads.h as this includes io-layer internal types
20  * which conflicts with objc.
21  * Hence the hack here.
22 */
23 void mono_threads_init_dead_letter (void) MONO_INTERNAL;
24 void mono_threads_install_dead_letter (void) MONO_INTERNAL;
25 void mono_thread_info_detach (void) MONO_INTERNAL;
26
27 static Class nsobject, nsthread, mono_dead_letter_class;
28 static SEL dealloc, release, currentThread, threadDictionary, init, alloc, objectForKey, setObjectForKey;
29 static id mono_dead_letter_key;
30
31 /*
32  * Our Mach bindings have a problem in that they might need to attach
33  * the runtime after the the user tls keys have been destroyed.
34  *
35  * This happens when a bound object is retained by NSThread, which is
36  * released very late in the TLS cleanup process.
37  *
38  * At that point, attaching the runtime leaves us in no position to
39  * detach it later using TLS destructors as pthread is done with user
40  * keys. This leaves us with a dead thread registered, which can cause
41  * all sorts of terrible problems.
42  *
43  * The tipical crash is when another thread is created at the exact
44  * same address of the previous one, cause thread registration to abort
45  * due to duplicate entries.
46  *
47  * So what do we do here?
48  * 
49  * Experimentation showns that threadDictionary is destroied after the
50  * problematic keys, so we add our dead letter object as an aditional
51  * way to be notified of thread death.
52  */
53 static void
54 mono_dead_letter_dealloc (id self, SEL _cmd)
55 {
56         struct objc_super super;
57         super.receiver = self;
58         super.class = nsobject;
59         objc_msgSendSuper (&super, dealloc);
60
61         mono_thread_info_detach ();
62 }
63
64 void
65 mono_threads_install_dead_letter (void)
66 {
67         id cur, dict;
68
69         /*
70          * See the 'Dispatch Objective-C Messages Using the Method Function’s Prototype' section in
71          * the '64-Bit Transition Guide for Cocoa Touch' as to why this is required.
72          *
73          * It doesn't hurt on other architectures either, so no need to #ifdef it only for ARM64.
74          */
75
76         id (*id_objc_msgSend_id)(id, SEL, id) = (id (*)(id, SEL, id)) objc_msgSend;
77         void (*objc_msgSend_id_id)(id, SEL, id, id) = (void (*)(id, SEL, id, id)) objc_msgSend;
78
79         cur = objc_msgSend ((id)nsthread, currentThread);
80         if (!cur)
81                 return;
82         dict = objc_msgSend (cur, threadDictionary);
83         if (dict && id_objc_msgSend_id (dict, objectForKey, mono_dead_letter_key) == nil) {
84                 id value = objc_msgSend (objc_msgSend ((id)mono_dead_letter_class, alloc), init);
85
86                 objc_msgSend_id_id (dict, setObjectForKey, value, mono_dead_letter_key);
87
88                 objc_msgSend (value, release);
89         }
90 }
91
92 void
93 mono_threads_init_dead_letter (void)
94 {
95         id nsstring = (id) objc_getClass ("NSString");
96         id nsautoreleasepool = (id) objc_getClass ("NSAutoreleasePool");
97         SEL stringWithUTF8String = sel_registerName ("stringWithUTF8String:");
98         SEL retain = sel_registerName ("retain");
99         id pool;
100
101         nsthread = (Class)objc_getClass ("NSThread");
102         nsobject = (Class)objc_getClass ("NSObject");
103
104         init = sel_registerName ("init");
105         alloc = sel_registerName ("alloc");
106         release = sel_registerName ("release");
107         dealloc = sel_registerName ("dealloc");
108         
109
110         currentThread = sel_registerName ("currentThread");
111         threadDictionary = sel_registerName ("threadDictionary");
112         setObjectForKey = sel_registerName ("setObject:forKey:");
113         objectForKey = sel_registerName ("objectForKey:");
114
115         // define the dead letter class
116         mono_dead_letter_class = objc_allocateClassPair (nsobject, "MonoDeadLetter", 0);
117         class_addMethod (mono_dead_letter_class, dealloc, (IMP)mono_dead_letter_dealloc, "v@:");
118         objc_registerClassPair (mono_dead_letter_class);
119
120         // create the dict key
121         pool = objc_msgSend (objc_msgSend (nsautoreleasepool, alloc), init);
122
123         id (*objc_msgSend_char)(id, SEL, const char*) = (id (*)(id, SEL, const char*)) objc_msgSend;
124         mono_dead_letter_key = objc_msgSend_char (nsstring, stringWithUTF8String, "mono-dead-letter");
125
126         objc_msgSend (mono_dead_letter_key, retain);
127         objc_msgSend (pool, release);
128 }
129 #endif