[mini] Always emit safepoints, except WASM
[mono-project.git] / mono / metadata / threads-types.h
blobc4045f5d06fbaec2872b50003fba93c4f142bd79
1 /**
2 * \file
3 * Generic thread typedef support (includes system-specific files)
5 * Author:
6 * Dick Porter (dick@ximian.com)
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.
13 #ifndef _MONO_METADATA_THREADS_TYPES_H_
14 #define _MONO_METADATA_THREADS_TYPES_H_
16 #include <glib.h>
18 #include <mono/utils/mono-forward-internal.h>
19 #include <mono/metadata/object.h>
20 #include "mono/metadata/handle.h"
21 #include "mono/utils/mono-compiler.h"
22 #include "mono/utils/mono-membar.h"
23 #include "mono/utils/mono-threads.h"
24 #include "mono/metadata/class-internals.h"
25 #include <mono/metadata/icalls.h>
27 /* This is a copy of System.Threading.ThreadState */
28 typedef enum {
29 ThreadState_Running = 0x00000000,
30 ThreadState_SuspendRequested = 0x00000002,
31 ThreadState_Background = 0x00000004,
32 ThreadState_Unstarted = 0x00000008,
33 ThreadState_Stopped = 0x00000010,
34 ThreadState_WaitSleepJoin = 0x00000020,
35 ThreadState_Suspended = 0x00000040,
36 ThreadState_AbortRequested = 0x00000080,
37 ThreadState_Aborted = 0x00000100
38 } MonoThreadState;
40 G_ENUM_FUNCTIONS (MonoThreadState)
42 /* This is a copy of System.Threading.ApartmentState */
43 typedef enum {
44 ThreadApartmentState_STA = 0x00000000,
45 ThreadApartmentState_MTA = 0x00000001,
46 ThreadApartmentState_Unknown = 0x00000002
47 } MonoThreadApartmentState;
49 typedef enum {
50 // These values match System.Threading.ThreadPriority.
51 // These values match System.Diagnostics.ThreadPriorityLevel and Windows, but are offset by 2.
52 MONO_THREAD_PRIORITY_LOWEST = 0,
53 MONO_THREAD_PRIORITY_BELOW_NORMAL = 1,
54 MONO_THREAD_PRIORITY_NORMAL = 2,
55 MONO_THREAD_PRIORITY_ABOVE_NORMAL = 3,
56 MONO_THREAD_PRIORITY_HIGHEST = 4,
57 } MonoThreadPriority;
59 #define SPECIAL_STATIC_NONE 0
60 #define SPECIAL_STATIC_THREAD 1
61 #define SPECIAL_STATIC_CONTEXT 2
63 /* It's safe to access System.Threading.InternalThread from native code via a
64 * raw pointer because all instances should be pinned. But for uniformity of
65 * icall wrapping, let's declare a MonoInternalThreadHandle anyway.
67 TYPED_HANDLE_DECL (MonoInternalThread);
69 typedef void (*MonoThreadCleanupFunc) (MonoNativeThreadId tid);
70 /* INFO has type MonoThreadInfo* */
71 typedef void (*MonoThreadNotifyPendingExcFunc) (gpointer info);
73 void
74 mono_thread_callbacks_init (void);
76 typedef enum {
77 MONO_THREAD_CREATE_FLAGS_NONE = 0x0,
78 MONO_THREAD_CREATE_FLAGS_THREADPOOL = 0x1,
79 MONO_THREAD_CREATE_FLAGS_DEBUGGER = 0x2,
80 MONO_THREAD_CREATE_FLAGS_FORCE_CREATE = 0x4,
81 MONO_THREAD_CREATE_FLAGS_SMALL_STACK = 0x8,
82 } MonoThreadCreateFlags;
84 // FIXME func should be MonoThreadStart and remove the template
85 MonoInternalThread*
86 mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, MonoThreadCreateFlags flags, MonoError *error);
88 #ifdef __cplusplus
89 template <typename T>
90 inline MonoInternalThread*
91 mono_thread_create_internal (MonoDomain *domain, T func, gpointer arg, MonoThreadCreateFlags flags, MonoError *error)
93 return mono_thread_create_internal(domain, (gpointer)func, arg, flags, error);
95 #endif
97 MonoInternalThreadHandle
98 mono_thread_create_internal_handle (MonoDomain *domain, gpointer func, gpointer arg, MonoThreadCreateFlags flags, MonoError *error);
100 #ifdef __cplusplus
101 template <typename T>
102 inline MonoInternalThreadHandle
103 mono_thread_create_internal_handle (MonoDomain *domain, T func, gpointer arg, MonoThreadCreateFlags flags, MonoError *error)
105 return mono_thread_create_internal_handle(domain, (gpointer)func, arg, flags, error);
107 #endif
109 void mono_threads_install_cleanup (MonoThreadCleanupFunc func);
111 ICALL_EXPORT
112 void ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name);
114 ICALL_EXPORT
115 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoInternalThread *this_obj);
117 ICALL_EXPORT
118 void ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this_obj, MonoObject *culture);
120 ICALL_EXPORT
121 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoInternalThread *this_obj);
123 ICALL_EXPORT
124 void ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this_obj, MonoObject *culture);
126 ICALL_EXPORT
127 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
129 ICALL_EXPORT
130 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
132 ICALL_EXPORT
133 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
135 ICALL_EXPORT
136 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
138 ICALL_EXPORT
139 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int(gint32 *location, gint32 value);
141 ICALL_EXPORT
142 gint64 ves_icall_System_Threading_Interlocked_Exchange_Long(gint64 *location, gint64 value);
144 ICALL_EXPORT
145 MonoObject *ves_icall_System_Threading_Interlocked_Exchange_Object(MonoObject **location, MonoObject *value);
147 ICALL_EXPORT
148 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr(gpointer *location, gpointer value);
150 ICALL_EXPORT
151 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single(gfloat *location, gfloat value);
153 ICALL_EXPORT
154 gdouble ves_icall_System_Threading_Interlocked_Exchange_Double(gdouble *location, gdouble value);
156 ICALL_EXPORT
157 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand);
159 ICALL_EXPORT
160 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int_Success(gint32 *location, gint32 value, gint32 comparand, MonoBoolean *success);
162 ICALL_EXPORT
163 gint64 ves_icall_System_Threading_Interlocked_CompareExchange_Long(gint64 *location, gint64 value, gint64 comparand);
165 ICALL_EXPORT
166 MonoObject *ves_icall_System_Threading_Interlocked_CompareExchange_Object(MonoObject **location, MonoObject *value, MonoObject *comparand);
168 ICALL_EXPORT
169 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand);
171 ICALL_EXPORT
172 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single(gfloat *location, gfloat value, gfloat comparand);
174 ICALL_EXPORT
175 gdouble ves_icall_System_Threading_Interlocked_CompareExchange_Double(gdouble *location, gdouble value, gdouble comparand);
177 ICALL_EXPORT
178 MonoObject* ves_icall_System_Threading_Interlocked_CompareExchange_T(MonoObject **location, MonoObject *value, MonoObject *comparand);
180 ICALL_EXPORT
181 MonoObject* ves_icall_System_Threading_Interlocked_Exchange_T(MonoObject **location, MonoObject *value);
183 ICALL_EXPORT
184 gint32 ves_icall_System_Threading_Interlocked_Add_Int(gint32 *location, gint32 value);
186 ICALL_EXPORT
187 gint64 ves_icall_System_Threading_Interlocked_Add_Long(gint64 *location, gint64 value);
189 ICALL_EXPORT
190 gint64 ves_icall_System_Threading_Interlocked_Read_Long(gint64 *location);
192 ICALL_EXPORT
193 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
195 ICALL_EXPORT
196 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
198 ICALL_EXPORT
199 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
201 ICALL_EXPORT
202 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
204 ICALL_EXPORT
205 gint8 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr);
207 ICALL_EXPORT
208 gint16 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr);
210 ICALL_EXPORT
211 gint32 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr);
213 ICALL_EXPORT
214 gint64 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr);
216 ICALL_EXPORT
217 void * ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr);
219 ICALL_EXPORT
220 void * ves_icall_System_Threading_Thread_VolatileReadObject (void *ptr);
222 ICALL_EXPORT
223 double ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr);
225 ICALL_EXPORT
226 float ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr);
228 ICALL_EXPORT
229 void ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8);
231 ICALL_EXPORT
232 void ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16);
234 ICALL_EXPORT
235 void ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32);
237 ICALL_EXPORT
238 void ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64);
240 ICALL_EXPORT
241 void ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *);
243 ICALL_EXPORT
244 void ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, MonoObject *);
246 ICALL_EXPORT
247 void ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float);
249 ICALL_EXPORT
250 void ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double);
252 ICALL_EXPORT
253 gint8 ves_icall_System_Threading_Volatile_Read1 (void *ptr);
255 ICALL_EXPORT
256 gint16 ves_icall_System_Threading_Volatile_Read2 (void *ptr);
258 ICALL_EXPORT
259 gint32 ves_icall_System_Threading_Volatile_Read4 (void *ptr);
261 ICALL_EXPORT
262 gint64 ves_icall_System_Threading_Volatile_Read8 (void *ptr);
264 ICALL_EXPORT
265 void * ves_icall_System_Threading_Volatile_ReadIntPtr (void *ptr);
267 ICALL_EXPORT
268 double ves_icall_System_Threading_Volatile_ReadDouble (void *ptr);
270 ICALL_EXPORT
271 float ves_icall_System_Threading_Volatile_ReadFloat (void *ptr);
273 ICALL_EXPORT
274 MonoObject* ves_icall_System_Threading_Volatile_Read_T (void *ptr);
276 ICALL_EXPORT
277 void ves_icall_System_Threading_Volatile_Write1 (void *ptr, gint8);
279 ICALL_EXPORT
280 void ves_icall_System_Threading_Volatile_Write2 (void *ptr, gint16);
282 ICALL_EXPORT
283 void ves_icall_System_Threading_Volatile_Write4 (void *ptr, gint32);
285 ICALL_EXPORT
286 void ves_icall_System_Threading_Volatile_Write8 (void *ptr, gint64);
288 ICALL_EXPORT
289 void ves_icall_System_Threading_Volatile_WriteIntPtr (void *ptr, void *);
291 ICALL_EXPORT
292 void ves_icall_System_Threading_Volatile_WriteFloat (void *ptr, float);
294 ICALL_EXPORT
295 void ves_icall_System_Threading_Volatile_WriteDouble (void *ptr, double);
297 ICALL_EXPORT
298 void ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value);
300 ICALL_EXPORT
301 void ves_icall_System_Threading_Thread_MemoryBarrier (void);
303 void
304 mono_threads_register_app_context (MonoAppContextHandle ctx, MonoError *error);
305 void
306 mono_threads_release_app_context (MonoAppContext* ctx, MonoError *error);
308 MONO_PROFILER_API MonoInternalThread *mono_thread_internal_current (void);
310 MonoInternalThreadHandle
311 mono_thread_internal_current_handle (void);
313 void mono_thread_internal_abort (MonoInternalThread *thread, gboolean appdomain_unload);
314 void mono_thread_internal_suspend_for_shutdown (MonoInternalThread *thread);
316 gboolean mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain);
318 void mono_thread_internal_reset_abort (MonoInternalThread *thread);
320 void mono_thread_internal_unhandled_exception (MonoObject* exc);
322 void mono_alloc_special_static_data_free (GHashTable *special_static_fields);
323 gboolean mono_thread_current_check_pending_interrupt (void);
325 void mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state);
326 void mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state);
327 gboolean mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test);
328 gboolean mono_thread_test_and_set_state (MonoInternalThread *thread, MonoThreadState test, MonoThreadState set);
329 void mono_thread_clear_and_set_state (MonoInternalThread *thread, MonoThreadState clear, MonoThreadState set);
331 void mono_thread_init_apartment_state (void);
332 void mono_thread_cleanup_apartment_state (void);
334 void mono_threads_set_shutting_down (void);
336 gunichar2* mono_thread_get_name (MonoInternalThread *this_obj, guint32 *name_len);
338 MONO_API MonoException* mono_thread_get_undeniable_exception (void);
340 ICALL_EXPORT
341 void ves_icall_thread_finish_async_abort (void);
343 MONO_PROFILER_API void mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean permanent, gboolean reset, MonoError *error);
345 void mono_thread_suspend_all_other_threads (void);
346 gboolean mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout);
348 void mono_thread_push_appdomain_ref (MonoDomain *domain);
349 void mono_thread_pop_appdomain_ref (void);
350 gboolean mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain);
352 gboolean mono_thread_interruption_requested (void);
354 MonoException*
355 mono_thread_interruption_checkpoint (void);
357 gboolean
358 mono_thread_interruption_checkpoint_bool (void);
360 void
361 mono_thread_interruption_checkpoint_void (void);
363 MonoExceptionHandle
364 mono_thread_interruption_checkpoint_handle (void);
366 MonoException* mono_thread_force_interruption_checkpoint_noraise (void);
367 gint32* mono_thread_interruption_request_flag (void);
369 uint32_t mono_alloc_special_static_data (uint32_t static_type, uint32_t size, uint32_t align, uintptr_t *bitmap, int numbits);
370 void* mono_get_special_static_data (uint32_t offset);
371 gpointer mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset);
373 void
374 mono_thread_resume_interruption (gboolean exec);
375 void mono_threads_perform_thread_dump (void);
377 // FIXME Correct the type of func and remove the template.
378 gboolean
379 mono_thread_create_checked (MonoDomain *domain, gpointer func, gpointer arg, MonoError *error);
381 #ifdef __cplusplus
382 template <typename T>
383 inline gboolean
384 mono_thread_create_checked (MonoDomain *domain, T func, gpointer arg, MonoError *error)
386 return mono_thread_create_checked (domain, (gpointer)func, arg, error);
388 #endif
390 void mono_threads_add_joinable_runtime_thread (MonoThreadInfo *thread_info);
391 void mono_threads_add_joinable_thread (gpointer tid);
392 void mono_threads_join_threads (void);
393 void mono_thread_join (gpointer tid);
395 ICALL_EXPORT
396 void ves_icall_System_Threading_Thread_GetStackTraces (MonoArray **out_threads, MonoArray **out_stack_traces);
398 MONO_API gpointer
399 mono_threads_attach_coop (MonoDomain *domain, gpointer *dummy);
401 MONO_API void
402 mono_threads_detach_coop (gpointer cookie, gpointer *dummy);
404 MonoDomain*
405 mono_threads_attach_coop_internal (MonoDomain *domain, gpointer *cookie, MonoStackData *stackdata);
407 void
408 mono_threads_detach_coop_internal (MonoDomain *orig_domain, gpointer cookie, MonoStackData *stackdata);
410 void mono_threads_begin_abort_protected_block (void);
411 gboolean mono_threads_end_abort_protected_block (void);
413 gboolean
414 mono_thread_internal_current_is_attached (void);
416 void
417 mono_thread_internal_describe (MonoInternalThread *internal, GString *str);
419 gboolean
420 mono_thread_internal_is_current (MonoInternalThread *internal);
422 gboolean
423 mono_thread_internal_is_current_handle (MonoInternalThreadHandle internal);
425 gboolean
426 mono_threads_is_current_thread_in_protected_block (void);
428 gboolean
429 mono_threads_is_critical_method (MonoMethod *method);
431 gpointer
432 mono_threads_enter_gc_unsafe_region_unbalanced_internal (MonoStackData *stackdata);
434 void
435 mono_threads_exit_gc_unsafe_region_unbalanced_internal (gpointer cookie, MonoStackData *stackdata);
437 gpointer
438 mono_threads_enter_gc_safe_region_unbalanced_internal (MonoStackData *stackdata);
440 void
441 mono_threads_exit_gc_safe_region_unbalanced_internal (gpointer cookie, MonoStackData *stackdata);
443 // Set directory to store thread dumps captured by SIGQUIT
444 void
445 mono_set_thread_dump_dir(gchar* dir);
447 MONO_COLD void
448 mono_set_pending_exception_handle (MonoExceptionHandle exc);
450 #define MONO_MAX_SUMMARY_NAME_LEN 140
451 #define MONO_MAX_THREAD_NAME_LEN 140
452 #define MONO_MAX_SUMMARY_THREADS 32
453 #define MONO_MAX_SUMMARY_FRAMES 80
455 typedef struct {
456 gboolean is_managed;
457 char str_descr [MONO_MAX_SUMMARY_NAME_LEN];
458 struct {
459 int token;
460 int il_offset;
461 int native_offset;
462 const char *guid;
464 #ifndef MONO_PRIVATE_CRASHES
465 // We use ifdef to make it a compile-time error to store this
466 // symbolicated string on release builds
467 const char *name;
468 #endif
470 } managed_data;
471 struct {
472 intptr_t ip;
473 gint32 offset;
474 const char *module;
475 gboolean is_trampoline;
476 gboolean has_name;
477 } unmanaged_data;
478 } MonoFrameSummary;
480 typedef struct {
481 guint64 offset_free_hash;
482 guint64 offset_rich_hash;
483 } MonoStackHash;
485 typedef struct {
486 gboolean done; // Needed because cond wait can have spurious wakeups
487 MonoSemType done_wait; // Readers are finished with this
489 // For managed stack walking
491 MonoDomain *domain;
492 MonoJitTlsData *jit_tls;
493 MonoLMF *lmf;
495 // Emitted attributes
497 gboolean is_managed;
499 char name [MONO_MAX_THREAD_NAME_LEN];
501 intptr_t managed_thread_ptr;
502 intptr_t info_addr;
503 intptr_t native_thread_id;
505 // Print reason we don't have a complete
506 // managed trace
507 const char *error_msg;
509 int num_managed_frames;
510 MonoFrameSummary managed_frames [MONO_MAX_SUMMARY_FRAMES];
512 int num_unmanaged_frames;
513 MonoFrameSummary unmanaged_frames [MONO_MAX_SUMMARY_FRAMES];
515 MonoStackHash hashes;
517 MonoContext *ctx;
518 MonoContext ctx_mem;
520 MonoClass *managed_exc_type;
521 } MonoThreadSummary;
523 gboolean
524 mono_threads_summarize (MonoContext *ctx, gchar **out, MonoStackHash *hashes, gboolean silent, gboolean signal_handler_controller, gchar *mem, size_t provided_size);
526 gboolean
527 mono_threads_summarize_execute (MonoContext *ctx, gchar **out, MonoStackHash *hashes, gboolean silent, gchar *mem, size_t provided_size);
529 gboolean
530 mono_threads_summarize_one (MonoThreadSummary *out, MonoContext *ctx);
532 #endif /* _MONO_METADATA_THREADS_TYPES_H_ */