[System] Use GZipStream from corefx
[mono-project.git] / mono / metadata / runtime.c
blob4e6e08c2b3ec778a3842f87c84dc773d984bf343
1 /*
2 * runtime.c: Runtime functions
4 * Authors:
5 * Jonathan Pryor
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 */
11 #include <config.h>
13 #include <glib.h>
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.h>
22 #include <mono/metadata/marshal.h>
23 #include <mono/utils/atomic.h>
25 static gboolean shutting_down_inited = FALSE;
26 static gboolean shutting_down = FALSE;
28 /**
29 * mono_runtime_set_shutting_down:
31 * Invoked by System.Environment.Exit to flag that the runtime
32 * is shutting down.
34 * Deprecated. This function can break the shutdown sequence.
36 void
37 mono_runtime_set_shutting_down (void)
39 shutting_down = TRUE;
42 /**
43 * mono_runtime_is_shutting_down:
45 * Returns whether the runtime has been flagged for shutdown.
47 * This is consumed by the P:System.Environment.HasShutdownStarted
48 * property.
51 gboolean
52 mono_runtime_is_shutting_down (void)
54 return shutting_down;
57 static void
58 fire_process_exit_event (MonoDomain *domain, gpointer user_data)
60 MonoError error;
61 MonoClassField *field;
62 gpointer pa [2];
63 MonoObject *delegate, *exc;
65 field = mono_class_get_field_from_name (mono_defaults.appdomain_class, "ProcessExit");
66 g_assert (field);
68 delegate = *(MonoObject **)(((char *)domain->domain) + field->offset);
69 if (delegate == NULL)
70 return;
72 pa [0] = domain;
73 pa [1] = NULL;
74 mono_runtime_delegate_try_invoke (delegate, pa, &exc, &error);
75 mono_error_cleanup (&error);
78 static void
79 mono_runtime_fire_process_exit_event (void)
81 #ifndef MONO_CROSS_COMPILE
82 mono_domain_foreach (fire_process_exit_event, NULL);
83 #endif
87 /**
88 * mono_runtime_try_shutdown:
90 * Try to initialize runtime shutdown.
92 * After this call completes the thread pool will stop accepting new jobs and no further threads will be created.
94 * Returns: TRUE if shutdown was initiated by this call or false is other thread beat this one.
96 gboolean
97 mono_runtime_try_shutdown (void)
99 if (InterlockedCompareExchange (&shutting_down_inited, TRUE, FALSE))
100 return FALSE;
102 mono_runtime_fire_process_exit_event ();
104 shutting_down = TRUE;
106 mono_threads_set_shutting_down ();
108 /* No new threads will be created after this point */
110 mono_runtime_set_shutting_down ();
112 /* This will kill the tp threads which cannot be suspended */
113 mono_threadpool_cleanup ();
115 /*TODO move the follow to here:
116 mono_thread_suspend_all_other_threads (); OR mono_thread_wait_all_other_threads
118 mono_runtime_quit ();
121 return TRUE;
125 gboolean
126 mono_runtime_is_critical_method (MonoMethod *method)
128 return FALSE;
132 Coordinate the creation of all remaining TLS slots in the runtime.
133 No further TLS slots should be created after this function finishes.
134 This restriction exists because AOT requires offsets to be constant
135 across runs.
137 void
138 mono_runtime_init_tls (void)
140 mono_marshal_init_tls ();
143 char*
144 mono_runtime_get_aotid (void)
146 int i;
147 guint8 aotid_sum = 0;
148 MonoDomain* domain = mono_domain_get ();
150 if (!domain->entry_assembly || !domain->entry_assembly->image)
151 return NULL;
153 guint8 (*aotid)[16] = &domain->entry_assembly->image->aotid;
155 for (i = 0; i < 16; ++i)
156 aotid_sum |= (*aotid)[i];
158 if (aotid_sum == 0)
159 return NULL;
161 return mono_guid_to_string ((guint8*) aotid);