Browse Source

workaround for broken OSX ucontext

David Rose 16 years ago
parent
commit
d001c75524
2 changed files with 18 additions and 2 deletions
  1. 12 2
      panda/src/pipeline/contextSwitch.c
  2. 6 0
      panda/src/pipeline/contextSwitch.h

+ 12 - 2
panda/src/pipeline/contextSwitch.c

@@ -15,6 +15,7 @@
 #include "contextSwitch.h"
 #include "contextSwitch.h"
 
 
 #include <stdlib.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 
 #ifdef THREAD_SIMPLE_IMPL
 #ifdef THREAD_SIMPLE_IMPL
 
 
@@ -31,7 +32,11 @@ void
 init_thread_context(struct ThreadContext *context, 
 init_thread_context(struct ThreadContext *context, 
                     unsigned char *stack, size_t stack_size,
                     unsigned char *stack, size_t stack_size,
                     ContextFunction *thread_func, void *data) {
                     ContextFunction *thread_func, void *data) {
-  getcontext(&context->_ucontext);
+  if (getcontext(&context->_ucontext) != 0) {
+    fprintf(stderr, "getcontext failed in init_thread_context!\n");
+    // Too bad for you.
+    abort();
+  }
 
 
   context->_ucontext.uc_stack.ss_sp = stack;
   context->_ucontext.uc_stack.ss_sp = stack;
   context->_ucontext.uc_stack.ss_size = stack_size;
   context->_ucontext.uc_stack.ss_size = stack_size;
@@ -49,7 +54,12 @@ save_thread_context(struct ThreadContext *context,
      (return from setcontext). */
      (return from setcontext). */
   volatile int context_return = 0;
   volatile int context_return = 0;
 
 
-  getcontext(&context->_ucontext);
+  if (getcontext(&context->_ucontext) != 0) {
+    fprintf(stderr, "getcontext failed!\n");
+    // Nothing to do here.
+    abort();
+  }
+
   if (context_return) {
   if (context_return) {
     /* We have just returned from setcontext.  In this case, return
     /* We have just returned from setcontext.  In this case, return
        from the function.  The stack is still good. */
        from the function.  The stack is still good. */

+ 6 - 0
panda/src/pipeline/contextSwitch.h

@@ -38,6 +38,12 @@
 
 
 struct ThreadContext {
 struct ThreadContext {
   ucontext_t _ucontext;
   ucontext_t _ucontext;
+#if defined(__APPLE__)
+  // Due to a bug in OSX 10.5, the system ucontext_t declaration
+  // doesn't reserve enough space, and we need to reserve some
+  // additional space to make room.
+  _STRUCT_MCONTEXT _extra_padding;
+#endif
 };
 };
 
 
 #else  /* HAVE_UCONTEXT_H */
 #else  /* HAVE_UCONTEXT_H */