1 #include "config.h"
2 
3 #include <mono/utils/mono-threads.h>
4 #include <mono/utils/mono-mmap.h>
5 
6 #if defined (USE_WASM_BACKEND)
7 
8 #include <emscripten.h>
9 
10 
11 #define round_down(addr, val) ((void*)((addr) & ~((val) - 1)))
12 
13 EMSCRIPTEN_KEEPALIVE
14 static int
wasm_get_stack_base(void)15 wasm_get_stack_base (void)
16 {
17 	return EM_ASM_INT ({
18 		return STACK_BASE;
19 	});
20 }
21 
22 EMSCRIPTEN_KEEPALIVE
23 static int
wasm_get_stack_size(void)24 wasm_get_stack_size (void)
25 {
26 	return EM_ASM_INT ({
27 		return TOTAL_STACK;
28 	});
29 }
30 
31 
32 int
mono_threads_get_max_stack_size(void)33 mono_threads_get_max_stack_size (void)
34 {
35 	return wasm_get_stack_size ();
36 }
37 
38 
39 void
mono_threads_suspend_init_signals(void)40 mono_threads_suspend_init_signals (void)
41 {
42 }
43 
44 void
mono_threads_suspend_init(void)45 mono_threads_suspend_init (void)
46 {
47 }
48 
49 void
mono_threads_suspend_register(MonoThreadInfo * info)50 mono_threads_suspend_register (MonoThreadInfo *info)
51 {
52 }
53 
54 gboolean
mono_threads_suspend_begin_async_resume(MonoThreadInfo * info)55 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
56 {
57 	return TRUE;
58 }
59 
60 void
mono_threads_suspend_free(MonoThreadInfo * info)61 mono_threads_suspend_free (MonoThreadInfo *info)
62 {
63 }
64 
65 gboolean
mono_threads_suspend_begin_async_suspend(MonoThreadInfo * info,gboolean interrupt_kernel)66 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
67 {
68 	return TRUE;
69 }
70 
71 gboolean
mono_threads_suspend_check_suspend_result(MonoThreadInfo * info)72 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
73 {
74 	return TRUE;
75 }
76 
77 void
mono_threads_suspend_abort_syscall(MonoThreadInfo * info)78 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
79 {
80 }
81 
82 //----
83 
84 gboolean
mono_native_thread_id_equals(MonoNativeThreadId id1,MonoNativeThreadId id2)85 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
86 {
87 	return id1 == id2;
88 }
89 
90 
91 MonoNativeThreadId
mono_native_thread_id_get(void)92 mono_native_thread_id_get (void)
93 {
94 	return (MonoNativeThreadId)1;
95 }
96 
97 MONO_API gboolean
mono_native_thread_create(MonoNativeThreadId * tid,gpointer func,gpointer arg)98 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
99 {
100 	g_error ("WASM doesn't support threading");
101 }
102 
103 static const char *thread_name;
104 
105 void
mono_native_thread_set_name(MonoNativeThreadId tid,const char * name)106 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
107 {
108 	thread_name = g_strdup (name);
109 }
110 
111 gboolean
mono_native_thread_join(MonoNativeThreadId tid)112 mono_native_thread_join (MonoNativeThreadId tid)
113 {
114 	g_error ("WASM doesn't support threading");
115 	return FALSE;
116 }
117 
118 //----
119 
120 gboolean
mono_threads_platform_yield(void)121 mono_threads_platform_yield (void)
122 {
123 	return TRUE;
124 }
125 
126 void
mono_threads_platform_get_stack_bounds(guint8 ** staddr,size_t * stsize)127 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
128 {
129 	*staddr = (void*)wasm_get_stack_base ();
130 	*stsize = wasm_get_stack_size ();
131 }
132 
133 
134 gboolean
mono_thread_platform_create_thread(MonoThreadStart thread_fn,gpointer thread_data,gsize * const stack_size,MonoNativeThreadId * tid)135 mono_thread_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *tid)
136 {
137 	g_warning ("WASM doesn't support threading");
138 	return FALSE;
139 }
140 
mono_threads_platform_init(void)141 void mono_threads_platform_init (void)
142 {
143 }
144 
145 void
mono_threads_platform_exit(gsize exit_code)146 mono_threads_platform_exit (gsize exit_code)
147 {
148 	g_error ("WASM doesn't support threading");
149 }
150 
151 gboolean
mono_threads_platform_in_critical_region(MonoNativeThreadId tid)152 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
153 {
154 	return FALSE;
155 }
156 
157 #endif
158