[finalizer] Suspend finalizer thread on after shutdown timeout (#4363)
[mono.git] / mono / metadata / threads-types.h
1 /*
2  * threads-types.h: Generic thread typedef support (includes
3  * system-specific files)
4  *
5  * Author:
6  *      Dick Porter (dick@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc
9  * (C) Copyright 2002-2006 Novell, Inc
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #ifndef _MONO_METADATA_THREADS_TYPES_H_
14 #define _MONO_METADATA_THREADS_TYPES_H_
15
16 #include <glib.h>
17
18 #include <mono/metadata/object.h>
19 #include "mono/utils/mono-compiler.h"
20 #include "mono/utils/mono-membar.h"
21 #include "mono/utils/mono-threads.h"
22
23 /* This is a copy of System.Threading.ThreadState */
24 typedef enum {
25         ThreadState_Running = 0x00000000,
26         ThreadState_StopRequested = 0x00000001,
27         ThreadState_SuspendRequested = 0x00000002,
28         ThreadState_Background = 0x00000004,
29         ThreadState_Unstarted = 0x00000008,
30         ThreadState_Stopped = 0x00000010,
31         ThreadState_WaitSleepJoin = 0x00000020,
32         ThreadState_Suspended = 0x00000040,
33         ThreadState_AbortRequested = 0x00000080,
34         ThreadState_Aborted = 0x00000100
35 } MonoThreadState; 
36
37 /* This is a copy of System.Threading.ApartmentState */
38 typedef enum {
39         ThreadApartmentState_STA = 0x00000000,
40         ThreadApartmentState_MTA = 0x00000001,
41         ThreadApartmentState_Unknown = 0x00000002
42 } MonoThreadApartmentState;
43
44 typedef enum {
45         MONO_THREAD_PRIORITY_LOWEST       = 0,
46         MONO_THREAD_PRIORITY_BELOW_NORMAL = 1,
47         MONO_THREAD_PRIORITY_NORMAL       = 2,
48         MONO_THREAD_PRIORITY_ABOVE_NORMAL = 3,
49         MONO_THREAD_PRIORITY_HIGHEST      = 4,
50 } MonoThreadPriority;
51
52 #define SPECIAL_STATIC_NONE 0
53 #define SPECIAL_STATIC_THREAD 1
54 #define SPECIAL_STATIC_CONTEXT 2
55
56 typedef struct _MonoInternalThread MonoInternalThread;
57
58 typedef void (*MonoThreadCleanupFunc) (MonoNativeThreadId tid);
59 /* INFO has type MonoThreadInfo* */
60 typedef void (*MonoThreadNotifyPendingExcFunc) (gpointer info);
61
62 MonoInternalThread* mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread, guint32 stack_size, MonoError *error);
63
64 void mono_threads_install_cleanup (MonoThreadCleanupFunc func);
65
66 void ves_icall_System_Threading_Thread_ConstructInternalThread (MonoThread *this_obj);
67 gpointer ves_icall_System_Threading_Thread_Thread_internal(MonoThread *this_obj, MonoObject *start);
68 void ves_icall_System_Threading_InternalThread_Thread_free_internal(MonoInternalThread *this_obj);
69 void ves_icall_System_Threading_Thread_Sleep_internal(gint32 ms);
70 gboolean ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms);
71 gint32 ves_icall_System_Threading_Thread_GetDomainID (void);
72 gboolean ves_icall_System_Threading_Thread_Yield (void);
73 MonoString* ves_icall_System_Threading_Thread_GetName_internal (MonoInternalThread *this_obj);
74 void ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name);
75 int ves_icall_System_Threading_Thread_GetPriority (MonoThread *this_obj);
76 void ves_icall_System_Threading_Thread_SetPriority (MonoThread *this_obj, int priority);
77 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoInternalThread *this_obj);
78 void ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this_obj, MonoObject *culture);
79 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoInternalThread *this_obj);
80 void ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this_obj, MonoObject *culture);
81 MonoThread *ves_icall_System_Threading_Thread_GetCurrentThread (void);
82
83 gint32 ves_icall_System_Threading_WaitHandle_WaitAll_internal(MonoArray *mono_handles, gint32 ms);
84 gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms);
85 gint32 ves_icall_System_Threading_WaitHandle_WaitOne_internal(gpointer handle, gint32 ms);
86 gint32 ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (gpointer toSignal, gpointer toWait, gint32 ms);
87
88 MonoArray* ves_icall_System_Threading_Thread_ByteArrayToRootDomain (MonoArray *arr);
89 MonoArray* ves_icall_System_Threading_Thread_ByteArrayToCurrentDomain (MonoArray *arr);
90
91 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
92 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
93 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
94 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
95
96 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int(gint32 *location, gint32 value);
97 gint64 ves_icall_System_Threading_Interlocked_Exchange_Long(gint64 *location, gint64 value);
98 MonoObject *ves_icall_System_Threading_Interlocked_Exchange_Object(MonoObject **location, MonoObject *value);
99 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr(gpointer *location, gpointer value);
100 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single(gfloat *location, gfloat value);
101 gdouble ves_icall_System_Threading_Interlocked_Exchange_Double(gdouble *location, gdouble value);
102
103 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand);
104 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int_Success(gint32 *location, gint32 value, gint32 comparand, MonoBoolean *success);
105 gint64 ves_icall_System_Threading_Interlocked_CompareExchange_Long(gint64 *location, gint64 value, gint64 comparand);
106 MonoObject *ves_icall_System_Threading_Interlocked_CompareExchange_Object(MonoObject **location, MonoObject *value, MonoObject *comparand);
107 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand);
108 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single(gfloat *location, gfloat value, gfloat comparand);
109 gdouble ves_icall_System_Threading_Interlocked_CompareExchange_Double(gdouble *location, gdouble value, gdouble comparand);
110 MonoObject* ves_icall_System_Threading_Interlocked_CompareExchange_T(MonoObject **location, MonoObject *value, MonoObject *comparand);
111 MonoObject* ves_icall_System_Threading_Interlocked_Exchange_T(MonoObject **location, MonoObject *value);
112
113 gint32 ves_icall_System_Threading_Interlocked_Add_Int(gint32 *location, gint32 value);
114 gint64 ves_icall_System_Threading_Interlocked_Add_Long(gint64 *location, gint64 value);
115 gint64 ves_icall_System_Threading_Interlocked_Read_Long(gint64 *location);
116
117 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
118 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
119
120 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
121 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
122
123 void ves_icall_System_Threading_Thread_Abort (MonoInternalThread *thread, MonoObject *state);
124 void ves_icall_System_Threading_Thread_ResetAbort (MonoThread *this_obj);
125 MonoObject* ves_icall_System_Threading_Thread_GetAbortExceptionState (MonoThread *thread);
126 void ves_icall_System_Threading_Thread_Suspend (MonoThread *this_obj);
127 void ves_icall_System_Threading_Thread_Resume (MonoThread *thread);
128 void ves_icall_System_Threading_Thread_ClrState (MonoInternalThread *thread, guint32 state);
129 void ves_icall_System_Threading_Thread_SetState (MonoInternalThread *thread, guint32 state);
130 guint32 ves_icall_System_Threading_Thread_GetState (MonoInternalThread *thread);
131
132 gint8 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr);
133 gint16 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr);
134 gint32 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr);
135 gint64 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr);
136 void * ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr);
137 void * ves_icall_System_Threading_Thread_VolatileReadObject (void *ptr);
138 double ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr);
139 float ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr);
140
141 void ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8);
142 void ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16);
143 void ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32);
144 void ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64);
145 void ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *);
146 void ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, MonoObject *);
147 void ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float);
148 void ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double);
149
150 gint8 ves_icall_System_Threading_Volatile_Read1 (void *ptr);
151 gint16 ves_icall_System_Threading_Volatile_Read2 (void *ptr);
152 gint32 ves_icall_System_Threading_Volatile_Read4 (void *ptr);
153 gint64 ves_icall_System_Threading_Volatile_Read8 (void *ptr);
154 void * ves_icall_System_Threading_Volatile_ReadIntPtr (void *ptr);
155 double ves_icall_System_Threading_Volatile_ReadDouble (void *ptr);
156 float ves_icall_System_Threading_Volatile_ReadFloat (void *ptr);
157 MonoObject* ves_icall_System_Threading_Volatile_Read_T (void *ptr);
158
159 void ves_icall_System_Threading_Volatile_Write1 (void *ptr, gint8);
160 void ves_icall_System_Threading_Volatile_Write2 (void *ptr, gint16);
161 void ves_icall_System_Threading_Volatile_Write4 (void *ptr, gint32);
162 void ves_icall_System_Threading_Volatile_Write8 (void *ptr, gint64);
163 void ves_icall_System_Threading_Volatile_WriteIntPtr (void *ptr, void *);
164 void ves_icall_System_Threading_Volatile_WriteFloat (void *ptr, float);
165 void ves_icall_System_Threading_Volatile_WriteDouble (void *ptr, double);
166 void ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value);
167
168 void ves_icall_System_Threading_Thread_MemoryBarrier (void);
169 void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this_obj);
170 void ves_icall_System_Threading_Thread_SpinWait_nop (void);
171
172 void ves_icall_System_Runtime_Remoting_Contexts_Context_RegisterContext (MonoAppContext *ctx);
173 void ves_icall_System_Runtime_Remoting_Contexts_Context_ReleaseContext (MonoAppContext *ctx);
174
175 MonoInternalThread *mono_thread_internal_current (void);
176
177 void mono_thread_internal_check_for_interruption_critical (MonoInternalThread *thread);
178
179 void mono_thread_internal_stop (MonoInternalThread *thread);
180 void mono_thread_internal_abort (MonoInternalThread *thread);
181 void mono_thread_internal_suspend_for_shutdown (MonoInternalThread *thread);
182
183 gboolean mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain);
184
185 void mono_thread_internal_reset_abort (MonoInternalThread *thread);
186
187 void mono_thread_internal_unhandled_exception (MonoObject* exc);
188
189 void mono_alloc_special_static_data_free (GHashTable *special_static_fields);
190 gboolean mono_thread_current_check_pending_interrupt (void);
191
192 void mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state);
193 void mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state);
194 gboolean mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test);
195 gboolean mono_thread_test_and_set_state (MonoInternalThread *thread, MonoThreadState test, MonoThreadState set);
196
197 void mono_thread_init_apartment_state (void);
198 void mono_thread_cleanup_apartment_state (void);
199
200 void mono_threads_set_shutting_down (void);
201
202 gunichar2* mono_thread_get_name (MonoInternalThread *this_obj, guint32 *name_len);
203
204 MONO_API MonoException* mono_thread_get_undeniable_exception (void);
205
206 void mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean permanent, gboolean reset, MonoError *error);
207
208 void mono_thread_suspend_all_other_threads (void);
209 gboolean mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout);
210
211 void mono_thread_push_appdomain_ref (MonoDomain *domain);
212 void mono_thread_pop_appdomain_ref (void);
213 gboolean mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain);
214
215 void mono_threads_clear_cached_culture (MonoDomain *domain);
216
217 MonoException* mono_thread_request_interruption (mono_bool running_managed);
218 gboolean mono_thread_interruption_requested (void);
219 MonoException* mono_thread_interruption_checkpoint (void);
220 MonoException* mono_thread_force_interruption_checkpoint_noraise (void);
221 gint32* mono_thread_interruption_request_flag (void);
222
223 uint32_t mono_alloc_special_static_data (uint32_t static_type, uint32_t size, uint32_t align, uintptr_t *bitmap, int numbits);
224 void*    mono_get_special_static_data   (uint32_t offset);
225 gpointer mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset);
226
227 MonoException* mono_thread_resume_interruption (void);
228 void mono_threads_perform_thread_dump (void);
229
230 gboolean
231 mono_thread_create_checked (MonoDomain *domain, gpointer func, gpointer arg, MonoError *error);
232
233 MonoThread *
234 mono_thread_attach_full (MonoDomain *domain, gboolean force_attach);
235
236 /* Can't include utils/mono-threads.h because of the THREAD_INFO_TYPE wizardry */
237 void mono_threads_add_joinable_thread (gpointer tid);
238 void mono_threads_join_threads (void);
239 void mono_thread_join (gpointer tid);
240
241 void mono_thread_detach_internal (MonoInternalThread *thread);
242
243 void ves_icall_System_Threading_Thread_GetStackTraces (MonoArray **out_threads, MonoArray **out_stack_traces);
244
245 MONO_API gpointer
246 mono_threads_attach_coop (MonoDomain *domain, gpointer *dummy);
247
248 MONO_API void
249 mono_threads_detach_coop (gpointer cookie, gpointer *dummy);
250
251 void mono_threads_begin_abort_protected_block (void);
252 gboolean mono_threads_end_abort_protected_block (void);
253 MonoException* mono_thread_try_resume_interruption (void);
254
255 gboolean
256 mono_thread_internal_current_is_attached (void);
257
258 void
259 mono_thread_internal_describe (MonoInternalThread *internal, GString *str);
260
261 gboolean
262 mono_thread_internal_is_current (MonoInternalThread *internal);
263
264 #endif /* _MONO_METADATA_THREADS_TYPES_H_ */