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 mono_threads_platform_register (MonoThreadInfo *info)
46 gpointer thread_handle;
48 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
49 if (thread_handle == INVALID_HANDLE_VALUE)
50 g_error ("%s: failed to create handle", __func__);
52 g_assert (!info->handle);
53 info->handle = thread_handle;
57 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize stack_size, MonoNativeThreadId *out_tid)
62 struct sched_param param;
65 res = pthread_attr_init (&attr);
68 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
69 if (stack_size == 0) {
70 #if HAVE_VALGRIND_MEMCHECK_H
71 if (RUNNING_ON_VALGRIND)
74 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
76 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
80 #ifdef PTHREAD_STACK_MIN
81 if (stack_size < PTHREAD_STACK_MIN)
82 stack_size = PTHREAD_STACK_MIN;
85 res = pthread_attr_setstacksize (&attr, stack_size);
87 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
89 memset (¶m, 0, sizeof (param));
91 res = pthread_attr_getschedpolicy (&attr, &policy);
93 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
95 #ifdef _POSIX_PRIORITY_SCHEDULING
98 /* Necessary to get valid priority range */
100 min = sched_get_priority_min (policy);
101 max = sched_get_priority_max (policy);
103 if (max > 0 && min >= 0 && max > min)
104 param.sched_priority = (max - min) / 2 + min;
111 param.sched_priority = 50;
117 param.sched_priority = 0;
120 g_error ("%s: unknown policy %d", __func__, policy);
124 res = pthread_attr_setschedparam (&attr, ¶m);
126 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
128 /* Actually start the thread */
129 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
140 mono_threads_platform_yield (void)
142 return sched_yield () == 0;
146 mono_threads_platform_exit (int exit_code)
148 #if defined(__native_client__)
149 nacl_shutdown_gc_thread();
152 mono_thread_info_detach ();
158 mono_threads_platform_unregister (MonoThreadInfo *info)
160 mono_threads_platform_set_exited (info);
164 mono_threads_get_max_stack_size (void)
168 /* If getrlimit fails, we don't enforce any limits. */
169 if (getrlimit (RLIMIT_STACK, &lim))
171 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
172 if (lim.rlim_max > (rlim_t)INT_MAX)
174 return (int)lim.rlim_max;
178 mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
180 g_assert (info->handle);
181 mono_w32handle_ref (info->handle);
186 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
188 mono_w32handle_ref (handle);
194 mono_threads_platform_close_thread_handle (HANDLE handle)
196 mono_w32handle_unref (handle);
200 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
202 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
203 #ifdef USE_TKILL_ON_ANDROID
204 int result, old_errno = errno;
205 result = tkill (info->native_handle, signum);
211 #elif defined(__native_client__)
212 /* Workaround pthread_kill abort() in NaCl glibc. */
214 #elif !defined(HAVE_PTHREAD_KILL)
215 g_error ("pthread_kill() is not supported by this platform");
217 return pthread_kill (mono_thread_info_get_tid (info), signum);
222 mono_native_thread_id_get (void)
224 return pthread_self ();
228 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
230 return pthread_equal (id1, id2);
234 * mono_native_thread_create:
236 * Low level thread creation function without any GC wrappers.
239 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
241 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
245 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
249 * We can't set the thread name for other threads, but we can at least make
250 * it work for threads that try to change their own name.
252 if (tid != mono_native_thread_id_get ())
256 pthread_setname_np ("");
260 strncpy (n, name, 63);
262 pthread_setname_np (n);
264 #elif defined (__NetBSD__)
266 pthread_setname_np (tid, "%s", (void*)"");
268 char n [PTHREAD_MAX_NAMELEN_NP];
270 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
271 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
272 pthread_setname_np (tid, "%s", (void*)n);
274 #elif defined (HAVE_PTHREAD_SETNAME_NP)
276 pthread_setname_np (tid, "");
280 strncpy (n, name, 16);
282 pthread_setname_np (tid, n);
288 mono_threads_platform_set_exited (MonoThreadInfo *info)
292 g_assert (info->handle);
293 if (mono_w32handle_issignalled (info->handle))
294 g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, info->handle, mono_thread_info_get_tid (info));
295 if (mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED)
296 g_error ("%s: handle %p thread %p has already exited, it's handle type is 'unused'", __func__, info->handle, mono_thread_info_get_tid (info));
298 thr_ret = mono_w32handle_lock_handle (info->handle);
299 g_assert (thr_ret == 0);
301 mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
303 thr_ret = mono_w32handle_unlock_handle (info->handle);
304 g_assert (thr_ret == 0);
306 /* The thread is no longer active, so unref it */
307 mono_w32handle_unref (info->handle);
312 static const gchar* thread_typename (void)
317 static gsize thread_typesize (void)
322 static MonoW32HandleOps thread_ops = {
327 NULL, /* special_wait */
330 thread_typename, /* typename */
331 thread_typesize, /* typesize */
335 mono_threads_platform_init (void)
337 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
339 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
342 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
344 #if defined(USE_POSIX_BACKEND)
347 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
349 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
351 if (!mono_threads_pthread_kill (info, sig)) {
352 mono_threads_add_to_pending_operation_set (info);
359 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
361 return info->suspend_can_continue;
365 This begins async resume. This function must do the following:
367 - Install an async target if one was requested.
368 - Notify the target to resume.
371 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
373 mono_threads_add_to_pending_operation_set (info);
374 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
378 mono_threads_suspend_register (MonoThreadInfo *info)
380 #if defined (PLATFORM_ANDROID)
381 info->native_handle = gettid ();
386 mono_threads_suspend_free (MonoThreadInfo *info)
391 mono_threads_suspend_init (void)
393 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
396 #endif /* defined(USE_POSIX_BACKEND) */