582bfc7e016b668ee47b33285b5eecea099e36b9
[sbcl.git] / src / runtime / x86-arch.h
1 /* FIXME: Aren't preprocessor symbols with underscore prefixes
2  * reserved for the system libraries? If so, it would be tidy to
3  * rename flags like _X86_ARCH_H so their names are in a part of the
4  * namespace that we control. */
5 #ifndef _X86_ARCH_H
6 #define _X86_ARCH_H
7
8 #ifndef SBCL_GENESIS_CONFIG
9 #error genesis/config.h (or sbcl.h) must be included before this file
10 #endif
11
12 #include "interr.h"                     /* for declaration of lose() */
13
14 #define ARCH_HAS_STACK_POINTER
15
16 /* FIXME: Do we also want
17  *   #define ARCH_HAS_FLOAT_REGISTERS
18  * here? (The answer wasn't obvious to me when merging the
19  * architecture-abstracting patches for CSR's SPARC port. -- WHN 2002-02-15) */
20
21 #define COMPILER_BARRIER \
22     do { __asm__ __volatile__ ( "" : : : "memory"); } while (0)
23
24 #ifdef LISP_FEATURE_WIN32
25 extern int os_number_of_processors;
26 #define yield_on_uniprocessor()                 \
27     do { if (os_number_of_processors<=1) SwitchToThread(); } while(0)
28 #else
29 /* Stubs are better than ifdef EVERYWHERE. */
30 #define yield_on_uniprocessor()                 \
31     do {} while(0)
32 #endif
33
34
35 static inline void
36 get_spinlock(volatile lispobj *word, unsigned long value)
37 {
38 #ifdef LISP_FEATURE_SB_THREAD
39     u32 eax=0;
40     if(*word==value)
41         lose("recursive get_spinlock: 0x%x,%ld\n",word,value);
42     do {
43 #if defined(LISP_FEATURE_DARWIN)
44         asm volatile ("xor %0,%0;\n\
45               lock/cmpxchg %1,%2"
46              : "=a" (eax)
47              : "r" (value), "m" (*word)
48              : "memory", "cc");
49 #else
50         if (eax!=0) {
51             asm volatile("rep; nop");
52         }
53         asm volatile ("xor %0,%0\n\
54               lock cmpxchg %1,%2"
55              : "=a" (eax)
56              : "r" (value), "m" (*word)
57              : "memory", "cc");
58 #endif
59         yield_on_uniprocessor();
60     } while(eax!=0);
61 #else
62     *word=value;
63 #endif
64 }
65
66 static inline void
67 release_spinlock(volatile lispobj *word)
68 {
69     /* See comment in RELEASE-SPINLOCK in target-thread.lisp. */
70     COMPILER_BARRIER;
71     *word=0;
72     COMPILER_BARRIER;
73 }
74
75 static inline lispobj
76 swap_lispobjs(volatile lispobj *dest, lispobj value)
77 {
78     lispobj old_value;
79 #if defined(LISP_FEATURE_DARWIN)
80     asm volatile ("lock/xchg %0,(%1)"
81          : "=r" (old_value)
82          : "r" (dest), "0" (value)
83          : "memory");
84 #else
85     asm volatile ("lock xchg %0,(%1)"
86          : "=r" (old_value)
87          : "r" (dest), "0" (value)
88          : "memory");
89 #endif
90     return old_value;
91 }
92
93 extern void fast_bzero_detect(void *, size_t);
94 extern void (*fast_bzero_pointer)(void *, size_t);
95
96 #endif /* _X86_ARCH_H */