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_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *out_tid)
44 struct sched_param param;
49 res = pthread_attr_init (&attr);
53 set_stack_size = *stack_size;
57 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
58 if (set_stack_size == 0) {
59 #if HAVE_VALGRIND_MEMCHECK_H
60 if (RUNNING_ON_VALGRIND)
61 set_stack_size = 1 << 20;
63 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
65 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
69 #ifdef PTHREAD_STACK_MIN
70 if (set_stack_size < PTHREAD_STACK_MIN)
71 set_stack_size = PTHREAD_STACK_MIN;
74 res = pthread_attr_setstacksize (&attr, set_stack_size);
76 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
78 memset (¶m, 0, sizeof (param));
80 res = pthread_attr_getschedpolicy (&attr, &policy);
82 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
84 #ifdef _POSIX_PRIORITY_SCHEDULING
87 /* Necessary to get valid priority range */
89 min = sched_get_priority_min (policy);
90 max = sched_get_priority_max (policy);
92 if (max > 0 && min >= 0 && max > min)
93 param.sched_priority = (max - min) / 2 + min;
100 param.sched_priority = 50;
106 param.sched_priority = 0;
109 g_error ("%s: unknown policy %d", __func__, policy);
113 res = pthread_attr_setschedparam (&attr, ¶m);
115 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
118 res = pthread_attr_getstacksize (&attr, &min_size);
120 g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", g_strerror (res), res);
122 *stack_size = min_size;
125 /* Actually start the thread */
126 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
137 mono_threads_platform_init (void)
142 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
148 mono_threads_platform_yield (void)
150 return sched_yield () == 0;
154 mono_threads_platform_exit (gsize exit_code)
156 pthread_exit ((gpointer) exit_code);
160 mono_threads_get_max_stack_size (void)
164 /* If getrlimit fails, we don't enforce any limits. */
165 if (getrlimit (RLIMIT_STACK, &lim))
167 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
168 if (lim.rlim_max > (rlim_t)INT_MAX)
170 return (int)lim.rlim_max;
174 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
176 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
177 #ifdef USE_TKILL_ON_ANDROID
178 int result, old_errno = errno;
179 result = tkill (info->native_handle, signum);
185 #elif defined(__native_client__)
186 /* Workaround pthread_kill abort() in NaCl glibc. */
188 #elif !defined(HAVE_PTHREAD_KILL)
189 g_error ("pthread_kill() is not supported by this platform");
191 return pthread_kill (mono_thread_info_get_tid (info), signum);
196 mono_native_thread_id_get (void)
198 return pthread_self ();
202 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
204 return pthread_equal (id1, id2);
208 * mono_native_thread_create:
210 * Low level thread creation function without any GC wrappers.
213 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
215 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
219 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
223 * We can't set the thread name for other threads, but we can at least make
224 * it work for threads that try to change their own name.
226 if (tid != mono_native_thread_id_get ())
230 pthread_setname_np ("");
234 strncpy (n, name, 63);
236 pthread_setname_np (n);
238 #elif defined (__NetBSD__)
240 pthread_setname_np (tid, "%s", (void*)"");
242 char n [PTHREAD_MAX_NAMELEN_NP];
244 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
245 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
246 pthread_setname_np (tid, "%s", (void*)n);
248 #elif defined (HAVE_PTHREAD_SETNAME_NP)
250 pthread_setname_np (tid, "");
254 strncpy (n, name, 16);
256 pthread_setname_np (tid, n);
262 mono_native_thread_join (MonoNativeThreadId tid)
266 return !pthread_join (tid, &res);
269 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
271 #if defined(USE_POSIX_BACKEND)
274 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
276 int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () : mono_threads_suspend_get_suspend_signal ();
278 if (!mono_threads_pthread_kill (info, sig)) {
279 mono_threads_add_to_pending_operation_set (info);
286 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
288 return info->suspend_can_continue;
292 This begins async resume. This function must do the following:
294 - Install an async target if one was requested.
295 - Notify the target to resume.
298 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
300 int sig = mono_threads_suspend_get_restart_signal ();
302 if (!mono_threads_pthread_kill (info, sig)) {
303 mono_threads_add_to_pending_operation_set (info);
310 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
312 /* We signal a thread to break it from the current syscall.
313 * This signal should not be interpreted as a suspend request. */
314 info->syscall_break_signal = TRUE;
315 if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
316 mono_threads_add_to_pending_operation_set (info);
321 mono_threads_suspend_needs_abort_syscall (void)
327 mono_threads_suspend_register (MonoThreadInfo *info)
329 #if defined (PLATFORM_ANDROID)
330 info->native_handle = gettid ();
335 mono_threads_suspend_free (MonoThreadInfo *info)
340 mono_threads_suspend_init (void)
344 #endif /* defined(USE_POSIX_BACKEND) */