[coop] Use GC safe/unsafe macros inside the runtime
[mono.git] / mono / utils / mono-threads-coop.h
1 /*
2  * mono-threads-coop.h: Cooperative suspend thread helpers
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2015 Xamarin
8  */
9
10 #ifndef __MONO_THREADS_COOP_H__
11 #define __MONO_THREADS_COOP_H__
12
13 #include <config.h>
14 #include <glib.h>
15
16 #include "checked-build.h"
17 #include "mono-threads-api.h"
18
19 G_BEGIN_DECLS
20
21 /* JIT specific interface */
22 extern volatile size_t mono_polling_required;
23
24 /* Runtime consumable API */
25
26 static gboolean G_GNUC_UNUSED
27 mono_threads_is_coop_enabled (void)
28 {
29 #if defined(USE_COOP_GC)
30         return TRUE;
31 #else
32         static gboolean is_coop_enabled = -1;
33         if (G_UNLIKELY (is_coop_enabled == -1))
34                 is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? TRUE : FALSE;
35         return is_coop_enabled;
36 #endif
37 }
38
39 /* Internal API */
40
41 void
42 mono_threads_state_poll (void);
43
44 static inline void
45 mono_threads_safepoint (void)
46 {
47         if (G_UNLIKELY (mono_polling_required))
48                 mono_threads_state_poll ();
49 }
50
51 /*
52  * The following are used for wrappers and trampolines as their
53  * calls might be unbalanced, due to exception unwinding.
54  */
55
56 gpointer
57 mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata);
58
59 void
60 mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdata);
61
62 gpointer
63 mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata);
64
65 void
66 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata);
67
68 #define MONO_ENTER_GC_UNSAFE_UNBALANCED \
69         do {    \
70                 gpointer __dummy;       \
71                 gpointer __reset_cookie = mono_threads_enter_gc_unsafe_region_unbalanced (&__dummy)
72
73 #define MONO_EXIT_GC_UNSAFE_UNBALANCED  \
74                 mono_threads_exit_gc_unsafe_region_unbalanced (__reset_cookie, &__dummy);       \
75         } while (0)
76
77 G_END_DECLS
78
79 #endif