context: move Mac swapcontext() to separate compilation unit
authorBrian Norris <banorris@uci.edu>
Thu, 4 Apr 2013 20:19:50 +0000 (13:19 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 4 Apr 2013 20:21:22 +0000 (13:21 -0700)
We really don't want model_swapcontext() to ever be inlined on Mac OSX,
since this could potentially cause subtle getcontext()-related bugs to
resurface. Just make it a separate compilation unit (*.cc file).

Makefile
context.cc [new file with mode: 0644]
context.h

index b653e7699c4c055dbfa99b68fab57c5fd95d303c..5120cce8845ce1e4858b4bd5e7cd9d2618e59203 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,8 @@ include common.mk
 OBJECTS = libthreads.o schedule.o model.o threads.o librace.o action.o \
          nodestack.o clockvector.o main.o snapshot-interface.o cyclegraph.o \
          datarace.o impatomic.o cmodelint.o \
 OBJECTS = libthreads.o schedule.o model.o threads.o librace.o action.o \
          nodestack.o clockvector.o main.o snapshot-interface.o cyclegraph.o \
          datarace.o impatomic.o cmodelint.o \
-         snapshot.o malloc.o mymemory.o common.o mutex.o promise.o conditionvariable.o
+         snapshot.o malloc.o mymemory.o common.o mutex.o promise.o conditionvariable.o \
+         context.o
 
 CPPFLAGS += -Iinclude -I.
 LDFLAGS = -ldl -lrt -rdynamic
 
 CPPFLAGS += -Iinclude -I.
 LDFLAGS = -ldl -lrt -rdynamic
diff --git a/context.cc b/context.cc
new file mode 100644 (file)
index 0000000..b5ae0ba
--- /dev/null
@@ -0,0 +1,27 @@
+#include "context.h"
+
+#ifdef MAC
+
+int model_swapcontext(ucontext_t *oucp, ucontext_t *ucp)
+{
+       /*
+        * Mac OSX swapcontext() clobbers some registers, so use a hand-rolled
+        * version with {get,set}context(). We can avoid the same problem
+        * (where optimizations can break the following code) because we don't
+        * statically link with the C library
+        */
+
+       /* volatile, so that 'i' doesn't get promoted to a register */
+       volatile int i = 0;
+
+       getcontext(oucp);
+
+       if (i == 0) {
+               i = 1;
+               setcontext(ucp);
+       }
+
+       return 0;
+}
+
+#endif /* MAC */
index 862cda5e392451f19bb9b5cfe63680562dbf44bb..ea32d2f5edf3e21a4a95623899df07919eb1cf35 100644 (file)
--- a/context.h
+++ b/context.h
@@ -8,30 +8,17 @@
 
 #include <ucontext.h>
 
 
 #include <ucontext.h>
 
-static inline int model_swapcontext(ucontext_t *oucp, ucontext_t *ucp)
-{
 #ifdef MAC
 #ifdef MAC
-       /*
-        * Mac OSX swapcontext() clobbers some registers, so use a hand-rolled
-        * version with {get,set}context(). We can avoid the same problem
-        * (where optimizations can break the following code) because we don't
-        * statically link with the C library
-        */
-
-       /* volatile, so that 'i' doesn't get promoted to a register */
-       volatile int i = 0;
 
 
-       getcontext(oucp);
+int model_swapcontext(ucontext_t *oucp, ucontext_t *ucp);
 
 
-       if (i == 0) {
-               i = 1;
-               setcontext(ucp);
-       }
+#else /* !MAC */
 
 
-       return 0;
-#else
+static inline int model_swapcontext(ucontext_t *oucp, ucontext_t *ucp)
+{
        return swapcontext(oucp, ucp);
        return swapcontext(oucp, ucp);
-#endif
 }
 
 }
 
+#endif /* !MAC */
+
 #endif /* __CONTEXT_H__ */
 #endif /* __CONTEXT_H__ */