[build] Skips RemoteExecuted bases tests on monodroid
[mono-project.git] / mono / utils / mono-threads-posix.c
blob5c5d8d9116b40f544090eb6038ee2e8ba0f7129d
1 /**
2 * \file
3 * Low-level threading, posix version
5 * Author:
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2011 Novell, Inc
9 */
11 #include <config.h>
13 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
14 #if defined (__MACH__)
15 #define _DARWIN_C_SOURCE 1
16 #endif
18 #if defined (__HAIKU__)
19 #include <os/kernel/OS.h>
20 #endif
22 #include <mono/utils/mono-threads.h>
23 #include <mono/utils/mono-coop-semaphore.h>
24 #include <mono/metadata/gc-internals.h>
25 #include <mono/utils/mono-threads-debug.h>
27 #include <errno.h>
29 #if defined(HOST_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
30 #define USE_TKILL_ON_ANDROID 1
31 #endif
33 #ifdef USE_TKILL_ON_ANDROID
34 extern int tkill (pid_t tid, int signal);
35 #endif
37 #if defined(_POSIX_VERSION) && !defined (TARGET_WASM)
39 #include <pthread.h>
41 #include <sys/resource.h>
43 gboolean
44 mono_thread_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *tid)
46 pthread_attr_t attr;
47 pthread_t thread;
48 gint res;
49 gsize set_stack_size;
51 res = pthread_attr_init (&attr);
52 if (res != 0)
53 g_error ("%s: pthread_attr_init failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
55 if (stack_size)
56 set_stack_size = *stack_size;
57 else
58 set_stack_size = 0;
60 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
61 if (set_stack_size == 0) {
62 #if HAVE_VALGRIND_MEMCHECK_H
63 if (RUNNING_ON_VALGRIND)
64 set_stack_size = 1 << 20;
65 else
66 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
67 #else
68 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
69 #endif
72 #ifdef PTHREAD_STACK_MIN
73 if (set_stack_size < PTHREAD_STACK_MIN)
74 set_stack_size = PTHREAD_STACK_MIN;
75 #endif
77 res = pthread_attr_setstacksize (&attr, set_stack_size);
78 if (res != 0)
79 g_error ("%s: pthread_attr_setstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
80 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
82 /* Actually start the thread */
83 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
84 if (res) {
85 res = pthread_attr_destroy (&attr);
86 if (res != 0)
87 g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
89 return FALSE;
92 if (tid)
93 *tid = thread;
95 if (stack_size) {
96 res = pthread_attr_getstacksize (&attr, stack_size);
97 if (res != 0)
98 g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
101 res = pthread_attr_destroy (&attr);
102 if (res != 0)
103 g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
105 return TRUE;
108 void
109 mono_threads_platform_init (void)
113 gboolean
114 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
116 return FALSE;
119 gboolean
120 mono_threads_platform_yield (void)
122 return sched_yield () == 0;
125 void
126 mono_threads_platform_exit (gsize exit_code)
128 pthread_exit ((gpointer) exit_code);
132 ves_icall_System_Threading_Thread_SystemMaxStackSize (MonoError *error)
134 struct rlimit lim;
136 /* If getrlimit fails, we don't enforce any limits. */
137 if (getrlimit (RLIMIT_STACK, &lim))
138 return INT_MAX;
139 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
140 if (lim.rlim_max > (rlim_t)INT_MAX)
141 return INT_MAX;
142 return (int)lim.rlim_max;
146 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
148 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
150 int result;
152 #ifdef USE_TKILL_ON_ANDROID
153 int old_errno = errno;
155 result = tkill (info->native_handle, signum);
157 if (result < 0) {
158 result = errno;
159 errno = old_errno;
161 #elif defined (HAVE_PTHREAD_KILL)
162 result = pthread_kill (mono_thread_info_get_tid (info), signum);
163 #else
164 result = -1;
165 g_error ("pthread_kill () is not supported by this platform");
166 #endif
169 * ESRCH just means the thread is gone; this is usually not fatal.
171 * ENOTSUP can occur if we try to send signals (e.g. for sampling) to Grand
172 * Central Dispatch threads on Apple platforms. This is kinda bad, but
173 * since there's really nothing we can do about it, we just ignore it and
174 * move on.
176 * All other error codes are ill-documented and usually stem from various
177 * OS-specific idiosyncracies. We want to know about these, so fail loudly.
178 * One example is EAGAIN on Linux, which indicates a signal queue overflow.
180 if (result &&
181 result != ESRCH
182 #if defined (__MACH__) && defined (ENOTSUP)
183 && result != ENOTSUP
184 #endif
186 g_error ("%s: pthread_kill failed with error %d - potential kernel OOM or signal queue overflow", __func__, result);
188 return result;
191 MonoNativeThreadId
192 mono_native_thread_id_get (void)
194 return pthread_self ();
197 gboolean
198 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
200 return pthread_equal (id1, id2);
204 * mono_native_thread_create:
206 * Low level thread creation function without any GC wrappers.
208 gboolean
209 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
211 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
214 void
215 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
217 #ifdef __MACH__
219 * We can't set the thread name for other threads, but we can at least make
220 * it work for threads that try to change their own name.
222 if (tid != mono_native_thread_id_get ())
223 return;
225 if (!name) {
226 pthread_setname_np ("");
227 } else {
228 char n [63];
230 strncpy (n, name, sizeof (n) - 1);
231 n [sizeof (n) - 1] = '\0';
232 pthread_setname_np (n);
234 #elif defined (__HAIKU__)
235 thread_id haiku_tid;
236 haiku_tid = get_pthread_thread_id (tid);
237 if (!name) {
238 rename_thread (haiku_tid, "");
239 } else {
240 rename_thread (haiku_tid, name);
242 #elif defined (__NetBSD__)
243 if (!name) {
244 pthread_setname_np (tid, "%s", (void*)"");
245 } else {
246 char n [PTHREAD_MAX_NAMELEN_NP];
248 strncpy (n, name, sizeof (n) - 1);
249 n [sizeof (n) - 1] = '\0';
250 pthread_setname_np (tid, "%s", (void*)n);
252 #elif defined (HAVE_PTHREAD_SETNAME_NP)
253 if (!name) {
254 pthread_setname_np (tid, "");
255 } else {
256 char n [16];
258 strncpy (n, name, sizeof (n) - 1);
259 n [sizeof (n) - 1] = '\0';
260 pthread_setname_np (tid, n);
262 #endif
265 gboolean
266 mono_native_thread_join (MonoNativeThreadId tid)
268 void *res;
270 return !pthread_join (tid, &res);
273 #endif /* defined(_POSIX_VERSION) */
275 #if defined(USE_POSIX_BACKEND)
277 gboolean
278 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
280 int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () : mono_threads_suspend_get_suspend_signal ();
282 if (!mono_threads_pthread_kill (info, sig)) {
283 mono_threads_add_to_pending_operation_set (info);
284 return TRUE;
286 return FALSE;
289 gboolean
290 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
292 return info->suspend_can_continue;
296 This begins async resume. This function must do the following:
298 - Install an async target if one was requested.
299 - Notify the target to resume.
301 gboolean
302 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
304 int sig = mono_threads_suspend_get_restart_signal ();
306 if (!mono_threads_pthread_kill (info, sig)) {
307 mono_threads_add_to_pending_operation_set (info);
308 return TRUE;
310 return FALSE;
313 void
314 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
316 /* We signal a thread to break it from the current syscall.
317 * This signal should not be interpreted as a suspend request. */
318 info->syscall_break_signal = TRUE;
319 if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
320 mono_threads_add_to_pending_operation_set (info);
324 void
325 mono_threads_suspend_register (MonoThreadInfo *info)
327 #if defined (HOST_ANDROID)
328 info->native_handle = gettid ();
329 #endif
332 void
333 mono_threads_suspend_free (MonoThreadInfo *info)
337 void
338 mono_threads_suspend_init (void)
342 #endif /* defined(USE_POSIX_BACKEND) */