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/w32handle.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) || 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* const stack_size, MonoNativeThreadId *out_tid)
62 struct sched_param param;
67 res = pthread_attr_init (&attr);
71 set_stack_size = *stack_size;
75 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
76 if (set_stack_size == 0) {
77 #if HAVE_VALGRIND_MEMCHECK_H
78 if (RUNNING_ON_VALGRIND)
79 set_stack_size = 1 << 20;
81 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
83 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
87 #ifdef PTHREAD_STACK_MIN
88 if (set_stack_size < PTHREAD_STACK_MIN)
89 set_stack_size = PTHREAD_STACK_MIN;
92 res = pthread_attr_setstacksize (&attr, set_stack_size);
94 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
96 memset (¶m, 0, sizeof (param));
98 res = pthread_attr_getschedpolicy (&attr, &policy);
100 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
102 #ifdef _POSIX_PRIORITY_SCHEDULING
105 /* Necessary to get valid priority range */
107 min = sched_get_priority_min (policy);
108 max = sched_get_priority_max (policy);
110 if (max > 0 && min >= 0 && max > min)
111 param.sched_priority = (max - min) / 2 + min;
118 param.sched_priority = 50;
124 param.sched_priority = 0;
127 g_error ("%s: unknown policy %d", __func__, policy);
131 res = pthread_attr_setschedparam (&attr, ¶m);
133 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
136 res = pthread_attr_getstacksize (&attr, &min_size);
138 g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", g_strerror (res), res);
140 *stack_size = min_size;
143 /* Actually start the thread */
144 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
155 mono_threads_platform_yield (void)
157 return sched_yield () == 0;
161 mono_threads_platform_exit (int exit_code)
163 #if defined(__native_client__)
164 nacl_shutdown_gc_thread();
167 mono_thread_info_detach ();
173 mono_threads_platform_unregister (MonoThreadInfo *info)
175 g_assert (info->handle);
177 /* The thread is no longer active, so unref it */
178 mono_w32handle_unref (info->handle);
183 mono_threads_get_max_stack_size (void)
187 /* If getrlimit fails, we don't enforce any limits. */
188 if (getrlimit (RLIMIT_STACK, &lim))
190 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
191 if (lim.rlim_max > (rlim_t)INT_MAX)
193 return (int)lim.rlim_max;
197 mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
199 g_assert (info->handle);
200 mono_w32handle_ref (info->handle);
205 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
207 mono_w32handle_ref (handle);
213 mono_threads_platform_close_thread_handle (HANDLE handle)
215 mono_w32handle_unref (handle);
219 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
221 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
222 #ifdef USE_TKILL_ON_ANDROID
223 int result, old_errno = errno;
224 result = tkill (info->native_handle, signum);
230 #elif defined(__native_client__)
231 /* Workaround pthread_kill abort() in NaCl glibc. */
233 #elif !defined(HAVE_PTHREAD_KILL)
234 g_error ("pthread_kill() is not supported by this platform");
236 return pthread_kill (mono_thread_info_get_tid (info), signum);
241 mono_native_thread_id_get (void)
243 return pthread_self ();
247 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
249 return pthread_equal (id1, id2);
253 * mono_native_thread_create:
255 * Low level thread creation function without any GC wrappers.
258 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
260 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
264 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
268 * We can't set the thread name for other threads, but we can at least make
269 * it work for threads that try to change their own name.
271 if (tid != mono_native_thread_id_get ())
275 pthread_setname_np ("");
279 strncpy (n, name, 63);
281 pthread_setname_np (n);
283 #elif defined (__NetBSD__)
285 pthread_setname_np (tid, "%s", (void*)"");
287 char n [PTHREAD_MAX_NAMELEN_NP];
289 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
290 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
291 pthread_setname_np (tid, "%s", (void*)n);
293 #elif defined (HAVE_PTHREAD_SETNAME_NP)
295 pthread_setname_np (tid, "");
299 strncpy (n, name, 16);
301 pthread_setname_np (tid, n);
307 mono_native_thread_join (MonoNativeThreadId tid)
311 return !pthread_join (tid, &res);
315 mono_threads_platform_set_exited (gpointer handle)
320 if (mono_w32handle_issignalled (handle))
321 g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, handle, mono_native_thread_id_get ());
322 if (mono_w32handle_get_type (handle) == MONO_W32HANDLE_UNUSED)
323 g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, handle, mono_native_thread_id_get ());
325 thr_ret = mono_w32handle_lock_handle (handle);
326 g_assert (thr_ret == 0);
328 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
330 thr_ret = mono_w32handle_unlock_handle (handle);
331 g_assert (thr_ret == 0);
334 static const gchar* thread_typename (void)
339 static gsize thread_typesize (void)
344 static MonoW32HandleOps thread_ops = {
349 NULL, /* special_wait */
352 thread_typename, /* typename */
353 thread_typesize, /* typesize */
357 mono_threads_platform_init (void)
359 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
361 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
364 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
366 #if defined(USE_POSIX_BACKEND)
369 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
371 int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () : mono_threads_suspend_get_suspend_signal ();
373 if (!mono_threads_pthread_kill (info, sig)) {
374 mono_threads_add_to_pending_operation_set (info);
381 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
383 return info->suspend_can_continue;
387 This begins async resume. This function must do the following:
389 - Install an async target if one was requested.
390 - Notify the target to resume.
393 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
395 mono_threads_add_to_pending_operation_set (info);
396 return mono_threads_pthread_kill (info, mono_threads_suspend_get_restart_signal ()) == 0;
400 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
402 /* We signal a thread to break it from the current syscall.
403 * This signal should not be interpreted as a suspend request. */
404 info->syscall_break_signal = TRUE;
405 if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
406 mono_threads_add_to_pending_operation_set (info);
411 mono_threads_suspend_needs_abort_syscall (void)
417 mono_threads_suspend_register (MonoThreadInfo *info)
419 #if defined (PLATFORM_ANDROID)
420 info->native_handle = gettid ();
425 mono_threads_suspend_free (MonoThreadInfo *info)
430 mono_threads_suspend_init (void)
434 #endif /* defined(USE_POSIX_BACKEND) */