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*);
45 MonoCoopSem registered;
50 inner_start_thread (void *arg)
52 StartInfo *start_info = (StartInfo *) arg;
53 void *t_arg = start_info->arg;
55 void *(*start_func)(void*) = start_info->start_routine;
56 guint32 flags = start_info->flags;
61 /* Register the thread with the io-layer */
62 handle = wapi_create_thread_handle ();
64 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 wapi_init_thread_info_priority(handle, start_info->priority);
76 if (flags & CREATE_SUSPENDED) {
77 info->create_suspended = TRUE;
78 mono_coop_sem_init (&info->create_suspended_sem, 0);
81 /* start_info is not valid after this */
82 mono_coop_sem_post (&(start_info->registered));
85 if (flags & CREATE_SUSPENDED) {
86 res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
89 mono_coop_sem_destroy (&info->create_suspended_sem);
92 /* Run the actual main function of the thread */
93 result = start_func (t_arg);
95 mono_threads_core_exit (GPOINTER_TO_UINT (result));
96 g_assert_not_reached ();
100 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, MonoThreadParm *tp, MonoNativeThreadId *out_tid)
105 StartInfo start_info;
108 struct sched_param sp;
110 res = pthread_attr_init (&attr);
113 if (tp->stack_size == 0) {
114 #if HAVE_VALGRIND_MEMCHECK_H
115 if (RUNNING_ON_VALGRIND)
116 stack_size = 1 << 20;
118 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
120 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
123 stack_size = tp->stack_size;
125 #ifdef PTHREAD_STACK_MIN
126 if (stack_size < PTHREAD_STACK_MIN)
127 stack_size = PTHREAD_STACK_MIN;
130 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
131 res = pthread_attr_setstacksize (&attr, stack_size);
136 * For policies that respect priorities set the prirority for the new thread
138 pthread_getschedparam(pthread_self(), &policy, &sp);
139 if ((policy == SCHED_FIFO) || (policy == SCHED_RR)) {
140 sp.sched_priority = wapi_thread_priority_to_posix_priority (tp->priority, policy);
141 res = pthread_attr_setschedparam (&attr, &sp);
144 memset (&start_info, 0, sizeof (StartInfo));
145 start_info.start_routine = (void *(*)(void *)) start_routine;
146 start_info.arg = arg;
147 start_info.flags = tp->creation_flags;
148 start_info.priority = tp->priority;
149 mono_coop_sem_init (&(start_info.registered), 0);
151 /* Actually start the thread */
152 res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
154 mono_coop_sem_destroy (&(start_info.registered));
158 /* Wait until the thread register itself in various places */
159 res = mono_coop_sem_wait (&start_info.registered, MONO_SEM_FLAGS_NONE);
160 g_assert (res != -1);
162 mono_coop_sem_destroy (&(start_info.registered));
167 return start_info.handle;
171 * mono_threads_core_resume_created:
173 * Resume a newly created thread created using CREATE_SUSPENDED.
176 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
178 mono_coop_sem_post (&info->create_suspended_sem);
182 mono_threads_core_yield (void)
184 return sched_yield () == 0;
188 mono_threads_core_exit (int exit_code)
190 MonoThreadInfo *current = mono_thread_info_current ();
192 #if defined(__native_client__)
193 nacl_shutdown_gc_thread();
196 wapi_thread_handle_set_exited (current->handle, exit_code);
198 mono_thread_info_detach ();
204 mono_threads_core_unregister (MonoThreadInfo *info)
207 wapi_thread_handle_set_exited (info->handle, 0);
213 mono_threads_core_open_handle (void)
215 MonoThreadInfo *info;
217 info = mono_thread_info_current ();
221 info->handle = wapi_create_thread_handle ();
223 wapi_ref_thread_handle (info->handle);
228 mono_threads_get_max_stack_size (void)
232 /* If getrlimit fails, we don't enforce any limits. */
233 if (getrlimit (RLIMIT_STACK, &lim))
235 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
236 if (lim.rlim_max > (rlim_t)INT_MAX)
238 return (int)lim.rlim_max;
242 mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
244 wapi_ref_thread_handle (handle);
250 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
252 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
253 #ifdef USE_TKILL_ON_ANDROID
254 int result, old_errno = errno;
255 result = tkill (info->native_handle, signum);
261 #elif defined(__native_client__)
262 /* Workaround pthread_kill abort() in NaCl glibc. */
264 #elif !defined(HAVE_PTHREAD_KILL)
265 g_error ("pthread_kill() is not supported by this platform");
267 return pthread_kill (mono_thread_info_get_tid (info), signum);
272 mono_native_thread_id_get (void)
274 return pthread_self ();
278 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
280 return pthread_equal (id1, id2);
284 * mono_native_thread_create:
286 * Low level thread creation function without any GC wrappers.
289 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
291 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
295 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
299 * We can't set the thread name for other threads, but we can at least make
300 * it work for threads that try to change their own name.
302 if (tid != mono_native_thread_id_get ())
306 pthread_setname_np ("");
310 strncpy (n, name, 63);
312 pthread_setname_np (n);
314 #elif defined (__NetBSD__)
316 pthread_setname_np (tid, "%s", (void*)"");
318 char n [PTHREAD_MAX_NAMELEN_NP];
320 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
321 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
322 pthread_setname_np (tid, "%s", (void*)n);
324 #elif defined (HAVE_PTHREAD_SETNAME_NP)
326 pthread_setname_np (tid, "");
330 strncpy (n, name, 16);
332 pthread_setname_np (tid, n);
337 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
339 #if defined(USE_POSIX_BACKEND)
342 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
344 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
346 if (!mono_threads_pthread_kill (info, sig)) {
347 mono_threads_add_to_pending_operation_set (info);
354 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
356 return info->suspend_can_continue;
360 This begins async resume. This function must do the following:
362 - Install an async target if one was requested.
363 - Notify the target to resume.
366 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
368 mono_threads_add_to_pending_operation_set (info);
369 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
373 mono_threads_platform_register (MonoThreadInfo *info)
375 #if defined (PLATFORM_ANDROID)
376 info->native_handle = gettid ();
381 mono_threads_platform_free (MonoThreadInfo *info)
386 mono_threads_init_platform (void)
388 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
391 #endif /* defined(USE_POSIX_BACKEND) */