update readme (#21797)
[mono-project.git] / mono / utils / mono-threads-coop.h
blob47c20c140253aa0dfd1db302cf15563c3bc508a5
1 /**
2 * \file
3 * Cooperative suspend thread helpers
5 * Author:
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2015 Xamarin
9 */
11 #ifndef __MONO_THREADS_COOP_H__
12 #define __MONO_THREADS_COOP_H__
14 #include <config.h>
15 #include <glib.h>
17 #include "checked-build.h"
18 #include "mono-threads.h"
19 #include "mono-threads-api.h"
20 #include "mono/metadata/icalls.h"
22 /* JIT specific interface */
23 extern volatile size_t mono_polling_required;
25 /* Internal API */
27 ICALL_EXTERN_C
28 void
29 mono_threads_state_poll (void);
31 // 0 also used internally for uninitialized
32 typedef enum {
33 MONO_THREADS_SUSPEND_FULL_PREEMPTIVE = 1,
34 MONO_THREADS_SUSPEND_FULL_COOP = 2,
35 MONO_THREADS_SUSPEND_HYBRID = 3,
36 } MonoThreadsSuspendPolicy;
38 static inline gboolean
39 mono_threads_suspend_policy_is_blocking_transition_enabled (MonoThreadsSuspendPolicy p)
41 switch (p) {
42 case MONO_THREADS_SUSPEND_FULL_COOP:
43 case MONO_THREADS_SUSPEND_HYBRID:
44 return TRUE;
45 case MONO_THREADS_SUSPEND_FULL_PREEMPTIVE:
46 return FALSE;
47 default:
48 g_assert_not_reached ();
52 static inline gboolean
53 mono_threads_suspend_policy_are_safepoints_enabled (MonoThreadsSuspendPolicy p)
55 switch (p) {
56 case MONO_THREADS_SUSPEND_FULL_COOP:
57 case MONO_THREADS_SUSPEND_HYBRID:
58 return TRUE;
59 default:
60 return FALSE;
64 static inline gboolean
65 mono_threads_suspend_policy_is_multiphase_stw_enabled (MonoThreadsSuspendPolicy p)
67 /* So far, hybrid suspend is the only one using a multi-phase STW */
68 return p == MONO_THREADS_SUSPEND_HYBRID;
71 gboolean
72 mono_threads_suspend_policy_is_blocking_transition_enabled (MonoThreadsSuspendPolicy p);
74 extern char mono_threads_suspend_policy_hidden_dont_modify;
76 static inline MonoThreadsSuspendPolicy
77 mono_threads_suspend_policy (void) {
78 return (MonoThreadsSuspendPolicy)mono_threads_suspend_policy_hidden_dont_modify;
81 void
82 mono_threads_suspend_policy_init (void);
84 const char*
85 mono_threads_suspend_policy_name (MonoThreadsSuspendPolicy p);
87 static inline gboolean
88 mono_threads_is_blocking_transition_enabled (void)
90 return mono_threads_suspend_policy_is_blocking_transition_enabled (mono_threads_suspend_policy ());
93 gboolean
94 mono_threads_is_cooperative_suspension_enabled (void);
96 gboolean
97 mono_threads_is_hybrid_suspension_enabled (void);
99 static inline gboolean
100 mono_threads_are_safepoints_enabled (void)
102 return mono_threads_suspend_policy_are_safepoints_enabled (mono_threads_suspend_policy ());
105 static inline gboolean
106 mono_threads_is_multiphase_stw_enabled (void)
108 return mono_threads_suspend_policy_is_multiphase_stw_enabled (mono_threads_suspend_policy ());
111 static inline void
112 mono_threads_safepoint (void)
114 if (G_UNLIKELY (mono_polling_required))
115 mono_threads_state_poll ();
118 /* Don't use this. */
119 void mono_threads_suspend_override_policy (MonoThreadsSuspendPolicy new_policy);
122 * The following are used when detaching a thread. We need to pass the MonoThreadInfo*
123 * as a paramater as the thread info TLS key is being destructed, meaning that
124 * mono_thread_info_current_unchecked will return NULL, which would lead to a
125 * runtime assertion error when trying to switch the state of the current thread.
128 MONO_PROFILER_API
129 gpointer
130 mono_threads_enter_gc_safe_region_with_info (THREAD_INFO_TYPE *info, MonoStackData *stackdata);
132 #define MONO_ENTER_GC_SAFE_WITH_INFO(info) \
133 do { \
134 MONO_STACKDATA (__gc_safe_dummy); \
135 gpointer __gc_safe_cookie = mono_threads_enter_gc_safe_region_with_info ((info), &__gc_safe_dummy)
137 #define MONO_EXIT_GC_SAFE_WITH_INFO MONO_EXIT_GC_SAFE
139 MONO_PROFILER_API
140 gpointer
141 mono_threads_enter_gc_unsafe_region_with_info (THREAD_INFO_TYPE *, MonoStackData *stackdata);
143 #define MONO_ENTER_GC_UNSAFE_WITH_INFO(info) \
144 do { \
145 MONO_STACKDATA (__gc_unsafe_dummy); \
146 gpointer __gc_unsafe_cookie = mono_threads_enter_gc_unsafe_region_with_info ((info), &__gc_unsafe_dummy)
148 #define MONO_EXIT_GC_UNSAFE_WITH_INFO MONO_EXIT_GC_UNSAFE
150 G_EXTERN_C // due to THREAD_INFO_TYPE varying
151 gpointer
152 mono_threads_enter_gc_unsafe_region_unbalanced_with_info (THREAD_INFO_TYPE *info, MonoStackData *stackdata);
154 extern char mono_threads_is_runtime_startup_finished_hidden_do_not_modify;
156 static inline gboolean
157 mono_threads_is_runtime_startup_finished (void)
159 return mono_threads_is_runtime_startup_finished_hidden_do_not_modify != 0;
162 void
163 mono_threads_set_runtime_startup_finished (void);
165 #endif