model: use get_thread() helper
[cdsspec-compiler.git] / context.h
1 /**
2  * @file context.h
3  * @brief ucontext header, since Mac OSX swapcontext() is broken
4  */
5
6 #ifndef __CONTEXT_H__
7 #define __CONTEXT_H__
8
9 #include <ucontext.h>
10
11 static inline int model_swapcontext(ucontext_t *oucp, ucontext_t *ucp)
12 {
13 #ifdef MAC
14         /*
15          * Mac OSX swapcontext() clobbers some registers, so use a hand-rolled
16          * version with {get,set}context(). We can avoid the same problem
17          * (where optimizations can break the following code) because we don't
18          * statically link with the C library
19          */
20
21         /* volatile, so that 'i' doesn't get promoted to a register */
22         volatile int i = 0;
23
24         getcontext(oucp);
25
26         if (i == 0) {
27                 i = 1;
28                 setcontext(ucp);
29         }
30
31         return 0;
32 #else
33         return swapcontext(oucp, ucp);
34 #endif
35 }
36
37 #endif /* __CONTEXT_H__ */