+void
+ves_icall_System_GCHandle_FreeHandle (guint32 handle)
+{
+ int idx = handle >> 2;
+ int type = handle & 0x3;
+
+ MONO_ARCH_SAVE_REGS;
+
+ EnterCriticalSection (&handle_section);
+
+#ifdef HAVE_BOEHM_GC
+ g_assert (type == gc_handle_types [idx]);
+ if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
+ if (gc_handles [idx] != (gpointer)-1)
+ GC_unregister_disappearing_link (&(gc_handles [idx]));
+ }
+#else
+ mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support"));
+#endif
+
+ gc_handles [idx] = (gpointer)-1;
+ gc_handle_types [idx] = (guint8)-1;
+ LeaveCriticalSection (&handle_section);
+}
+
+gpointer
+ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
+{
+ MonoObject *obj;
+ int type = handle & 0x3;
+
+ MONO_ARCH_SAVE_REGS;
+
+ if (gc_handles) {
+ EnterCriticalSection (&handle_section);
+ obj = gc_handles [handle >> 2];
+ g_assert (gc_handle_types [handle >> 2] == type);
+ LeaveCriticalSection (&handle_section);
+ if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
+ obj = REVEAL_POINTER (obj);
+ if (obj == (MonoObject *) -1)
+ return NULL;
+ }
+ return obj;
+ }
+ return NULL;
+}
+
+#if HAVE_BOEHM_GC
+
+static HANDLE finalizer_event;
+static volatile gboolean finished=FALSE;
+
+static void finalize_notify (void)
+{
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
+#endif
+
+ SetEvent (finalizer_event);
+}
+
+static guint32 finalizer_thread (gpointer unused)
+{
+ guint32 stack_start;
+
+ mono_thread_new_init (GetCurrentThreadId (), &stack_start, NULL);
+
+ while(!finished) {
+ /* Wait to be notified that there's at least one
+ * finaliser to run
+ */
+ WaitForSingleObject (finalizer_event, INFINITE);
+
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION ": invoking finalizers");
+#endif
+
+ GC_invoke_finalizers ();
+ }
+
+ return(0);
+}
+
+/*
+ * Enable or disable the separate finalizer thread.
+ * It's currently disabled because it still requires some
+ * work in the rest of the runtime.
+ */
+#define ENABLE_FINALIZER_THREAD
+
+#ifdef WITH_INCLUDED_LIBGC
+/* from threads.c */
+extern void mono_gc_stop_world (void);
+extern void mono_gc_start_world (void);
+extern void mono_gc_push_all_stacks (void);
+
+static void mono_gc_lock (void)
+{
+ EnterCriticalSection (&allocator_section);
+}
+
+static void mono_gc_unlock (void)
+{
+ LeaveCriticalSection (&allocator_section);
+}
+
+static GCThreadFunctions mono_gc_thread_vtable = {
+ NULL,
+
+ mono_gc_lock,
+ mono_gc_unlock,
+
+ mono_gc_stop_world,
+ NULL,
+ mono_gc_push_all_stacks,
+ mono_gc_start_world
+};
+#endif /* WITH_INCLUDED_LIBGC */
+
+void mono_gc_init (void)
+{
+ HANDLE gc_thread;
+
+ InitializeCriticalSection (&handle_section);
+ InitializeCriticalSection (&allocator_section);
+
+#ifdef WITH_INCLUDED_LIBGC
+ gc_thread_vtable = &mono_gc_thread_vtable;
+#endif
+
+#ifdef ENABLE_FINALIZER_THREAD
+
+ if (getenv ("GC_DONT_GC"))
+ return;
+
+ finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
+ if (finalizer_event == NULL) {
+ g_assert_not_reached ();
+ }
+
+ GC_finalize_on_demand = 1;
+ GC_finalizer_notifier = finalize_notify;
+
+ /* Don't use mono_thread_create here, because we don't want
+ * the runtime to wait for this thread to exit when it's
+ * cleaning up.
+ */
+ gc_thread = CreateThread (NULL, 0, finalizer_thread, NULL, 0, NULL);
+ if (gc_thread == NULL) {
+ g_assert_not_reached ();
+ }
+#endif
+}
+
+void mono_gc_cleanup (void)
+{
+#ifdef DEBUG
+ g_message (G_GNUC_PRETTY_FUNCTION ": cleaning up finalizer");
+#endif
+
+#ifdef ENABLE_FINALIZER_THREAD
+ finished = TRUE;
+ finalize_notify ();
+#endif
+}
+
+#else
+
+/* no Boehm GC support. */
+void mono_gc_init (void)
+{
+ InitializeCriticalSection (&handle_section);
+}
+
+void mono_gc_cleanup (void)
+{
+}
+
+#endif
+