2003-12-20 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / domain.c
1
2 /*
3  * domain.c: MonoDomain functions
4  *
5  * Author:
6  *      Dietmar Maurer (dietmar@ximian.com)
7  *      Patrik Torstensson
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <string.h>
15
16 #include <mono/os/gc_wrapper.h>
17
18 #include <mono/metadata/object.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/assembly.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/cil-coff.h>
23 #include <mono/metadata/rawbuffer.h>
24
25 //#define DEBUG_DOMAIN_UNLOAD
26
27 static guint32 appdomain_thread_id = -1;
28 static guint32 context_thread_id = -1;
29
30 static gint32 appdomain_id_counter = 0;
31
32 static MonoGHashTable * appdomains_list = NULL;
33
34 static CRITICAL_SECTION appdomains_mutex;
35
36 MonoDomain *mono_root_domain = NULL;
37
38 static MonoJitInfoTable *
39 mono_jit_info_table_new (void)
40 {
41         return g_array_new (FALSE, FALSE, sizeof (gpointer));
42 }
43
44 static void
45 mono_jit_info_table_free (MonoJitInfoTable *table)
46 {
47         g_array_free (table, TRUE);
48 }
49
50 static int
51 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
52 {
53         int left = 0, right = table->len;
54
55         while (left < right) {
56                 int pos = (left + right) / 2;
57                 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
58                 char *start = ji->code_start;
59                 char *end = start + ji->code_size;
60
61                 if (addr < start)
62                         right = pos;
63                 else if (addr >= end) 
64                         left = pos + 1;
65                 else
66                         return pos;
67         }
68
69         return left;
70 }
71
72 MonoJitInfo *
73 mono_jit_info_table_find (MonoDomain *domain, char *addr)
74 {
75         MonoJitInfoTable *table = domain->jit_info_table;
76         int left = 0, right;
77
78         mono_domain_lock (domain);
79
80         right = table->len;
81         while (left < right) {
82                 int pos = (left + right) / 2;
83                 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
84                 char *start = ji->code_start;
85                 char *end = start + ji->code_size;
86
87                 if (addr < start)
88                         right = pos;
89                 else if (addr >= end) 
90                         left = pos + 1;
91                 else {
92                         mono_domain_unlock (domain);
93                         return ji;
94                 }
95         }
96         mono_domain_unlock (domain);
97
98         /* maybe it is shared code, so we also search in the root domain */
99         if (domain != mono_root_domain)
100                 return mono_jit_info_table_find (mono_root_domain, addr);
101
102         return NULL;
103 }
104
105 void
106 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
107 {
108         MonoJitInfoTable *table = domain->jit_info_table;
109         gpointer start = ji->code_start;
110         int pos;
111
112         mono_domain_lock (domain);
113         pos = mono_jit_info_table_index (table, start);
114
115         g_array_insert_val (table, pos, ji);
116         mono_domain_unlock (domain);
117 }
118
119 static int
120 ldstr_hash (const char* str)
121 {
122         guint len, h;
123         const char *end;
124         len = mono_metadata_decode_blob_size (str, &str) - 1;
125         end = str + len;
126         /* if len == 0 *str will point to the mark byte */
127         h = len? *str: 0;
128         /*
129          * FIXME: The distribution may not be so nice with lots of
130          * null chars in the string.
131          */
132         for (str += 1; str < end; str++)
133                 h = (h << 5) - h + *str;
134         return h;
135 }
136
137 static gboolean
138 ldstr_equal (const char *str1, const char *str2) {
139         int len, len2;
140         len = mono_metadata_decode_blob_size (str1, NULL) - 1;
141         len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
142         if (len != len2)
143                 return 0;
144         return memcmp (str1, str2, len) == 0;
145 }
146
147 static gboolean
148 mono_string_equal (MonoString *s1, MonoString *s2)
149 {
150         int l1 = mono_string_length (s1);
151         int l2 = mono_string_length (s2);
152
153         if (l1 != l2)
154                 return FALSE;
155
156         return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0; 
157 }
158
159 static guint
160 mono_string_hash (MonoString *s)
161 {
162         const guint16 *p = mono_string_chars (s);
163         int i, len = mono_string_length (s);
164         guint h = 0;
165
166         for (i = 0; i < len; i++) {
167                 h = (h << 5) - h + *p;
168                 p++;
169         }
170
171         return h;       
172 }
173
174 #if HAVE_BOEHM_GC
175 static void
176 domain_finalizer (void *obj, void *data) {
177         g_print ("domain finalized\n");
178 }
179 #endif
180
181 MonoDomain *
182 mono_domain_create (void)
183 {
184         MonoDomain *domain;
185
186 #if HAVE_BOEHM_GC
187         domain = GC_MALLOC (sizeof (MonoDomain));
188         GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
189 #else
190         domain = g_new0 (MonoDomain, 1);
191 #endif
192         domain->domain = NULL;
193         domain->setup = NULL;
194         domain->friendly_name = NULL;
195         domain->search_path = NULL;
196
197         domain->mp = mono_mempool_new ();
198         domain->code_mp = mono_mempool_new ();
199         domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
200         domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
201         domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
202         domain->proxy_vtable_hash = mono_g_hash_table_new (NULL, NULL);
203         domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
204         domain->jit_code_hash = g_hash_table_new (NULL, NULL);
205         domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
206         domain->jit_info_table = mono_jit_info_table_new ();
207         domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
208         domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
209         domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
210
211         InitializeCriticalSection (&domain->lock);
212
213         EnterCriticalSection (&appdomains_mutex);
214         mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
215         LeaveCriticalSection (&appdomains_mutex);
216
217         return domain;
218 }
219
220 /**
221  * mono_init:
222  * 
223  * Creates the initial application domain and initializes the mono_defaults
224  * structure.
225  * This function is guaranteed to not run any IL code.
226  *
227  * Returns: the initial domain.
228  */
229 MonoDomain *
230 mono_init (const char *filename)
231 {
232         static MonoDomain *domain = NULL;
233         MonoAssembly *ass;
234         MonoImageOpenStatus status = MONO_IMAGE_OK;
235         MonoAssemblyName corlib_aname;
236
237         if (domain)
238                 g_assert_not_reached ();
239
240         appdomain_thread_id = TlsAlloc ();
241         context_thread_id = TlsAlloc ();
242
243         InitializeCriticalSection (&appdomains_mutex);
244
245         mono_metadata_init ();
246         mono_raw_buffer_init ();
247         mono_images_init ();
248         mono_assemblies_init ();
249         mono_loader_init ();
250
251         // FIXME: When should we release this memory?
252         appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
253
254         domain = mono_domain_create ();
255         mono_root_domain = domain;
256
257         TlsSetValue (appdomain_thread_id, domain);
258
259         /* find the corlib */
260         corlib_aname.name = "mscorlib";
261         ass = mono_assembly_load (&corlib_aname, NULL, &status);
262         if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
263                 switch (status){
264                 case MONO_IMAGE_ERROR_ERRNO:
265                         g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
266                         g_print ("It should have been installed in the `%s' directory.\n", MONO_ASSEMBLIES);
267                         break;
268                 case MONO_IMAGE_IMAGE_INVALID:
269                         g_print ("The file %s/mscorlib.dll is an invalid CIL image\n", MONO_ASSEMBLIES);
270                         break;
271                 case MONO_IMAGE_MISSING_ASSEMBLYREF:
272                         g_print ("Minning assembly reference in %s/mscorlib.dll\n", MONO_ASSEMBLIES);
273                         break;
274                 case MONO_IMAGE_OK:
275                         /* to suppress compiler warning */
276                         break;
277                 }
278                 
279                 exit (1);
280         }
281         mono_defaults.corlib = ass->image;
282
283         mono_defaults.object_class = mono_class_from_name (
284                 mono_defaults.corlib, "System", "Object");
285         g_assert (mono_defaults.object_class != 0);
286
287         mono_defaults.void_class = mono_class_from_name (
288                 mono_defaults.corlib, "System", "Void");
289         g_assert (mono_defaults.void_class != 0);
290
291         mono_defaults.boolean_class = mono_class_from_name (
292                 mono_defaults.corlib, "System", "Boolean");
293         g_assert (mono_defaults.boolean_class != 0);
294
295         mono_defaults.byte_class = mono_class_from_name (
296                 mono_defaults.corlib, "System", "Byte");
297         g_assert (mono_defaults.byte_class != 0);
298
299         mono_defaults.sbyte_class = mono_class_from_name (
300                 mono_defaults.corlib, "System", "SByte");
301         g_assert (mono_defaults.sbyte_class != 0);
302
303         mono_defaults.int16_class = mono_class_from_name (
304                 mono_defaults.corlib, "System", "Int16");
305         g_assert (mono_defaults.int16_class != 0);
306
307         mono_defaults.uint16_class = mono_class_from_name (
308                 mono_defaults.corlib, "System", "UInt16");
309         g_assert (mono_defaults.uint16_class != 0);
310
311         mono_defaults.int32_class = mono_class_from_name (
312                 mono_defaults.corlib, "System", "Int32");
313         g_assert (mono_defaults.int32_class != 0);
314
315         mono_defaults.uint32_class = mono_class_from_name (
316                 mono_defaults.corlib, "System", "UInt32");
317         g_assert (mono_defaults.uint32_class != 0);
318
319         mono_defaults.uint_class = mono_class_from_name (
320                 mono_defaults.corlib, "System", "UIntPtr");
321         g_assert (mono_defaults.uint_class != 0);
322
323         mono_defaults.int_class = mono_class_from_name (
324                 mono_defaults.corlib, "System", "IntPtr");
325         g_assert (mono_defaults.int_class != 0);
326
327         mono_defaults.int64_class = mono_class_from_name (
328                 mono_defaults.corlib, "System", "Int64");
329         g_assert (mono_defaults.int64_class != 0);
330
331         mono_defaults.uint64_class = mono_class_from_name (
332                 mono_defaults.corlib, "System", "UInt64");
333         g_assert (mono_defaults.uint64_class != 0);
334
335         mono_defaults.single_class = mono_class_from_name (
336                 mono_defaults.corlib, "System", "Single");
337         g_assert (mono_defaults.single_class != 0);
338
339         mono_defaults.double_class = mono_class_from_name (
340                 mono_defaults.corlib, "System", "Double");
341         g_assert (mono_defaults.double_class != 0);
342
343         mono_defaults.char_class = mono_class_from_name (
344                 mono_defaults.corlib, "System", "Char");
345         g_assert (mono_defaults.char_class != 0);
346
347         mono_defaults.string_class = mono_class_from_name (
348                 mono_defaults.corlib, "System", "String");
349         g_assert (mono_defaults.string_class != 0);
350
351         mono_defaults.enum_class = mono_class_from_name (
352                 mono_defaults.corlib, "System", "Enum");
353         g_assert (mono_defaults.enum_class != 0);
354
355         mono_defaults.array_class = mono_class_from_name (
356                 mono_defaults.corlib, "System", "Array");
357         g_assert (mono_defaults.array_class != 0);
358
359         mono_defaults.delegate_class = mono_class_from_name (
360                 mono_defaults.corlib, "System", "Delegate");
361         g_assert (mono_defaults.delegate_class != 0 );
362
363         mono_defaults.multicastdelegate_class = mono_class_from_name (
364                 mono_defaults.corlib, "System", "MulticastDelegate");
365         g_assert (mono_defaults.multicastdelegate_class != 0 );
366
367         mono_defaults.asyncresult_class = mono_class_from_name (
368                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", 
369                 "AsyncResult");
370         g_assert (mono_defaults.asyncresult_class != 0 );
371
372         mono_defaults.waithandle_class = mono_class_from_name (
373                 mono_defaults.corlib, "System.Threading", "WaitHandle");
374         g_assert (mono_defaults.waithandle_class != 0 );
375
376         mono_defaults.typehandle_class = mono_class_from_name (
377                 mono_defaults.corlib, "System", "RuntimeTypeHandle");
378         g_assert (mono_defaults.typehandle_class != 0);
379
380         mono_defaults.methodhandle_class = mono_class_from_name (
381                 mono_defaults.corlib, "System", "RuntimeMethodHandle");
382         g_assert (mono_defaults.methodhandle_class != 0);
383
384         mono_defaults.fieldhandle_class = mono_class_from_name (
385                 mono_defaults.corlib, "System", "RuntimeFieldHandle");
386         g_assert (mono_defaults.fieldhandle_class != 0);
387
388         mono_defaults.monotype_class = mono_class_from_name (
389                 mono_defaults.corlib, "System", "MonoType");
390         g_assert (mono_defaults.monotype_class != 0);
391
392         mono_defaults.exception_class = mono_class_from_name (
393                 mono_defaults.corlib, "System", "Exception");
394         g_assert (mono_defaults.exception_class != 0);
395
396         mono_defaults.threadabortexception_class = mono_class_from_name (
397                 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
398         g_assert (mono_defaults.threadabortexception_class != 0);
399
400         mono_defaults.thread_class = mono_class_from_name (
401                 mono_defaults.corlib, "System.Threading", "Thread");
402         g_assert (mono_defaults.thread_class != 0);
403
404         mono_defaults.appdomain_class = mono_class_from_name (
405                 mono_defaults.corlib, "System", "AppDomain");
406         g_assert (mono_defaults.appdomain_class != 0);
407
408         mono_defaults.transparent_proxy_class = mono_class_from_name (
409                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
410         g_assert (mono_defaults.transparent_proxy_class != 0);
411
412         mono_defaults.real_proxy_class = mono_class_from_name (
413                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
414         g_assert (mono_defaults.real_proxy_class != 0);
415
416         mono_defaults.mono_method_message_class = mono_class_from_name (
417                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
418         g_assert (mono_defaults.mono_method_message_class != 0);
419
420         mono_defaults.field_info_class = mono_class_from_name (
421                 mono_defaults.corlib, "System.Reflection", "FieldInfo");
422         g_assert (mono_defaults.field_info_class != 0);
423
424         mono_defaults.method_info_class = mono_class_from_name (
425                 mono_defaults.corlib, "System.Reflection", "MethodInfo");
426         g_assert (mono_defaults.method_info_class != 0);
427
428         mono_defaults.stringbuilder_class = mono_class_from_name (
429                 mono_defaults.corlib, "System.Text", "StringBuilder");
430         g_assert (mono_defaults.stringbuilder_class != 0);
431
432         mono_defaults.math_class = mono_class_from_name (
433                 mono_defaults.corlib, "System", "Math");
434         g_assert (mono_defaults.math_class != 0);
435
436         mono_defaults.stack_frame_class = mono_class_from_name (
437                 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
438         g_assert (mono_defaults.stack_frame_class != 0);
439
440         mono_defaults.stack_trace_class = mono_class_from_name (
441                 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
442         g_assert (mono_defaults.stack_trace_class != 0);
443
444         mono_defaults.marshal_class = mono_class_from_name (
445                 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
446         g_assert (mono_defaults.marshal_class != 0);
447
448         mono_defaults.iserializeable_class = mono_class_from_name (
449                 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
450         g_assert (mono_defaults.iserializeable_class != 0);
451
452         mono_defaults.serializationinfo_class = mono_class_from_name (
453                 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
454         g_assert (mono_defaults.serializationinfo_class != 0);
455
456         mono_defaults.streamingcontext_class = mono_class_from_name (
457                 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
458         g_assert (mono_defaults.streamingcontext_class != 0);
459
460         mono_defaults.typed_reference_class =  mono_class_from_name (
461                 mono_defaults.corlib, "System", "TypedReference");
462         g_assert (mono_defaults.typed_reference_class != 0);
463
464         mono_defaults.argumenthandle_class =  mono_class_from_name (
465                 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
466         g_assert (mono_defaults.argumenthandle_class != 0);
467
468         mono_defaults.marshalbyrefobject_class =  mono_class_from_name (
469                 mono_defaults.corlib, "System", "MarshalByRefObject");
470         g_assert (mono_defaults.marshalbyrefobject_class != 0);
471
472         mono_defaults.monitor_class =  mono_class_from_name (
473                 mono_defaults.corlib, "System.Threading", "Monitor");
474         g_assert (mono_defaults.monitor_class != 0);
475
476         mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
477                 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
478         g_assert (mono_defaults.iremotingtypeinfo_class != 0);
479
480         domain->friendly_name = g_path_get_basename (filename);
481
482         return domain;
483 }
484
485 /**
486  * mono_domain_get:
487  *
488  * Returns the current domain.
489  */
490 inline MonoDomain *
491 mono_domain_get ()
492 {
493         return ((MonoDomain *)TlsGetValue (appdomain_thread_id));
494 }
495
496 /**
497  * mono_domain_set_internal:
498  * @domain: the new domain
499  *
500  * Sets the current domain to @domain.
501  */
502 inline void
503 mono_domain_set_internal (MonoDomain *domain)
504 {
505         TlsSetValue (appdomain_thread_id, domain);
506         TlsSetValue (context_thread_id, domain->default_context);
507 }
508
509 typedef struct {
510         MonoDomainFunc func;
511         gpointer user_data;
512 } DomainInfo;
513
514 static void
515 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
516 {
517         MonoGHashTable *dest = (MonoGHashTable*)user_data;
518
519         mono_g_hash_table_insert (dest, key, data);
520 }
521
522 static void
523 foreach_domain (gpointer key, gpointer data, gpointer user_data)
524 {
525         DomainInfo *dom_info = user_data;
526
527         dom_info->func ((MonoDomain*)data, dom_info->user_data);
528 }
529
530 void
531 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
532 {
533         DomainInfo dom_info;
534         MonoGHashTable *copy;
535
536         /*
537          * Create a copy of the hashtable to avoid calling the user callback
538          * inside the lock because that could lead to deadlocks.
539          * We can do this because this function is not perf. critical.
540          */
541         copy = mono_g_hash_table_new (NULL, NULL);
542         EnterCriticalSection (&appdomains_mutex);
543         mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
544         LeaveCriticalSection (&appdomains_mutex);
545
546         dom_info.func = func;
547         dom_info.user_data = user_data;
548         mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
549
550         mono_g_hash_table_destroy (copy);
551 }
552
553 /**
554  * mono_domain_assembly_open:
555  * @domain: the application domain
556  * @name: file name of the assembly
557  *
558  * fixme: maybe we should integrate this with mono_assembly_open ??
559  */
560 MonoAssembly *
561 mono_domain_assembly_open (MonoDomain *domain, const char *name)
562 {
563         MonoAssembly *ass;
564
565         mono_domain_lock (domain);
566         if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
567                 mono_domain_unlock (domain);
568                 return ass;
569         }
570         mono_domain_unlock (domain);
571
572         if (!(ass = mono_assembly_open (name, NULL)))
573                 return NULL;
574
575         return ass;
576 }
577
578 static void
579 remove_assembly (gpointer key, gpointer value, gpointer user_data)
580 {
581         mono_assembly_close ((MonoAssembly *)value);
582 }
583
584 static void
585 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
586 {
587         g_slist_free (value);
588 }
589
590 void
591 mono_domain_free (MonoDomain *domain, gboolean force)
592 {
593         if ((domain == mono_root_domain) && !force) {
594                 g_warning ("cant unload root domain");
595                 return;
596         }
597
598         EnterCriticalSection (&appdomains_mutex);
599         mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
600         LeaveCriticalSection (&appdomains_mutex);
601         
602         g_free (domain->friendly_name);
603         g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
604
605         mono_g_hash_table_destroy (domain->env);
606         g_hash_table_destroy (domain->assemblies);
607         mono_g_hash_table_destroy (domain->class_vtable_hash);
608         mono_g_hash_table_destroy (domain->proxy_vtable_hash);
609         mono_g_hash_table_destroy (domain->static_data_hash);
610         g_hash_table_destroy (domain->jit_code_hash);
611         mono_g_hash_table_destroy (domain->ldstr_table);
612         mono_jit_info_table_free (domain->jit_info_table);
613 #ifdef DEBUG_DOMAIN_UNLOAD
614         mono_mempool_invalidate (domain->mp);
615         mono_mempool_invalidate (domain->code_mp);
616 #else
617         mono_mempool_destroy (domain->mp);
618         mono_mempool_destroy (domain->code_mp);
619 #endif  
620         if (domain->jump_target_hash) {
621                 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
622                 g_hash_table_destroy (domain->jump_target_hash);
623         }
624         mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
625         g_hash_table_destroy (domain->finalizable_objects_hash);
626         if (domain->special_static_fields)
627                 g_hash_table_destroy (domain->special_static_fields);
628         DeleteCriticalSection (&domain->lock);
629         domain->setup = NULL;
630
631         /* FIXME: anything else required ? */
632
633 #if HAVE_BOEHM_GC
634 #else
635         g_free (domain);
636 #endif
637
638         if ((domain == mono_root_domain))
639                 mono_root_domain = NULL;
640 }
641
642 /**
643  * mono_domain_get_id:
644  *
645  * Returns the a domain for a specific domain id.
646  */
647 MonoDomain * 
648 mono_domain_get_by_id (gint32 domainid) 
649 {
650         MonoDomain * domain;
651
652         EnterCriticalSection (&appdomains_mutex);
653         domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
654         LeaveCriticalSection (&appdomains_mutex);
655
656         return domain;
657 }
658
659 void 
660 mono_context_set (MonoAppContext * new_context)
661 {
662         TlsSetValue (context_thread_id, new_context);
663 }
664
665 MonoAppContext * 
666 mono_context_get ()
667 {
668         return ((MonoAppContext *)TlsGetValue (context_thread_id));
669 }