Add NoData as valid return state for Statements with params
[mono.git] / mono / metadata / sgen-os-mach.c
index 8b3a986110b09f45f63f887423fd0121e351d878..7d599e20908e59e6559b0e1bafbaa41e01e76f4f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * sgen-os-mach.c: Simple generational GC.
+ * sgen-os-mach.c: Mach-OS support.
  *
  * Author:
  *     Paolo Molaro (lupus@ximian.com)
  *     Geoff Norton (gnorton@novell.com)
  *
  * Copyright 2010 Novell, Inc (http://www.novell.com)
+ * Copyright (C) 2012 Xamarin Inc
  *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- * 
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License 2.0 as published by the Free Software Foundation;
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License 2.0 along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 #include "config.h"
 #ifdef HAVE_SGEN_GC
+
+
 #include <glib.h>
-#include "metadata/gc-internal.h"
 #include "metadata/sgen-gc.h"
 #include "metadata/sgen-archdep.h"
+#include "metadata/sgen-protocol.h"
 #include "metadata/object-internals.h"
+#include "metadata/gc-internal.h"
 
 #if defined(__MACH__)
 #include "utils/mach-support.h"
 #endif
 
-/* LOCKING: assumes the GC lock is held */
 #if defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED
-int
-mono_sgen_thread_handshake (int signum)
+gboolean
+sgen_resume_thread (SgenThreadInfo *info)
+{
+       return thread_resume (info->info.native_handle) == KERN_SUCCESS;
+}
+
+gboolean
+sgen_suspend_thread (SgenThreadInfo *info)
 {
-       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;
        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;
+       gpointer stack_start;
+
+       state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
+       mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
+
+       ret = thread_suspend (info->info.native_handle);
+       if (ret != KERN_SUCCESS)
+               return FALSE;
+
+       ret = mono_mach_arch_get_thread_state (info->info.native_handle, state, &num_state);
+       if (ret != KERN_SUCCESS)
+               return FALSE;
+
+       mono_mach_arch_thread_state_to_mcontext (state, mctx);
+       ctx.uc_mcontext = mctx;
+
+       info->stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
+       info->stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
+       info->stack_start = NULL;
+       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
-       MonoContext monoctx;
+               mono_sigctx_to_monoctx (&ctx, &info->ctx);
 #else
-       gpointer regs [ARCH_NUM_REGS];
+               ARCH_COPY_SIGCTX_REGS (&info->regs, &ctx);
 #endif
-       gpointer stack_start;
+       } 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, NULL);
 
-       int count, i;
+       SGEN_LOG (2, "thread %p stopped at %p stack_start=%p", (void*)(gsize)info->info.native_handle, info->stopped_ip, info->stack_start);
 
-       mono_mach_get_threads (&thread_list, &num_threads);
+       binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->stopped_ip);
 
-       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;
-                               }
+       return TRUE;
+}
 
-                               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;
-                               }
+void
+sgen_wait_for_suspend_ack (int count)
+{
+    /* mach thread_resume is synchronous so we dont need to wait for them */
+}
 
+/* LOCKING: assumes the GC lock is held */
+int
+sgen_thread_handshake (BOOL suspend)
+{
+       SgenThreadInfo *cur_thread = mono_thread_info_current ();
+       kern_return_t ret;
+       SgenThreadInfo *info;
 
-                               info = mono_sgen_thread_info_lookup (pt);
+       int count = 0;
 
-                               /* 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;
+       cur_thread->suspend_done = TRUE;
+       FOREACH_THREAD_SAFE (info) {
+               if (info == cur_thread || sgen_is_worker_thread (mono_thread_info_get_tid (info)))
+                       continue;
 
-                                       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->suspend_done = FALSE;
+               if (info->gc_disabled)
+                       continue;
 
-#ifdef USE_MONO_CTX
-                                               mono_sigctx_to_monoctx (&ctx, &monoctx);
-                                               info->monoctx = &monoctx;
-#else
-                                               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;
-                               }
-                       }
-                       count ++;
-
-                       mach_port_deallocate (task, t);
+               if (suspend) {
+                       if (!sgen_suspend_thread (info))
+                               continue;
                } else {
-                       mach_port_deallocate (task, t);
+                       ret = thread_resume (info->info.native_handle);
+                       if (ret != KERN_SUCCESS)
+                               continue;
                }
-       }
+               count ++;
+       } END_FOREACH_THREAD_SAFE
+       return count;
+}
 
-       mono_mach_free_threads (thread_list, num_threads);
+void
+sgen_os_init (void)
+{
+}
 
-       mach_port_deallocate (task, cur_thread);
+int
+mono_gc_get_suspend_signal (void)
+{
+       return -1;
+}
 
-       return count;
+int
+mono_gc_get_restart_signal (void)
+{
+       return -1;
 }
 #endif
 #endif