Miscellaneous cleanups for threaded darwin platforms
authorCyrus Harmon <ch-lisp@bobobeach.com>
Sun, 29 Jul 2012 13:33:45 +0000 (06:33 -0700)
committerPaul Khuong <pvk@pvk.ca>
Wed, 1 Aug 2012 22:09:59 +0000 (18:09 -0400)
 * Gather some related declarations in fewer (conditionalised) places

 * Lay down some infrastructure for mach port different from threads'
   addresses

 * Slightly modified by Paul Khuong

src/compiler/generic/objdef.lisp
src/runtime/darwin-os.c
src/runtime/thread.c
src/runtime/thread.h
src/runtime/x86-64-bsd-os.c
src/runtime/x86-64-darwin-os.c
src/runtime/x86-darwin-os.c

index dcc1c72..938990c 100644 (file)
   ;; Same as above for the location of the current control stack
   ;; pointer.  This is also used on threaded x86oids to allow LDB to
   ;; print an approximation of the CSP as needed.
-  #!+(and sb-thread)
+  #!+sb-thread
   (control-stack-pointer :c-type "lispobj *")
+  #!+mach-exception-handler
+  (mach-port-name :c-type "mach_port_name_t")
   ;; KLUDGE: On alpha, until STEPPING we have been lucky and the 32
   ;; bit slots came in pairs. However the C compiler will align
   ;; interrupt_contexts on a double word boundary. This logic should
index fbbae26..9cbf128 100644 (file)
@@ -148,10 +148,32 @@ mach_thread_init(mach_port_t thread_exception_port)
     return ret;
 }
 
+kern_return_t
+mach_lisp_thread_init(struct thread *thread) {
+    mach_port_t port = (mach_port_t) thread;
+    kern_return_t ret;
+    ret = mach_thread_init(port);
+    thread->mach_port_name = port;
+
+    return ret;
+}
+
+kern_return_t
+mach_lisp_thread_destroy(struct thread *thread) {
+    mach_port_t port = (mach_port_t) thread;
+
+    FSHOW((stderr, "Deallocating mach port %x\n", port));
+    mach_port_move_member(current_mach_task, port, MACH_PORT_NULL);
+    mach_port_deallocate(current_mach_task, port);
+
+    return mach_port_destroy(current_mach_task, port);
+}
+
 void
 setup_mach_exceptions() {
+    mach_port_t port = (mach_port_t) all_threads;
     setup_mach_exception_handling_thread();
-    mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
+    mach_thread_init(port);
 }
 
 pid_t
index 7a18c5a..4b42d1d 100644 (file)
@@ -392,14 +392,7 @@ new_thread_trampoline(struct thread *th)
                   (sizeof (struct interrupt_data)));
 
 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
-    FSHOW((stderr, "Deallocating mach port %x\n", THREAD_STRUCT_TO_EXCEPTION_PORT(th)));
-    mach_port_move_member(current_mach_task,
-                          THREAD_STRUCT_TO_EXCEPTION_PORT(th),
-                          MACH_PORT_NULL);
-    mach_port_deallocate(current_mach_task,
-                         THREAD_STRUCT_TO_EXCEPTION_PORT(th));
-    mach_port_destroy(current_mach_task,
-                      THREAD_STRUCT_TO_EXCEPTION_PORT(th));
+    mach_lisp_thread_destroy(th);
 #endif
 
     schedule_thread_post_mortem(th);
@@ -590,12 +583,6 @@ create_thread_struct(lispobj initial_function) {
     return th;
 }
 
-#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
-mach_port_t setup_mach_exception_handling_thread();
-kern_return_t mach_thread_init(mach_port_t thread_exception_port);
-
-#endif
-
 void create_initial_thread(lispobj initial_function) {
     struct thread *th=create_thread_struct(initial_function);
 #ifdef LISP_FEATURE_SB_THREAD
index b8b99d3..76ec1a6 100644 (file)
@@ -229,8 +229,8 @@ static inline struct thread *arch_os_get_current_thread(void)
 }
 
 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
-#define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
-#define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
+extern kern_return_t mach_lisp_thread_init(struct thread *thread);
+extern kern_return_t mach_lisp_thread_destroy(struct thread *thread);
 #endif
 
 extern void create_initial_thread(lispobj);
index 8c54503..3d9a9c7 100644 (file)
 #include <machine/fpu.h>
 #endif
 
-#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
-#include <mach/mach.h>
-
-kern_return_t mach_thread_init(mach_port_t thread_exception_port);
-#endif
-
 /* KLUDGE: There is strong family resemblance in the signal context
  * stuff in FreeBSD and OpenBSD, but in detail they're different in
  * almost every line of code. It would be nice to find some way to
@@ -155,7 +149,7 @@ int arch_os_thread_init(struct thread *thread) {
 #endif
 
 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
-    mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread));
+    mach_lisp_thread_init(thread);
 #endif
 
 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
index 1050ade..8a25e63 100644 (file)
@@ -65,8 +65,6 @@ pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER;
 
 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
 
-kern_return_t mach_thread_init(mach_port_t thread_exception_port);
-
 void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context);
 void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context);
 void memory_fault_handler(int signal, siginfo_t *siginfo,
index bdc1854..edcd85c 100644 (file)
@@ -44,10 +44,6 @@ void set_data_desc_addr(data_desc_t* desc, void* addr)
 
 #endif
 
-#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
-kern_return_t mach_thread_init(mach_port_t thread_exception_port);
-#endif
-
 int arch_os_thread_init(struct thread *thread) {
 #ifdef LISP_FEATURE_SB_THREAD
     int n;
@@ -79,7 +75,7 @@ int arch_os_thread_init(struct thread *thread) {
     pthread_setspecific(specials,thread);
 #endif
 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
-    mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread));
+    mach_lisp_thread_init(thread);
 #endif
 
 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
@@ -386,7 +382,6 @@ catch_exception_raise(mach_port_t exception_port,
                       exception_data_t code_vector,
                       mach_msg_type_number_t code_count)
 {
-    struct thread *th = (struct thread*) exception_port;
     x86_thread_state32_t thread_state;
     mach_msg_type_number_t state_count;
     vm_address_t region_addr;
@@ -400,6 +395,10 @@ catch_exception_raise(mach_port_t exception_port,
     siginfo_t siginfo;
     kern_return_t ret, dealloc_ret;
 
+    struct thread *th = (struct thread*) exception_port;
+
+    FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
+
     /* Get state and info */
     state_count = x86_THREAD_STATE32_COUNT;
     if ((ret = thread_get_state(thread,