2 * runtime.c: Runtime functions
7 * Copyright 2010 Novell, Inc (http://www.novell.com)
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/class.h>
16 #include <mono/metadata/class-internals.h>
17 #include <mono/metadata/runtime.h>
18 #include <mono/metadata/monitor.h>
19 #include <mono/metadata/threads-types.h>
20 #include <mono/metadata/threadpool.h>
21 #include <mono/metadata/marshal.h>
22 #include <mono/utils/atomic.h>
24 static gboolean shutting_down_inited = FALSE;
25 static gboolean shutting_down = FALSE;
28 * mono_runtime_set_shutting_down:
30 * Invoked by System.Environment.Exit to flag that the runtime
33 * Deprecated. This function can break the shutdown sequence.
36 mono_runtime_set_shutting_down (void)
42 * mono_runtime_is_shutting_down:
44 * Returns whether the runtime has been flagged for shutdown.
46 * This is consumed by the P:System.Environment.HasShutdownStarted
51 mono_runtime_is_shutting_down (void)
57 fire_process_exit_event (MonoDomain *domain, gpointer user_data)
59 MonoClassField *field;
61 MonoObject *delegate, *exc;
63 field = mono_class_get_field_from_name (mono_defaults.appdomain_class, "ProcessExit");
66 delegate = *(MonoObject **)(((char *)domain->domain) + field->offset);
72 mono_runtime_delegate_invoke (delegate, pa, &exc);
76 mono_runtime_fire_process_exit_event (void)
78 #ifndef MONO_CROSS_COMPILE
79 mono_domain_foreach (fire_process_exit_event, NULL);
85 * Try to initialize runtime shutdown.
86 * After this call completes the thread pool will stop accepting new jobs and no further threads will be created.
88 * @return true if shutdown was initiated by this call or false is other thread beat this one
91 mono_runtime_try_shutdown (void)
93 if (InterlockedCompareExchange (&shutting_down_inited, TRUE, FALSE))
96 mono_runtime_fire_process_exit_event ();
100 mono_threads_set_shutting_down ();
102 /* No new threads will be created after this point */
104 mono_runtime_set_shutting_down ();
106 /* This will kill the tp threads which cannot be suspended */
107 mono_thread_pool_cleanup ();
109 /*TODO move the follow to here:
110 mono_thread_suspend_all_other_threads (); OR mono_thread_wait_all_other_threads
112 mono_runtime_quit ();
120 mono_runtime_is_critical_method (MonoMethod *method)
122 if (mono_monitor_is_il_fastpath_wrapper (method))
128 Coordinate the creation of all remaining TLS slots in the runtime.
129 No further TLS slots should be created after this function finishes.
130 This restriction exists because AOT requires offsets to be constant
134 mono_runtime_init_tls (void)
136 mono_marshal_init_tls ();
137 mono_thread_pool_init_tls ();
138 mono_thread_init_tls ();