+GC_PTR GC_get_thread_stack_base()
+{
+# ifdef HAVE_PTHREAD_GETATTR_NP
+ pthread_t my_pthread;
+ pthread_attr_t attr;
+ ptr_t stack_addr;
+ size_t stack_size;
+
+ my_pthread = pthread_self();
+ if (pthread_getattr_np (my_pthread, &attr) != 0)
+ {
+# ifdef DEBUG_THREADS
+ GC_printf1("Can not determine stack base for attached thread");
+# endif
+ return 0;
+ }
+ pthread_attr_getstack (&attr, (void **) &stack_addr, &stack_size);
+ pthread_attr_destroy (&attr);
+
+# ifdef DEBUG_THREADS
+ GC_printf1("attached thread stack address: 0x%x\n", stack_addr);
+# endif
+
+# ifdef STACK_GROWS_DOWN
+ return stack_addr + stack_size;
+# else
+ return stack_addr;
+# endif
+
+# else
+# ifdef DEBUG_THREADS
+ GC_printf1("Can not determine stack base for attached thread");
+# endif
+ return 0;
+# endif
+}
+
+void GC_register_my_thread()
+{
+ GC_thread me;
+ pthread_t my_pthread;
+
+ my_pthread = pthread_self();
+# ifdef DEBUG_THREADS
+ GC_printf1("Attaching thread 0x%lx\n", my_pthread);
+ GC_printf1("pid = %ld\n", (long) getpid());
+# endif
+
+ /* Check to ensure this thread isn't attached already. */
+ LOCK();
+ me = GC_lookup_thread (my_pthread);
+ UNLOCK();
+ if (me != 0)
+ {
+# ifdef DEBUG_THREADS
+ GC_printf1("Attempt to re-attach known thread 0x%lx\n", my_pthread);
+# endif
+ return;
+ }
+
+ LOCK();
+ GC_in_thread_creation = TRUE;
+ me = GC_new_thread(my_pthread);
+ GC_in_thread_creation = FALSE;
+
+ me -> flags |= DETACHED;
+
+#ifdef GC_DARWIN_THREADS
+ me -> stop_info.mach_thread = mach_thread_self();
+#else
+ me -> stack_end = GC_get_thread_stack_base();
+ if (me -> stack_end == 0)
+ GC_abort("Can not determine stack base for attached thread");
+
+# ifdef STACK_GROWS_DOWN
+ me -> stop_info.stack_ptr = me -> stack_end - 0x10;
+# else
+ me -> stop_info.stack_ptr = me -> stack_end + 0x10;
+# endif
+#endif
+
+# ifdef IA64
+ me -> backing_store_end = (ptr_t)
+ (GC_save_regs_in_stack() & ~(GC_page_size - 1));
+ /* This is also < 100% convincing. We should also read this */
+ /* from /proc, but the hook to do so isn't there yet. */
+# endif /* IA64 */
+
+# if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
+ GC_init_thread_local(me);
+# endif
+ UNLOCK();
+}
+
+void GC_unregister_my_thread()
+{
+ pthread_t my_pthread;
+
+ my_pthread = pthread_self();
+
+# ifdef DEBUG_THREADS
+ GC_printf1("Detaching thread 0x%lx\n", my_pthread);
+# endif
+
+ GC_thread_exit_proc (0);
+}
+