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 info->owned_mutexes = g_ptr_array_new ();
50 thread_handle = mono_w32handle_new (MONO_W32HANDLE_THREAD, NULL);
51 if (thread_handle == INVALID_HANDLE_VALUE)
52 g_error ("%s: failed to create handle", __func__);
54 g_assert (!info->handle);
55 info->handle = thread_handle;
59 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize stack_size, MonoNativeThreadId *out_tid)
64 struct sched_param param;
67 res = pthread_attr_init (&attr);
70 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
71 if (stack_size == 0) {
72 #if HAVE_VALGRIND_MEMCHECK_H
73 if (RUNNING_ON_VALGRIND)
76 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
78 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
82 #ifdef PTHREAD_STACK_MIN
83 if (stack_size < PTHREAD_STACK_MIN)
84 stack_size = PTHREAD_STACK_MIN;
87 res = pthread_attr_setstacksize (&attr, stack_size);
89 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
91 memset (¶m, 0, sizeof (param));
93 res = pthread_attr_getschedpolicy (&attr, &policy);
95 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", g_strerror (res), res);
97 #ifdef _POSIX_PRIORITY_SCHEDULING
100 /* Necessary to get valid priority range */
102 min = sched_get_priority_min (policy);
103 max = sched_get_priority_max (policy);
105 if (max > 0 && min >= 0 && max > min)
106 param.sched_priority = (max - min) / 2 + min;
113 param.sched_priority = 50;
119 param.sched_priority = 0;
122 g_error ("%s: unknown policy %d", __func__, policy);
126 res = pthread_attr_setschedparam (&attr, ¶m);
128 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", g_strerror (res), res);
130 /* Actually start the thread */
131 res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
142 mono_threads_platform_yield (void)
144 return sched_yield () == 0;
148 mono_threads_platform_exit (int exit_code)
150 #if defined(__native_client__)
151 nacl_shutdown_gc_thread();
154 mono_thread_info_detach ();
160 mono_threads_platform_unregister (MonoThreadInfo *info)
162 mono_threads_platform_set_exited (info);
166 mono_threads_get_max_stack_size (void)
170 /* If getrlimit fails, we don't enforce any limits. */
171 if (getrlimit (RLIMIT_STACK, &lim))
173 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
174 if (lim.rlim_max > (rlim_t)INT_MAX)
176 return (int)lim.rlim_max;
180 mono_threads_platform_duplicate_handle (MonoThreadInfo *info)
182 g_assert (info->handle);
183 mono_w32handle_ref (info->handle);
188 mono_threads_platform_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
190 mono_w32handle_ref (handle);
196 mono_threads_platform_close_thread_handle (HANDLE handle)
198 mono_w32handle_unref (handle);
202 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
204 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
205 #ifdef USE_TKILL_ON_ANDROID
206 int result, old_errno = errno;
207 result = tkill (info->native_handle, signum);
213 #elif defined(__native_client__)
214 /* Workaround pthread_kill abort() in NaCl glibc. */
216 #elif !defined(HAVE_PTHREAD_KILL)
217 g_error ("pthread_kill() is not supported by this platform");
219 return pthread_kill (mono_thread_info_get_tid (info), signum);
224 mono_native_thread_id_get (void)
226 return pthread_self ();
230 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
232 return pthread_equal (id1, id2);
236 * mono_native_thread_create:
238 * Low level thread creation function without any GC wrappers.
241 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
243 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
247 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
251 * We can't set the thread name for other threads, but we can at least make
252 * it work for threads that try to change their own name.
254 if (tid != mono_native_thread_id_get ())
258 pthread_setname_np ("");
262 strncpy (n, name, 63);
264 pthread_setname_np (n);
266 #elif defined (__NetBSD__)
268 pthread_setname_np (tid, "%s", (void*)"");
270 char n [PTHREAD_MAX_NAMELEN_NP];
272 strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
273 n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
274 pthread_setname_np (tid, "%s", (void*)n);
276 #elif defined (HAVE_PTHREAD_SETNAME_NP)
278 pthread_setname_np (tid, "");
282 strncpy (n, name, 16);
284 pthread_setname_np (tid, n);
290 mono_threads_platform_set_exited (MonoThreadInfo *info)
292 gpointer mutex_handle;
297 g_assert (info->handle);
299 if (mono_w32handle_issignalled (info->handle))
300 g_error ("%s: handle %p thread %p has already exited, it's handle is signalled", __func__, info->handle, mono_thread_info_get_tid (info));
301 if (mono_w32handle_get_type (info->handle) == MONO_W32HANDLE_UNUSED)
302 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));
304 pid = wapi_getpid ();
305 tid = pthread_self ();
307 for (i = 0; i < info->owned_mutexes->len; i++) {
308 mutex_handle = g_ptr_array_index (info->owned_mutexes, i);
309 wapi_mutex_abandon (mutex_handle, pid, tid);
310 mono_thread_info_disown_mutex (info, mutex_handle);
313 g_ptr_array_free (info->owned_mutexes, TRUE);
315 thr_ret = mono_w32handle_lock_handle (info->handle);
316 g_assert (thr_ret == 0);
318 mono_w32handle_set_signal_state (info->handle, TRUE, TRUE);
320 thr_ret = mono_w32handle_unlock_handle (info->handle);
321 g_assert (thr_ret == 0);
323 /* The thread is no longer active, so unref it */
324 mono_w32handle_unref (info->handle);
330 mono_threads_platform_describe (MonoThreadInfo *info, GString *text)
334 g_string_append_printf (text, "thread handle %p state : ", info->handle);
336 mono_thread_info_describe_interrupt_token (info, text);
338 g_string_append_printf (text, ", owns (");
339 for (i = 0; i < info->owned_mutexes->len; i++)
340 g_string_append_printf (text, i > 0 ? ", %p" : "%p", g_ptr_array_index (info->owned_mutexes, i));
341 g_string_append_printf (text, ")");
345 mono_threads_platform_own_mutex (MonoThreadInfo *info, gpointer mutex_handle)
347 mono_w32handle_ref (mutex_handle);
349 g_ptr_array_add (info->owned_mutexes, mutex_handle);
353 mono_threads_platform_disown_mutex (MonoThreadInfo *info, gpointer mutex_handle)
355 mono_w32handle_unref (mutex_handle);
357 g_ptr_array_remove (info->owned_mutexes, mutex_handle);
360 static const gchar* thread_typename (void)
365 static gsize thread_typesize (void)
370 static MonoW32HandleOps thread_ops = {
375 NULL, /* special_wait */
378 thread_typename, /* typename */
379 thread_typesize, /* typesize */
383 mono_threads_platform_init (void)
385 mono_w32handle_register_ops (MONO_W32HANDLE_THREAD, &thread_ops);
387 mono_w32handle_register_capabilities (MONO_W32HANDLE_THREAD, MONO_W32HANDLE_CAP_WAIT);
390 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
392 #if defined(USE_POSIX_BACKEND)
395 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
397 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
399 if (!mono_threads_pthread_kill (info, sig)) {
400 mono_threads_add_to_pending_operation_set (info);
407 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
409 return info->suspend_can_continue;
413 This begins async resume. This function must do the following:
415 - Install an async target if one was requested.
416 - Notify the target to resume.
419 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
421 mono_threads_add_to_pending_operation_set (info);
422 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
426 mono_threads_suspend_register (MonoThreadInfo *info)
428 #if defined (PLATFORM_ANDROID)
429 info->native_handle = gettid ();
434 mono_threads_suspend_free (MonoThreadInfo *info)
439 mono_threads_suspend_init (void)
441 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
444 #endif /* defined(USE_POSIX_BACKEND) */