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