[coop] Cleanup
[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 G_BEGIN_DECLS
17
18 /* JIT specific interface */
19 extern volatile size_t mono_polling_required;
20
21 /* Runtime consumable API */
22
23 static gboolean G_GNUC_UNUSED
24 mono_threads_is_coop_enabled (void)
25 {
26 #if defined(USE_COOP_GC)
27         return TRUE;
28 #else
29         static gboolean is_coop_enabled = -1;
30         if (G_UNLIKELY (is_coop_enabled == -1))
31                 is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? TRUE : FALSE;
32         return is_coop_enabled;
33 #endif
34 }
35
36 /* Internal API */
37
38 void
39 mono_threads_state_poll (void);
40
41 gpointer
42 mono_threads_prepare_blocking (gpointer stackdata);
43
44 void
45 mono_threads_finish_blocking (gpointer cookie, gpointer stackdata);
46
47 gpointer
48 mono_threads_reset_blocking_start (gpointer stackdata);
49
50 void
51 mono_threads_reset_blocking_end (gpointer cookie, gpointer stackdata);
52
53 static inline void
54 mono_threads_safepoint (void)
55 {
56         if (G_UNLIKELY (mono_polling_required))
57                 mono_threads_state_poll ();
58 }
59
60 #define MONO_PREPARE_BLOCKING   \
61         do {    \
62                 gpointer __dummy;       \
63                 gpointer __blocking_cookie = mono_threads_prepare_blocking (&__dummy)
64
65 #define MONO_FINISH_BLOCKING \
66                 mono_threads_finish_blocking (__blocking_cookie, &__dummy);     \
67         } while (0)
68
69 #define MONO_PREPARE_RESET_BLOCKING     \
70         do {    \
71                 gpointer __dummy;       \
72                 gpointer __reset_cookie = mono_threads_reset_blocking_start (&__dummy)
73
74 #define MONO_FINISH_RESET_BLOCKING \
75                 mono_threads_reset_blocking_end (__reset_cookie, &__dummy);     \
76         } while (0)
77
78 G_END_DECLS
79
80 #endif