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-threads-posix-signals.h>
19 #include <mono/utils/mono-coop-semaphore.h>
20 #include <mono/metadata/gc-internals.h>
21 #include <mono/utils/w32handle.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) || defined(__native_client__)
37 #include <sys/resource.h>
39 #if defined(__native_client__)
40 void nacl_shutdown_gc_thread(void);
44 mono_threads_platform_register (MonoThreadInfo *info)
46 gpointer thread_handle;
48 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
49 if (thread_handle == INVALID_HANDLE_VALUE)
50 g_error ("%s: failed to create handle", __func__);
52 g_assert (!info->handle);
53 info->handle = thread_handle;
57 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize stack_size, MonoNativeThreadId *out_tid)
62 struct sched_param param;
65 res = pthread_attr_init (&attr);
68 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
69 if (stack_size == 0) {
70 #if HAVE_VALGRIND_MEMCHECK_H
71 if (RUNNING_ON_VALGRIND)
74 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
76 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
80 #ifdef PTHREAD_STACK_MIN
81 if (stack_size < PTHREAD_STACK_MIN)
82 stack_size = PTHREAD_STACK_MIN;
85 res = pthread_attr_setstacksize (&attr, stack_size);
87 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
89 memset (¶m, 0, sizeof (param));
91 res = pthread_attr_getschedpolicy (&attr, &policy);
93 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
95 #ifdef _POSIX_PRIORITY_SCHEDULING
98 /* Necessary to get valid priority range */
100 min = sched_get_priority_min (policy);
101 max = sched_get_priority_max (policy);
103 if (max > 0 && min >= 0 && max > min)
104 param.sched_priority = (max - min) / 2 + min;
111 param.sched_priority = 50;
117 param.sched_priority = 0;
120 g_error ("%s: unknown policy %d", __func__, policy);
124 res = pthread_attr_setschedparam (&attr, ¶m);
126 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
128 /* Actually start the thread */
129 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
140 mono_threads_platform_yield (void)
142 return sched_yield () == 0;
146 mono_threads_platform_exit (int exit_code)
148 #if defined(__native_client__)
149 nacl_shutdown_gc_thread();
152 mono_thread_info_detach ();
158 mono_threads_platform_unregister (MonoThreadInfo *info)
160 g_assert (info->handle);
162 /* The thread is no longer active, so unref it */
163 mono_w32handle_unref (info->handle);
168 mono_threads_get_max_stack_size (void)
172 /* If getrlimit fails, we don't enforce any limits. */
173 if (getrlimit (RLIMIT_STACK, &lim))
175 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
176 if (lim.rlim_max > (rlim_t)INT_MAX)
178 return (int)lim.rlim_max;
182 mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
184 g_assert (info->handle);
185 mono_w32handle_ref (info->handle);
190 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
192 mono_w32handle_ref (handle);
198 mono_threads_platform_close_thread_handle (HANDLE handle)
200 mono_w32handle_unref (handle);
204 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
206 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
207 #ifdef USE_TKILL_ON_ANDROID
208 int result, old_errno = errno;
209 result = tkill (info->native_handle, signum);
215 #elif defined(__native_client__)
216 /* Workaround pthread_kill abort() in NaCl glibc. */
218 #elif !defined(HAVE_PTHREAD_KILL)
219 g_error ("pthread_kill() is not supported by this platform");
221 return pthread_kill (mono_thread_info_get_tid (info), signum);
226 mono_native_thread_id_get (void)
228 return pthread_self ();
232 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
234 return pthread_equal (id1, id2);
238 * mono_native_thread_create:
240 * Low level thread creation function without any GC wrappers.
243 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
245 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
249 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
253 * We can't set the thread name for other threads, but we can at least make
254 * it work for threads that try to change their own name.
256 if (tid != mono_native_thread_id_get ())
260 pthread_setname_np ("");
264 strncpy (n, name, 63);
266 pthread_setname_np (n);
268 #elif defined (__NetBSD__)
270 pthread_setname_np (tid, "%s", (void*)"");
272 char n [PTHREAD_MAX_NAMELEN_NP];
274 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
275 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
276 pthread_setname_np (tid, "%s", (void*)n);
278 #elif defined (HAVE_PTHREAD_SETNAME_NP)
280 pthread_setname_np (tid, "");
284 strncpy (n, name, 16);
286 pthread_setname_np (tid, n);
292 mono_threads_platform_set_exited (gpointer handle)
297 if (mono_w32handle_issignalled (handle))
298 g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, handle, mono_native_thread_id_get ());
299 if (mono_w32handle_get_type (handle) == MONO_W32HANDLE_UNUSED)
300 g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, handle, mono_native_thread_id_get ());
302 thr_ret = mono_w32handle_lock_handle (handle);
303 g_assert (thr_ret == 0);
305 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
307 thr_ret = mono_w32handle_unlock_handle (handle);
308 g_assert (thr_ret == 0);
311 static const gchar* thread_typename (void)
316 static gsize thread_typesize (void)
321 static MonoW32HandleOps thread_ops = {
326 NULL, /* special_wait */
329 thread_typename, /* typename */
330 thread_typesize, /* typesize */
334 mono_threads_platform_init (void)
336 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
338 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
341 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
343 #if defined(USE_POSIX_BACKEND)
346 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
348 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
350 if (!mono_threads_pthread_kill (info, sig)) {
351 mono_threads_add_to_pending_operation_set (info);
358 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
360 return info->suspend_can_continue;
364 This begins async resume. This function must do the following:
366 - Install an async target if one was requested.
367 - Notify the target to resume.
370 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
372 mono_threads_add_to_pending_operation_set (info);
373 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
377 mono_threads_suspend_register (MonoThreadInfo *info)
379 #if defined (PLATFORM_ANDROID)
380 info->native_handle = gettid ();
385 mono_threads_suspend_free (MonoThreadInfo *info)
390 mono_threads_suspend_init (void)
392 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
395 #endif /* defined(USE_POSIX_BACKEND) */