3 * Low-level threading, posix version
6 * Rodrigo Kumpera (kumpera@gmail.com)
13 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
14 #if defined (__MACH__)
15 #define _DARWIN_C_SOURCE 1
18 #include <mono/utils/mono-threads.h>
19 #include <mono/utils/mono-coop-semaphore.h>
20 #include <mono/metadata/gc-internals.h>
21 #include <mono/utils/mono-threads-debug.h>
25 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
26 #define USE_TKILL_ON_ANDROID 1
29 #ifdef USE_TKILL_ON_ANDROID
30 extern int tkill (pid_t tid, int signal);
33 #if defined(_POSIX_VERSION)
37 #include <sys/resource.h>
40 mono_thread_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *tid)
47 res = pthread_attr_init (&attr);
49 g_error ("%s: pthread_attr_init failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
52 set_stack_size = *stack_size;
56 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
57 if (set_stack_size == 0) {
58 #if HAVE_VALGRIND_MEMCHECK_H
59 if (RUNNING_ON_VALGRIND)
60 set_stack_size = 1 << 20;
62 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
64 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
68 #ifdef PTHREAD_STACK_MIN
69 if (set_stack_size < PTHREAD_STACK_MIN)
70 set_stack_size = PTHREAD_STACK_MIN;
73 res = pthread_attr_setstacksize (&attr, set_stack_size);
75 g_error ("%s: pthread_attr_setstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
76 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
78 /* Actually start the thread */
79 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
81 res = pthread_attr_destroy (&attr);
83 g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
92 res = pthread_attr_getstacksize (&attr, stack_size);
94 g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
97 res = pthread_attr_destroy (&attr);
99 g_error ("%s: pthread_attr_destroy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
105 mono_threads_platform_init (void)
110 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
116 mono_threads_platform_yield (void)
118 return sched_yield () == 0;
122 mono_threads_platform_exit (gsize exit_code)
124 pthread_exit ((gpointer) exit_code);
128 mono_threads_get_max_stack_size (void)
132 /* If getrlimit fails, we don't enforce any limits. */
133 if (getrlimit (RLIMIT_STACK, &lim))
135 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
136 if (lim.rlim_max > (rlim_t)INT_MAX)
138 return (int)lim.rlim_max;
142 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
144 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
148 #ifdef USE_TKILL_ON_ANDROID
149 int old_errno = errno;
151 result = tkill (info->native_handle, signum);
157 #elif defined (HAVE_PTHREAD_KILL)
158 result = pthread_kill (mono_thread_info_get_tid (info), signum);
160 g_error ("pthread_kill () is not supported by this platform");
163 if (result && result != ESRCH)
164 g_error ("%s: pthread_kill failed with error %d - potential kernel OOM or signal queue overflow", __func__, result);
170 mono_native_thread_id_get (void)
172 return pthread_self ();
176 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
178 return pthread_equal (id1, id2);
182 * mono_native_thread_create:
184 * Low level thread creation function without any GC wrappers.
187 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
189 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
193 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
197 * We can't set the thread name for other threads, but we can at least make
198 * it work for threads that try to change their own name.
200 if (tid != mono_native_thread_id_get ())
204 pthread_setname_np ("");
208 memcpy (n, name, sizeof (n) - 1);
209 n [sizeof (n) - 1] = '\0';
210 pthread_setname_np (n);
212 #elif defined (__NetBSD__)
214 pthread_setname_np (tid, "%s", (void*)"");
216 char n [PTHREAD_MAX_NAMELEN_NP];
218 memcpy (n, name, sizeof (n) - 1);
219 n [sizeof (n) - 1] = '\0';
220 pthread_setname_np (tid, "%s", (void*)n);
222 #elif defined (HAVE_PTHREAD_SETNAME_NP)
224 pthread_setname_np (tid, "");
228 memcpy (n, name, sizeof (n) - 1);
229 n [sizeof (n) - 1] = '\0';
230 pthread_setname_np (tid, n);
236 mono_native_thread_join (MonoNativeThreadId tid)
240 return !pthread_join (tid, &res);
243 #endif /* defined(_POSIX_VERSION) */
245 #if defined(USE_POSIX_BACKEND)
248 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
250 int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () : mono_threads_suspend_get_suspend_signal ();
252 if (!mono_threads_pthread_kill (info, sig)) {
253 mono_threads_add_to_pending_operation_set (info);
260 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
262 return info->suspend_can_continue;
266 This begins async resume. This function must do the following:
268 - Install an async target if one was requested.
269 - Notify the target to resume.
272 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
274 int sig = mono_threads_suspend_get_restart_signal ();
276 if (!mono_threads_pthread_kill (info, sig)) {
277 mono_threads_add_to_pending_operation_set (info);
284 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
286 /* We signal a thread to break it from the current syscall.
287 * This signal should not be interpreted as a suspend request. */
288 info->syscall_break_signal = TRUE;
289 if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
290 mono_threads_add_to_pending_operation_set (info);
295 mono_threads_suspend_register (MonoThreadInfo *info)
297 #if defined (PLATFORM_ANDROID)
298 info->native_handle = gettid ();
303 mono_threads_suspend_free (MonoThreadInfo *info)
308 mono_threads_suspend_init (void)
312 #endif /* defined(USE_POSIX_BACKEND) */