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);
45 GPtrArray *owned_mutexes;
47 } MonoW32HandleThread;
50 thread_handle_create (void)
52 MonoW32HandleThread thread_data;
53 gpointer thread_handle;
55 thread_data.id = pthread_self ();
56 thread_data.owned_mutexes = g_ptr_array_new ();
57 thread_data.priority = MONO_THREAD_PRIORITY_NORMAL;
59 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, (gpointer) &thread_data);
60 if (thread_handle == INVALID_HANDLE_VALUE)
63 /* We need to keep the handle alive, as long as the corresponding managed
64 * thread object is alive. The handle is going to be unref when calling
65 * the finalizer on the MonoThreadInternal object */
66 mono_w32handle_ref (thread_handle);
72 win32_priority_to_posix_priority (MonoThreadPriority priority, int policy)
74 g_assert (priority >= MONO_THREAD_PRIORITY_LOWEST);
75 g_assert (priority <= MONO_THREAD_PRIORITY_HIGHEST);
77 /* Necessary to get valid priority range */
78 #ifdef _POSIX_PRIORITY_SCHEDULING
81 min = sched_get_priority_min (policy);
82 max = sched_get_priority_max (policy);
84 /* Partition priority range linearly (cross-multiply) */
85 if (max > 0 && min >= 0 && max > min)
86 return (int)((double) priority * (max - min) / (MONO_THREAD_PRIORITY_HIGHEST - MONO_THREAD_PRIORITY_LOWEST));
104 mono_threads_platform_register (MonoThreadInfo *info)
106 g_assert (!info->handle);
107 info->handle = thread_handle_create ();
111 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize stack_size, MonoNativeThreadId *out_tid)
117 res = pthread_attr_init (&attr);
120 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
121 if (stack_size == 0) {
122 #if HAVE_VALGRIND_MEMCHECK_H
123 if (RUNNING_ON_VALGRIND)
124 stack_size = 1 << 20;
126 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
128 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
132 #ifdef PTHREAD_STACK_MIN
133 if (stack_size < PTHREAD_STACK_MIN)
134 stack_size = PTHREAD_STACK_MIN;
137 res = pthread_attr_setstacksize (&attr, stack_size);
139 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
141 /* Actually start the thread */
142 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
153 mono_threads_platform_yield (void)
155 return sched_yield () == 0;
159 mono_threads_platform_exit (int exit_code)
161 #if defined(__native_client__)
162 nacl_shutdown_gc_thread();
165 mono_thread_info_detach ();
171 mono_threads_platform_unregister (MonoThreadInfo *info)
173 mono_threads_platform_set_exited (info);
177 mono_threads_get_max_stack_size (void)
181 /* If getrlimit fails, we don't enforce any limits. */
182 if (getrlimit (RLIMIT_STACK, &lim))
184 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
185 if (lim.rlim_max > (rlim_t)INT_MAX)
187 return (int)lim.rlim_max;
191 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
193 mono_w32handle_ref (handle);
199 mono_threads_platform_close_thread_handle (HANDLE handle)
201 mono_w32handle_unref (handle);
205 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
207 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
208 #ifdef USE_TKILL_ON_ANDROID
209 int result, old_errno = errno;
210 result = tkill (info->native_handle, signum);
216 #elif defined(__native_client__)
217 /* Workaround pthread_kill abort() in NaCl glibc. */
219 #elif !defined(HAVE_PTHREAD_KILL)
220 g_error ("pthread_kill() is not supported by this platform");
222 return pthread_kill (mono_thread_info_get_tid (info), signum);
227 mono_native_thread_id_get (void)
229 return pthread_self ();
233 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
235 return pthread_equal (id1, id2);
239 * mono_native_thread_create:
241 * Low level thread creation function without any GC wrappers.
244 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
246 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
250 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
254 * We can't set the thread name for other threads, but we can at least make
255 * it work for threads that try to change their own name.
257 if (tid != mono_native_thread_id_get ())
261 pthread_setname_np ("");
265 strncpy (n, name, 63);
267 pthread_setname_np (n);
269 #elif defined (__NetBSD__)
271 pthread_setname_np (tid, "%s", (void*)"");
273 char n [PTHREAD_MAX_NAMELEN_NP];
275 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
276 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
277 pthread_setname_np (tid, "%s", (void*)n);
279 #elif defined (HAVE_PTHREAD_SETNAME_NP)
281 pthread_setname_np (tid, "");
285 strncpy (n, name, 16);
287 pthread_setname_np (tid, n);
293 mono_threads_platform_set_exited (MonoThreadInfo *info)
295 MonoW32HandleThread *thread_data;
296 gpointer mutex_handle;
301 g_assert (info->handle);
303 if (mono_w32handle_issignalled (info->handle) || mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED) {
304 /* We must have already deliberately finished
305 * with this thread, so don't do any more now */
309 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
310 g_error ("unknown thread handle %p", info->handle);
312 pid = wapi_getpid ();
313 tid = pthread_self ();
315 for (i = 0; i < thread_data->owned_mutexes->len; i++) {
316 mutex_handle = g_ptr_array_index (thread_data->owned_mutexes, i);
317 wapi_mutex_abandon (mutex_handle, pid, tid);
318 mono_thread_info_disown_mutex (info, mutex_handle);
321 g_ptr_array_free (thread_data->owned_mutexes, TRUE);
323 thr_ret = mono_w32handle_lock_handle (info->handle);
324 g_assert (thr_ret == 0);
326 mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
328 thr_ret = mono_w32handle_unlock_handle (info->handle);
329 g_assert (thr_ret == 0);
331 /* The thread is no longer active, so unref it */
332 mono_w32handle_unref (info->handle);
338 mono_threads_platform_describe (MonoThreadInfo *info, GString *text)
340 MonoW32HandleThread *thread_data;
343 g_assert (info->handle);
345 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
346 g_error ("unknown thread handle %p", info->handle);
348 g_string_append_printf (text, "thread handle %p state : ", info->handle);
350 mono_thread_info_describe_interrupt_token (info, text);
352 g_string_append_printf (text, ", owns (");
353 for (i = 0; i < thread_data->owned_mutexes->len; i++)
354 g_string_append_printf (text, i > 0 ? ", %p" : "%p", g_ptr_array_index (thread_data->owned_mutexes, i));
355 g_string_append_printf (text, ")");
359 mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
361 MonoW32HandleThread *thread_data;
363 g_assert (info->handle);
365 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
366 g_error ("unknown thread handle %p", info->handle);
368 mono_w32handle_ref (mutex_handle);
370 g_ptr_array_add (thread_data->owned_mutexes, mutex_handle);
374 mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
376 MonoW32HandleThread *thread_data;
378 g_assert (info->handle);
380 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
381 g_error ("unknown thread handle %p", info->handle);
383 mono_w32handle_unref (mutex_handle);
385 g_ptr_array_remove (thread_data->owned_mutexes, mutex_handle);
389 mono_threads_platform_get_priority (MonoThreadInfo *info)
391 MonoW32HandleThread *thread_data;
393 g_assert (info->handle);
395 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer *)&thread_data))
396 return MONO_THREAD_PRIORITY_NORMAL;
398 return thread_data->priority;
402 mono_threads_platform_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
404 MonoW32HandleThread *thread_data;
405 int policy, posix_priority;
406 struct sched_param param;
408 g_assert (info->handle);
410 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
413 switch (pthread_getschedparam (thread_data->id, &policy, ¶m)) {
417 g_warning ("pthread_getschedparam: error looking up thread id %x", (gsize)thread_data->id);
423 posix_priority = win32_priority_to_posix_priority (priority, policy);
424 if (posix_priority < 0)
427 param.sched_priority = posix_priority;
428 switch (pthread_setschedparam (thread_data->id, policy, ¶m)) {
432 g_warning ("%s: pthread_setschedprio: error looking up thread id %x", __func__, (gsize)thread_data->id);
435 g_warning ("%s: priority %d not supported", __func__, priority);
438 g_warning ("%s: permission denied", __func__);
444 thread_data->priority = priority;
449 static void thread_details (gpointer data)
451 MonoW32HandleThread *thread = (MonoW32HandleThread*) data;
452 g_print ("id: %p, owned_mutexes: %d, priority: %d",
453 thread->id, thread->owned_mutexes->len, thread->priority);
456 static const gchar* thread_typename (void)
461 static gsize thread_typesize (void)
463 return sizeof (MonoW32HandleThread);
466 static MonoW32HandleOps thread_ops = {
471 NULL, /* special_wait */
473 thread_details, /* details */
474 thread_typename, /* typename */
475 thread_typesize, /* typesize */
479 mono_threads_platform_init (void)
481 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
483 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
486 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
488 #if defined(USE_POSIX_BACKEND)
491 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
493 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
495 if (!mono_threads_pthread_kill (info, sig)) {
496 mono_threads_add_to_pending_operation_set (info);
503 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
505 return info->suspend_can_continue;
509 This begins async resume. This function must do the following:
511 - Install an async target if one was requested.
512 - Notify the target to resume.
515 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
517 mono_threads_add_to_pending_operation_set (info);
518 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
522 mono_threads_suspend_register (MonoThreadInfo *info)
524 #if defined (PLATFORM_ANDROID)
525 info->native_handle = gettid ();
530 mono_threads_suspend_free (MonoThreadInfo *info)
535 mono_threads_suspend_init (void)
537 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
540 #endif /* defined(USE_POSIX_BACKEND) */