First set of licensing changes
[mono.git] / mono / metadata / runtime.c
1 /*
2  * runtime.c: Runtime functions
3  *
4  * Authors:
5  *  Jonathan Pryor 
6  *
7  * Copyright 2010 Novell, Inc (http://www.novell.com)
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 #include <config.h>
12
13 #include <glib.h>
14
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/class.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/runtime.h>
19 #include <mono/metadata/monitor.h>
20 #include <mono/metadata/threads-types.h>
21 #include <mono/metadata/threadpool-ms.h>
22 #include <mono/metadata/marshal.h>
23 #include <mono/utils/atomic.h>
24
25 static gboolean shutting_down_inited = FALSE;
26 static gboolean shutting_down = FALSE;
27
28 /** 
29  * mono_runtime_set_shutting_down:
30  *
31  * Invoked by System.Environment.Exit to flag that the runtime
32  * is shutting down.
33  *
34  * Deprecated. This function can break the shutdown sequence.
35  */
36 void
37 mono_runtime_set_shutting_down (void)
38 {
39         shutting_down = TRUE;
40 }
41
42 /**
43  * mono_runtime_is_shutting_down:
44  *
45  * Returns whether the runtime has been flagged for shutdown.
46  *
47  * This is consumed by the P:System.Environment.HasShutdownStarted
48  * property.
49  *
50  */
51 gboolean
52 mono_runtime_is_shutting_down (void)
53 {
54         return shutting_down;
55 }
56
57 static void
58 fire_process_exit_event (MonoDomain *domain, gpointer user_data)
59 {
60         MonoClassField *field;
61         gpointer pa [2];
62         MonoObject *delegate, *exc;
63
64         field = mono_class_get_field_from_name (mono_defaults.appdomain_class, "ProcessExit");
65         g_assert (field);
66
67         delegate = *(MonoObject **)(((char *)domain->domain) + field->offset); 
68         if (delegate == NULL)
69                 return;
70
71         pa [0] = domain;
72         pa [1] = NULL;
73         mono_runtime_delegate_invoke (delegate, pa, &exc);
74 }
75
76 static void
77 mono_runtime_fire_process_exit_event (void)
78 {
79 #ifndef MONO_CROSS_COMPILE
80         mono_domain_foreach (fire_process_exit_event, NULL);
81 #endif
82 }
83
84
85 /**
86  * mono_runtime_try_shutdown:
87  *
88  * Try to initialize runtime shutdown.
89  *
90  * After this call completes the thread pool will stop accepting new jobs and no further threads will be created.
91  *
92  * Returns: TRUE if shutdown was initiated by this call or false is other thread beat this one.
93  */
94 gboolean
95 mono_runtime_try_shutdown (void)
96 {
97         if (InterlockedCompareExchange (&shutting_down_inited, TRUE, FALSE))
98                 return FALSE;
99
100         mono_runtime_fire_process_exit_event ();
101
102         shutting_down = TRUE;
103
104         mono_threads_set_shutting_down ();
105
106         /* No new threads will be created after this point */
107
108         mono_runtime_set_shutting_down ();
109
110         /* This will kill the tp threads which cannot be suspended */
111         mono_threadpool_ms_cleanup ();
112
113         /*TODO move the follow to here:
114         mono_thread_suspend_all_other_threads (); OR  mono_thread_wait_all_other_threads
115
116         mono_runtime_quit ();
117         */
118
119         return TRUE;
120 }
121
122
123 gboolean
124 mono_runtime_is_critical_method (MonoMethod *method)
125 {
126         return FALSE;
127 }
128
129 /*
130 Coordinate the creation of all remaining TLS slots in the runtime.
131 No further TLS slots should be created after this function finishes.
132 This restriction exists because AOT requires offsets to be constant
133 across runs.
134 */
135 void
136 mono_runtime_init_tls (void)
137 {
138         mono_marshal_init_tls ();
139         mono_thread_init_tls ();
140 }