From e9d2d6e08dcf6962aa7b2818389d9a6d451d7d14 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Thu, 4 Apr 2013 13:19:50 -0700 Subject: [PATCH] context: move Mac swapcontext() to separate compilation unit 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 | 3 ++- context.cc | 27 +++++++++++++++++++++++++++ context.h | 25 ++++++------------------- 3 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 context.cc diff --git a/Makefile b/Makefile index b653e769..5120cce8 100644 --- 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 \ - 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 diff --git a/context.cc b/context.cc new file mode 100644 index 00000000..b5ae0ba5 --- /dev/null +++ b/context.cc @@ -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 */ diff --git a/context.h b/context.h index 862cda5e..ea32d2f5 100644 --- a/context.h +++ b/context.h @@ -8,30 +8,17 @@ #include -static inline int model_swapcontext(ucontext_t *oucp, ucontext_t *ucp) -{ #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); -#endif } +#endif /* !MAC */ + #endif /* __CONTEXT_H__ */ -- 2.34.1