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