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 res = mono_coop_sem_post (&(start_info->registered));
68 start_info->handle = handle;
70 info = mono_thread_info_attach (&result);
72 info->runtime_thread = TRUE;
73 info->handle = handle;
75 wapi_init_thread_info_priority(handle, start_info->priority);
77 if (flags & CREATE_SUSPENDED) {
78 info->create_suspended = TRUE;
79 mono_coop_sem_init (&info->create_suspended_sem, 0);
82 /* start_info is not valid after this */
83 res = mono_coop_sem_post (&(start_info->registered));
87 if (flags & CREATE_SUSPENDED) {
88 res = mono_coop_sem_wait (&info->create_suspended_sem, MONO_SEM_FLAGS_NONE);
91 mono_coop_sem_destroy (&info->create_suspended_sem);
94 /* Run the actual main function of the thread */
95 result = start_func (t_arg);
97 mono_threads_core_exit (GPOINTER_TO_UINT (result));
98 g_assert_not_reached ();
102 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, MonoThreadParm *tp, MonoNativeThreadId *out_tid)
107 StartInfo start_info;
110 struct sched_param sp;
112 res = pthread_attr_init (&attr);
115 if (tp->stack_size == 0) {
116 #if HAVE_VALGRIND_MEMCHECK_H
117 if (RUNNING_ON_VALGRIND)
118 stack_size = 1 << 20;
120 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
122 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
125 stack_size = tp->stack_size;
127 #ifdef PTHREAD_STACK_MIN
128 if (stack_size < PTHREAD_STACK_MIN)
129 stack_size = PTHREAD_STACK_MIN;
132 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
133 res = pthread_attr_setstacksize (&attr, stack_size);
138 * For policies that respect priorities set the prirority for the new thread
140 pthread_getschedparam(pthread_self(), &policy, &sp);
141 if ((policy == SCHED_FIFO) || (policy == SCHED_RR)) {
142 sp.sched_priority = wapi_thread_priority_to_posix_priority (tp->priority, policy);
143 res = pthread_attr_setschedparam (&attr, &sp);
146 memset (&start_info, 0, sizeof (StartInfo));
147 start_info.start_routine = (void *(*)(void *)) start_routine;
148 start_info.arg = arg;
149 start_info.flags = tp->creation_flags;
150 start_info.priority = tp->priority;
151 mono_coop_sem_init (&(start_info.registered), 0);
153 /* Actually start the thread */
154 res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
156 mono_coop_sem_destroy (&(start_info.registered));
160 /* Wait until the thread register itself in various places */
161 res = mono_coop_sem_wait (&start_info.registered, MONO_SEM_FLAGS_NONE);
162 g_assert (res != -1);
164 mono_coop_sem_destroy (&(start_info.registered));
169 return start_info.handle;
173 * mono_threads_core_resume_created:
175 * Resume a newly created thread created using CREATE_SUSPENDED.
178 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
180 mono_coop_sem_post (&info->create_suspended_sem);
184 mono_threads_core_yield (void)
186 return sched_yield () == 0;
190 mono_threads_core_exit (int exit_code)
192 MonoThreadInfo *current = mono_thread_info_current ();
194 #if defined(__native_client__)
195 nacl_shutdown_gc_thread();
198 wapi_thread_handle_set_exited (current->handle, exit_code);
200 mono_thread_info_detach ();
206 mono_threads_core_unregister (MonoThreadInfo *info)
209 wapi_thread_handle_set_exited (info->handle, 0);
215 mono_threads_core_open_handle (void)
217 MonoThreadInfo *info;
219 info = mono_thread_info_current ();
223 info->handle = wapi_create_thread_handle ();
225 wapi_ref_thread_handle (info->handle);
230 mono_threads_get_max_stack_size (void)
234 /* If getrlimit fails, we don't enforce any limits. */
235 if (getrlimit (RLIMIT_STACK, &lim))
237 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
238 if (lim.rlim_max > (rlim_t)INT_MAX)
240 return (int)lim.rlim_max;
244 mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
246 wapi_ref_thread_handle (handle);
252 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
254 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
255 #ifdef USE_TKILL_ON_ANDROID
256 int result, old_errno = errno;
257 result = tkill (info->native_handle, signum);
263 #elif defined(__native_client__)
264 /* Workaround pthread_kill abort() in NaCl glibc. */
266 #elif !defined(HAVE_PTHREAD_KILL)
267 g_error ("pthread_kill() is not supported by this platform");
269 return pthread_kill (mono_thread_info_get_tid (info), signum);
274 mono_native_thread_id_get (void)
276 return pthread_self ();
280 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
282 return pthread_equal (id1, id2);
286 * mono_native_thread_create:
288 * Low level thread creation function without any GC wrappers.
291 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
293 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
297 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
301 * We can't set the thread name for other threads, but we can at least make
302 * it work for threads that try to change their own name.
304 if (tid != mono_native_thread_id_get ())
308 pthread_setname_np ("");
312 strncpy (n, name, 63);
314 pthread_setname_np (n);
316 #elif defined (__NetBSD__)
318 pthread_setname_np (tid, "%s", (void*)"");
320 char n [PTHREAD_MAX_NAMELEN_NP];
322 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
323 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
324 pthread_setname_np (tid, "%s", (void*)n);
326 #elif defined (HAVE_PTHREAD_SETNAME_NP)
328 pthread_setname_np (tid, "");
332 strncpy (n, name, 16);
334 pthread_setname_np (tid, n);
339 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
341 #if defined(USE_POSIX_BACKEND)
344 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
346 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
348 if (!mono_threads_pthread_kill (info, sig)) {
349 mono_threads_add_to_pending_operation_set (info);
356 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
358 return info->suspend_can_continue;
362 This begins async resume. This function must do the following:
364 - Install an async target if one was requested.
365 - Notify the target to resume.
368 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
370 mono_threads_add_to_pending_operation_set (info);
371 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
375 mono_threads_platform_register (MonoThreadInfo *info)
377 #if defined (PLATFORM_ANDROID)
378 info->native_handle = gettid ();
383 mono_threads_platform_free (MonoThreadInfo *info)
388 mono_threads_init_platform (void)
390 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
393 #endif /* defined(USE_POSIX_BACKEND) */