Leverage SgenThreadInfo::mach_port.
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 15 Mar 2011 16:02:39 +0000 (17:02 +0100)
committerRodrigo Kumpera <kumpera@gmail.com>
Tue, 15 Mar 2011 16:06:18 +0000 (17:06 +0100)
* mach-support-*.c (mono_mach_arch_get_tls_value_from_thread):
This function now receives pthread_t instead of mach_port
as argument, so avoiding an expensive call to
pthread_from_mach_thread_np.

* sgen-os-mach.c (mono_sgen_thread_handshake): Loop over
the registered threads insted of asking the kernel to enumerate
them. Avoid calling pthread_from_mach_thread_np which takes
a lock and is O(n) on the number of threads.

mono/metadata/sgen-os-mach.c
mono/utils/mach-support-amd64.c
mono/utils/mach-support-arm.c
mono/utils/mach-support-x86.c
mono/utils/mach-support.h

index 8b3a986110b09f45f63f887423fd0121e351d878..f9e996a22a7728bfc4f1056bd5fbb6f3083dd09a 100644 (file)
 int
 mono_sgen_thread_handshake (int signum)
 {
-       task_t task = current_task ();
-       thread_port_t cur_thread = mach_thread_self ();
-       thread_act_array_t thread_list;
-       mach_msg_type_number_t num_threads;
+       SgenThreadInfo *cur_thread = mono_sgen_thread_info_lookup (ARCH_GET_THREAD ());
        mach_msg_type_number_t num_state;
        thread_state_t state;
        kern_return_t ret;
        ucontext_t ctx;
        mcontext_t mctx;
-       pthread_t exception_thread = mono_gc_get_mach_exception_thread ();
 
        SgenThreadInfo *info;
 #ifdef USE_MONO_CTX
@@ -64,78 +60,54 @@ mono_sgen_thread_handshake (int signum)
 #endif
        gpointer stack_start;
 
-       int count, i;
+       int count;
 
-       mono_mach_get_threads (&thread_list, &num_threads);
+       FOREACH_THREAD (info) {
+               if (info == cur_thread || mono_sgen_is_worker_thread (info->id))
+                       continue;
 
-       for (i = 0, count = 0; i < num_threads; i++) {
-               thread_port_t t = thread_list [i];
-               pthread_t pt = pthread_from_mach_thread_np (t);
-               if (t != cur_thread && pt != exception_thread && !mono_sgen_is_worker_thread (pt)) {
-                       if (signum == suspend_signal_num) {
-                               ret = thread_suspend (t);
-                               if (ret != KERN_SUCCESS) {
-                                       mach_port_deallocate (task, t);
-                                       continue;
-                               }
+               if (signum == suspend_signal_num) {
+                       ret = thread_suspend (info->mach_port);
+                       if (ret != KERN_SUCCESS)
+                               continue;
 
-                               state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
-                               ret = mono_mach_arch_get_thread_state (t, state, &num_state);
-                               if (ret != KERN_SUCCESS) {
-                                       mach_port_deallocate (task, t);
-                                       continue;
-                               }
+                       state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
+                       ret = mono_mach_arch_get_thread_state (info->mach_port, state, &num_state);
+                       if (ret != KERN_SUCCESS)
+                               continue;
 
+                       mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
+                       mono_mach_arch_thread_state_to_mcontext (state, mctx);
+                       ctx.uc_mcontext = mctx;
 
-                               info = mono_sgen_thread_info_lookup (pt);
-
-                               /* Ensure that the runtime is aware of this thread */
-                               if (info != NULL) {
-                                       mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
-                                       mono_mach_arch_thread_state_to_mcontext (state, mctx);
-                                       ctx.uc_mcontext = mctx;
-
-                                       info->stopped_domain = mono_mach_arch_get_tls_value_from_thread (t, mono_pthread_key_for_tls (mono_domain_get_tls_key ()));
-                                       info->stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
-                                       stack_start = (char*) mono_mach_arch_get_sp (state) - REDZONE_SIZE;
-                                       /* If stack_start is not within the limits, then don't set it in info and we will be restarted. */
-                                       if (stack_start >= info->stack_start_limit && info->stack_start <= info->stack_end) {
-                                               info->stack_start = stack_start;
+                       info->stopped_domain = mono_mach_arch_get_tls_value_from_thread ((pthread_t)info->id, mono_pthread_key_for_tls (mono_domain_get_tls_key ()));
+                       info->stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
+                       stack_start = (char*) mono_mach_arch_get_sp (state) - REDZONE_SIZE;
+                       /* If stack_start is not within the limits, then don't set it in info and we will be restarted. */
+                       if (stack_start >= info->stack_start_limit && info->stack_start <= info->stack_end) {
+                               info->stack_start = stack_start;
 
 #ifdef USE_MONO_CTX
-                                               mono_sigctx_to_monoctx (&ctx, &monoctx);
-                                               info->monoctx = &monoctx;
+                               mono_sigctx_to_monoctx (&ctx, &monoctx);
+                               info->monoctx = &monoctx;
 #else
-                                               ARCH_COPY_SIGCTX_REGS (regs, &ctx);
-                                               info->stopped_regs = regs;
+                               ARCH_COPY_SIGCTX_REGS (regs, &ctx);
+                               info->stopped_regs = regs;
 #endif
-                                       } else {
-                                               g_assert (!info->stack_start);
-                                       }
-
-                                       /* Notify the JIT */
-                                       if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
-                                               mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, &ctx);
-                               }
                        } else {
-                               ret = thread_resume (t);
-                               if (ret != KERN_SUCCESS) {
-                                       mach_port_deallocate (task, t);
-                                       continue;
-                               }
+                               g_assert (!info->stack_start);
                        }
-                       count ++;
 
-                       mach_port_deallocate (task, t);
+                       /* Notify the JIT */
+                       if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
+                               mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, &ctx);
                } else {
-                       mach_port_deallocate (task, t);
+                       ret = thread_resume (info->mach_port);
+                       if (ret != KERN_SUCCESS)
+                               continue;
                }
-       }
-
-       mono_mach_free_threads (thread_list, num_threads);
-
-       mach_port_deallocate (task, cur_thread);
-
+               count ++;
+       } END_FOREACH_THREAD
        return count;
 }
 #endif
index 506e21a642ca8652d5cdf632777950f66b619aa1..2b544e7b3e6a9071a5174c76689deb2f1a374769 100644 (file)
@@ -67,13 +67,13 @@ mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mac
 }
 
 void *
-mono_mach_arch_get_tls_value_from_thread (thread_port_t thread, guint32 key)
+mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
 {
        /* OSX stores TLS values in a hidden array inside the pthread_t structure
         * They are keyed off a giant array offset 0x60 into the pointer.  This value
         * is baked into their pthread_getspecific implementation
         */
-       intptr_t *p = (intptr_t *) pthread_from_mach_thread_np (thread);
+       intptr_t *p = (intptr_t *)thread;
        intptr_t **tsd = (intptr_t **) (p + 0x60);
 
        return (void *) tsd [key];
index a5d27d46871b0d99403ecaa86e1cdb65e1412a81..a5bac74cc5bc480c90d1efb157ec8ea3d6a8ac09 100644 (file)
@@ -67,13 +67,13 @@ mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mac
 }
 
 void *
-mono_mach_arch_get_tls_value_from_thread (thread_port_t thread, guint32 key)
+mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
 {
        /* OSX stores TLS values in a hidden array inside the pthread_t structure
         * They are keyed off a giant array offset 0x48 into the pointer.  This value
         * is baked into their pthread_getspecific implementation
         */
-       intptr_t *p = (intptr_t *) pthread_from_mach_thread_np (thread);
+       intptr_t *p = (intptr_t *) thread;
        intptr_t **tsd = (intptr_t **) (p + 0x48 + (key << 2));
 
        return (void *) *tsd;
index a5b7def4337d00fb101e85de2a605c320f475b3b..313cad07812851108a65cbf1c415330ceaad7cca 100644 (file)
@@ -67,13 +67,13 @@ mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mac
 }
 
 void *
-mono_mach_arch_get_tls_value_from_thread (thread_port_t thread, guint32 key)
+mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
 {
        /* OSX stores TLS values in a hidden array inside the pthread_t structure
         * They are keyed off a giant array offset 0x48 into the pointer.  This value
         * is baked into their pthread_getspecific implementation
         */
-       intptr_t *p = (intptr_t *) pthread_from_mach_thread_np (thread);
+       intptr_t *p = (intptr_t *) thread;
        intptr_t **tsd = (intptr_t **) (p + 0x48);
 
        return (void *) tsd [key];
index 8eae6d5d6b647ac2ace44fa62f411d5c09f9d464..5bc6bbb3625df4cf797c0f59fd3525584d591e79 100644 (file)
@@ -30,7 +30,7 @@ int mono_mach_arch_get_thread_state_size (void) MONO_INTERNAL;
 kern_return_t mono_mach_get_threads (thread_act_array_t *threads, guint32 *count) MONO_INTERNAL;
 kern_return_t mono_mach_free_threads (thread_act_array_t threads, guint32 count) MONO_INTERNAL;
 kern_return_t mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count) MONO_INTERNAL;
-void *mono_mach_arch_get_tls_value_from_thread (thread_port_t thread, guint32 key) MONO_INTERNAL;
+void *mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key) MONO_INTERNAL;
 
 #endif
 #endif /* __MONO_MACH_SUPPORT_H__ */