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 win32_priority_to_posix_priority (MonoThreadPriority priority, int policy)
46 g_assert (priority >= MONO_THREAD_PRIORITY_LOWEST);
47 g_assert (priority <= MONO_THREAD_PRIORITY_HIGHEST);
48 g_assert (MONO_THREAD_PRIORITY_LOWEST < MONO_THREAD_PRIORITY_HIGHEST);
50 /* Necessary to get valid priority range */
51 #ifdef _POSIX_PRIORITY_SCHEDULING
54 min = sched_get_priority_min (policy);
55 max = sched_get_priority_max (policy);
57 if (max > 0 && min >= 0 && max > min) {
58 double srange, drange, sposition, dposition;
59 srange = MONO_THREAD_PRIORITY_HIGHEST - MONO_THREAD_PRIORITY_LOWEST;
61 sposition = priority - MONO_THREAD_PRIORITY_LOWEST;
62 dposition = (sposition / srange) * drange;
63 return (int)(dposition + min);
82 mono_threads_platform_register (MonoThreadInfo *info)
84 gpointer thread_handle;
86 info->owned_mutexes = g_ptr_array_new ();
87 info->priority = MONO_THREAD_PRIORITY_NORMAL;
89 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
90 if (thread_handle == INVALID_HANDLE_VALUE)
91 g_error ("%s: failed to create handle", __func__);
93 /* We need to keep the handle alive, as long as the corresponding managed
94 * thread object is alive. The handle is going to be unref when calling
95 * the finalizer on the MonoThreadInternal object */
96 mono_w32handle_ref (thread_handle);
98 g_assert (!info->handle);
99 info->handle = thread_handle;
103 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize stack_size, MonoNativeThreadId *out_tid)
109 res = pthread_attr_init (&attr);
112 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
113 if (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;
124 #ifdef PTHREAD_STACK_MIN
125 if (stack_size < PTHREAD_STACK_MIN)
126 stack_size = PTHREAD_STACK_MIN;
129 res = pthread_attr_setstacksize (&attr, stack_size);
131 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
133 /* Actually start the thread */
134 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
145 mono_threads_platform_yield (void)
147 return sched_yield () == 0;
151 mono_threads_platform_exit (int exit_code)
153 #if defined(__native_client__)
154 nacl_shutdown_gc_thread();
157 mono_thread_info_detach ();
163 mono_threads_platform_unregister (MonoThreadInfo *info)
165 mono_threads_platform_set_exited (info);
169 mono_threads_get_max_stack_size (void)
173 /* If getrlimit fails, we don't enforce any limits. */
174 if (getrlimit (RLIMIT_STACK, &lim))
176 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
177 if (lim.rlim_max > (rlim_t)INT_MAX)
179 return (int)lim.rlim_max;
183 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
185 mono_w32handle_ref (handle);
191 mono_threads_platform_close_thread_handle (HANDLE handle)
193 mono_w32handle_unref (handle);
197 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
199 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
200 #ifdef USE_TKILL_ON_ANDROID
201 int result, old_errno = errno;
202 result = tkill (info->native_handle, signum);
208 #elif defined(__native_client__)
209 /* Workaround pthread_kill abort() in NaCl glibc. */
211 #elif !defined(HAVE_PTHREAD_KILL)
212 g_error ("pthread_kill() is not supported by this platform");
214 return pthread_kill (mono_thread_info_get_tid (info), signum);
219 mono_native_thread_id_get (void)
221 return pthread_self ();
225 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
227 return pthread_equal (id1, id2);
231 * mono_native_thread_create:
233 * Low level thread creation function without any GC wrappers.
236 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
238 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
242 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
246 * We can't set the thread name for other threads, but we can at least make
247 * it work for threads that try to change their own name.
249 if (tid != mono_native_thread_id_get ())
253 pthread_setname_np ("");
257 strncpy (n, name, 63);
259 pthread_setname_np (n);
261 #elif defined (__NetBSD__)
263 pthread_setname_np (tid, "%s", (void*)"");
265 char n [PTHREAD_MAX_NAMELEN_NP];
267 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
268 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
269 pthread_setname_np (tid, "%s", (void*)n);
271 #elif defined (HAVE_PTHREAD_SETNAME_NP)
273 pthread_setname_np (tid, "");
277 strncpy (n, name, 16);
279 pthread_setname_np (tid, n);
285 mono_threads_platform_set_exited (MonoThreadInfo *info)
287 gpointer mutex_handle;
292 g_assert (info->handle);
294 if (mono_w32handle_issignalled (info->handle))
295 g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, info->handle, mono_thread_info_get_tid (info));
296 if (mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED)
297 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));
299 pid = wapi_getpid ();
300 tid = pthread_self ();
302 for (i = 0; i < info->owned_mutexes->len; i++) {
303 mutex_handle = g_ptr_array_index (info->owned_mutexes, i);
304 wapi_mutex_abandon (mutex_handle, pid, tid);
305 mono_thread_info_disown_mutex (info, mutex_handle);
308 g_ptr_array_free (info->owned_mutexes, TRUE);
310 thr_ret = mono_w32handle_lock_handle (info->handle);
311 g_assert (thr_ret == 0);
313 mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
315 thr_ret = mono_w32handle_unlock_handle (info->handle);
316 g_assert (thr_ret == 0);
318 /* The thread is no longer active, so unref it */
319 mono_w32handle_unref (info->handle);
325 mono_threads_platform_describe (MonoThreadInfo *info, GString *text)
329 g_string_append_printf (text, "thread handle %p state : ", info->handle);
331 mono_thread_info_describe_interrupt_token (info, text);
333 g_string_append_printf (text, ", owns (");
334 for (i = 0; i < info->owned_mutexes->len; i++)
335 g_string_append_printf (text, i > 0 ? ", %p" : "%p", g_ptr_array_index (info->owned_mutexes, i));
336 g_string_append_printf (text, ")");
340 mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
342 mono_w32handle_ref (mutex_handle);
344 g_ptr_array_add (info->owned_mutexes, mutex_handle);
348 mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
350 mono_w32handle_unref (mutex_handle);
352 g_ptr_array_remove (info->owned_mutexes, mutex_handle);
356 mono_threads_platform_get_priority (MonoThreadInfo *info)
358 return info->priority;
362 mono_threads_platform_set_priority (MonoThreadInfo *info, MonoThreadPriority priority)
364 int policy, posix_priority;
365 struct sched_param param;
368 tid = mono_thread_info_get_tid (info);
370 switch (pthread_getschedparam (tid, &policy, ¶m)) {
374 g_warning ("pthread_getschedparam: error looking up thread id %x", (gsize)tid);
380 posix_priority = win32_priority_to_posix_priority (priority, policy);
381 if (posix_priority < 0)
384 param.sched_priority = posix_priority;
385 switch (pthread_setschedparam (tid, policy, ¶m)) {
389 g_warning ("%s: pthread_setschedprio: error looking up thread id %x", __func__, (gsize)tid);
392 g_warning ("%s: priority %d not supported", __func__, priority);
395 g_warning ("%s: permission denied", __func__);
401 info->priority = priority;
406 static const gchar* thread_typename (void)
411 static gsize thread_typesize (void)
416 static MonoW32HandleOps thread_ops = {
421 NULL, /* special_wait */
424 thread_typename, /* typename */
425 thread_typesize, /* typesize */
429 mono_threads_platform_init (void)
431 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
433 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
436 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
438 #if defined(USE_POSIX_BACKEND)
441 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
443 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
445 if (!mono_threads_pthread_kill (info, sig)) {
446 mono_threads_add_to_pending_operation_set (info);
453 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
455 return info->suspend_can_continue;
459 This begins async resume. This function must do the following:
461 - Install an async target if one was requested.
462 - Notify the target to resume.
465 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
467 mono_threads_add_to_pending_operation_set (info);
468 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
472 mono_threads_suspend_register (MonoThreadInfo *info)
474 #if defined (PLATFORM_ANDROID)
475 info->native_handle = gettid ();
480 mono_threads_suspend_free (MonoThreadInfo *info)
485 mono_threads_suspend_init (void)
487 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
490 #endif /* defined(USE_POSIX_BACKEND) */