Merge pull request #2996 from UCIS/patch-7
[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 gboolean
27 mono_threads_is_coop_enabled (void);
28
29 /* Internal API */
30
31 void
32 mono_threads_state_poll (void);
33
34 static inline void
35 mono_threads_safepoint (void)
36 {
37         if (G_UNLIKELY (mono_polling_required))
38                 mono_threads_state_poll ();
39 }
40
41 /*
42  * The following are used for wrappers and trampolines as their
43  * calls might be unbalanced, due to exception unwinding.
44  */
45
46 gpointer
47 mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata);
48
49 void
50 mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdata);
51
52 gpointer
53 mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata);
54
55 void
56 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata);
57
58 #define MONO_ENTER_GC_UNSAFE_UNBALANCED \
59         do {    \
60                 gpointer __dummy;       \
61                 gpointer __reset_cookie = mono_threads_enter_gc_unsafe_region_unbalanced (&__dummy)
62
63 #define MONO_EXIT_GC_UNSAFE_UNBALANCED  \
64                 mono_threads_exit_gc_unsafe_region_unbalanced (__reset_cookie, &__dummy);       \
65         } while (0)
66
67 G_END_DECLS
68
69 #endif