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/metadata/w32mutex-utils.h>
22 #include <mono/utils/w32handle.h>
26 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
27 #define USE_TKILL_ON_ANDROID 1
30 #ifdef USE_TKILL_ON_ANDROID
31 extern int tkill (pid_t tid, int signal);
34 #if defined(_POSIX_VERSION) || defined(__native_client__)
38 #include <sys/resource.h>
40 #if defined(__native_client__)
41 void nacl_shutdown_gc_thread(void);
45 mono_threads_platform_register (MonoThreadInfo *info)
47 gpointer thread_handle;
49 info->owned_mutexes = g_ptr_array_new ();
51 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
52 if (thread_handle == INVALID_HANDLE_VALUE)
53 g_error ("%s: failed to create handle", __func__);
55 g_assert (!info->handle);
56 info->handle = thread_handle;
60 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize stack_size, MonoNativeThreadId *out_tid)
65 struct sched_param param;
68 res = pthread_attr_init (&attr);
71 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
72 if (stack_size == 0) {
73 #if HAVE_VALGRIND_MEMCHECK_H
74 if (RUNNING_ON_VALGRIND)
77 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
79 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
83 #ifdef PTHREAD_STACK_MIN
84 if (stack_size < PTHREAD_STACK_MIN)
85 stack_size = PTHREAD_STACK_MIN;
88 res = pthread_attr_setstacksize (&attr, stack_size);
90 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
92 memset (¶m, 0, sizeof (param));
94 res = pthread_attr_getschedpolicy (&attr, &policy);
96 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
98 #ifdef _POSIX_PRIORITY_SCHEDULING
101 /* Necessary to get valid priority range */
103 min = sched_get_priority_min (policy);
104 max = sched_get_priority_max (policy);
106 if (max > 0 && min >= 0 && max > min)
107 param.sched_priority = (max - min) / 2 + min;
114 param.sched_priority = 50;
120 param.sched_priority = 0;
123 g_error ("%s: unknown policy %d", __func__, policy);
127 res = pthread_attr_setschedparam (&attr, ¶m);
129 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
131 /* Actually start the thread */
132 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
143 mono_threads_platform_yield (void)
145 return sched_yield () == 0;
149 mono_threads_platform_exit (int exit_code)
151 #if defined(__native_client__)
152 nacl_shutdown_gc_thread();
155 mono_thread_info_detach ();
161 mono_threads_platform_unregister (MonoThreadInfo *info)
163 mono_threads_platform_set_exited (info);
167 mono_threads_get_max_stack_size (void)
171 /* If getrlimit fails, we don't enforce any limits. */
172 if (getrlimit (RLIMIT_STACK, &lim))
174 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
175 if (lim.rlim_max > (rlim_t)INT_MAX)
177 return (int)lim.rlim_max;
181 mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
183 g_assert (info->handle);
184 mono_w32handle_ref (info->handle);
189 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
191 mono_w32handle_ref (handle);
197 mono_threads_platform_close_thread_handle (HANDLE handle)
199 mono_w32handle_unref (handle);
203 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
205 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
206 #ifdef USE_TKILL_ON_ANDROID
207 int result, old_errno = errno;
208 result = tkill (info->native_handle, signum);
214 #elif defined(__native_client__)
215 /* Workaround pthread_kill abort() in NaCl glibc. */
217 #elif !defined(HAVE_PTHREAD_KILL)
218 g_error ("pthread_kill() is not supported by this platform");
220 return pthread_kill (mono_thread_info_get_tid (info), signum);
225 mono_native_thread_id_get (void)
227 return pthread_self ();
231 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
233 return pthread_equal (id1, id2);
237 * mono_native_thread_create:
239 * Low level thread creation function without any GC wrappers.
242 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
244 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
248 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
252 * We can't set the thread name for other threads, but we can at least make
253 * it work for threads that try to change their own name.
255 if (tid != mono_native_thread_id_get ())
259 pthread_setname_np ("");
263 strncpy (n, name, 63);
265 pthread_setname_np (n);
267 #elif defined (__NetBSD__)
269 pthread_setname_np (tid, "%s", (void*)"");
271 char n [PTHREAD_MAX_NAMELEN_NP];
273 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
274 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
275 pthread_setname_np (tid, "%s", (void*)n);
277 #elif defined (HAVE_PTHREAD_SETNAME_NP)
279 pthread_setname_np (tid, "");
283 strncpy (n, name, 16);
285 pthread_setname_np (tid, n);
291 mono_threads_platform_set_exited (MonoThreadInfo *info)
293 gpointer mutex_handle;
297 g_assert (info->handle);
299 if (mono_w32handle_issignalled (info->handle))
300 g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, info->handle, mono_thread_info_get_tid (info));
301 if (mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED)
302 g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, info->handle, mono_thread_info_get_tid (info));
304 tid = pthread_self ();
306 for (i = 0; i < info->owned_mutexes->len; i++) {
307 mutex_handle = g_ptr_array_index (info->owned_mutexes, i);
308 mono_w32mutex_abandon (mutex_handle, tid);
309 mono_thread_info_disown_mutex (info, mutex_handle);
312 g_ptr_array_free (info->owned_mutexes, TRUE);
314 thr_ret = mono_w32handle_lock_handle (info->handle);
315 g_assert (thr_ret == 0);
317 mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
319 thr_ret = mono_w32handle_unlock_handle (info->handle);
320 g_assert (thr_ret == 0);
322 /* The thread is no longer active, so unref it */
323 mono_w32handle_unref (info->handle);
329 mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
331 mono_w32handle_ref (mutex_handle);
333 g_ptr_array_add (info->owned_mutexes, mutex_handle);
337 mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
339 mono_w32handle_unref (mutex_handle);
341 g_ptr_array_remove (info->owned_mutexes, mutex_handle);
344 static const gchar* thread_typename (void)
349 static gsize thread_typesize (void)
354 static MonoW32HandleOps thread_ops = {
359 NULL, /* special_wait */
362 thread_typename, /* typename */
363 thread_typesize, /* typesize */
367 mono_threads_platform_init (void)
369 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
371 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
374 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
376 #if defined(USE_POSIX_BACKEND)
379 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
381 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
383 if (!mono_threads_pthread_kill (info, sig)) {
384 mono_threads_add_to_pending_operation_set (info);
391 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
393 return info->suspend_can_continue;
397 This begins async resume. This function must do the following:
399 - Install an async target if one was requested.
400 - Notify the target to resume.
403 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
405 mono_threads_add_to_pending_operation_set (info);
406 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
410 mono_threads_suspend_register (MonoThreadInfo *info)
412 #if defined (PLATFORM_ANDROID)
413 info->native_handle = gettid ();
418 mono_threads_suspend_free (MonoThreadInfo *info)
423 mono_threads_suspend_init (void)
425 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
428 #endif /* defined(USE_POSIX_BACKEND) */