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>
38 #ifdef MONO_THREADS_PLATFORM_HAS_ATTR_SETSCHED
40 mono_threads_platform_reset_priority (pthread_attr_t *attr)
42 struct sched_param param;
46 memset (¶m, 0, sizeof (param));
48 res = pthread_attr_getschedpolicy (attr, &policy);
50 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
52 #ifdef _POSIX_PRIORITY_SCHEDULING
55 /* Necessary to get valid priority range */
57 min = sched_get_priority_min (policy);
58 max = sched_get_priority_max (policy);
60 if (max > 0 && min >= 0 && max > min)
61 param.sched_priority = (max - min) / 2 + min;
68 param.sched_priority = 50;
74 param.sched_priority = 0;
77 g_warning ("%s: unknown policy %d", __func__, policy);
82 res = pthread_attr_setschedparam (attr, ¶m);
84 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
89 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *out_tid)
97 res = pthread_attr_init (&attr);
101 set_stack_size = *stack_size;
105 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
106 if (set_stack_size == 0) {
107 #if HAVE_VALGRIND_MEMCHECK_H
108 if (RUNNING_ON_VALGRIND)
109 set_stack_size = 1 << 20;
111 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
113 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
117 #ifdef PTHREAD_STACK_MIN
118 if (set_stack_size < PTHREAD_STACK_MIN)
119 set_stack_size = PTHREAD_STACK_MIN;
122 res = pthread_attr_setstacksize (&attr, set_stack_size);
124 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
126 mono_threads_platform_reset_priority (&attr);
129 res = pthread_attr_getstacksize (&attr, &min_stack_size);
131 g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", g_strerror (res), res);
133 *stack_size = min_stack_size;
136 /* Actually start the thread */
137 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
148 mono_threads_platform_init (void)
153 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
159 mono_threads_platform_yield (void)
161 return sched_yield () == 0;
165 mono_threads_platform_exit (gsize exit_code)
167 pthread_exit ((gpointer) exit_code);
171 mono_threads_get_max_stack_size (void)
175 /* If getrlimit fails, we don't enforce any limits. */
176 if (getrlimit (RLIMIT_STACK, &lim))
178 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
179 if (lim.rlim_max > (rlim_t)INT_MAX)
181 return (int)lim.rlim_max;
185 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
187 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
188 #ifdef USE_TKILL_ON_ANDROID
189 int result, old_errno = errno;
190 result = tkill (info->native_handle, signum);
196 #elif defined(__native_client__)
197 /* Workaround pthread_kill abort() in NaCl glibc. */
199 #elif !defined(HAVE_PTHREAD_KILL)
200 g_error ("pthread_kill() is not supported by this platform");
202 return pthread_kill (mono_thread_info_get_tid (info), signum);
207 mono_native_thread_id_get (void)
209 return pthread_self ();
213 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
215 return pthread_equal (id1, id2);
219 * mono_native_thread_create:
221 * Low level thread creation function without any GC wrappers.
224 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
226 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
230 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
234 * We can't set the thread name for other threads, but we can at least make
235 * it work for threads that try to change their own name.
237 if (tid != mono_native_thread_id_get ())
241 pthread_setname_np ("");
245 memcpy (n, name, sizeof (n) - 1);
246 n [sizeof (n) - 1] = '\0';
247 pthread_setname_np (n);
249 #elif defined (__NetBSD__)
251 pthread_setname_np (tid, "%s", (void*)"");
253 char n [PTHREAD_MAX_NAMELEN_NP];
255 memcpy (n, name, sizeof (n) - 1);
256 n [sizeof (n) - 1] = '\0';
257 pthread_setname_np (tid, "%s", (void*)n);
259 #elif defined (HAVE_PTHREAD_SETNAME_NP)
261 pthread_setname_np (tid, "");
265 memcpy (n, name, sizeof (n) - 1);
266 n [sizeof (n) - 1] = '\0';
267 pthread_setname_np (tid, n);
273 mono_native_thread_join (MonoNativeThreadId tid)
277 return !pthread_join (tid, &res);
280 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
282 #if defined(USE_POSIX_BACKEND)
285 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
287 int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () : mono_threads_suspend_get_suspend_signal ();
289 if (!mono_threads_pthread_kill (info, sig)) {
290 mono_threads_add_to_pending_operation_set (info);
297 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
299 return info->suspend_can_continue;
303 This begins async resume. This function must do the following:
305 - Install an async target if one was requested.
306 - Notify the target to resume.
309 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
311 int sig = mono_threads_suspend_get_restart_signal ();
313 if (!mono_threads_pthread_kill (info, sig)) {
314 mono_threads_add_to_pending_operation_set (info);
321 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
323 /* We signal a thread to break it from the current syscall.
324 * This signal should not be interpreted as a suspend request. */
325 info->syscall_break_signal = TRUE;
326 if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
327 mono_threads_add_to_pending_operation_set (info);
332 mono_threads_suspend_needs_abort_syscall (void)
338 mono_threads_suspend_register (MonoThreadInfo *info)
340 #if defined (PLATFORM_ANDROID)
341 info->native_handle = gettid ();
346 mono_threads_suspend_free (MonoThreadInfo *info)
351 mono_threads_suspend_init (void)
355 #endif /* defined(USE_POSIX_BACKEND) */