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>
24 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
25 #define USE_TKILL_ON_ANDROID 1
28 #ifdef USE_TKILL_ON_ANDROID
29 extern int tkill (pid_t tid, int signal);
32 #if defined(_POSIX_VERSION) || defined(__native_client__)
34 #include <sys/resource.h>
36 #if defined(__native_client__)
37 void nacl_shutdown_gc_thread(void);
41 void *(*start_routine)(void*);
44 MonoCoopSem registered;
49 inner_start_thread (void *arg)
51 StartInfo *start_info = (StartInfo *) arg;
52 void *t_arg = start_info->arg;
54 void *(*start_func)(void*) = start_info->start_routine;
55 guint32 flags = start_info->flags;
60 /* Register the thread with the io-layer */
61 handle = wapi_create_thread_handle ();
63 res = mono_coop_sem_post (&(start_info->registered));
67 start_info->handle = handle;
69 info = mono_thread_info_attach (&result);
71 info->runtime_thread = TRUE;
72 info->handle = handle;
74 if (flags & CREATE_SUSPENDED) {
75 info->create_suspended = TRUE;
76 mono_coop_sem_init (&info->create_suspended_sem, 0);
79 /* start_info is not valid after this */
80 res = mono_coop_sem_post (&(start_info->registered));
84 if (flags & CREATE_SUSPENDED) {
85 res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
88 mono_coop_sem_destroy (&info->create_suspended_sem);
91 /* Run the actual main function of the thread */
92 result = start_func (t_arg);
94 mono_threads_core_exit (GPOINTER_TO_UINT (result));
95 g_assert_not_reached ();
99 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
104 StartInfo start_info;
106 res = pthread_attr_init (&attr);
109 if (stack_size == 0) {
110 #if HAVE_VALGRIND_MEMCHECK_H
111 if (RUNNING_ON_VALGRIND)
112 stack_size = 1 << 20;
114 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
116 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
120 #ifdef PTHREAD_STACK_MIN
121 if (stack_size < PTHREAD_STACK_MIN)
122 stack_size = PTHREAD_STACK_MIN;
125 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
126 res = pthread_attr_setstacksize (&attr, stack_size);
130 memset (&start_info, 0, sizeof (StartInfo));
131 start_info.start_routine = (void *(*)(void *)) start_routine;
132 start_info.arg = arg;
133 start_info.flags = creation_flags;
134 mono_coop_sem_init (&(start_info.registered), 0);
136 /* Actually start the thread */
137 res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
139 mono_coop_sem_destroy (&(start_info.registered));
143 /* Wait until the thread register itself in various places */
144 res = mono_coop_sem_wait (&start_info.registered, MONO_SEM_FLAGS_NONE);
145 g_assert (res != -1);
147 mono_coop_sem_destroy (&(start_info.registered));
152 return start_info.handle;
156 * mono_threads_core_resume_created:
158 * Resume a newly created thread created using CREATE_SUSPENDED.
161 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
163 mono_coop_sem_post (&info->create_suspended_sem);
167 mono_threads_core_yield (void)
169 return sched_yield () == 0;
173 mono_threads_core_exit (int exit_code)
175 MonoThreadInfo *current = mono_thread_info_current ();
177 #if defined(__native_client__)
178 nacl_shutdown_gc_thread();
181 wapi_thread_handle_set_exited (current->handle, exit_code);
183 mono_thread_info_detach ();
189 mono_threads_core_unregister (MonoThreadInfo *info)
192 wapi_thread_handle_set_exited (info->handle, 0);
198 mono_threads_core_open_handle (void)
200 MonoThreadInfo *info;
202 info = mono_thread_info_current ();
206 info->handle = wapi_create_thread_handle ();
208 wapi_ref_thread_handle (info->handle);
213 mono_threads_get_max_stack_size (void)
217 /* If getrlimit fails, we don't enforce any limits. */
218 if (getrlimit (RLIMIT_STACK, &lim))
220 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
221 if (lim.rlim_max > (rlim_t)INT_MAX)
223 return (int)lim.rlim_max;
227 mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
229 wapi_ref_thread_handle (handle);
235 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
237 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
238 #ifdef USE_TKILL_ON_ANDROID
239 int result, old_errno = errno;
240 result = tkill (info->native_handle, signum);
246 #elif defined(__native_client__)
247 /* Workaround pthread_kill abort() in NaCl glibc. */
249 #elif !defined(HAVE_PTHREAD_KILL)
250 g_error ("pthread_kill() is not supported by this platform");
252 return pthread_kill (mono_thread_info_get_tid (info), signum);
257 mono_native_thread_id_get (void)
259 return pthread_self ();
263 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
265 return pthread_equal (id1, id2);
269 * mono_native_thread_create:
271 * Low level thread creation function without any GC wrappers.
274 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
276 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
280 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
284 * We can't set the thread name for other threads, but we can at least make
285 * it work for threads that try to change their own name.
287 if (tid != mono_native_thread_id_get ())
291 pthread_setname_np ("");
295 strncpy (n, name, 63);
297 pthread_setname_np (n);
299 #elif defined (HAVE_PTHREAD_SETNAME_NP)
301 pthread_setname_np (tid, "");
305 strncpy (n, name, 16);
307 pthread_setname_np (tid, n);
312 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
314 #if defined(USE_POSIX_BACKEND)
317 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
319 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
321 if (!mono_threads_pthread_kill (info, sig)) {
322 mono_threads_add_to_pending_operation_set (info);
329 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
331 return info->suspend_can_continue;
335 This begins async resume. This function must do the following:
337 - Install an async target if one was requested.
338 - Notify the target to resume.
341 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
343 mono_threads_add_to_pending_operation_set (info);
344 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
348 mono_threads_platform_register (MonoThreadInfo *info)
350 #if defined (PLATFORM_ANDROID)
351 info->native_handle = gettid ();
356 mono_threads_platform_free (MonoThreadInfo *info)
361 mono_threads_init_platform (void)
363 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
366 #endif /* defined(USE_POSIX_BACKEND) */