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/metadata/gc-internals.h>
23 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
24 #define USE_TKILL_ON_ANDROID 1
27 #ifdef USE_TKILL_ON_ANDROID
28 extern int tkill (pid_t tid, int signal);
31 #if defined(_POSIX_VERSION) || defined(__native_client__)
33 #include <sys/resource.h>
35 #if defined(__native_client__)
36 void nacl_shutdown_gc_thread(void);
40 void *(*start_routine)(void*);
43 MonoSemType registered;
48 inner_start_thread (void *arg)
50 StartInfo *start_info = (StartInfo *) arg;
51 void *t_arg = start_info->arg;
53 void *(*start_func)(void*) = start_info->start_routine;
54 guint32 flags = start_info->flags;
59 /* Register the thread with the io-layer */
60 handle = wapi_create_thread_handle ();
62 res = MONO_SEM_POST (&(start_info->registered));
66 start_info->handle = handle;
68 info = mono_thread_info_attach (&result);
69 MONO_PREPARE_BLOCKING;
71 info->runtime_thread = TRUE;
72 info->handle = handle;
74 if (flags & CREATE_SUSPENDED) {
75 info->create_suspended = TRUE;
76 MONO_SEM_INIT (&info->create_suspended_sem, 0);
79 /* start_info is not valid after this */
80 res = MONO_SEM_POST (&(start_info->registered));
84 if (flags & CREATE_SUSPENDED) {
85 while (MONO_SEM_WAIT (&info->create_suspended_sem) != 0 &&
87 MONO_SEM_DESTROY (&info->create_suspended_sem);
91 /* Run the actual main function of the thread */
92 result = start_func (t_arg);
94 mono_threads_core_exit (GPOINTER_TO_UINT (result));
95 g_assert_not_reached ();
99 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
104 StartInfo start_info;
106 res = pthread_attr_init (&attr);
109 if (stack_size == 0) {
110 #if HAVE_VALGRIND_MEMCHECK_H
111 if (RUNNING_ON_VALGRIND)
112 stack_size = 1 << 20;
114 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
116 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
120 #ifdef PTHREAD_STACK_MIN
121 if (stack_size < PTHREAD_STACK_MIN)
122 stack_size = PTHREAD_STACK_MIN;
125 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
126 res = pthread_attr_setstacksize (&attr, stack_size);
130 memset (&start_info, 0, sizeof (StartInfo));
131 start_info.start_routine = (void *(*)(void *)) start_routine;
132 start_info.arg = arg;
133 start_info.flags = creation_flags;
134 MONO_SEM_INIT (&(start_info.registered), 0);
136 /* Actually start the thread */
137 res = mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
139 MONO_SEM_DESTROY (&(start_info.registered));
144 /* Wait until the thread register itself in various places */
145 while (MONO_SEM_WAIT (&(start_info.registered)) != 0) {
146 /*if (EINTR != errno) ABORT("sem_wait failed"); */
148 MONO_FINISH_TRY_BLOCKING;
150 MONO_SEM_DESTROY (&(start_info.registered));
155 return start_info.handle;
159 * mono_threads_core_resume_created:
161 * Resume a newly created thread created using CREATE_SUSPENDED.
164 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
166 MONO_SEM_POST (&info->create_suspended_sem);
170 mono_threads_core_yield (void)
172 return sched_yield () == 0;
176 mono_threads_core_exit (int exit_code)
178 MonoThreadInfo *current = mono_thread_info_current ();
180 #if defined(__native_client__)
181 nacl_shutdown_gc_thread();
184 wapi_thread_handle_set_exited (current->handle, exit_code);
186 mono_thread_info_detach ();
192 mono_threads_core_unregister (MonoThreadInfo *info)
195 wapi_thread_handle_set_exited (info->handle, 0);
201 mono_threads_core_open_handle (void)
203 MonoThreadInfo *info;
205 info = mono_thread_info_current ();
209 info->handle = wapi_create_thread_handle ();
211 wapi_ref_thread_handle (info->handle);
216 mono_threads_get_max_stack_size (void)
220 /* If getrlimit fails, we don't enforce any limits. */
221 if (getrlimit (RLIMIT_STACK, &lim))
223 /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
224 if (lim.rlim_max > (rlim_t)INT_MAX)
226 return (int)lim.rlim_max;
230 mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
232 wapi_ref_thread_handle (handle);
238 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
240 THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
241 #ifdef USE_TKILL_ON_ANDROID
242 int result, old_errno = errno;
243 result = tkill (info->native_handle, signum);
249 #elif defined(__native_client__)
250 /* Workaround pthread_kill abort() in NaCl glibc. */
252 #elif !defined(HAVE_PTHREAD_KILL)
253 g_error ("pthread_kill() is not supported by this platform");
255 return pthread_kill (mono_thread_info_get_tid (info), signum);
260 mono_native_thread_id_get (void)
262 return pthread_self ();
266 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
268 return pthread_equal (id1, id2);
272 * mono_native_thread_create:
274 * Low level thread creation function without any GC wrappers.
277 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
279 return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
283 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
285 #if defined (HAVE_PTHREAD_SETNAME_NP) && !defined (__MACH__)
287 pthread_setname_np (tid, "");
291 strncpy (n, name, 16);
293 pthread_setname_np (tid, n);
298 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
300 #if defined(USE_POSIX_BACKEND)
303 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
305 int sig = interrupt_kernel ? mono_threads_posix_get_abort_signal () : mono_threads_posix_get_suspend_signal ();
307 if (!mono_threads_pthread_kill (info, sig)) {
308 mono_threads_add_to_pending_operation_set (info);
315 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
317 return info->suspend_can_continue;
321 This begins async resume. This function must do the following:
323 - Install an async target if one was requested.
324 - Notify the target to resume.
327 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
329 mono_threads_add_to_pending_operation_set (info);
330 return mono_threads_pthread_kill (info, mono_threads_posix_get_restart_signal ()) == 0;
334 mono_threads_platform_register (MonoThreadInfo *info)
336 #if defined (PLATFORM_ANDROID)
337 info->native_handle = gettid ();
342 mono_threads_platform_free (MonoThreadInfo *info)
347 mono_threads_init_platform (void)
349 mono_threads_posix_init_signals (MONO_THREADS_POSIX_INIT_SIGNALS_SUSPEND_RESTART);
352 #endif /* defined(USE_POSIX_BACKEND) */