2 * mono-threads-posix.c: Low-level threading, posix version
5 * Rodrigo Kumpera (kumpera@gmail.com)
12 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
13 #if defined (__MACH__)
14 #define _DARWIN_C_SOURCE 1
17 #include <mono/utils/mono-threads.h>
18 #include <mono/utils/mono-coop-semaphore.h>
19 #include <mono/metadata/gc-internals.h>
20 #include <mono/utils/mono-threads-debug.h>
24 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
25 #define USE_TKILL_ON_ANDROID 1
28 #ifdef USE_TKILL_ON_ANDROID
29 extern int tkill (pid_t tid, int signal);
32 #if defined(_POSIX_VERSION) || defined(__native_client__)
36 #include <sys/resource.h>
39 mono_thread_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *tid)
46 res = pthread_attr_init (&attr);
48 g_error ("%s: pthread_attr_init failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
51 set_stack_size = *stack_size;
55 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
56 if (set_stack_size == 0) {
57 #if HAVE_VALGRIND_MEMCHECK_H
58 if (RUNNING_ON_VALGRIND)
59 set_stack_size = 1 << 20;
61 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
63 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
67 #ifdef PTHREAD_STACK_MIN
68 if (set_stack_size < PTHREAD_STACK_MIN)
69 set_stack_size = PTHREAD_STACK_MIN;
72 res = pthread_attr_setstacksize (&attr, set_stack_size);
74 g_error ("%s: pthread_attr_setstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
75 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
77 /* Actually start the thread */
78 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
80 res = pthread_attr_destroy (&attr);
82 g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
91 res = pthread_attr_getstacksize (&attr, stack_size);
93 g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
96 res = pthread_attr_destroy (&attr);
98 g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
104 mono_threads_platform_init (void)
109 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
115 mono_threads_platform_yield (void)
117 return sched_yield () == 0;
121 mono_threads_platform_exit (gsize exit_code)
123 pthread_exit ((gpointer) exit_code);
127 mono_threads_get_max_stack_size (void)
131 /* If getrlimit fails, we don't enforce any limits. */
132 if (getrlimit (RLIMIT_STACK, &lim))
134 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
135 if (lim.rlim_max > (rlim_t)INT_MAX)
137 return (int)lim.rlim_max;
141 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
143 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
144 #ifdef USE_TKILL_ON_ANDROID
145 int result, old_errno = errno;
146 result = tkill (info->native_handle, signum);
152 #elif defined(__native_client__)
153 /* Workaround pthread_kill abort() in NaCl glibc. */
155 #elif !defined(HAVE_PTHREAD_KILL)
156 g_error ("pthread_kill() is not supported by this platform");
158 return pthread_kill (mono_thread_info_get_tid (info), signum);
163 mono_native_thread_id_get (void)
165 return pthread_self ();
169 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
171 return pthread_equal (id1, id2);
175 * mono_native_thread_create:
177 * Low level thread creation function without any GC wrappers.
180 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
182 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
186 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
190 * We can't set the thread name for other threads, but we can at least make
191 * it work for threads that try to change their own name.
193 if (tid != mono_native_thread_id_get ())
197 pthread_setname_np ("");
201 memcpy (n, name, sizeof (n) - 1);
202 n [sizeof (n) - 1] = '\0';
203 pthread_setname_np (n);
205 #elif defined (__NetBSD__)
207 pthread_setname_np (tid, "%s", (void*)"");
209 char n [PTHREAD_MAX_NAMELEN_NP];
211 memcpy (n, name, sizeof (n) - 1);
212 n [sizeof (n) - 1] = '\0';
213 pthread_setname_np (tid, "%s", (void*)n);
215 #elif defined (HAVE_PTHREAD_SETNAME_NP)
217 pthread_setname_np (tid, "");
221 memcpy (n, name, sizeof (n) - 1);
222 n [sizeof (n) - 1] = '\0';
223 pthread_setname_np (tid, n);
229 mono_native_thread_join (MonoNativeThreadId tid)
233 return !pthread_join (tid, &res);
236 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
238 #if defined(USE_POSIX_BACKEND)
241 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
243 int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () : mono_threads_suspend_get_suspend_signal ();
245 if (!mono_threads_pthread_kill (info, sig)) {
246 mono_threads_add_to_pending_operation_set (info);
253 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
255 return info->suspend_can_continue;
259 This begins async resume. This function must do the following:
261 - Install an async target if one was requested.
262 - Notify the target to resume.
265 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
267 int sig = mono_threads_suspend_get_restart_signal ();
269 if (!mono_threads_pthread_kill (info, sig)) {
270 mono_threads_add_to_pending_operation_set (info);
277 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
279 /* We signal a thread to break it from the current syscall.
280 * This signal should not be interpreted as a suspend request. */
281 info->syscall_break_signal = TRUE;
282 if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
283 mono_threads_add_to_pending_operation_set (info);
288 mono_threads_suspend_register (MonoThreadInfo *info)
290 #if defined (PLATFORM_ANDROID)
291 info->native_handle = gettid ();
296 mono_threads_suspend_free (MonoThreadInfo *info)
301 mono_threads_suspend_init (void)
305 #endif /* defined(USE_POSIX_BACKEND) */