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 thread_handle_create (void)
46 MonoW32HandleThread thread_data;
47 gpointer thread_handle;
49 thread_data.id = pthread_self ();
50 thread_data.owned_mutexes = g_ptr_array_new ();
51 thread_data.priority = THREAD_PRIORITY_NORMAL;
53 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, (gpointer) &thread_data);
54 if (thread_handle == INVALID_HANDLE_VALUE)
57 /* We need to keep the handle alive, as long as the corresponding managed
58 * thread object is alive. The handle is going to be unref when calling
59 * the finalizer on the MonoThreadInternal object */
60 mono_w32handle_ref (thread_handle);
66 void *(*start_routine)(void*);
70 MonoCoopSem registered;
75 inner_start_thread (void *arg)
77 StartInfo *start_info = (StartInfo *) arg;
78 void *t_arg = start_info->arg;
80 void *(*start_func)(void*) = start_info->start_routine;
81 guint32 flags = start_info->flags;
86 /* Register the thread with the io-layer */
87 handle = thread_handle_create ();
89 mono_coop_sem_post (&(start_info->registered));
92 start_info->handle = handle;
94 info = mono_thread_info_attach (&result);
96 info->runtime_thread = TRUE;
97 info->handle = handle;
99 wapi_init_thread_info_priority(handle, start_info->priority);
101 if (flags & CREATE_SUSPENDED) {
102 info->create_suspended = TRUE;
103 mono_coop_sem_init (&info->create_suspended_sem, 0);
106 /* start_info is not valid after this */
107 mono_coop_sem_post (&(start_info->registered));
110 if (flags & CREATE_SUSPENDED) {
111 res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
112 g_assert (res != -1);
114 mono_coop_sem_destroy (&info->create_suspended_sem);
117 /* Run the actual main function of the thread */
118 result = start_func (t_arg);
120 mono_threads_platform_exit (GPOINTER_TO_UINT (result));
121 g_assert_not_reached ();
125 mono_threads_platform_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, MonoThreadParm *tp, MonoNativeThreadId *out_tid)
130 StartInfo start_info;
133 struct sched_param sp;
135 res = pthread_attr_init (&attr);
138 if (tp->stack_size == 0) {
139 #if HAVE_VALGRIND_MEMCHECK_H
140 if (RUNNING_ON_VALGRIND)
141 stack_size = 1 << 20;
143 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
145 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
148 stack_size = tp->stack_size;
150 #ifdef PTHREAD_STACK_MIN
151 if (stack_size < PTHREAD_STACK_MIN)
152 stack_size = PTHREAD_STACK_MIN;
155 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
156 res = pthread_attr_setstacksize (&attr, stack_size);
161 * For policies that respect priorities set the prirority for the new thread
163 pthread_getschedparam(pthread_self(), &policy, &sp);
164 if ((policy == SCHED_FIFO) || (policy == SCHED_RR)) {
165 sp.sched_priority = wapi_thread_priority_to_posix_priority (tp->priority, policy);
166 res = pthread_attr_setschedparam (&attr, &sp);
169 memset (&start_info, 0, sizeof (StartInfo));
170 start_info.start_routine = (void *(*)(void *)) start_routine;
171 start_info.arg = arg;
172 start_info.flags = tp->creation_flags;
173 start_info.priority = tp->priority;
174 mono_coop_sem_init (&(start_info.registered), 0);
176 /* Actually start the thread */
177 res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
179 mono_coop_sem_destroy (&(start_info.registered));
183 /* Wait until the thread register itself in various places */
184 res = mono_coop_sem_wait (&start_info.registered, MONO_SEM_FLAGS_NONE);
185 g_assert (res != -1);
187 mono_coop_sem_destroy (&(start_info.registered));
192 return start_info.handle;
196 * mono_threads_platform_resume_created:
198 * Resume a newly created thread created using CREATE_SUSPENDED.
201 mono_threads_platform_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
203 mono_coop_sem_post (&info->create_suspended_sem);
207 mono_threads_platform_yield (void)
209 return sched_yield () == 0;
213 mono_threads_platform_exit (int exit_code)
215 MonoThreadInfo *current = mono_thread_info_current ();
217 #if defined(__native_client__)
218 nacl_shutdown_gc_thread();
221 mono_threads_platform_set_exited (current);
223 mono_thread_info_detach ();
229 mono_threads_platform_unregister (MonoThreadInfo *info)
232 mono_threads_platform_set_exited (info);
238 mono_threads_platform_open_handle (void)
240 MonoThreadInfo *info;
242 info = mono_thread_info_current ();
246 info->handle = thread_handle_create ();
248 mono_w32handle_ref (info->handle);
253 mono_threads_get_max_stack_size (void)
257 /* If getrlimit fails, we don't enforce any limits. */
258 if (getrlimit (RLIMIT_STACK, &lim))
260 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
261 if (lim.rlim_max > (rlim_t)INT_MAX)
263 return (int)lim.rlim_max;
267 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
269 mono_w32handle_ref (handle);
275 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
277 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
278 #ifdef USE_TKILL_ON_ANDROID
279 int result, old_errno = errno;
280 result = tkill (info->native_handle, signum);
286 #elif defined(__native_client__)
287 /* Workaround pthread_kill abort() in NaCl glibc. */
289 #elif !defined(HAVE_PTHREAD_KILL)
290 g_error ("pthread_kill() is not supported by this platform");
292 return pthread_kill (mono_thread_info_get_tid (info), signum);
297 mono_native_thread_id_get (void)
299 return pthread_self ();
303 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
305 return pthread_equal (id1, id2);
309 * mono_native_thread_create:
311 * Low level thread creation function without any GC wrappers.
314 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
316 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
320 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
324 * We can't set the thread name for other threads, but we can at least make
325 * it work for threads that try to change their own name.
327 if (tid != mono_native_thread_id_get ())
331 pthread_setname_np ("");
335 strncpy (n, name, 63);
337 pthread_setname_np (n);
339 #elif defined (__NetBSD__)
341 pthread_setname_np (tid, "%s", (void*)"");
343 char n [PTHREAD_MAX_NAMELEN_NP];
345 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
346 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
347 pthread_setname_np (tid, "%s", (void*)n);
349 #elif defined (HAVE_PTHREAD_SETNAME_NP)
351 pthread_setname_np (tid, "");
355 strncpy (n, name, 16);
357 pthread_setname_np (tid, n);
363 mono_threads_platform_set_exited (MonoThreadInfo *info)
365 MonoW32HandleThread *thread_data;
366 gpointer mutex_handle;
371 if (!info->handle || mono_w32handle_issignalled (info->handle) || mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED) {
372 /* We must have already deliberately finished
373 * with this thread, so don't do any more now */
377 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
378 g_error ("unknown thread handle %p", info->handle);
380 pid = wapi_getpid ();
381 tid = pthread_self ();
383 for (i = 0; i < thread_data->owned_mutexes->len; i++) {
384 mutex_handle = g_ptr_array_index (thread_data->owned_mutexes, i);
385 wapi_mutex_abandon (mutex_handle, pid, tid);
386 mono_thread_info_disown_mutex (info, mutex_handle);
389 g_ptr_array_free (thread_data->owned_mutexes, TRUE);
391 thr_ret = mono_w32handle_lock_handle (info->handle);
392 g_assert (thr_ret == 0);
394 mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
396 thr_ret = mono_w32handle_unlock_handle (info->handle);
397 g_assert (thr_ret == 0);
399 /* The thread is no longer active, so unref it */
400 mono_w32handle_unref (info->handle);
406 mono_threads_platform_describe (MonoThreadInfo *info, GString *text)
408 MonoW32HandleThread *thread_data;
411 g_assert (info->handle);
413 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
414 g_error ("unknown thread handle %p", info->handle);
416 g_string_append_printf (text, "thread handle %p state : ", info->handle);
418 mono_thread_info_describe_interrupt_token (info, text);
420 g_string_append_printf (text, ", owns (");
421 for (i = 0; i < thread_data->owned_mutexes->len; i++)
422 g_string_append_printf (text, i > 0 ? ", %p" : "%p", g_ptr_array_index (thread_data->owned_mutexes, i));
423 g_string_append_printf (text, ")");
427 mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
429 MonoW32HandleThread *thread_data;
431 g_assert (info->handle);
433 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
434 g_error ("unknown thread handle %p", info->handle);
436 mono_w32handle_ref (mutex_handle);
438 g_ptr_array_add (thread_data->owned_mutexes, mutex_handle);
442 mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
444 MonoW32HandleThread *thread_data;
446 g_assert (info->handle);
448 if (!mono_w32handle_lookup (info->handle, MONO_W32HANDLE_THREAD, (gpointer*) &thread_data))
449 g_error ("unknown thread handle %p", info->handle);
451 mono_w32handle_unref (mutex_handle);
453 g_ptr_array_remove (thread_data->owned_mutexes, mutex_handle);
456 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
458 #if defined(USE_POSIX_BACKEND)
461 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
463 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
465 if (!mono_threads_pthread_kill (info, sig)) {
466 mono_threads_add_to_pending_operation_set (info);
473 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
475 return info->suspend_can_continue;
479 This begins async resume. This function must do the following:
481 - Install an async target if one was requested.
482 - Notify the target to resume.
485 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
487 mono_threads_add_to_pending_operation_set (info);
488 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
492 mono_threads_suspend_register (MonoThreadInfo *info)
494 #if defined (PLATFORM_ANDROID)
495 info->native_handle = gettid ();
498 g_assert (!info->handle);
499 info->handle = thread_handle_create ();
503 mono_threads_suspend_free (MonoThreadInfo *info)
508 mono_threads_suspend_init (void)
510 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
513 #endif /* defined(USE_POSIX_BACKEND) */