[2020-02] Fix leak in assembly-specific dllmap lookups (#21053)
[mono-project.git] / mono / metadata / threads-types.h
blobdc06fb565387afd4ea3c1b3832977ad262d6bdba
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
110 mono_thread_manage_internal (void);
112 /* Data owned by a MonoInternalThread that must live until both the finalizer
113 * for MonoInternalThread has run, and the underlying machine thread has
114 * detached.
116 * Normally a thread is first detached and then the InternalThread object is
117 * finalized and collected. However during shutdown, when the root domain is
118 * finalized, all the InternalThread objects are finalized first and the
119 * machine threads are detached later.
121 typedef struct {
122 MonoRefCount ref;
123 MonoCoopMutex *synch_cs;
124 } MonoLongLivedThreadData;
126 void mono_threads_install_cleanup (MonoThreadCleanupFunc func);
128 ICALL_EXPORT
129 void ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name);
131 ICALL_EXPORT
132 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentCulture (MonoInternalThread *this_obj);
134 ICALL_EXPORT
135 void ves_icall_System_Threading_Thread_SetCachedCurrentCulture (MonoThread *this_obj, MonoObject *culture);
137 ICALL_EXPORT
138 MonoObject* ves_icall_System_Threading_Thread_GetCachedCurrentUICulture (MonoInternalThread *this_obj);
140 ICALL_EXPORT
141 void ves_icall_System_Threading_Thread_SetCachedCurrentUICulture (MonoThread *this_obj, MonoObject *culture);
143 ICALL_EXPORT
144 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
146 ICALL_EXPORT
147 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
149 ICALL_EXPORT
150 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
152 ICALL_EXPORT
153 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
155 ICALL_EXPORT
156 gint32 ves_icall_System_Threading_Interlocked_Exchange_Int(gint32 *location, gint32 value);
158 ICALL_EXPORT
159 gint64 ves_icall_System_Threading_Interlocked_Exchange_Long(gint64 *location, gint64 value);
161 ICALL_EXPORT
162 void ves_icall_System_Threading_Interlocked_Exchange_Object (MonoObject *volatile*location, MonoObject *volatile*value, MonoObject *volatile*res);
164 ICALL_EXPORT
165 gpointer ves_icall_System_Threading_Interlocked_Exchange_IntPtr(gpointer *location, gpointer value);
167 ICALL_EXPORT
168 gfloat ves_icall_System_Threading_Interlocked_Exchange_Single(gfloat *location, gfloat value);
170 ICALL_EXPORT
171 gdouble ves_icall_System_Threading_Interlocked_Exchange_Double(gdouble *location, gdouble value);
173 ICALL_EXPORT
174 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int(gint32 *location, gint32 value, gint32 comparand);
176 ICALL_EXPORT
177 gint32 ves_icall_System_Threading_Interlocked_CompareExchange_Int_Success(gint32 *location, gint32 value, gint32 comparand, MonoBoolean *success);
179 ICALL_EXPORT
180 gint64 ves_icall_System_Threading_Interlocked_CompareExchange_Long(gint64 *location, gint64 value, gint64 comparand);
182 ICALL_EXPORT
183 void ves_icall_System_Threading_Interlocked_CompareExchange_Object (MonoObject *volatile*location, MonoObject *volatile*value, MonoObject *volatile*comparand, MonoObject *volatile*res);
185 ICALL_EXPORT
186 gpointer ves_icall_System_Threading_Interlocked_CompareExchange_IntPtr(gpointer *location, gpointer value, gpointer comparand);
188 ICALL_EXPORT
189 gfloat ves_icall_System_Threading_Interlocked_CompareExchange_Single(gfloat *location, gfloat value, gfloat comparand);
191 ICALL_EXPORT
192 gdouble ves_icall_System_Threading_Interlocked_CompareExchange_Double(gdouble *location, gdouble value, gdouble comparand);
194 ICALL_EXPORT
195 gint32 ves_icall_System_Threading_Interlocked_Add_Int(gint32 *location, gint32 value);
197 ICALL_EXPORT
198 gint64 ves_icall_System_Threading_Interlocked_Add_Long(gint64 *location, gint64 value);
200 ICALL_EXPORT
201 gint64 ves_icall_System_Threading_Interlocked_Read_Long(gint64 *location);
203 ICALL_EXPORT
204 gint32 ves_icall_System_Threading_Interlocked_Increment_Int(gint32 *location);
206 ICALL_EXPORT
207 gint64 ves_icall_System_Threading_Interlocked_Increment_Long(gint64 *location);
209 ICALL_EXPORT
210 gint32 ves_icall_System_Threading_Interlocked_Decrement_Int(gint32 *location);
212 ICALL_EXPORT
213 gint64 ves_icall_System_Threading_Interlocked_Decrement_Long(gint64 * location);
215 ICALL_EXPORT
216 void ves_icall_System_Threading_Interlocked_MemoryBarrierProcessWide (void);
218 ICALL_EXPORT
219 gint8 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr);
221 ICALL_EXPORT
222 gint16 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr);
224 ICALL_EXPORT
225 gint32 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr);
227 ICALL_EXPORT
228 gint64 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr);
230 ICALL_EXPORT
231 void * ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr);
233 ICALL_EXPORT
234 void * ves_icall_System_Threading_Thread_VolatileReadObject (void *ptr);
236 ICALL_EXPORT
237 double ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr);
239 ICALL_EXPORT
240 float ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr);
242 ICALL_EXPORT
243 void ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8);
245 ICALL_EXPORT
246 void ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16);
248 ICALL_EXPORT
249 void ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32);
251 ICALL_EXPORT
252 void ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64);
254 ICALL_EXPORT
255 void ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *);
257 ICALL_EXPORT
258 void ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, MonoObject *);
260 ICALL_EXPORT
261 void ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float);
263 ICALL_EXPORT
264 void ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double);
266 ICALL_EXPORT
267 gint64 ves_icall_System_Threading_Volatile_Read8 (void *ptr);
269 ICALL_EXPORT
270 guint64 ves_icall_System_Threading_Volatile_ReadU8 (void *ptr);
272 ICALL_EXPORT
273 double ves_icall_System_Threading_Volatile_ReadDouble (void *ptr);
275 ICALL_EXPORT
276 void ves_icall_System_Threading_Volatile_Write8 (void *ptr, gint64);
278 ICALL_EXPORT
279 void ves_icall_System_Threading_Volatile_WriteU8 (void *ptr, guint64);
281 ICALL_EXPORT
282 void ves_icall_System_Threading_Volatile_WriteDouble (void *ptr, double);
284 ICALL_EXPORT
285 void ves_icall_System_Threading_Thread_MemoryBarrier (void);
287 void
288 mono_threads_register_app_context (MonoAppContextHandle ctx, MonoError *error);
289 void
290 mono_threads_release_app_context (MonoAppContext* ctx, MonoError *error);
292 MONO_PROFILER_API MonoInternalThread *mono_thread_internal_current (void);
294 MonoInternalThreadHandle
295 mono_thread_internal_current_handle (void);
297 void mono_thread_internal_abort (MonoInternalThread *thread, gboolean appdomain_unload);
298 void mono_thread_internal_suspend_for_shutdown (MonoInternalThread *thread);
300 gboolean mono_thread_internal_has_appdomain_ref (MonoInternalThread *thread, MonoDomain *domain);
302 void mono_thread_internal_reset_abort (MonoInternalThread *thread);
304 void mono_thread_internal_unhandled_exception (MonoObject* exc);
306 void mono_alloc_special_static_data_free (GHashTable *special_static_fields);
307 gboolean mono_thread_current_check_pending_interrupt (void);
309 void mono_thread_set_state (MonoInternalThread *thread, MonoThreadState state);
310 void mono_thread_clr_state (MonoInternalThread *thread, MonoThreadState state);
311 gboolean mono_thread_test_state (MonoInternalThread *thread, MonoThreadState test);
312 gboolean mono_thread_test_and_set_state (MonoInternalThread *thread, MonoThreadState test, MonoThreadState set);
313 void mono_thread_clear_and_set_state (MonoInternalThread *thread, MonoThreadState clear, MonoThreadState set);
315 void mono_thread_init_apartment_state (void);
316 void mono_thread_cleanup_apartment_state (void);
318 void mono_threads_set_shutting_down (void);
320 MONO_API MonoException* mono_thread_get_undeniable_exception (void);
322 ICALL_EXPORT
323 void ves_icall_thread_finish_async_abort (void);
325 #if HOST_WIN32
327 void
328 mono_thread_set_name_windows (HANDLE thread_handle, PCWSTR thread_name);
330 #define MONO_THREAD_NAME_WINDOWS_CONSTANT(x) L ## x
332 #else
334 #define mono_thread_set_name_windows(thread_handle, thread_name) /* nothing */
336 #define MONO_THREAD_NAME_WINDOWS_CONSTANT(x) NULL
338 #endif
340 typedef enum {
341 MonoSetThreadNameFlag_None = 0x0000,
342 MonoSetThreadNameFlag_Permanent = 0x0001,
343 MonoSetThreadNameFlag_Reset = 0x0002,
344 MonoSetThreadNameFlag_Constant = 0x0004,
345 MonoSetThreadNameFlag_RepeatedlyButOptimized = 0x0008,
346 } MonoSetThreadNameFlags;
348 G_ENUM_FUNCTIONS (MonoSetThreadNameFlags)
350 MONO_PROFILER_API
351 void
352 mono_thread_set_name (MonoInternalThread *thread,
353 const char* name8, size_t name8_length, const gunichar2* name16,
354 MonoSetThreadNameFlags flags, MonoError *error);
356 #define mono_thread_set_name_constant_ignore_error(thread, name, flags) \
357 mono_thread_set_name ((thread), name, G_N_ELEMENTS (name) - 1, \
358 MONO_THREAD_NAME_WINDOWS_CONSTANT (name), \
359 (flags) | MonoSetThreadNameFlag_Constant, NULL)
361 #ifndef ENABLE_NETCORE
362 void mono_thread_suspend_all_other_threads (void);
363 #endif
364 gboolean mono_threads_abort_appdomain_threads (MonoDomain *domain, int timeout);
366 void mono_thread_push_appdomain_ref (MonoDomain *domain);
367 void mono_thread_pop_appdomain_ref (void);
368 gboolean mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain);
370 gboolean mono_thread_interruption_requested (void);
372 ICALL_EXTERN_C
373 MonoException*
374 mono_thread_interruption_checkpoint (void);
376 gboolean
377 mono_thread_interruption_checkpoint_bool (void);
379 void
380 mono_thread_interruption_checkpoint_void (void);
382 MonoExceptionHandle
383 mono_thread_interruption_checkpoint_handle (void);
385 ICALL_EXTERN_C
386 MonoException* mono_thread_force_interruption_checkpoint_noraise (void);
389 * mono_thread_interruption_request_flag:
391 * A flag that will be non-zero if an interruption has
392 * been requested for a thread. The thread to interrupt may not be the current
393 * thread, so an additional call to mono_thread_interruption_requested () or
394 * mono_thread_interruption_checkpoint () is always needed if the flag is not
395 * zero.
397 extern gint32 mono_thread_interruption_request_flag;
399 uint32_t mono_alloc_special_static_data (uint32_t static_type, uint32_t size, uint32_t align, uintptr_t *bitmap, int numbits);
401 ICALL_EXTERN_C
402 void* mono_get_special_static_data (uint32_t offset);
404 gpointer mono_get_special_static_data_for_thread (MonoInternalThread *thread, guint32 offset);
406 void
407 mono_thread_resume_interruption (gboolean exec);
408 void mono_threads_perform_thread_dump (void);
410 // FIXME Correct the type of func and remove the template.
411 gboolean
412 mono_thread_create_checked (MonoDomain *domain, gpointer func, gpointer arg, MonoError *error);
414 #ifdef __cplusplus
415 template <typename T>
416 inline gboolean
417 mono_thread_create_checked (MonoDomain *domain, T func, gpointer arg, MonoError *error)
419 return mono_thread_create_checked (domain, (gpointer)func, arg, error);
421 #endif
423 void mono_threads_add_joinable_runtime_thread (MonoThreadInfo *thread_info);
424 void mono_threads_add_joinable_thread (gpointer tid);
425 void mono_threads_join_threads (void);
426 void mono_thread_join (gpointer tid);
428 MONO_API gpointer
429 mono_threads_attach_coop (MonoDomain *domain, gpointer *dummy);
431 MONO_API void
432 mono_threads_detach_coop (gpointer cookie, gpointer *dummy);
434 MonoDomain*
435 mono_threads_attach_coop_internal (MonoDomain *domain, gpointer *cookie, MonoStackData *stackdata);
437 void
438 mono_threads_detach_coop_internal (MonoDomain *orig_domain, gpointer cookie, MonoStackData *stackdata);
440 void mono_threads_begin_abort_protected_block (void);
441 gboolean mono_threads_end_abort_protected_block (void);
443 gboolean
444 mono_thread_internal_current_is_attached (void);
446 void
447 mono_thread_internal_describe (MonoInternalThread *internal, GString *str);
449 gboolean
450 mono_thread_internal_is_current (MonoInternalThread *internal);
452 gboolean
453 mono_threads_is_current_thread_in_protected_block (void);
455 gboolean
456 mono_threads_is_critical_method (MonoMethod *method);
458 gpointer
459 mono_threads_enter_gc_unsafe_region_unbalanced_internal (MonoStackData *stackdata);
461 void
462 mono_threads_exit_gc_unsafe_region_unbalanced_internal (gpointer cookie, MonoStackData *stackdata);
464 gpointer
465 mono_threads_enter_gc_safe_region_unbalanced_internal (MonoStackData *stackdata);
467 void
468 mono_threads_exit_gc_safe_region_unbalanced_internal (gpointer cookie, MonoStackData *stackdata);
470 // Set directory to store thread dumps captured by SIGQUIT
471 void
472 mono_set_thread_dump_dir(gchar* dir);
474 MONO_COLD void
475 mono_set_pending_exception_handle (MonoExceptionHandle exc);
477 #define MONO_MAX_SUMMARY_NAME_LEN 140
478 #define MONO_MAX_THREAD_NAME_LEN 140
479 #define MONO_MAX_SUMMARY_THREADS 32
480 #define MONO_MAX_SUMMARY_FRAMES 80
481 #define MONO_MAX_SUMMARY_EXCEPTIONS 15
483 typedef struct {
484 gboolean is_managed;
485 char str_descr [MONO_MAX_SUMMARY_NAME_LEN];
486 struct {
487 int token;
488 int il_offset;
489 int native_offset;
490 const char *guid;
492 #ifndef MONO_PRIVATE_CRASHES
493 // We use ifdef to make it a compile-time error to store this
494 // symbolicated string on release builds
495 const char *name;
496 #endif
497 const char *filename;
498 guint32 image_size;
499 guint32 time_date_stamp;
500 } managed_data;
501 struct {
502 intptr_t ip;
503 gint32 offset;
504 char module [MONO_MAX_SUMMARY_NAME_LEN];
505 gboolean is_trampoline;
506 gboolean has_name;
507 } unmanaged_data;
508 } MonoFrameSummary;
510 typedef struct {
511 MonoClass *managed_exc_type;
513 int num_managed_frames;
514 MonoFrameSummary managed_frames [MONO_MAX_SUMMARY_FRAMES];
515 } MonoExcSummary;
517 typedef struct {
518 guint64 offset_free_hash;
519 guint64 offset_rich_hash;
520 } MonoStackHash;
522 typedef struct {
523 gboolean done; // Needed because cond wait can have spurious wakeups
524 MonoSemType done_wait; // Readers are finished with this
526 // For managed stack walking
528 MonoDomain *domain;
529 MonoJitTlsData *jit_tls;
530 MonoLMF *lmf;
532 // Emitted attributes
534 gboolean is_managed;
536 char name [MONO_MAX_THREAD_NAME_LEN];
538 intptr_t info_addr;
539 intptr_t native_thread_id;
541 // Print reason we don't have a complete
542 // managed trace
543 const char *error_msg;
545 int num_managed_frames;
546 MonoFrameSummary managed_frames [MONO_MAX_SUMMARY_FRAMES];
548 int num_unmanaged_frames;
549 MonoFrameSummary unmanaged_frames [MONO_MAX_SUMMARY_FRAMES];
551 int num_exceptions;
552 MonoExcSummary exceptions [MONO_MAX_SUMMARY_EXCEPTIONS];
554 MonoStackHash hashes;
556 MonoContext *ctx;
557 MonoContext ctx_mem;
558 } MonoThreadSummary;
560 void
561 mono_threads_summarize_init (void);
563 gboolean
564 mono_threads_summarize (MonoContext *ctx, gchar **out, MonoStackHash *hashes, gboolean silent, gboolean signal_handler_controller, gchar *mem, size_t provided_size);
566 gboolean
567 mono_threads_summarize_execute (MonoContext *ctx, gchar **out, MonoStackHash *hashes, gboolean silent, gchar *mem, size_t provided_size);
569 gboolean
570 mono_threads_summarize_one (MonoThreadSummary *out, MonoContext *ctx);
572 #endif /* _MONO_METADATA_THREADS_TYPES_H_ */